grade MDL-19797 Added set_url calls
[moodle.git] / search / query.php
CommitLineData
682d4032 1<?php
3319ef85 2 /**
3 * Global Search Engine for Moodle
4 *
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
11 *
12 * The query page - accepts a user-entered query string and returns results.
13 *
14 * Queries are boolean-aware, e.g.:
15 *
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
20 *
21 * Examples:
22 *
23 * 'earthquake +author:michael'
24 * Searches for documents written by 'michael' that contain 'earthquake'
25 *
26 * 'earthquake +doctype:wiki'
27 * Search all wiki pages for 'earthquake'
28 *
29 * '+author:helen +author:foster'
30 * All articles written by Helen Foster
31 *
32 */
70f52c70 33
3319ef85 34 /**
35 * includes and requires
36 */
37 require_once('../config.php');
964a5e92 38 require_once($CFG->dirroot.'/search/lib.php');
eef868d1 39
3319ef85 40 if ($CFG->forcelogin) {
41 require_login();
42 }
70f52c70 43
3319ef85 44 if (empty($CFG->enableglobalsearch)) {
32487831 45 print_error('globalsearchdisabled', 'search');
3319ef85 46 }
70f52c70 47
3319ef85 48 $adv = new Object();
70f52c70 49
3319ef85 50/// check for php5, but don't die yet (see line 52)
eef868d1 51
964a5e92 52 require_once($CFG->dirroot.'/search/querylib.php');
32487831 53
964a5e92 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 = stripslashes(optional_param('query_string', '', PARAM_CLEAN));
32487831 58
70f52c70 59/// discard harmfull searches
32487831 60
964a5e92 61 if (!isset($CFG->block_search_utf8dir)){
62 set_config('block_search_utf8dir', 1);
63 }
70f52c70 64
65/// discard harmfull searches
32487831 66
964a5e92 67 if (preg_match("/^[\*\?]+$/", $query_string)){
68 $query_string = '';
69 $error = get_string('fullwildcardquery','search');
70 }
70f52c70 71
964a5e92 72
73 if ($pages && isset($_SESSION['search_advanced_query'])) {
74 // if both are set, then we are busy browsing through the result pages of an advanced query
75 $adv = unserialize($_SESSION['search_advanced_query']);
76 } elseif ($advanced) {
77 // otherwise we are dealing with a new advanced query
78 unset($_SESSION['search_advanced_query']);
79 session_unregister('search_advanced_query');
70f52c70 80
964a5e92 81 // chars to strip from strings (whitespace)
82 $chars = " \t\n\r\0\x0B,-+";
70f52c70 83
964a5e92 84 // retrieve advanced query variables
85 $adv->mustappear = trim(optional_param('mustappear', '', PARAM_CLEAN), $chars);
86 $adv->notappear = trim(optional_param('notappear', '', PARAM_CLEAN), $chars);
87 $adv->canappear = trim(optional_param('canappear', '', PARAM_CLEAN), $chars);
88 $adv->module = optional_param('module', '', PARAM_CLEAN);
89 $adv->title = trim(optional_param('title', '', PARAM_CLEAN), $chars);
90 $adv->author = trim(optional_param('author', '', PARAM_CLEAN), $chars);
70f52c70 91 }
964a5e92 92
93 if ($advanced) {
94 //parse the advanced variables into a query string
95 //TODO: move out to external query class (QueryParse?)
70f52c70 96
964a5e92 97 $query_string = '';
70f52c70 98
964a5e92 99 // get all available module types adding third party modules
100 $module_types = array_merge(array('all'), array_values(search_get_document_types()));
101 $module_types = array_merge($module_types, array_values(search_get_document_types('X_SEARCH_TYPE')));
102 $adv->module = in_array($adv->module, $module_types) ? $adv->module : 'all';
70f52c70 103
964a5e92 104 // convert '1 2' into '+1 +2' for required words field
105 if (strlen(trim($adv->mustappear)) > 0) {
106 $query_string = ' +'.implode(' +', preg_split("/[\s,;]+/", $adv->mustappear));
70f52c70 107 }
108
964a5e92 109 // convert '1 2' into '-1 -2' for not wanted words field
110 if (strlen(trim($adv->notappear)) > 0) {
111 $query_string .= ' -'.implode(' -', preg_split("/[\s,;]+/", $adv->notappear));
70f52c70 112 }
113
964a5e92 114 // this field is left untouched, apart from whitespace being stripped
115 if (strlen(trim($adv->canappear)) > 0) {
116 $query_string .= ' '.implode(' ', preg_split("/[\s,;]+/", $adv->canappear));
70f52c70 117 }
118
964a5e92 119 // add module restriction
4021d539 120 $doctypestr = 'doctype';
121 $titlestr = 'title';
122 $authorstr = 'author';
964a5e92 123 if ($adv->module != 'all') {
124 $query_string .= " +{$doctypestr}:".$adv->module;
70f52c70 125 }
126
964a5e92 127 // create title search string
128 if (strlen(trim($adv->title)) > 0) {
129 $query_string .= " +{$titlestr}:".implode(" +{$titlestr}:", preg_split("/[\s,;]+/", $adv->title));
70f52c70 130 }
131
964a5e92 132 // create author search string
133 if (strlen(trim($adv->author)) > 0) {
134 $query_string .= " +{$authorstr}:".implode(" +{$authorstr}:", preg_split("/[\s,;]+/", $adv->author));
70f52c70 135 }
136
964a5e92 137 // save our options if the query is valid
138 if (!empty($query_string)) {
139 $_SESSION['search_advanced_query'] = serialize($adv);
70f52c70 140 }
141 }
964a5e92 142
143 // normalise page number
144 if ($page_number < 1) {
145 $page_number = 1;
70f52c70 146 }
964a5e92 147
148 //run the query against the index ensuring internal coding works in UTF-8
149 Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
150 $sq = new SearchQuery($query_string, $page_number, 10, false);
70f52c70 151
3319ef85 152 if (!$site = get_site()) {
153 redirect("index.php");
70f52c70 154 }
155
3319ef85 156 $strsearch = get_string('search', 'search');
157 $strquery = get_string('enteryoursearchquery', 'search');
70f52c70 158
964a5e92 159 // print the header
964a5e92 160 $site = get_site();
766ccfbd 161
162 $PAGE->navbar->add($strsearch, new moodle_url($CFG->wwwroot.'/search/index.php'));
163 $PAGE->navbar->add($strquery, new moodle_url($CFG->wwwroot.'/search/stats.php'));
164 $PAGE->set_title($strsearch);
165 $PAGE->set_heading($site->fullname);
166 echo $OUTPUT->header();
70f52c70 167
e3c7f155 168 if (!empty($error)){
169 notice ($error);
170 }
70f52c70 171
172 echo $OUTPUT->box_start();
f7a1496e 173 echo $OUTPUT->heading($strquery);
70f52c70 174
175 echo $OUTPUT->box_start();
176
3319ef85 177 $vars = get_object_vars($adv);
70f52c70 178
3319ef85 179 if (isset($vars)) {
180 foreach ($vars as $key => $value) {
964a5e92 181 // htmlentities breaks non-ascii chars ??
182 $adv->key = stripslashes($value);
183 //$adv->$key = stripslashes(htmlentities($value));
70f52c70 184 }
3319ef85 185 }
186 ?>
3319ef85 187 <form id="query" method="get" action="query.php">
70f52c70 188 <?php
189 if (!$advanced) {
3319ef85 190 ?>
964a5e92 191 <input type="text" name="query_string" length="50" value="<?php p($query_string) ?>" />&nbsp;
192 <input type="submit" value="<?php print_string('search', 'search') ?>" /> &nbsp;
3319ef85 193 <a href="query.php?a=1"><?php print_string('advancedsearch', 'search') ?></a> |
194 <a href="stats.php"><?php print_string('statistics', 'search') ?></a>
70f52c70 195 <?php
196 }
e3c7f155 197 else {
70f52c70 198 echo $OUTPUT->box_start();
3319ef85 199 ?>
200 <input type="hidden" name="a" value="<?php print $advanced; ?>"/>
70f52c70 201
3319ef85 202 <table border="0" cellpadding="3" cellspacing="3">
70f52c70 203
3319ef85 204 <tr>
205 <td width="240"><?php print_string('thesewordsmustappear', 'search') ?>:</td>
206 <td><input type="text" name="mustappear" length="50" value="<?php print $adv->mustappear; ?>" /></td>
207 </tr>
70f52c70 208
3319ef85 209 <tr>
210 <td><?php print_string('thesewordsmustnotappear', 'search') ?>:</td>
211 <td><input type="text" name="notappear" length="50" value="<?php print $adv->notappear; ?>" /></td>
212 </tr>
70f52c70 213
3319ef85 214 <tr>
215 <td><?php print_string('thesewordshelpimproverank', 'search') ?>:</td>
216 <td><input type="text" name="canappear" length="50" value="<?php print $adv->canappear; ?>" /></td>
217 </tr>
70f52c70 218
3319ef85 219 <tr>
220 <td><?php print_string('whichmodulestosearch?', 'search') ?>:</td>
221 <td>
222 <select name="module">
70f52c70 223 <?php
3319ef85 224 foreach($module_types as $mod) {
225 if ($mod == $adv->module) {
226 if ($mod != 'all'){
227 print "<option value='$mod' selected=\"selected\">".get_string('modulenameplural', $mod)."</option>\n";
228 }
229 else{
230 print "<option value='$mod' selected=\"selected\">".get_string('all', 'search')."</option>\n";
231 }
70f52c70 232 }
3319ef85 233 else {
234 if ($mod != 'all'){
235 print "<option value='$mod'>".get_string('modulenameplural', $mod)."</option>\n";
236 }
237 else{
238 print "<option value='$mod'>".get_string('all', 'search')."</option>\n";
239 }
70f52c70 240 }
241 }
3319ef85 242 ?>
243 </select>
244 </td>
245 </tr>
70f52c70 246
3319ef85 247 <tr>
248 <td><?php print_string('wordsintitle', 'search') ?>:</td>
249 <td><input type="text" name="title" length="50" value="<?php print $adv->title; ?>" /></td>
250 </tr>
70f52c70 251
3319ef85 252 <tr>
253 <td><?php print_string('authorname', 'search') ?>:</td>
254 <td><input type="text" name="author" length="50" value="<?php print $adv->author; ?>" /></td>
255 </tr>
70f52c70 256
3319ef85 257 <tr>
258 <td colspan="3" align="center"><br /><input type="submit" value="<?php print_string('search', 'search') ?>" /></td>
259 </tr>
70f52c70 260
3319ef85 261 <tr>
262 <td colspan="3" align="center">
263 <table border="0" cellpadding="0" cellspacing="0">
264 <tr>
265 <td><a href="query.php"><?php print_string('normalsearch', 'search') ?></a> |</td>
266 <td>&nbsp;<a href="stats.php"><?php print_string('statistics', 'search') ?></a></td>
267 </tr>
268 </table>
269 </td>
270 </tr>
6e780562 271 </table>
3319ef85 272 <?php
70f52c70 273 echo $OUTPUT->box_end();
274 }
3319ef85 275 ?>
276 </form>
277 <br/>
70f52c70 278
4021d539 279 <div align="center">
3319ef85 280 <?php
281 print_string('searching', 'search') . ': ';
70f52c70 282
3319ef85 283 if ($sq->is_valid_index()) {
284 //use cached variable to show up-to-date index size (takes deletions into account)
285 print $CFG->search_index_size;
70f52c70 286 }
e3c7f155 287 else {
3319ef85 288 print "0";
70f52c70 289 }
290
3319ef85 291 print ' ';
292 print_string('documents', 'search');
293 print '.';
70f52c70 294
e3c7f155 295 if (!$sq->is_valid_index() and has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
3319ef85 296 print '<p>' . get_string('noindexmessage', 'search') . '<a href="indexersplash.php">' . get_string('createanindex', 'search')."</a></p>\n";
70f52c70 297 }
298
3319ef85 299 ?>
300 </div>
301 <?php
70f52c70 302 echo $OUTPUT->box_end();
303
3efa38a4 304/// prints all the results in a box
305
3319ef85 306 if ($sq->is_valid()) {
70f52c70 307 echo $OUTPUT->box_start();
308
3319ef85 309 search_stopwatch();
310 $hit_count = $sq->count();
70f52c70 311
3319ef85 312 print "<br />";
70f52c70 313
decf9db1 314 print $hit_count.' '.get_string('resultsreturnedfor', 'search') . " '".s($query_string)."'.";
3319ef85 315 print "<br />";
70f52c70 316
3319ef85 317 if ($hit_count > 0) {
318 $page_links = $sq->page_numbers();
319 $hits = $sq->results();
70f52c70 320
3319ef85 321 if ($advanced) {
322 // if in advanced mode, search options are saved in the session, so
323 // we can remove the query string var from the page links, and replace
324 // it with a=1 (Advanced = on) instead
325 $page_links = preg_replace("/query_string=[^&]+/", 'a=1', $page_links);
70f52c70 326 }
327
3319ef85 328 print "<ol>";
70f52c70 329
3319ef85 330 $typestr = get_string('type', 'search');
331 $scorestr = get_string('score', 'search');
332 $authorstr = get_string('author', 'search');
3efa38a4 333
334 $searchables = search_collect_searchables(false, false);
335
70f52c70 336 foreach ($hits as $listing) {
337
338 if ($listing->doctype == 'user'){ // A special handle for users
c5339263 339 $icon = $OUTPUT->user_picture(moodle_user_picture($listing->userid, 0));
964a5e92 340 } else {
e63f88c9 341 $iconpath = $OUTPUT->mod_icon_url('icon', $listing->doctype);
964a5e92 342 $icon = "<img align=\"top\" src=\"".$iconpath."\" class=\"activityicon\" alt=\"\"/>";
343 }
344 $coursename = $DB->get_field('course', 'fullname', array('id' => $listing->courseid));
3efa38a4 345 $courseword = mb_convert_case(get_string('course', 'moodle'), MB_CASE_LOWER, 'UTF-8');
964a5e92 346 $course = ($listing->doctype != 'user') ? '<strong> ('.$courseword.': \''.$coursename.'\')</strong>' : '' ;
347
3319ef85 348 $title_post_processing_function = $listing->doctype.'_link_post_processing';
3efa38a4 349 $searchable_instance = $searchables[$listing->doctype];
350 if ($searchable_instance->location == 'internal'){
351 require_once "{$CFG->dirroot}/search/documents/{$listing->doctype}_document.php";
352 } else {
353 require_once "{$CFG->dirroot}/{$searchable_instance->location}/{$listing->doctype}/search_document.php";
354 }
3319ef85 355 if (function_exists($title_post_processing_function)) {
356 $listing->title = $title_post_processing_function($listing->title);
357 }
70f52c70 358
964a5e92 359 echo "<li value='".($listing->number + 1)."'><a href='"
3efa38a4 360 .str_replace('DEFAULT_POPUP_SETTINGS', DEFAULT_POPUP_SETTINGS ,$listing->url)
964a5e92 361 ."'>$icon $listing->title</a> $course<br />\n";
362 echo "{$typestr}: " . $listing->doctype . ", {$scorestr}: " . round($listing->score, 3);
363 if (!empty($listing->author) && !is_numeric($listing->author)){
3efa38a4 364 echo ", {$authorstr}: ".$listing->author."\n"
b93b987d 365 ."</li>\n";
366 }
70f52c70 367 }
3efa38a4 368 echo "</ol>";
369 echo $page_links;
70f52c70 370 }
371 echo $OUTPUT->box_end();
3319ef85 372 ?>
4021d539 373 <div align="center">
70f52c70 374 <?php
3319ef85 375 print_string('ittook', 'search');
70f52c70 376 search_stopwatch();
3319ef85 377 print_string('tofetchtheseresults', 'search');
378 ?>.
379 </div>
70f52c70 380
3319ef85 381 <?php
382 }
70f52c70 383 echo $OUTPUT->box_end();
daa2cd33 384 echo $OUTPUT->footer();
f7a1496e 385?>