3 * Global Search Engine for Moodle
4 * Michael Champanis (mchampan) [cynnical@gmail.com]
5 * review 1.8+ : Valery Fremaux [valery.fremaux@club-internet.fr]
8 * The query page - accepts a user-entered query string and returns results.
10 * Queries are boolean-aware, e.g.:
13 * '-' term must not be present
14 * '' (no modifier) term's presence increases rank, but isn't required
15 * 'field:' search this field
19 * 'earthquake +author:michael'
20 * Searches for documents written by 'michael' that contain 'earthquake'
22 * 'earthquake +doctype:wiki'
23 * Search all wiki pages for 'earthquake'
25 * '+author:helen +author:foster'
26 * All articles written by Helen Foster
30 require_once('../config.php');
31 require_once("$CFG->dirroot/search/lib.php");
33 if ($CFG->forcelogin
) {
37 if (empty($CFG->enableglobalsearch
)) {
38 error(get_string('globalsearchdisabled', 'search'));
43 // check for php5, but don't die yet (see line 52)
44 if ($check = search_check_php5()) {
45 require_once("{$CFG->dirroot}/search/querylib.php");
47 $page_number = optional_param('page', -1, PARAM_INT
);
48 $pages = ($page_number == -1) ?
false : true;
49 $advanced = (optional_param('a', '0', PARAM_INT
) == '1') ?
true : false;
50 $query_string = optional_param('query_string', '', PARAM_CLEAN
);
52 if ($pages && isset($_SESSION['search_advanced_query'])) {
53 // if both are set, then we are busy browsing through the result pages of an advanced query
54 $adv = unserialize($_SESSION['search_advanced_query']);
57 // otherwise we are dealing with a new advanced query
58 unset($_SESSION['search_advanced_query']);
59 session_unregister('search_advanced_query');
61 // chars to strip from strings (whitespace)
62 $chars = " \t\n\r\0\x0B,-+";
64 // retrieve advanced query variables
65 $adv->mustappear
= trim(optional_param('mustappear', '', PARAM_CLEAN
), $chars);
66 $adv->notappear
= trim(optional_param('notappear', '', PARAM_CLEAN
), $chars);
67 $adv->canappear
= trim(optional_param('canappear', '', PARAM_CLEAN
), $chars);
68 $adv->module
= optional_param('module', '', PARAM_CLEAN
);
69 $adv->title
= trim(optional_param('title', '', PARAM_CLEAN
), $chars);
70 $adv->author
= trim(optional_param('author', '', PARAM_CLEAN
), $chars);
74 //parse the advanced variables into a query string
75 //TODO: move out to external query class (QueryParse?)
79 // get all available module types
80 $module_types = array_merge(array('all'), array_values(search_get_document_types()));
81 $adv->module
= in_array($adv->module
, $module_types) ?
$adv->module
: 'all';
83 // convert '1 2' into '+1 +2' for required words field
84 if (strlen(trim($adv->mustappear
)) > 0) {
85 $query_string = ' +'.implode(' +', preg_split("/[\s,;]+/", $adv->mustappear
));
88 // convert '1 2' into '-1 -2' for not wanted words field
89 if (strlen(trim($adv->notappear
)) > 0) {
90 $query_string .= ' -'.implode(' -', preg_split("/[\s,;]+/", $adv->notappear
));
93 // this field is left untouched, apart from whitespace being stripped
94 if (strlen(trim($adv->canappear
)) > 0) {
95 $query_string .= ' '.implode(' ', preg_split("/[\s,;]+/", $adv->canappear
));
98 // add module restriction
99 $doctypestr = get_string('doctype', 'search');
100 $titlestr = get_string('title', 'search');
101 $authorstr = get_string('author', 'search');
102 if ($adv->module
!= 'all') {
103 $query_string .= " +{$doctypestr}:".$adv->module
;
106 // create title search string
107 if (strlen(trim($adv->title
)) > 0) {
108 $query_string .= " +{$titlestr}:".implode(" +{$titlestr}:", preg_split("/[\s,;]+/", $adv->title
));
111 // create author search string
112 if (strlen(trim($adv->author
)) > 0) {
113 $query_string .= " +{$authorstr}:".implode(" +{$authorstr}:", preg_split("/[\s,;]+/", $adv->author
));
116 // save our options if the query is valid
117 if (!empty($query_string)) {
118 $_SESSION['search_advanced_query'] = serialize($adv);
122 // normalise page number
123 if ($page_number < 1) {
127 //run the query against the index
128 $sq = new SearchQuery($query_string, $page_number, 10, false);
131 if (!$site = get_site()) {
132 redirect("index.php");
135 $strsearch = get_string('search', 'search');
136 $strquery = get_string('enteryoursearchquery', 'search');
138 print_header("$site->shortname: $strsearch: $strquery", "$site->fullname",
139 "<a href=\"index.php\">$strsearch</a> -> $strquery");
141 //keep things pretty, even if php5 isn't available
143 print_heading(search_check_php5(true));
149 print_heading($strquery);
153 $vars = get_object_vars($adv);
156 foreach ($vars as $key => $value) {
157 $adv->$key = stripslashes(htmlentities($value));
162 <form id
="query" method
="get" action
="query.php">
166 <input type
="text" name
="query_string" length
="50" value
="<?php print stripslashes($query_string) ?>" />
167  
;<input type
="submit" value
="<?php print_string('search', 'search') ?>" />  
;
168 <a href
="query.php?a=1"><?php
print_string('advancedsearch', 'search') ?
></a
> |
169 <a href
="stats.php"><?php
print_string('statistics', 'search') ?
></a
>
175 <input type
="hidden" name
="a" value
="<?php print $advanced; ?>"/>
177 <table border
="0" cellpadding
="3" cellspacing
="3">
180 <td width
="240"><?php
print_string('thesewordsmustappear', 'search') ?
>:</td
>
181 <td
><input type
="text" name
="mustappear" length
="50" value
="<?php print $adv->mustappear; ?>" /></td
>
185 <td
><?php
print_string('thesewordsmustnotappear', 'search') ?
>:</td
>
186 <td
><input type
="text" name
="notappear" length
="50" value
="<?php print $adv->notappear; ?>" /></td
>
190 <td
><?php
print_string('thesewordshelpimproverank', 'search') ?
>:</td
>
191 <td
><input type
="text" name
="canappear" length
="50" value
="<?php print $adv->canappear; ?>" /></td
>
195 <td
><?php
print_string('whichmodulestosearch?', 'search') ?
>:</td
>
197 <select name
="module">
199 foreach($module_types as $mod) {
200 if ($mod == $adv->module
) {
202 print "<option value='$mod' selected=\"selected\">".get_string('modulenameplural', $mod)."</option>\n";
205 print "<option value='$mod' selected=\"selected\">".get_string('all', 'search')."</option>\n";
210 print "<option value='$mod'>".get_string('modulenameplural', $mod)."</option>\n";
213 print "<option value='$mod'>".get_string('all', 'search')."</option>\n";
223 <td
><?php
print_string('wordsintitle', 'search') ?
>:</td
>
224 <td
><input type
="text" name
="title" length
="50" value
="<?php print $adv->title; ?>" /></td
>
228 <td
><?php
print_string('authorname', 'search') ?
>:</td
>
229 <td
><input type
="text" name
="author" length
="50" value
="<?php print $adv->author; ?>" /></td
>
233 <td colspan
="3" align
="center"><br
/><input type
="submit" value
="<?php print_string('search', 'search') ?>" /></td
>
237 <td colspan
="3" align
="center">
238 <table border
="0" cellpadding
="0" cellspacing
="0">
240 <td
><a href
="query.php"><?php
print_string('normalsearch', 'search') ?
></a
> |
</td
>
241 <td
> 
;<a href
="stats.php"><?php
print_string('statistics', 'search') ?
></a
></td
>
256 print_string('searching', 'search') . ': ';
258 if ($sq->is_valid_index()) {
259 //use cached variable to show up-to-date index size (takes deletions into account)
260 print $CFG->search_index_size
;
267 print_string('documents', 'search');
270 if (!$sq->is_valid_index() and isadmin()) {
271 print '<p>' . get_string('noindexmessage', 'search') . '<a href="indexersplash.php">' . get_string('createanindex', 'search')."</a></p>\n";
279 // prints all the results in a box
280 if ($sq->is_valid()) {
284 $hit_count = $sq->count();
288 print $hit_count.' '.get_string('resultsreturnedfor', 'search') . " '".stripslashes($query_string)."'.";
291 if ($hit_count > 0) {
292 $page_links = $sq->page_numbers();
293 $hits = $sq->results();
296 // if in advanced mode, search options are saved in the session, so
297 // we can remove the query string var from the page links, and replace
298 // it with a=1 (Advanced = on) instead
299 $page_links = preg_replace("/query_string=[^&]+/", 'a=1', $page_links);
304 $typestr = get_string('type', 'search');
305 $scorestr = get_string('score', 'search');
306 $authorstr = get_string('author', 'search');
307 foreach ($hits as $listing) {
308 if ($CFG->unicodedb
) $listing->title
= mb_convert_encoding($listing->title
, 'auto', 'UTF8');
309 $title_post_processing_function = $listing->doctype
.'_link_post_processing';
310 require_once "{$CFG->dirroot}/search/documents/{$listing->doctype}_document.php";
311 if (function_exists($title_post_processing_function))
312 $listing->title
= $title_post_processing_function($listing->title
);
313 print "<li value='".($listing->number+
1)."'><a href='".str_replace('DEFAULT_POPUP_SETTINGS', DEFAULT_POPUP_SETTINGS
,$listing->url
)."'>$listing->title</a><br />\n"
314 ."<em>".search_shorten_url($listing->url
, 70)."</em><br />\n"
315 ."{$typestr}: ".$listing->doctype
.", {$scorestr}: ".round($listing->score
, 3).", {$authorstr}: ".$listing->author
."\n"
327 print_string('ittook', 'search');
329 print_string('tofetchtheseresults', 'search');