diff --git a/lang/en/forum.php b/lang/en/forum.php index e57fa4b9a91..6a67c61135c 100644 --- a/lang/en/forum.php +++ b/lang/en/forum.php @@ -138,7 +138,15 @@ $string['replyforum'] = 'Reply to forum'; $string['rsssubscriberssdiscussions'] = 'Display the RSS feed for \'$a\' discussions'; $string['rsssubscriberssposts'] = 'Display the RSS feed for \'$a\' posts'; $string['search'] = 'Search'; +$string['searchforumintro'] = 'Please enter search terms into one or more of the following fields:'; $string['searchforums'] = 'Search forums'; +$string['searchfullwords'] = 'These words should appear as whole words'; +$string['searchnotwords'] = 'These words should NOT be included'; +$string['searchphrase'] = 'This exact phrase must appear in the post'; +$string['searchsubject'] = 'These words should be in the subject'; +$string['searchuser'] = 'This name should match the author'; +$string['searchuserid'] = 'The Moodle ID of the author'; +$string['searchwords'] = 'These words can appear anywhere in the post'; $string['searcholderposts'] = 'Search older posts...'; $string['searchresults'] = 'Search results'; $string['sendinratings'] = 'Send in my latest ratings'; diff --git a/lib/searchlib.php b/lib/searchlib.php index 3b55a6e0fcc..4738a09fc36 100644 --- a/lib/searchlib.php +++ b/lib/searchlib.php @@ -9,6 +9,7 @@ define("TOKEN_META","1"); define("TOKEN_EXACT","2"); define("TOKEN_NEGATE","3"); define("TOKEN_STRING","4"); +define("TOKEN_USERID","5"); // Class to hold token/value pairs after they're parsed. @@ -50,6 +51,17 @@ class search_lexer extends Lexer{ //Set up the state machine and pattern matches for transitions. + // Patterns to handle strings of the form userid:foo + + // If we see the string user: while in the base accept state, start + // parsing a username and go to the inuserid state. + $this->addEntryPattern("userid:\S+","accept","inuserid"); + + // Snarf everything into the username until we see whitespace, then exit + // back to the base accept state. + $this->addExitPattern("\s","inuserid"); + + // Patterns to handle strings of the form user:foo // If we see the string user: while in the base accept state, start @@ -60,6 +72,7 @@ class search_lexer extends Lexer{ // back to the base accept state. $this->addExitPattern("\s","inusername"); + // Patterns to handle strings of the form meta:foo // If we see the string meta: while in the base accept state, start @@ -138,6 +151,17 @@ class search_parser { return true; } + // State for handling userid:foo constructs. Potentially emits a token. + function inuserid($content){ + if (strlen($content) < 8) { // State exit or missing parameter. + return true; + } + // Strip off the userid: part and add the reminder to the parsed token array + $param = trim(substr($content,7)); + $this->tokens[] = new search_token(TOKEN_USERID,$param); + return true; + } + // State for handling user:foo constructs. Potentially emits a token. function inusername($content){ @@ -262,6 +286,9 @@ function search_generate_SQL($parsetree, $datafield, $metafield, $mainidfield, $ case TOKEN_USER: $SQLString .= "(($mainidfield = $useridfield) AND (($userfirstnamefield $LIKE '%$value%') OR ($userlastnamefield $LIKE '%$value%')))"; break; + case TOKEN_USERID: + $SQLString .= "($useridfield = $value)"; + break; case TOKEN_NEGATE: $SQLString .= "(NOT (($datafield $LIKE '%$value%') OR ($metafield $LIKE '%$value%')))"; break; diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 1d90e8662d9..4709306e53a 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -1002,16 +1002,7 @@ function forum_search_posts($searchterms, $courseid, $page=0, $recordsperpage=50 $onlyvisibletable = ""; } - switch ($CFG->dbtype) { - case "mysql": - $limit = "LIMIT $page,$recordsperpage"; - break; - case "postgres7": - $limit = "LIMIT $recordsperpage OFFSET ".($page * $recordsperpage); - break; - default: - $limit = "LIMIT $recordsperpage,$page"; - } + $limit = sql_paging_limit($page, $recordsperpage); /// Some differences in syntax for PostgreSQL if ($CFG->dbtype == "postgres7") { @@ -1041,6 +1032,7 @@ function forum_search_posts($searchterms, $courseid, $page=0, $recordsperpage=50 $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"); @@ -3326,12 +3318,12 @@ function forum_tp_clean_read_records() { $sql = 'SELECT fr.id, fr.userid, fr.postid '. 'FROM '.$CFG->prefix.'forum_posts fp, '.$CFG->prefix.'forum_read fr '. 'WHERE fp.modified < '.$cutoffdate.' AND fp.id = fr.postid'; -echo $sql.'
'; if (($oldreadposts = get_records_sql($sql))) { -echo 'Deleting records: ';print_object($oldreadposts); foreach($oldreadposts as $oldreadpost) { delete_records('forum_read', 'userid', $oldreadpost->userid, 'postid', $oldreadpost->postid); } } -} + +} + ?> diff --git a/mod/forum/search.php b/mod/forum/search.php index e27155e1501..e2f4cd0bb79 100644 --- a/mod/forum/search.php +++ b/mod/forum/search.php @@ -3,21 +3,49 @@ require_once("../../config.php"); require_once("lib.php"); - require_variable($id); // course id - optional_variable($search, ""); // search string - optional_variable($page, "0"); // which page to show - optional_variable($perpage, "20"); // which page to show + $id = required_param('id', PARAM_INT); // course id + $search = trim(optional_param('search', '', PARAM_NOTAGS)); // search string + $page = optional_param('page', 0, PARAM_INT); // which page to show + $perpage = optional_param('perpage', 20, PARAM_INT); // which page to show - $search = trim(strip_tags($search)); + $user = trim(optional_param('user', '', PARAM_NOTAGS)); // Names to search for + $userid = trim(optional_param('userid', 0, PARAM_INT)); // UserID to search for + $subject = trim(optional_param('subject', '', PARAM_NOTAGS)); // Subject + $phrase = trim(optional_param('phrase', '', PARAM_NOTAGS)); // Phrase + $words = trim(optional_param('words', '', PARAM_NOTAGS)); // Words + $fullwords = trim(optional_param('fullwords', '', PARAM_NOTAGS)); // Whole words + $notwords = trim(optional_param('notwords', '', PARAM_NOTAGS)); // Words we don't want + + + if (empty($search)) { // Check the other parameters instead + if (!empty($words)) { + $search .= ' '.$words; + } + if (!empty($userid)) { + $search .= ' userid:'.$userid; + } + if (!empty($user)) { + $search .= ' '.forum_clean_search_terms($user, 'user:'); + } + if (!empty($subject)) { + $search .= ' '.forum_clean_search_terms($subject, 'subject:'); + } + if (!empty($fullwords)) { + $search .= ' '.forum_clean_search_terms($fullwords, '+'); + } + if (!empty($notwords)) { + $search .= ' '.forum_clean_search_terms($notwords, '-'); + } + if (!empty($phrase)) { + $search .= ' "'.$phrase.'"'; + } + $individualparams = true; + } else { + $individualparams = false; + } if ($search) { - $searchterms = explode(" ", $search); // Search for words independently - foreach ($searchterms as $key => $searchterm) { - if (strlen($searchterm) < 2) { - unset($searchterms[$key]); - } - } - $search = s(trim(implode(" ", $searchterms))); + $search = forum_clean_search_terms($search); } if (! $course = get_record("course", "id", $id)) { @@ -32,135 +60,189 @@ $strsearch = get_string("search", "forum"); $strsearchresults = get_string("searchresults", "forum"); $strpage = get_string("page"); - $strmissingsearchterms = get_string('missingsearchterms','forum'); - - $searchform = forum_print_search_form($course, $search, true, "navbar"); if (!$search) { print_header_simple("$strsearch", "", - "id\">$strforums -> $strsearch", "search.search", + "id\">$strforums -> $strsearch", 'search.words', "", "", " ", navmenu($course)); - print_simple_box_start("center"); - echo "
"; - echo "
"; - echo $searchform; - echo "

"; - print_string("searchhelp"); - echo "

"; - echo "
"; - print_simple_box_end(); + forum_print_big_search_form($course); + print_footer($course); + exit; } - if ($search) { - $strippedsearch = str_replace("user:","",$search); - $strippedsearch = str_replace("subject:","",$strippedsearch); - $strippedsearch = str_replace(""","",$strippedsearch); - if ($group = user_group($id, $USER->id)) { - $groupid = $group->id; - } else { - $groupid = 0; - } - if (!$posts = forum_search_posts($searchterms, $course->id, $page*$perpage, $perpage, $totalcount, $groupid)) { +/// We need to do a search now and print results - print_header_simple("$strsearchresults", "", - "id\">$strforums -> - id\">$strsearch -> \"$search\"", "search.search", - "", "", " ", navmenu($course)); - print_heading(get_string("nopostscontaining", "forum", $search)); + $searchterms = explode(' ', $search); - print_simple_box_start("center"); - echo "
"; - echo "
"; - echo $searchform; - echo "

"; - print_string("searchhelp"); - echo "

"; - echo "
"; - print_simple_box_end(); - print_footer($course); - exit; - } + $searchform = forum_print_search_form($course, "", true, "navbar"); + + if ($group = user_group($course->id, $USER->id)) { + $groupid = $group->id; + } else { + $groupid = 0; + } + if (!$posts = forum_search_posts($searchterms, $course->id, $page*$perpage, $perpage, $totalcount, $groupid)) { print_header_simple("$strsearchresults", "", - "id\">$strforums -> - id\">$strsearch -> \"$search\"", "search.search", - "", "", $searchform, navmenu($course)); + "id\">$strforums -> + id\">$strsearch -> ".stripslashes($search), 'search.words', + "", "", " ", navmenu($course)); + print_heading(get_string("nopostscontaining", "forum", $search)); - print_heading("$strsearchresults: $totalcount"); - - echo "
"; - print_paging_bar($totalcount, $page, $perpage, "search.php?search=$search&id=$course->id&perpage=$perpage&"); - echo "
"; - - //added to implement highlighting of search terms found only in HTML markup - //fiedorow - 9/2/2005 - $searchterms = explode(" ", $strippedsearch); // Search for words independently - foreach ($searchterms as $key => $searchterm) { - if (preg_match('/^\-/',$searchterm)) { - unset($searchterms[$key]); - } else { - $searchterms[$key] = preg_replace('/^\+/','',$searchterm); - } + if (!$individualparams) { + $words = $search; } - foreach ($posts as $post) { - - if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) { - error("Discussion ID was incorrect"); - } - if (! $forum = get_record("forum", "id", "$discussion->forum")) { - error("Could not find forum $discussion->forum"); - } - - $post->subject = highlight("$strippedsearch", $post->subject); - $discussion->name = highlight("$strippedsearch", $discussion->name); - - $fullsubject = "id\">$forum->name"; - if ($forum->type != "single") { - $fullsubject .= " -> id\">$discussion->name"; - if ($post->parent != 0) { - $fullsubject .= " -> discussion&parent=$post->id\">$post->subject"; - } - } - - $post->subject = $fullsubject; - - /// Add the forum id to the post object - used by read tracking. - $post->forum = $forum->id; - - //Indicate search terms only found in HTML markup - //Use highlight() with nonsense tags to spot search terms in the - //actual text content first. - //fiedorow - 9/2/2005 - $missing_terms = ""; - $message = highlight($strippedsearch,format_text($post->message, $post->format, NULL, $course->id), - 0,'',''); - foreach ($searchterms as $searchterm) { - if (preg_match("/$searchterm/i",$message) && !preg_match('/'.$searchterm.'<\/fgw9sdpq4>/i',$message)) { - $missing_terms .= " $searchterm"; - } - } - $message = str_replace('','',$message); - $message = str_replace('','',$message); - - if ($missing_terms) { - $post->message = '

