From: moodler Date: Mon, 31 Jan 2005 07:41:47 +0000 (+0000) Subject: Forum searching is now implemented using a lex-based method, which allows X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=855c0839fc358fa4ce9ceb297c1950c5fd2d8448;p=moodle.git 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!! --- diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 8b6ac34a9f..94f5dafd2a 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -969,7 +969,8 @@ function forum_search_posts($searchterms, $courseid, $page=0, $recordsperpage=50 /// global $CFG; - + require_once($CFG->libdir.'/searchlib.php'); + if (!isteacher($courseid)) { $notteacherforum = "AND f.type <> 'teacher'"; @@ -1008,39 +1009,30 @@ function forum_search_posts($searchterms, $courseid, $page=0, $recordsperpage=50 } $messagesearch = ""; - $subjectsearch = ""; - - - foreach ($searchterms as $searchterm) { - if (strlen($searchterm) < 2) { - continue; - } - if ($messagesearch) { - $messagesearch .= " AND "; - } - 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%' "; + $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 + // by the parser, but this should be examined carefully for security implications. + $searchstring = str_replace("\\\"","\"",$searchstring); + $parser = new search_parser(); + $lexer = new search_lexer(&$parser); + if ($lexer->parse($searchstring)) { + $parsearray = $parser->get_parsed_array(); + $messagesearch = search_generate_SQL($parsearray,"p.message","p.subject","p.userid","u.id","u.firstname","u.lastname"); + } + $selectsql = "{$CFG->prefix}forum_posts p, {$CFG->prefix}forum_discussions d, {$CFG->prefix}user u, {$CFG->prefix}forum f $onlyvisibletable - WHERE ($messagesearch OR $subjectsearch) + WHERE ($messagesearch) AND p.userid = u.id AND p.discussion = d.id AND d.course = '$courseid'