MDL-10680:
[moodle-linuxchix.git] / search / query.php
blob97526fd1800980df8f59085b0a48e646d8090d34
1 <?php
2 /**
3 * Global Search Engine for Moodle
4 * Michael Champanis (mchampan) [cynnical@gmail.com]
5 * review 1.8+ : Valery Fremaux [valery.fremaux@club-internet.fr]
6 * 2007/08/02
8 * The query page - accepts a user-entered query string and returns results.
10 * Queries are boolean-aware, e.g.:
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
17 * Examples:
19 * 'earthquake +author:michael'
20 * Searches for documents written by 'michael' that contain 'earthquake'
22 * 'earthquake +doctype:wiki'
23 * Search all wiki pages for 'earthquake'
25 * '+author:helen +author:foster'
26 * All articles written by Helen Foster
30 require_once('../config.php');
31 require_once("$CFG->dirroot/search/lib.php");
33 if ($CFG->forcelogin) {
34 require_login();
37 if (empty($CFG->enableglobalsearch)) {
38 error(get_string('globalsearchdisabled', 'search'));
41 $adv = new Object();
43 // check for php5, but don't die yet (see line 52)
44 if ($check = search_check_php5()) {
45 require_once("{$CFG->dirroot}/search/querylib.php");
47 $page_number = optional_param('page', -1, PARAM_INT);
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);
52 if ($pages && isset($_SESSION['search_advanced_query'])) {
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']);
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');
61 // chars to strip from strings (whitespace)
62 $chars = " \t\n\r\0\x0B,-+";
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);
73 if ($advanced) {
74 //parse the advanced variables into a query string
75 //TODO: move out to external query class (QueryParse?)
77 $query_string = '';
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';
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));
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));
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));
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;
106 // create title search string
107 if (strlen(trim($adv->title)) > 0) {
108 $query_string .= " +{$titlestr}:".implode(" +{$titlestr}:", preg_split("/[\s,;]+/", $adv->title));
111 // create author search string
112 if (strlen(trim($adv->author)) > 0) {
113 $query_string .= " +{$authorstr}:".implode(" +{$authorstr}:", preg_split("/[\s,;]+/", $adv->author));
116 // save our options if the query is valid
117 if (!empty($query_string)) {
118 $_SESSION['search_advanced_query'] = serialize($adv);
122 // normalise page number
123 if ($page_number < 1) {
124 $page_number = 1;
127 //run the query against the index
128 $sq = new SearchQuery($query_string, $page_number, 10, false);
131 if (!$site = get_site()) {
132 redirect("index.php");
135 $strsearch = get_string('search', 'search');
136 $strquery = get_string('enteryoursearchquery', 'search');
138 print_header("$site->shortname: $strsearch: $strquery", "$site->fullname",
139 "<a href=\"index.php\">$strsearch</a> -> $strquery");
141 //keep things pretty, even if php5 isn't available
142 if (!$check) {
143 print_heading(search_check_php5(true));
144 print_footer();
145 exit(0);
148 print_box_start();
149 print_heading($strquery);
151 print_box_start();
153 $vars = get_object_vars($adv);
155 if (isset($vars)) {
156 foreach ($vars as $key => $value) {
157 $adv->$key = stripslashes(htmlentities($value));
162 <form id="query" method="get" action="query.php">
163 <?php
164 if (!$advanced) {
166 <input type="text" name="query_string" length="50" value="<?php print stripslashes($query_string) ?>" />
167 &nbsp;<input type="submit" value="<?php print_string('search', 'search') ?>" /> &nbsp;
168 <a href="query.php?a=1"><?php print_string('advancedsearch', 'search') ?></a> |
169 <a href="stats.php"><?php print_string('statistics', 'search') ?></a>
170 <?php
172 else {
173 print_box_start();
175 <input type="hidden" name="a" value="<?php print $advanced; ?>"/>
177 <table border="0" cellpadding="3" cellspacing="3">
179 <tr>
180 <td width="240"><?php print_string('thesewordsmustappear', 'search') ?>:</td>
181 <td><input type="text" name="mustappear" length="50" value="<?php print $adv->mustappear; ?>" /></td>
182 </tr>
184 <tr>
185 <td><?php print_string('thesewordsmustnotappear', 'search') ?>:</td>
186 <td><input type="text" name="notappear" length="50" value="<?php print $adv->notappear; ?>" /></td>
187 </tr>
189 <tr>
190 <td><?php print_string('thesewordshelpimproverank', 'search') ?>:</td>
191 <td><input type="text" name="canappear" length="50" value="<?php print $adv->canappear; ?>" /></td>
192 </tr>
194 <tr>
195 <td><?php print_string('whichmodulestosearch?', 'search') ?>:</td>
196 <td>
197 <select name="module">
198 <?php
199 foreach($module_types as $mod) {
200 if ($mod == $adv->module) {
201 if ($mod != 'all'){
202 print "<option value='$mod' selected=\"selected\">".get_string('modulenameplural', $mod)."</option>\n";
204 else{
205 print "<option value='$mod' selected=\"selected\">".get_string('all', 'search')."</option>\n";
208 else {
209 if ($mod != 'all'){
210 print "<option value='$mod'>".get_string('modulenameplural', $mod)."</option>\n";
212 else{
213 print "<option value='$mod'>".get_string('all', 'search')."</option>\n";
218 </select>
219 </td>
220 </tr>
222 <tr>
223 <td><?php print_string('wordsintitle', 'search') ?>:</td>
224 <td><input type="text" name="title" length="50" value="<?php print $adv->title; ?>" /></td>
225 </tr>
227 <tr>
228 <td><?php print_string('authorname', 'search') ?>:</td>
229 <td><input type="text" name="author" length="50" value="<?php print $adv->author; ?>" /></td>
230 </tr>
232 <tr>
233 <td colspan="3" align="center"><br /><input type="submit" value="<?php print_string('search', 'search') ?>" /></td>
234 </tr>
236 <tr>
237 <td colspan="3" align="center">
238 <table border="0" cellpadding="0" cellspacing="0">
239 <tr>
240 <td><a href="query.php"><?php print_string('normalsearch', 'search') ?></a> |</td>
241 <td>&nbsp;<a href="stats.php"><?php print_string('statistics', 'search') ?></a></td>
242 </tr>
243 </table>
244 </td>
245 </tr>
246 </table>
247 <?php
248 print_box_end();
251 </form>
252 <br/>
254 <div align="center">
255 <?php
256 print_string('searching', 'search') . ': ';
258 if ($sq->is_valid_index()) {
259 //use cached variable to show up-to-date index size (takes deletions into account)
260 print $CFG->search_index_size;
262 else {
263 print "0";
266 print ' ';
267 print_string('documents', 'search');
268 print '.';
270 if (!$sq->is_valid_index() and isadmin()) {
271 print '<p>' . get_string('noindexmessage', 'search') . '<a href="indexersplash.php">' . get_string('createanindex', 'search')."</a></p>\n";
275 </div>
276 <?php
277 print_box_end();
279 // prints all the results in a box
280 if ($sq->is_valid()) {
281 print_box_start();
283 search_stopwatch();
284 $hit_count = $sq->count();
286 print "<br />";
288 print $hit_count.' '.get_string('resultsreturnedfor', 'search') . " '".stripslashes($query_string)."'.";
289 print "<br />";
291 if ($hit_count > 0) {
292 $page_links = $sq->page_numbers();
293 $hits = $sq->results();
295 if ($advanced) {
296 // if in advanced mode, search options are saved in the session, so
297 // we can remove the query string var from the page links, and replace
298 // it with a=1 (Advanced = on) instead
299 $page_links = preg_replace("/query_string=[^&]+/", 'a=1', $page_links);
302 print "<ol>";
304 $typestr = get_string('type', 'search');
305 $scorestr = get_string('score', 'search');
306 $authorstr = get_string('author', 'search');
307 foreach ($hits as $listing) {
308 if ($CFG->unicodedb) $listing->title = mb_convert_encoding($listing->title, 'auto', 'UTF8');
309 $title_post_processing_function = $listing->doctype.'_link_post_processing';
310 require_once "{$CFG->dirroot}/search/documents/{$listing->doctype}_document.php";
311 if (function_exists($title_post_processing_function))
312 $listing->title = $title_post_processing_function($listing->title);
313 print "<li value='".($listing->number+1)."'><a href='".str_replace('DEFAULT_POPUP_SETTINGS', DEFAULT_POPUP_SETTINGS ,$listing->url)."'>$listing->title</a><br />\n"
314 ."<em>".search_shorten_url($listing->url, 70)."</em><br />\n"
315 ."{$typestr}: ".$listing->doctype.", {$scorestr}: ".round($listing->score, 3).", {$authorstr}: ".$listing->author."\n"
316 ."</li>\n";
319 print "</ol>";
320 print $page_links;
323 print_box_end();
325 <div align="center">
326 <?php
327 print_string('ittook', 'search');
328 search_stopwatch();
329 print_string('tofetchtheseresults', 'search');
331 </div>
333 <?php
335 print_box_end();
336 print_footer();