'.$strmissingsearchterms.' '.$missing_terms.'

'.$message; - } - - $fulllink = "discussion#$post->id\">".get_string("postincontext", "forum").""; - //search terms already highlighted - fiedorow - 9/2/2005 - forum_print_post($post, $course->id, false, false, false, false, $fulllink); - - echo "
"; - } - - echo "
"; - print_paging_bar($totalcount, $page, $perpage, "search.php?search=".urlencode($search)."&id=$course->id&perpage=$perpage&"); - echo "
"; + forum_print_big_search_form($course); + exit; } + print_header_simple("$strsearchresults", "", + "id\">$strforums -> + id\">$strsearch -> ".stripslashes($search), 'search.words', + "", "", $searchform, navmenu($course)); + + print_heading("$strsearchresults: $totalcount"); + + print_paging_bar($totalcount, $page, $perpage, "search.php?search=$search&id=$course->id&perpage=$perpage&"); + + //added to implement highlighting of search terms found only in HTML markup + //fiedorow - 9/2/2005 + $strippedsearch = str_replace('user:','',$search); + $strippedsearch = str_replace('subject:','',$strippedsearch); + $strippedsearch = str_replace('"','',$strippedsearch); + $searchterms = explode(" ", $strippedsearch); // Search for words independently + foreach ($searchterms as $key => $searchterm) { + if (preg_match('/^\-/',$searchterm)) { + unset($searchterms[$key]); + } else { + $searchterms[$key] = preg_replace('/^\+/','',$searchterm); + } + } + + foreach ($posts as $post) { + + if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) { + error("Discussion ID was incorrect"); + } + if (! $forum = get_record("forum", "id", "$discussion->forum")) { + error("Could not find forum $discussion->forum"); + } + + $post->subject = highlight("$strippedsearch", $post->subject); + $discussion->name = highlight("$strippedsearch", $discussion->name); + + $fullsubject = "id\">$forum->name"; + if ($forum->type != "single") { + $fullsubject .= " -> id\">$discussion->name"; + if ($post->parent != 0) { + $fullsubject .= " -> discussion&parent=$post->id\">$post->subject"; + } + } + + $post->subject = $fullsubject; + + /// Add the forum id to the post object - used by read tracking. + $post->forum = $forum->id; + + //Indicate search terms only found in HTML markup + //Use highlight() with nonsense tags to spot search terms in the + //actual text content first. fiedorow - 9/2/2005 + $missing_terms = ""; + $message = highlight($strippedsearch,format_text($post->message, $post->format, NULL, $course->id), + 0,'',''); + + foreach ($searchterms as $searchterm) { + if (preg_match("/$searchterm/i",$message) && !preg_match('/'.$searchterm.'<\/fgw9sdpq4>/i',$message)) { + $missing_terms .= " $searchterm"; + } + } + + $message = str_replace('','',$message); + $message = str_replace('','',$message); + + if ($missing_terms) { + $strmissingsearchterms = get_string('missingsearchterms','forum'); + $post->message = '

