fixing header/formatting
[moodle.git] / search / query.php
CommitLineData
682d4032 1<?php
2f338ab5 2/**
3* Global Search Engine for Moodle
4* Michael Champanis (mchampan) [cynnical@gmail.com]
f1776f8f 5* review 1.8+ : Valery Fremaux [valery.fremaux@club-internet.fr]
2f338ab5 6* 2007/08/02
7*
8* The query page - accepts a user-entered query string and returns results.
9*
10* Queries are boolean-aware, e.g.:
11*
12* '+' term required
13* '-' term must not be present
14* '' (no modifier) term's presence increases rank, but isn't required
15* 'field:' search this field
16*
17* Examples:
18*
19* 'earthquake +author:michael'
20* Searches for documents written by 'michael' that contain 'earthquake'
21*
22* 'earthquake +doctype:wiki'
23* Search all wiki pages for 'earthquake'
24*
25* '+author:helen +author:foster'
26* All articles written by Helen Foster
27*
28*/
29
30require_once('../config.php');
31require_once("$CFG->dirroot/search/lib.php");
32
33if ($CFG->forcelogin) {
721d74b0 34 require_login();
2f338ab5 35}
721d74b0 36
2f338ab5 37if (empty($CFG->enableglobalsearch)) {
38 error(get_string('globalsearchdisabled', 'search'));
39}
721d74b0 40
2f338ab5 41$adv = new Object();
6fbe0284 42
2f338ab5 43// check for php5, but don't die yet (see line 52)
44if ($check = search_check_php5()) {
45 require_once("{$CFG->dirroot}/search/querylib.php");
eef868d1 46
791a4cec 47 $page_number = optional_param('page', -1, PARAM_INT);
eef868d1 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);
51
6e780562 52 if ($pages && isset($_SESSION['search_advanced_query'])) {
2f338ab5 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']);
f1776f8f 55 }
2f338ab5 56 else if ($advanced) {
57 // otherwise we are dealing with a new advanced query
58 unset($_SESSION['search_advanced_query']);
59 session_unregister('search_advanced_query');
f1776f8f 60
2f338ab5 61 // chars to strip from strings (whitespace)
62 $chars = " \t\n\r\0\x0B,-+";
f1776f8f 63
2f338ab5 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);
f1776f8f 71 }
eef868d1 72
6e780562 73 if ($advanced) {
2f338ab5 74 //parse the advanced variables into a query string
75 //TODO: move out to external query class (QueryParse?)
f1776f8f 76
2f338ab5 77 $query_string = '';
f1776f8f 78
2f338ab5 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';
f1776f8f 82
2f338ab5 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));
f1776f8f 86 }
87
2f338ab5 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));
f1776f8f 91 }
92
2f338ab5 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));
f1776f8f 96 }
97
2f338ab5 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;
f1776f8f 104 }
105
2f338ab5 106 // create title search string
107 if (strlen(trim($adv->title)) > 0) {
108 $query_string .= " +{$titlestr}:".implode(" +{$titlestr}:", preg_split("/[\s,;]+/", $adv->title));
f1776f8f 109 }
110
2f338ab5 111 // create author search string
112 if (strlen(trim($adv->author)) > 0) {
113 $query_string .= " +{$authorstr}:".implode(" +{$authorstr}:", preg_split("/[\s,;]+/", $adv->author));
f1776f8f 114 }
115
2f338ab5 116 // save our options if the query is valid
117 if (!empty($query_string)) {
118 $_SESSION['search_advanced_query'] = serialize($adv);
f1776f8f 119 }
120 }
2f338ab5 121
122 // normalise page number
a25a3912 123 if ($page_number < 1) {
2f338ab5 124 $page_number = 1;
f1776f8f 125 }
eef868d1 126
791a4cec 127 //run the query against the index
eef868d1 128 $sq = new SearchQuery($query_string, $page_number, 10, false);
f1776f8f 129}
eef868d1 130
2f338ab5 131if (!$site = get_site()) {
682d4032 132 redirect("index.php");
f1776f8f 133}
eef868d1 134
001f3652 135$strsearch = get_string('search', 'search');
2f338ab5 136$strquery = get_string('enteryoursearchquery', 'search');
f1776f8f 137
001f3652 138$navlinks[] = array('name' => $strsearch, 'link' => "index.php", 'type' => 'misc');
139$navlinks[] = array('name' => $strquery, 'link' => null, 'type' => 'misc');
140$navigation = build_navigation($navlinks);
d052434c 141$site = get_site();
142print_header("$strsearch", "$site->fullname" , $navigation, "", "", true, "&nbsp;", navmenu($site));
eef868d1 143
2f338ab5 144//keep things pretty, even if php5 isn't available
145if (!$check) {
682d4032 146 print_heading(search_check_php5(true));
147 print_footer();
148 exit(0);
f1776f8f 149}
eef868d1 150
2f338ab5 151print_box_start();
152print_heading($strquery);
eef868d1 153
2f338ab5 154print_box_start();
eef868d1 155
2f338ab5 156$vars = get_object_vars($adv);
eef868d1 157
2f338ab5 158if (isset($vars)) {
6fbe0284 159 foreach ($vars as $key => $value) {
001f3652 160 // htmlentities breaks non-ascii chars
161 $adv->key = stripslashes($value);
162 //$adv->$key = stripslashes(htmlentities($value));
f1776f8f 163 }
2f338ab5 164}
682d4032 165?>
166
b7dc2256 167<form id="query" method="get" action="query.php">
f1776f8f 168<?php
169if (!$advanced) {
2f338ab5 170?>
171 <input type="text" name="query_string" length="50" value="<?php print stripslashes($query_string) ?>" />
172 &nbsp;<input type="submit" value="<?php print_string('search', 'search') ?>" /> &nbsp;
173 <a href="query.php?a=1"><?php print_string('advancedsearch', 'search') ?></a> |
174 <a href="stats.php"><?php print_string('statistics', 'search') ?></a>
f1776f8f 175<?php
176}
2f338ab5 177else {
178 print_box_start();
eef868d1 179 ?>
6e780562 180 <input type="hidden" name="a" value="<?php print $advanced; ?>"/>
eef868d1 181
6e780562 182 <table border="0" cellpadding="3" cellspacing="3">
eef868d1 183
6e780562 184 <tr>
2f338ab5 185 <td width="240"><?php print_string('thesewordsmustappear', 'search') ?>:</td>
6fbe0284 186 <td><input type="text" name="mustappear" length="50" value="<?php print $adv->mustappear; ?>" /></td>
6e780562 187 </tr>
eef868d1 188
6e780562 189 <tr>
2f338ab5 190 <td><?php print_string('thesewordsmustnotappear', 'search') ?>:</td>
6fbe0284 191 <td><input type="text" name="notappear" length="50" value="<?php print $adv->notappear; ?>" /></td>
6e780562 192 </tr>
193
194 <tr>
2f338ab5 195 <td><?php print_string('thesewordshelpimproverank', 'search') ?>:</td>
6fbe0284 196 <td><input type="text" name="canappear" length="50" value="<?php print $adv->canappear; ?>" /></td>
eef868d1 197 </tr>
198
6e780562 199 <tr>
2f338ab5 200 <td><?php print_string('whichmodulestosearch?', 'search') ?>:</td>
6e780562 201 <td>
eef868d1 202 <select name="module">
f1776f8f 203<?php
2f338ab5 204 foreach($module_types as $mod) {
205 if ($mod == $adv->module) {
206 if ($mod != 'all'){
207 print "<option value='$mod' selected=\"selected\">".get_string('modulenameplural', $mod)."</option>\n";
208 }
209 else{
210 print "<option value='$mod' selected=\"selected\">".get_string('all', 'search')."</option>\n";
211 }
f1776f8f 212 }
2f338ab5 213 else {
214 if ($mod != 'all'){
215 print "<option value='$mod'>".get_string('modulenameplural', $mod)."</option>\n";
216 }
217 else{
218 print "<option value='$mod'>".get_string('all', 'search')."</option>\n";
219 }
f1776f8f 220 }
221 }
2f338ab5 222?>
6e780562 223 </select>
224 </td>
225 </tr>
eef868d1 226
6e780562 227 <tr>
2f338ab5 228 <td><?php print_string('wordsintitle', 'search') ?>:</td>
6fbe0284 229 <td><input type="text" name="title" length="50" value="<?php print $adv->title; ?>" /></td>
6e780562 230 </tr>
eef868d1 231
6e780562 232 <tr>
2f338ab5 233 <td><?php print_string('authorname', 'search') ?>:</td>
6fbe0284 234 <td><input type="text" name="author" length="50" value="<?php print $adv->author; ?>" /></td>
6e780562 235 </tr>
eef868d1 236
6e780562 237 <tr>
2f338ab5 238 <td colspan="3" align="center"><br /><input type="submit" value="<?php print_string('search', 'search') ?>" /></td>
6e780562 239 </tr>
eef868d1 240
6e780562 241 <tr>
242 <td colspan="3" align="center">
243 <table border="0" cellpadding="0" cellspacing="0">
244 <tr>
2f338ab5 245 <td><a href="query.php"><?php print_string('normalsearch', 'search') ?></a> |</td>
246 <td>&nbsp;<a href="stats.php"><?php print_string('statistics', 'search') ?></a></td>
6e780562 247 </tr>
248 </table>
249 </td>
250 </tr>
eef868d1 251 </table>
2f338ab5 252<?php
253 print_box_end();
f1776f8f 254 }
2f338ab5 255?>
682d4032 256</form>
71502268 257<br/>
682d4032 258
2f338ab5 259<div align="center">
682d4032 260<?php
2f338ab5 261print_string('searching', 'search') . ': ';
6e780562 262
2f338ab5 263if ($sq->is_valid_index()) {
791a4cec 264 //use cached variable to show up-to-date index size (takes deletions into account)
265 print $CFG->search_index_size;
f1776f8f 266}
2f338ab5 267else {
b585dc5f 268 print "0";
f1776f8f 269}
eef868d1 270
2f338ab5 271print ' ';
272print_string('documents', 'search');
273print '.';
682d4032 274
2f338ab5 275if (!$sq->is_valid_index() and isadmin()) {
276 print '<p>' . get_string('noindexmessage', 'search') . '<a href="indexersplash.php">' . get_string('createanindex', 'search')."</a></p>\n";
f1776f8f 277}
6e780562 278
2f338ab5 279?>
280</div>
281<?php
282print_box_end();
eef868d1 283
2f338ab5 284// prints all the results in a box
285if ($sq->is_valid()) {
286 print_box_start();
f1776f8f 287
eef868d1 288 search_stopwatch();
289 $hit_count = $sq->count();
f1776f8f 290
6fbe0284 291 print "<br />";
f1776f8f 292
2f338ab5 293 print $hit_count.' '.get_string('resultsreturnedfor', 'search') . " '".stripslashes($query_string)."'.";
6fbe0284 294 print "<br />";
f1776f8f 295
eef868d1 296 if ($hit_count > 0) {
2f338ab5 297 $page_links = $sq->page_numbers();
298 $hits = $sq->results();
f1776f8f 299
2f338ab5 300 if ($advanced) {
301 // if in advanced mode, search options are saved in the session, so
302 // we can remove the query string var from the page links, and replace
303 // it with a=1 (Advanced = on) instead
304 $page_links = preg_replace("/query_string=[^&]+/", 'a=1', $page_links);
f1776f8f 305 }
306
2f338ab5 307 print "<ol>";
f1776f8f 308
2f338ab5 309 $typestr = get_string('type', 'search');
310 $scorestr = get_string('score', 'search');
311 $authorstr = get_string('author', 'search');
312 foreach ($hits as $listing) {
001f3652 313 //if ($CFG->unicodedb) {
314 //$listing->title = mb_convert_encoding($listing->title, 'auto', 'UTF8');
315 //}
2f338ab5 316 $title_post_processing_function = $listing->doctype.'_link_post_processing';
317 require_once "{$CFG->dirroot}/search/documents/{$listing->doctype}_document.php";
001f3652 318 if (function_exists($title_post_processing_function)) {
2f338ab5 319 $listing->title = $title_post_processing_function($listing->title);
001f3652 320 }
321
2f338ab5 322 print "<li value='".($listing->number+1)."'><a href='".str_replace('DEFAULT_POPUP_SETTINGS', DEFAULT_POPUP_SETTINGS ,$listing->url)."'>$listing->title</a><br />\n"
323 ."<em>".search_shorten_url($listing->url, 70)."</em><br />\n"
324 ."{$typestr}: ".$listing->doctype.", {$scorestr}: ".round($listing->score, 3).", {$authorstr}: ".$listing->author."\n"
325 ."</li>\n";
326 }
f1776f8f 327
2f338ab5 328 print "</ol>";
329 print $page_links;
f1776f8f 330 }
2f338ab5 331
332 print_box_end();
682d4032 333?>
682d4032 334<div align="center">
f1776f8f 335<?php
2f338ab5 336 print_string('ittook', 'search');
f1776f8f 337 search_stopwatch();
2f338ab5 338 print_string('tofetchtheseresults', 'search');
339?>.
682d4032 340</div>
341
342<?php
2f338ab5 343}
344print_box_end();
345print_footer();
f1776f8f 346?>