3 * Global Search Engine for Moodle
7 * @subpackage search_engine
8 * @author Michael Champanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
10 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
12 * The query page - accepts a user-entered query string and returns results.
14 * Queries are boolean-aware, e.g.:
17 * '-' term must not be present
18 * '' (no modifier) term's presence increases rank, but isn't required
19 * 'field:' search this field
23 * 'earthquake +author:michael'
24 * Searches for documents written by 'michael' that contain 'earthquake'
26 * 'earthquake +doctype:wiki'
27 * Search all wiki pages for 'earthquake'
29 * '+author:helen +author:foster'
30 * All articles written by Helen Foster
35 * includes and requires
37 require_once('../config.php');
38 require_once("$CFG->dirroot/search/lib.php");
39 // include "debugging.php";
42 if ($CFG->forcelogin
) {
46 if (empty($CFG->enableglobalsearch
)) {
47 error(get_string('globalsearchdisabled', 'search'));
52 /// check for php5, but don't die yet (see line 52)
54 require_once($CFG->dirroot
.'/search/querylib.php');
56 $page_number = optional_param('page', -1, PARAM_INT
);
57 $pages = ($page_number == -1) ?
false : true;
58 $advanced = (optional_param('a', '0', PARAM_INT
) == '1') ?
true : false;
59 $query_string = stripslashes(optional_param('query_string', '', PARAM_CLEAN
));
61 /// discard harmfull searches
63 if (!isset($CFG->block_search_utf8dir
)){
64 set_config('block_search_utf8dir', 1);
67 /// discard harmfull searches
69 if (preg_match("/^[\*\?]+$/", $query_string)){
71 $error = get_string('fullwildcardquery','search');
75 if ($pages && isset($_SESSION['search_advanced_query'])) {
76 // if both are set, then we are busy browsing through the result pages of an advanced query
77 $adv = unserialize($_SESSION['search_advanced_query']);
78 } elseif ($advanced) {
79 // otherwise we are dealing with a new advanced query
80 unset($_SESSION['search_advanced_query']);
81 session_unregister('search_advanced_query');
83 // chars to strip from strings (whitespace)
84 $chars = " \t\n\r\0\x0B,-+";
86 // retrieve advanced query variables
87 $adv->mustappear
= trim(optional_param('mustappear', '', PARAM_CLEAN
), $chars);
88 $adv->notappear
= trim(optional_param('notappear', '', PARAM_CLEAN
), $chars);
89 $adv->canappear
= trim(optional_param('canappear', '', PARAM_CLEAN
), $chars);
90 $adv->module
= optional_param('module', '', PARAM_CLEAN
);
91 $adv->title
= trim(optional_param('title', '', PARAM_CLEAN
), $chars);
92 $adv->author
= trim(optional_param('author', '', PARAM_CLEAN
), $chars);
96 //parse the advanced variables into a query string
97 //TODO: move out to external query class (QueryParse?)
101 // get all available module types adding third party modules
102 $module_types = array_merge(array('all'), array_values(search_get_document_types()));
103 $module_types = array_merge($module_types, array_values(search_get_document_types('X_SEARCH_TYPE')));
104 $adv->module
= in_array($adv->module
, $module_types) ?
$adv->module
: 'all';
106 // convert '1 2' into '+1 +2' for required words field
107 if (strlen(trim($adv->mustappear
)) > 0) {
108 $query_string = ' +'.implode(' +', preg_split("/[\s,;]+/", $adv->mustappear
));
111 // convert '1 2' into '-1 -2' for not wanted words field
112 if (strlen(trim($adv->notappear
)) > 0) {
113 $query_string .= ' -'.implode(' -', preg_split("/[\s,;]+/", $adv->notappear
));
116 // this field is left untouched, apart from whitespace being stripped
117 if (strlen(trim($adv->canappear
)) > 0) {
118 $query_string .= ' '.implode(' ', preg_split("/[\s,;]+/", $adv->canappear
));
121 // add module restriction
122 $doctypestr = 'doctype';
124 $authorstr = 'author';
125 if ($adv->module
!= 'all') {
126 $query_string .= " +{$doctypestr}:".$adv->module
;
129 // create title search string
130 if (strlen(trim($adv->title
)) > 0) {
131 $query_string .= " +{$titlestr}:".implode(" +{$titlestr}:", preg_split("/[\s,;]+/", $adv->title
));
134 // create author search string
135 if (strlen(trim($adv->author
)) > 0) {
136 $query_string .= " +{$authorstr}:".implode(" +{$authorstr}:", preg_split("/[\s,;]+/", $adv->author
));
139 // save our options if the query is valid
140 if (!empty($query_string)) {
141 $_SESSION['search_advanced_query'] = serialize($adv);
145 // normalise page number
146 if ($page_number < 1) {
150 //run the query against the index ensuring internal coding works in UTF-8
151 Zend_Search_Lucene_Analysis_Analyzer
::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
152 $sq = new SearchQuery($query_string, $page_number, 10, false);
154 if (!$site = get_site()) {
155 redirect("index.php");
158 $strsearch = get_string('search', 'search');
159 $strquery = get_string('enteryoursearchquery', 'search');
161 // if ($CFG->version < 2007032200){ NOT RELIABLE
162 if (!function_exists('build_navigation')){
163 print_header("$site->shortname: $strsearch: $strquery", "$site->fullname",
164 "<a href=\"index.php\">$strsearch</a> -> $strquery");
166 $navlinks[] = array('name' => $strsearch, 'link' => "index.php", 'type' => 'misc');
167 $navlinks[] = array('name' => $strquery, 'link' => null, 'type' => 'misc');
168 $navigation = build_navigation($navlinks);
170 print_header("$strsearch", "$site->fullname" , $navigation, "", "", true, " ", navmenu($site));
178 print_heading($strquery);
182 $vars = get_object_vars($adv);
185 foreach ($vars as $key => $value) {
186 // htmlentities breaks non-ascii chars
187 $adv->key
= stripslashes($value);
188 //$adv->$key = stripslashes(htmlentities($value));
192 <form id
="query" method
="get" action
="query.php">
196 <input type
="text" name
="query_string" length
="50" value
="<?php p($query_string) ?>" />
197  
;<input type
="submit" value
="<?php print_string('search', 'search') ?>" />  
;
198 <a href
="query.php?a=1"><?php
print_string('advancedsearch', 'search') ?
></a
> |
199 <a href
="stats.php"><?php
print_string('statistics', 'search') ?
></a
>
205 <input type
="hidden" name
="a" value
="<?php print $advanced; ?>"/>
207 <table border
="0" cellpadding
="3" cellspacing
="3">
210 <td width
="240"><?php
print_string('thesewordsmustappear', 'search') ?
>:</td
>
211 <td
><input type
="text" name
="mustappear" length
="50" value
="<?php print $adv->mustappear; ?>" /></td
>
215 <td
><?php
print_string('thesewordsmustnotappear', 'search') ?
>:</td
>
216 <td
><input type
="text" name
="notappear" length
="50" value
="<?php print $adv->notappear; ?>" /></td
>
220 <td
><?php
print_string('thesewordshelpimproverank', 'search') ?
>:</td
>
221 <td
><input type
="text" name
="canappear" length
="50" value
="<?php print $adv->canappear; ?>" /></td
>
225 <td
><?php
print_string('whichmodulestosearch?', 'search') ?
>:</td
>
227 <select name
="module">
229 foreach($module_types as $mod) {
230 if ($mod == $adv->module
) {
232 print "<option value='$mod' selected=\"selected\">".get_string('modulenameplural', $mod)."</option>\n";
235 print "<option value='$mod' selected=\"selected\">".get_string('all', 'search')."</option>\n";
240 print "<option value='$mod'>".get_string('modulenameplural', $mod)."</option>\n";
243 print "<option value='$mod'>".get_string('all', 'search')."</option>\n";
253 <td
><?php
print_string('wordsintitle', 'search') ?
>:</td
>
254 <td
><input type
="text" name
="title" length
="50" value
="<?php print $adv->title; ?>" /></td
>
258 <td
><?php
print_string('authorname', 'search') ?
>:</td
>
259 <td
><input type
="text" name
="author" length
="50" value
="<?php print $adv->author; ?>" /></td
>
263 <td colspan
="3" align
="center"><br
/><input type
="submit" value
="<?php print_string('search', 'search') ?>" /></td
>
267 <td colspan
="3" align
="center">
268 <table border
="0" cellpadding
="0" cellspacing
="0">
270 <td
><a href
="query.php"><?php
print_string('normalsearch', 'search') ?
></a
> |
</td
>
271 <td
> 
;<a href
="stats.php"><?php
print_string('statistics', 'search') ?
></a
></td
>
286 print_string('searching', 'search') . ': ';
288 if ($sq->is_valid_index()) {
289 //use cached variable to show up-to-date index size (takes deletions into account)
290 print $CFG->search_index_size
;
297 print_string('documents', 'search');
300 if (!$sq->is_valid_index() and has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM
))) {
301 print '<p>' . get_string('noindexmessage', 'search') . '<a href="indexersplash.php">' . get_string('createanindex', 'search')."</a></p>\n";
309 /// prints all the results in a box
311 if ($sq->is_valid()) {
315 $hit_count = $sq->count();
319 print $hit_count.' '.get_string('resultsreturnedfor', 'search') . " '".s($query_string)."'.";
322 if ($hit_count > 0) {
323 $page_links = $sq->page_numbers();
324 $hits = $sq->results();
327 // if in advanced mode, search options are saved in the session, so
328 // we can remove the query string var from the page links, and replace
329 // it with a=1 (Advanced = on) instead
330 $page_links = preg_replace("/query_string=[^&]+/", 'a=1', $page_links);
335 $typestr = get_string('type', 'search');
336 $scorestr = get_string('score', 'search');
337 $authorstr = get_string('author', 'search');
339 $searchables = search_collect_searchables(false, false);
341 foreach ($hits as $listing) {
343 if ($listing->doctype
== 'user'){ // A special handle for users
344 $icon = print_user_picture ($listing->userid
, 0, true, 0, true, false) ;
346 $iconpath = $CFG->modpixpath
.'/'.$listing->doctype
.'/icon.gif';
347 $icon = "<img align=\"top\" src=\"".$iconpath."\" class=\"activityicon\" alt=\"\"/>";
349 $coursename = get_field('course', 'fullname', 'id', $listing->courseid
);
350 $courseword = mb_convert_case(get_string('course', 'moodle'), MB_CASE_LOWER
, 'UTF-8');
351 $course = ($listing->doctype
!= 'user') ?
'<strong> ('.$courseword.': \''.$coursename.'\')</strong>' : '' ;
353 $title_post_processing_function = $listing->doctype
.'_link_post_processing';
354 $searchable_instance = $searchables[$listing->doctype
];
355 if ($searchable_instance->location
== 'internal'){
356 require_once "{$CFG->dirroot}/search/documents/{$listing->doctype}_document.php";
358 require_once "{$CFG->dirroot}/{$searchable_instance->location}/{$listing->doctype}/search_document.php";
360 if (function_exists($title_post_processing_function)) {
361 $listing->title
= $title_post_processing_function($listing->title
);
364 echo "<li value='".($listing->number +
1)."'><a href='"
365 .str_replace('DEFAULT_POPUP_SETTINGS', DEFAULT_POPUP_SETTINGS
,$listing->url
)
366 ."'>$icon $listing->title</a> $course<br />\n";
367 // print "<li value='".($listing->number+1)."'><a href='".str_replace('DEFAULT_POPUP_SETTINGS', DEFAULT_POPUP_SETTINGS ,$listing->url)."'>$listing->title</a><br />\n"
368 // ."<em>".search_shorten_url($listing->url, 70)."</em><br />\n"
369 echo "{$typestr}: " . $listing->doctype
. ", {$scorestr}: " . round($listing->score
, 3);
370 if (!empty($listing->author
) && !is_numeric($listing->author
)){
371 echo ", {$authorstr}: ".$listing->author
."\n"
382 print_string('ittook', 'search');
384 print_string('tofetchtheseresults', 'search');