'.$strmissingsearchterms.' '.$missing_terms.'

'.$message; + } + + $fulllink = "discussion#$post->id\">".get_string("postincontext", "forum").""; + //search terms already highlighted - fiedorow - 9/2/2005 + forum_print_post($post, $course->id, false, false, false, false, $fulllink); + + echo "
"; + } + + print_paging_bar($totalcount, $page, $perpage, "search.php?search=".urlencode($search)."&id=$course->id&perpage=$perpage&"); + print_footer($course); + + +function forum_print_big_search_form($course) { + global $words, $subject, $phrase, $user, $userid, $fullwords, $notwords; + + print_simple_box(get_string('searchforumintro', 'forum'), 'center', '', '', 'searchbox', 'intro'); + + print_simple_box_start("center"); + echo '
'; + echo ''; + echo ''; + + echo ''; + echo ''; + echo ''; + echo ''; + + echo ''; + echo ''; + echo ''; + echo ''; + + echo ''; + echo ''; + echo ''; + echo ''; + + echo ''; + echo ''; + echo ''; + echo ''; + + echo ''; + echo ''; + echo ''; + echo ''; + + echo ''; + echo ''; + echo ''; + echo ''; + + echo ''; + echo ''; + echo ''; + + echo ''; + echo '
'; + print_simple_box_end(); +} + + +function forum_clean_search_terms($words, $prefix='') { + $searchterms = explode(' ', $words); + foreach ($searchterms as $key => $searchterm) { + if (strlen($searchterm) < 2) { + unset($searchterms[$key]); + } else if ($prefix) { + $searchterms[$key] = $prefix.$searchterm; + } + } + return trim(implode(' ', $searchterms)); +} + ?>