weekly release 1.9.14+
[moodle.git] / search / query.php
blobccca0d29acb87f1013e923f1ae7fe6dd91dd0821
1 <?php
2 /**
3 * Global Search Engine for Moodle
5 * @package search
6 * @category core
7 * @subpackage search_engine
8 * @author Michael Champanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
9 * @date 2008/03/31
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.:
16 * '+' term required
17 * '-' term must not be present
18 * '' (no modifier) term's presence increases rank, but isn't required
19 * 'field:' search this field
21 * Examples:
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
34 /**
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) {
43 require_login();
46 if (empty($CFG->enableglobalsearch)) {
47 error(get_string('globalsearchdisabled', 'search'));
50 $adv = new Object();
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)){
70 $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);
95 if ($advanced) {
96 //parse the advanced variables into a query string
97 //TODO: move out to external query class (QueryParse?)
99 $query_string = '';
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';
123 $titlestr = 'title';
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) {
147 $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");
165 } else {
166 $navlinks[] = array('name' => $strsearch, 'link' => "index.php", 'type' => 'misc');
167 $navlinks[] = array('name' => $strquery, 'link' => null, 'type' => 'misc');
168 $navigation = build_navigation($navlinks);
169 $site = get_site();
170 print_header("$strsearch", "$site->fullname" , $navigation, "", "", true, "&nbsp;", navmenu($site));
173 if (!empty($error)){
174 notice ($error);
177 print_box_start();
178 print_heading($strquery);
180 print_box_start();
182 $vars = get_object_vars($adv);
184 if (isset($vars)) {
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">
193 <?php
194 if (!$advanced) {
196 <input type="text" name="query_string" length="50" value="<?php p($query_string) ?>" />
197 &nbsp;<input type="submit" value="<?php print_string('search', 'search') ?>" /> &nbsp;
198 <a href="query.php?a=1"><?php print_string('advancedsearch', 'search') ?></a> |
199 <a href="stats.php"><?php print_string('statistics', 'search') ?></a>
200 <?php
202 else {
203 print_box_start();
205 <input type="hidden" name="a" value="<?php p($advanced); ?>"/>
207 <table border="0" cellpadding="3" cellspacing="3">
209 <tr>
210 <td width="240"><?php print_string('thesewordsmustappear', 'search') ?>:</td>
211 <td><input type="text" name="mustappear" length="50" value="<?php p($adv->mustappear); ?>" /></td>
212 </tr>
214 <tr>
215 <td><?php print_string('thesewordsmustnotappear', 'search') ?>:</td>
216 <td><input type="text" name="notappear" length="50" value="<?php p($adv->notappear); ?>" /></td>
217 </tr>
219 <tr>
220 <td><?php print_string('thesewordshelpimproverank', 'search') ?>:</td>
221 <td><input type="text" name="canappear" length="50" value="<?php p($adv->canappear); ?>" /></td>
222 </tr>
224 <tr>
225 <td><?php print_string('whichmodulestosearch?', 'search') ?>:</td>
226 <td>
227 <select name="module">
228 <?php
229 foreach($module_types as $mod) {
230 if ($mod == $adv->module) {
231 if ($mod != 'all'){
232 print "<option value='$mod' selected=\"selected\">".get_string('modulenameplural', $mod)."</option>\n";
234 else{
235 print "<option value='$mod' selected=\"selected\">".get_string('all', 'search')."</option>\n";
238 else {
239 if ($mod != 'all'){
240 print "<option value='$mod'>".get_string('modulenameplural', $mod)."</option>\n";
242 else{
243 print "<option value='$mod'>".get_string('all', 'search')."</option>\n";
248 </select>
249 </td>
250 </tr>
252 <tr>
253 <td><?php print_string('wordsintitle', 'search') ?>:</td>
254 <td><input type="text" name="title" length="50" value="<?php p($adv->title); ?>" /></td>
255 </tr>
257 <tr>
258 <td><?php print_string('authorname', 'search') ?>:</td>
259 <td><input type="text" name="author" length="50" value="<?php p($adv->author); ?>" /></td>
260 </tr>
262 <tr>
263 <td colspan="3" align="center"><br /><input type="submit" value="<?php p(get_string('search', 'search')) ?>" /></td>
264 </tr>
266 <tr>
267 <td colspan="3" align="center">
268 <table border="0" cellpadding="0" cellspacing="0">
269 <tr>
270 <td><a href="query.php"><?php print_string('normalsearch', 'search') ?></a> |</td>
271 <td>&nbsp;<a href="stats.php"><?php print_string('statistics', 'search') ?></a></td>
272 </tr>
273 </table>
274 </td>
275 </tr>
276 </table>
277 <?php
278 print_box_end();
281 </form>
282 <br/>
284 <div align="center">
285 <?php
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;
292 else {
293 print "0";
296 print ' ';
297 print_string('documents', 'search');
298 print '.';
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";
305 </div>
306 <?php
307 print_box_end();
309 /// prints all the results in a box
311 if ($sq->is_valid()) {
312 print_box_start();
314 search_stopwatch();
315 $hit_count = $sq->count();
317 print "<br />";
319 print $hit_count.' '.get_string('resultsreturnedfor', 'search') . " '".s($query_string)."'.";
320 print "<br />";
322 if ($hit_count > 0) {
323 $page_links = $sq->page_numbers();
324 $hits = $sq->results();
326 if ($advanced) {
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);
333 print "<ol>";
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) ;
345 } else {
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";
357 } else {
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"
372 ."</li>\n";
375 echo "</ol>";
376 echo $page_links;
378 print_box_end();
380 <div align="center">
381 <?php
382 print_string('ittook', 'search');
383 search_stopwatch();
384 print_string('tofetchtheseresults', 'search');
386 </div>
388 <?php
390 print_box_end();
391 print_footer();