mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 17:06:53 +02:00
Forum searching is now implemented using a lex-based method, which allows
extra capabilities such as: - "phrase searching" - user matching: user:tony - subject matching: subject:thing This code is all from Tony Hursh. THANKS TONY!!
This commit is contained in:
parent
792a590da7
commit
855c0839fc
1 changed files with 19 additions and 27 deletions
|
@ -969,6 +969,7 @@ function forum_search_posts($searchterms, $courseid, $page=0, $recordsperpage=50
|
||||||
///
|
///
|
||||||
|
|
||||||
global $CFG;
|
global $CFG;
|
||||||
|
require_once($CFG->libdir.'/searchlib.php');
|
||||||
|
|
||||||
if (!isteacher($courseid)) {
|
if (!isteacher($courseid)) {
|
||||||
$notteacherforum = "AND f.type <> 'teacher'";
|
$notteacherforum = "AND f.type <> 'teacher'";
|
||||||
|
@ -1008,39 +1009,30 @@ function forum_search_posts($searchterms, $courseid, $page=0, $recordsperpage=50
|
||||||
}
|
}
|
||||||
|
|
||||||
$messagesearch = "";
|
$messagesearch = "";
|
||||||
$subjectsearch = "";
|
$searchstring = "";
|
||||||
|
// Need to concat these back together for parser to work.
|
||||||
|
foreach($searchterms as $searchterm){
|
||||||
|
if ($searchstring != "") {
|
||||||
|
$searchstring .= " ";
|
||||||
|
}
|
||||||
|
$searchstring .= $searchterm;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We need to allow quoted strings for the search. The quotes *should* be stripped
|
||||||
foreach ($searchterms as $searchterm) {
|
// by the parser, but this should be examined carefully for security implications.
|
||||||
if (strlen($searchterm) < 2) {
|
$searchstring = str_replace("\\\"","\"",$searchstring);
|
||||||
continue;
|
$parser = new search_parser();
|
||||||
}
|
$lexer = new search_lexer(&$parser);
|
||||||
if ($messagesearch) {
|
if ($lexer->parse($searchstring)) {
|
||||||
$messagesearch .= " AND ";
|
$parsearray = $parser->get_parsed_array();
|
||||||
}
|
$messagesearch = search_generate_SQL($parsearray,"p.message","p.subject","p.userid","u.id","u.firstname","u.lastname");
|
||||||
if ($subjectsearch) {
|
|
||||||
$subjectsearch .= " AND ";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (substr($searchterm,0,1) == "+") {
|
|
||||||
$searchterm = substr($searchterm,1);
|
|
||||||
$messagesearch .= " p.message $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
|
|
||||||
$subjectsearch .= " p.subject $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
|
|
||||||
} else if (substr($searchterm,0,1) == "-") {
|
|
||||||
$searchterm = substr($searchterm,1);
|
|
||||||
$messagesearch .= " p.message $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
|
|
||||||
$subjectsearch .= " p.subject $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
|
|
||||||
} else {
|
|
||||||
$messagesearch .= " p.message $LIKE '%$searchterm%' ";
|
|
||||||
$subjectsearch .= " p.subject $LIKE '%$searchterm%' ";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$selectsql = "{$CFG->prefix}forum_posts p,
|
$selectsql = "{$CFG->prefix}forum_posts p,
|
||||||
{$CFG->prefix}forum_discussions d,
|
{$CFG->prefix}forum_discussions d,
|
||||||
{$CFG->prefix}user u,
|
{$CFG->prefix}user u,
|
||||||
{$CFG->prefix}forum f $onlyvisibletable
|
{$CFG->prefix}forum f $onlyvisibletable
|
||||||
WHERE ($messagesearch OR $subjectsearch)
|
WHERE ($messagesearch)
|
||||||
AND p.userid = u.id
|
AND p.userid = u.id
|
||||||
AND p.discussion = d.id
|
AND p.discussion = d.id
|
||||||
AND d.course = '$courseid'
|
AND d.course = '$courseid'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue