mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 08:56:36 +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;
|
||||
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;
|
||||
$searchstring = "";
|
||||
// Need to concat these back together for parser to work.
|
||||
foreach($searchterms as $searchterm){
|
||||
if ($searchstring != "") {
|
||||
$searchstring .= " ";
|
||||
}
|
||||
if ($messagesearch) {
|
||||
$messagesearch .= " AND ";
|
||||
}
|
||||
if ($subjectsearch) {
|
||||
$subjectsearch .= " AND ";
|
||||
$searchstring .= $searchterm;
|
||||
}
|
||||
|
||||
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%' ";
|
||||
}
|
||||
// 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'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue