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');
40 if ($CFG->forcelogin
) {
44 if (empty($CFG->enableglobalsearch
)) {
45 print_error('globalsearchdisabled', 'search');
48 $adv = new stdClass();
50 /// check for php5, but don't die yet (see line 52)
52 require_once($CFG->dirroot
.'/search/querylib.php');
54 $page_number = optional_param('page', -1, PARAM_INT
);
55 $pages = ($page_number == -1) ?
false : true;
56 $advanced = (optional_param('a', '0', PARAM_INT
) == '1') ?
true : false;
57 $query_string = optional_param('query_string', '', PARAM_CLEAN
);
59 $url = new moodle_url('/search/query.php');
60 if ($page_number !== -1) {
61 $url->param('page', $page_number);
64 $url->param('a', '1');
68 /// discard harmfull searches
70 if (!isset($CFG->block_search_utf8dir
)){
71 set_config('block_search_utf8dir', 1);
74 /// discard harmfull searches
76 if (preg_match("/^[\*\?]+$/", $query_string)){
78 $error = get_string('fullwildcardquery','search');
82 if ($pages && isset($_SESSION['search_advanced_query'])) {
83 // if both are set, then we are busy browsing through the result pages of an advanced query
84 $adv = unserialize($_SESSION['search_advanced_query']);
85 } elseif ($advanced) {
86 // otherwise we are dealing with a new advanced query
87 unset($_SESSION['search_advanced_query']);
88 session_unregister('search_advanced_query');
90 // chars to strip from strings (whitespace)
91 $chars = " \t\n\r\0\x0B,-+";
93 // retrieve advanced query variables
94 $adv->mustappear
= trim(optional_param('mustappear', '', PARAM_CLEAN
), $chars);
95 $adv->notappear
= trim(optional_param('notappear', '', PARAM_CLEAN
), $chars);
96 $adv->canappear
= trim(optional_param('canappear', '', PARAM_CLEAN
), $chars);
97 $adv->module
= optional_param('module', '', PARAM_CLEAN
);
98 $adv->title
= trim(optional_param('title', '', PARAM_CLEAN
), $chars);
99 $adv->author
= trim(optional_param('author', '', PARAM_CLEAN
), $chars);
103 //parse the advanced variables into a query string
104 //TODO: move out to external query class (QueryParse?)
108 // get all available module types adding third party modules
109 $module_types = array_merge(array('all'), array_values(search_get_document_types()));
110 $module_types = array_merge($module_types, array_values(search_get_document_types('X_SEARCH_TYPE')));
111 $adv->module
= in_array($adv->module
, $module_types) ?
$adv->module
: 'all';
113 // convert '1 2' into '+1 +2' for required words field
114 if (strlen(trim($adv->mustappear
)) > 0) {
115 $query_string = ' +'.implode(' +', preg_split("/[\s,;]+/", $adv->mustappear
));
118 // convert '1 2' into '-1 -2' for not wanted words field
119 if (strlen(trim($adv->notappear
)) > 0) {
120 $query_string .= ' -'.implode(' -', preg_split("/[\s,;]+/", $adv->notappear
));
123 // this field is left untouched, apart from whitespace being stripped
124 if (strlen(trim($adv->canappear
)) > 0) {
125 $query_string .= ' '.implode(' ', preg_split("/[\s,;]+/", $adv->canappear
));
128 // add module restriction
129 $doctypestr = 'doctype';
131 $authorstr = 'author';
132 if ($adv->module
!= 'all') {
133 $query_string .= " +{$doctypestr}:".$adv->module
;
136 // create title search string
137 if (strlen(trim($adv->title
)) > 0) {
138 $query_string .= " +{$titlestr}:".implode(" +{$titlestr}:", preg_split("/[\s,;]+/", $adv->title
));
141 // create author search string
142 if (strlen(trim($adv->author
)) > 0) {
143 $query_string .= " +{$authorstr}:".implode(" +{$authorstr}:", preg_split("/[\s,;]+/", $adv->author
));
146 // save our options if the query is valid
147 if (!empty($query_string)) {
148 $_SESSION['search_advanced_query'] = serialize($adv);
152 // normalise page number
153 if ($page_number < 1) {
157 //run the query against the index ensuring internal coding works in UTF-8
158 Zend_Search_Lucene_Analysis_Analyzer
::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
159 $sq = new SearchQuery($query_string, $page_number, 10, false);
163 $strsearch = get_string('search', 'search');
164 $strquery = get_string('enteryoursearchquery', 'search');
168 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM
));
169 $PAGE->navbar
->add($strsearch, new moodle_url('/search/index.php'));
170 $PAGE->navbar
->add($strquery, new moodle_url('/search/stats.php'));
171 $PAGE->set_title($strsearch);
172 $PAGE->set_heading($site->fullname
);
173 echo $OUTPUT->header();
179 echo $OUTPUT->box_start();
180 echo $OUTPUT->heading($strquery);
182 echo $OUTPUT->box_start();
184 $vars = get_object_vars($adv);
187 foreach ($vars as $key => $value) {
188 // htmlentities breaks non-ascii chars ??
190 //$adv->$key = htmlentities($value);
194 <form id
="query" method
="get" action
="query.php">
198 <input type
="text" name
="query_string" length
="50" value
="<?php p($query_string) ?>" /> 
;
199 <input type
="submit" value
="<?php print_string('search', 'search') ?>" />  
;
200 <a href
="query.php?a=1"><?php
print_string('advancedsearch', 'search') ?
></a
> |
201 <a href
="stats.php"><?php
print_string('statistics', 'search') ?
></a
>
205 echo $OUTPUT->box_start();
207 <input type
="hidden" name
="a" value
="<?php p($advanced); ?>"/>
209 <table border
="0" cellpadding
="3" cellspacing
="3">
212 <td width
="240"><?php
print_string('thesewordsmustappear', 'search') ?
>:</td
>
213 <td
><input type
="text" name
="mustappear" length
="50" value
="<?php p($adv->mustappear); ?>" /></td
>
217 <td
><?php
print_string('thesewordsmustnotappear', 'search') ?
>:</td
>
218 <td
><input type
="text" name
="notappear" length
="50" value
="<?php p($adv->notappear); ?>" /></td
>
222 <td
><?php
print_string('thesewordshelpimproverank', 'search') ?
>:</td
>
223 <td
><input type
="text" name
="canappear" length
="50" value
="<?php p($adv->canappear); ?>" /></td
>
227 <td
><?php
print_string('whichmodulestosearch?', 'search') ?
>:</td
>
229 <select name
="module">
231 foreach($module_types as $mod) {
232 if ($mod == $adv->module
) {
234 print "<option value='$mod' selected=\"selected\">".get_string('modulenameplural', $mod)."</option>\n";
237 print "<option value='$mod' selected=\"selected\">".get_string('all', 'search')."</option>\n";
242 print "<option value='$mod'>".get_string('modulenameplural', $mod)."</option>\n";
245 print "<option value='$mod'>".get_string('all', 'search')."</option>\n";
255 <td
><?php
print_string('wordsintitle', 'search') ?
>:</td
>
256 <td
><input type
="text" name
="title" length
="50" value
="<?php p($adv->title); ?>" /></td
>
260 <td
><?php
print_string('authorname', 'search') ?
>:</td
>
261 <td
><input type
="text" name
="author" length
="50" value
="<?php p($adv->author); ?>" /></td
>
265 <td colspan
="3" align
="center"><br
/><input type
="submit" value
="<?php p(get_string('search', 'search')) ?>" /></td
>
269 <td colspan
="3" align
="center">
270 <table border
="0" cellpadding
="0" cellspacing
="0">
272 <td
><a href
="query.php"><?php
print_string('normalsearch', 'search') ?
></a
> |
</td
>
273 <td
> 
;<a href
="stats.php"><?php
print_string('statistics', 'search') ?
></a
></td
>
280 echo $OUTPUT->box_end();
288 print_string('searching', 'search') . ': ';
290 if ($sq->is_valid_index()) {
291 //use cached variable to show up-to-date index size (takes deletions into account)
292 print $CFG->search_index_size
;
299 print_string('documents', 'search');
302 if (!$sq->is_valid_index() and has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM
))) {
303 print '<p>' . get_string('noindexmessage', 'search') . '<a href="indexersplash.php">' . get_string('createanindex', 'search')."</a></p>\n";
309 echo $OUTPUT->box_end();
311 /// prints all the results in a box
313 if ($sq->is_valid()) {
314 echo $OUTPUT->box_start();
317 $hit_count = $sq->count();
321 print $hit_count.' '.get_string('resultsreturnedfor', 'search') . " '".s($query_string)."'.";
324 if ($hit_count > 0) {
325 $page_links = $sq->page_numbers();
326 $hits = $sq->results();
329 // if in advanced mode, search options are saved in the session, so
330 // we can remove the query string var from the page links, and replace
331 // it with a=1 (Advanced = on) instead
332 $page_links = preg_replace("/query_string=[^&]+/", 'a=1', $page_links);
337 $typestr = get_string('type', 'search');
338 $scorestr = get_string('score', 'search');
339 $authorstr = get_string('author', 'search');
341 $searchables = search_collect_searchables(false, false);
343 //build a list of distinct user objects needed for results listing.
345 foreach ($hits as $listing) {
346 if ($listing->doctype
== 'user' and !isset($hitusers[$listing->userid
])) {
347 $hitusers[$listing->userid
] = $DB->get_record('user', array('id' => $listing->userid
));
351 foreach ($hits as $listing) {
353 if ($listing->doctype
== 'user') { // A special handle for users
354 $icon = $OUTPUT->user_picture($hitusers[$listing->userid
]);
356 $iconpath = $OUTPUT->pix_url('icon', $listing->doctype
);
357 $icon = "<img align=\"top\" src=\"".$iconpath."\" class=\"activityicon\" alt=\"\"/>";
359 $coursename = $DB->get_field('course', 'fullname', array('id' => $listing->courseid
));
360 $courseword = mb_convert_case(get_string('course', 'moodle'), MB_CASE_LOWER
, 'UTF-8');
361 $course = ($listing->doctype
!= 'user') ?
'<strong> ('.$courseword.': \''.$coursename.'\')</strong>' : '' ;
363 $title_post_processing_function = $listing->doctype
.'_link_post_processing';
364 $searchable_instance = $searchables[$listing->doctype
];
365 if ($searchable_instance->location
== 'internal'){
366 require_once "{$CFG->dirroot}/search/documents/{$listing->doctype}_document.php";
368 require_once "{$CFG->dirroot}/{$searchable_instance->location}/{$listing->doctype}/search_document.php";
370 if (function_exists($title_post_processing_function)) {
371 $listing->title
= $title_post_processing_function($listing->title
);
374 echo "<li value='".($listing->number +
1)."'><a href='"
375 .str_replace('DEFAULT_POPUP_SETTINGS', DEFAULT_POPUP_SETTINGS
,$listing->url
)
376 ."'>$icon $listing->title</a> $course<br />\n";
377 echo "{$typestr}: " . $listing->doctype
. ", {$scorestr}: " . round($listing->score
, 3);
378 if (!empty($listing->author
) && !is_numeric($listing->author
)){
379 echo ", {$authorstr}: ".$listing->author
."\n"
386 echo $OUTPUT->box_end();
390 print_string('ittook', 'search');
392 print_string('tofetchtheseresults', 'search');
398 echo $OUTPUT->box_end();
399 echo $OUTPUT->footer();