MDL-14679 towards tags conversion
[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 */
33
34 /**
35 * includes and requires
36 */
37 require_once('../config.php');
38 require_once("$CFG->dirroot/search/lib.php");
eef868d1 39
eef868d1 40
3319ef85 41 if ($CFG->forcelogin) {
42 require_login();
43 }
44
45 if (empty($CFG->enableglobalsearch)) {
adddd26d 46 error(get_string('globalsearchdisabled', 'search'));
3319ef85 47 }
48
49 $adv = new Object();
50
51/// check for php5, but don't die yet (see line 52)
eef868d1 52
e3c7f155 53 if ($check = search_check_php5()) {
54 require_once("{$CFG->dirroot}/search/querylib.php");
55
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 = optional_param('query_string', '', PARAM_CLEAN);
dd93d8e9 60
e3c7f155 61 /**
62 * discard harmfull searches
63 */
64 if (preg_match("/^[\*\?]+$/", $query_string)){
65 $query_string = '';
66 $error = get_string('fullwildcardquery','search');
67 }
dd93d8e9 68
e3c7f155 69
70 if ($pages && isset($_SESSION['search_advanced_query'])) {
71 // if both are set, then we are busy browsing through the result pages of an advanced query
72 $adv = unserialize($_SESSION['search_advanced_query']);
73 } elseif ($advanced) {
74 // otherwise we are dealing with a new advanced query
75 unset($_SESSION['search_advanced_query']);
76 session_unregister('search_advanced_query');
77
78 // chars to strip from strings (whitespace)
79 $chars = " \t\n\r\0\x0B,-+";
80
81 // retrieve advanced query variables
82 $adv->mustappear = trim(optional_param('mustappear', '', PARAM_CLEAN), $chars);
83 $adv->notappear = trim(optional_param('notappear', '', PARAM_CLEAN), $chars);
84 $adv->canappear = trim(optional_param('canappear', '', PARAM_CLEAN), $chars);
85 $adv->module = optional_param('module', '', PARAM_CLEAN);
86 $adv->title = trim(optional_param('title', '', PARAM_CLEAN), $chars);
87 $adv->author = trim(optional_param('author', '', PARAM_CLEAN), $chars);
dd93d8e9 88 }
e3c7f155 89
90 if ($advanced) {
91 //parse the advanced variables into a query string
92 //TODO: move out to external query class (QueryParse?)
93
94 $query_string = '';
95
96 // get all available module types
97 $module_types = array_merge(array('all'), array_values(search_get_document_types()));
98 $adv->module = in_array($adv->module, $module_types) ? $adv->module : 'all';
99
100 // convert '1 2' into '+1 +2' for required words field
101 if (strlen(trim($adv->mustappear)) > 0) {
102 $query_string = ' +'.implode(' +', preg_split("/[\s,;]+/", $adv->mustappear));
103 }
104
105 // convert '1 2' into '-1 -2' for not wanted words field
106 if (strlen(trim($adv->notappear)) > 0) {
107 $query_string .= ' -'.implode(' -', preg_split("/[\s,;]+/", $adv->notappear));
108 }
109
110 // this field is left untouched, apart from whitespace being stripped
111 if (strlen(trim($adv->canappear)) > 0) {
112 $query_string .= ' '.implode(' ', preg_split("/[\s,;]+/", $adv->canappear));
113 }
114
115 // add module restriction
116 $doctypestr = get_string('doctype', 'search');
117 $titlestr = get_string('title', 'search');
118 $authorstr = get_string('author', 'search');
119 if ($adv->module != 'all') {
120 $query_string .= " +{$doctypestr}:".$adv->module;
121 }
122
123 // create title search string
124 if (strlen(trim($adv->title)) > 0) {
125 $query_string .= " +{$titlestr}:".implode(" +{$titlestr}:", preg_split("/[\s,;]+/", $adv->title));
126 }
127
128 // create author search string
129 if (strlen(trim($adv->author)) > 0) {
130 $query_string .= " +{$authorstr}:".implode(" +{$authorstr}:", preg_split("/[\s,;]+/", $adv->author));
131 }
132
133 // save our options if the query is valid
134 if (!empty($query_string)) {
135 $_SESSION['search_advanced_query'] = serialize($adv);
136 }
dd93d8e9 137 }
e3c7f155 138
139 // normalise page number
140 if ($page_number < 1) {
141 $page_number = 1;
f1776f8f 142 }
e3c7f155 143
144 //run the query against the index
145 Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());
146 $sq = new SearchQuery($query_string, $page_number, 10, false);
f1776f8f 147 }
e3c7f155 148
3319ef85 149 if (!$site = get_site()) {
150 redirect("index.php");
f1776f8f 151 }
3319ef85 152
153 $strsearch = get_string('search', 'search');
154 $strquery = get_string('enteryoursearchquery', 'search');
155
adddd26d 156// if ($CFG->version < 2007032200){ NOT RELIABLE
157 if (!function_exists('build_navigation')){
3319ef85 158 print_header("$site->shortname: $strsearch: $strquery", "$site->fullname",
159 "<a href=\"index.php\">$strsearch</a> -> $strquery");
160 } else {
161 $navlinks[] = array('name' => $strsearch, 'link' => "index.php", 'type' => 'misc');
162 $navlinks[] = array('name' => $strquery, 'link' => null, 'type' => 'misc');
163 $navigation = build_navigation($navlinks);
164 $site = get_site();
165 print_header("$strsearch", "$site->fullname" , $navigation, "", "", true, "&nbsp;", navmenu($site));
166 }
167
e3c7f155 168 //keep things pretty, even if php5 isn't available
169 if (!$check) {
170 print_heading(search_check_php5(true));
171 print_footer();
172 exit(0);
173 }
174
175 if (!empty($error)){
176 notice ($error);
177 }
178
2f338ab5 179 print_box_start();
3319ef85 180 print_heading($strquery);
181
182 print_box_start();
183
184 $vars = get_object_vars($adv);
185
186 if (isset($vars)) {
187 foreach ($vars as $key => $value) {
188 // htmlentities breaks non-ascii chars
189 $adv->key = stripslashes($value);
190 //$adv->$key = stripslashes(htmlentities($value));
f1776f8f 191 }
3319ef85 192 }
193 ?>
3319ef85 194 <form id="query" method="get" action="query.php">
195 <?php
196 if (!$advanced) {
197 ?>
198 <input type="text" name="query_string" length="50" value="<?php print stripslashes($query_string) ?>" />
199 &nbsp;<input type="submit" value="<?php print_string('search', 'search') ?>" /> &nbsp;
200 <a href="query.php?a=1"><?php print_string('advancedsearch', 'search') ?></a> |
201 <a href="stats.php"><?php print_string('statistics', 'search') ?></a>
202 <?php
e3c7f155 203 }
204 else {
3319ef85 205 print_box_start();
206 ?>
207 <input type="hidden" name="a" value="<?php print $advanced; ?>"/>
208
209 <table border="0" cellpadding="3" cellspacing="3">
210
211 <tr>
212 <td width="240"><?php print_string('thesewordsmustappear', 'search') ?>:</td>
213 <td><input type="text" name="mustappear" length="50" value="<?php print $adv->mustappear; ?>" /></td>
214 </tr>
215
216 <tr>
217 <td><?php print_string('thesewordsmustnotappear', 'search') ?>:</td>
218 <td><input type="text" name="notappear" length="50" value="<?php print $adv->notappear; ?>" /></td>
219 </tr>
220
221 <tr>
222 <td><?php print_string('thesewordshelpimproverank', 'search') ?>:</td>
223 <td><input type="text" name="canappear" length="50" value="<?php print $adv->canappear; ?>" /></td>
224 </tr>
225
226 <tr>
227 <td><?php print_string('whichmodulestosearch?', 'search') ?>:</td>
228 <td>
229 <select name="module">
230 <?php
231 foreach($module_types as $mod) {
232 if ($mod == $adv->module) {
233 if ($mod != 'all'){
234 print "<option value='$mod' selected=\"selected\">".get_string('modulenameplural', $mod)."</option>\n";
235 }
236 else{
237 print "<option value='$mod' selected=\"selected\">".get_string('all', 'search')."</option>\n";
238 }
239 }
240 else {
241 if ($mod != 'all'){
242 print "<option value='$mod'>".get_string('modulenameplural', $mod)."</option>\n";
243 }
244 else{
245 print "<option value='$mod'>".get_string('all', 'search')."</option>\n";
246 }
247 }
248 }
249 ?>
250 </select>
251 </td>
252 </tr>
253
254 <tr>
255 <td><?php print_string('wordsintitle', 'search') ?>:</td>
256 <td><input type="text" name="title" length="50" value="<?php print $adv->title; ?>" /></td>
257 </tr>
258
259 <tr>
260 <td><?php print_string('authorname', 'search') ?>:</td>
261 <td><input type="text" name="author" length="50" value="<?php print $adv->author; ?>" /></td>
262 </tr>
263
264 <tr>
265 <td colspan="3" align="center"><br /><input type="submit" value="<?php print_string('search', 'search') ?>" /></td>
266 </tr>
267
268 <tr>
269 <td colspan="3" align="center">
270 <table border="0" cellpadding="0" cellspacing="0">
271 <tr>
272 <td><a href="query.php"><?php print_string('normalsearch', 'search') ?></a> |</td>
273 <td>&nbsp;<a href="stats.php"><?php print_string('statistics', 'search') ?></a></td>
274 </tr>
275 </table>
276 </td>
277 </tr>
6e780562 278 </table>
3319ef85 279 <?php
280 print_box_end();
281 }
282 ?>
283 </form>
284 <br/>
285
286 <div align="center">
287 <?php
288 print_string('searching', 'search') . ': ';
289
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;
e3c7f155 293 }
294 else {
3319ef85 295 print "0";
f1776f8f 296 }
f1776f8f 297
3319ef85 298 print ' ';
299 print_string('documents', 'search');
300 print '.';
f1776f8f 301
e3c7f155 302 if (!$sq->is_valid_index() and has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
3319ef85 303 print '<p>' . get_string('noindexmessage', 'search') . '<a href="indexersplash.php">' . get_string('createanindex', 'search')."</a></p>\n";
304 }
f1776f8f 305
3319ef85 306 ?>
307 </div>
308 <?php
309 print_box_end();
f1776f8f 310
3319ef85 311 // prints all the results in a box
312 if ($sq->is_valid()) {
313 print_box_start();
f1776f8f 314
3319ef85 315 search_stopwatch();
316 $hit_count = $sq->count();
f1776f8f 317
3319ef85 318 print "<br />";
f1776f8f 319
3319ef85 320 print $hit_count.' '.get_string('resultsreturnedfor', 'search') . " '".stripslashes($query_string)."'.";
321 print "<br />";
f1776f8f 322
3319ef85 323 if ($hit_count > 0) {
324 $page_links = $sq->page_numbers();
325 $hits = $sq->results();
326
327 if ($advanced) {
328 // if in advanced mode, search options are saved in the session, so
329 // we can remove the query string var from the page links, and replace
330 // it with a=1 (Advanced = on) instead
331 $page_links = preg_replace("/query_string=[^&]+/", 'a=1', $page_links);
332 }
333
334 print "<ol>";
335
336 $typestr = get_string('type', 'search');
337 $scorestr = get_string('score', 'search');
338 $authorstr = get_string('author', 'search');
339 foreach ($hits as $listing) {
340 //if ($CFG->unicodedb) {
341 //$listing->title = mb_convert_encoding($listing->title, 'auto', 'UTF8');
342 //}
343 $title_post_processing_function = $listing->doctype.'_link_post_processing';
344 require_once "{$CFG->dirroot}/search/documents/{$listing->doctype}_document.php";
345 if (function_exists($title_post_processing_function)) {
346 $listing->title = $title_post_processing_function($listing->title);
347 }
348
349 print "<li value='".($listing->number+1)."'><a href='".str_replace('DEFAULT_POPUP_SETTINGS', DEFAULT_POPUP_SETTINGS ,$listing->url)."'>$listing->title</a><br />\n"
350 ."<em>".search_shorten_url($listing->url, 70)."</em><br />\n"
b93b987d 351 ."{$typestr}: ".$listing->doctype.", {$scorestr}: ".round($listing->score, 3);
352 if (!empty($listing->author)){
353 print ", {$authorstr}: ".$listing->author."\n"
354 ."</li>\n";
355 }
356 }
3319ef85 357 print "</ol>";
358 print $page_links;
b93b987d 359 }
3319ef85 360 print_box_end();
361 ?>
362 <div align="center">
363 <?php
364 print_string('ittook', 'search');
365 search_stopwatch();
366 print_string('tofetchtheseresults', 'search');
367 ?>.
368 </div>
369
370 <?php
371 }
2f338ab5 372 print_box_end();
3319ef85 373 print_footer();
adddd26d 374?>