MDL-10902:
[moodle-linuxchix.git] / search / query.php
blob3ffb857a6f2927bba02293e2ca1d48b1bd2a677e
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');
137 $navlinks = array();
138 $navlinks[] = array('name' => $strsearch, 'link' => "index.php", 'type' => 'misc');
139 $navlinks[] = array('name' => $strquery, 'link' => null, 'type' => 'misc');
140 $navigation = build_navigation($navlinks);
141 print_header("$site->shortname: $strsearch: $strquery", "$site->fullname", $navigation);
143 //keep things pretty, even if php5 isn't available
144 if (!$check) {
145 print_heading(search_check_php5(true));
146 print_footer();
147 exit(0);
150 print_box_start();
151 print_heading($strquery);
153 print_box_start();
155 $vars = get_object_vars($adv);
157 if (isset($vars)) {
158 foreach ($vars as $key => $value) {
159 $adv->$key = stripslashes(htmlentities($value));
164 <form id="query" method="get" action="query.php">
165 <?php
166 if (!$advanced) {
168 <input type="text" name="query_string" length="50" value="<?php print stripslashes($query_string) ?>" />
169 &nbsp;<input type="submit" value="<?php print_string('search', 'search') ?>" /> &nbsp;
170 <a href="query.php?a=1"><?php print_string('advancedsearch', 'search') ?></a> |
171 <a href="stats.php"><?php print_string('statistics', 'search') ?></a>
172 <?php
174 else {
175 print_box_start();
177 <input type="hidden" name="a" value="<?php print $advanced; ?>"/>
179 <table border="0" cellpadding="3" cellspacing="3">
181 <tr>
182 <td width="240"><?php print_string('thesewordsmustappear', 'search') ?>:</td>
183 <td><input type="text" name="mustappear" length="50" value="<?php print $adv->mustappear; ?>" /></td>
184 </tr>
186 <tr>
187 <td><?php print_string('thesewordsmustnotappear', 'search') ?>:</td>
188 <td><input type="text" name="notappear" length="50" value="<?php print $adv->notappear; ?>" /></td>
189 </tr>
191 <tr>
192 <td><?php print_string('thesewordshelpimproverank', 'search') ?>:</td>
193 <td><input type="text" name="canappear" length="50" value="<?php print $adv->canappear; ?>" /></td>
194 </tr>
196 <tr>
197 <td><?php print_string('whichmodulestosearch?', 'search') ?>:</td>
198 <td>
199 <select name="module">
200 <?php
201 foreach($module_types as $mod) {
202 if ($mod == $adv->module) {
203 if ($mod != 'all'){
204 print "<option value='$mod' selected=\"selected\">".get_string('modulenameplural', $mod)."</option>\n";
206 else{
207 print "<option value='$mod' selected=\"selected\">".get_string('all', 'search')."</option>\n";
210 else {
211 if ($mod != 'all'){
212 print "<option value='$mod'>".get_string('modulenameplural', $mod)."</option>\n";
214 else{
215 print "<option value='$mod'>".get_string('all', 'search')."</option>\n";
220 </select>
221 </td>
222 </tr>
224 <tr>
225 <td><?php print_string('wordsintitle', 'search') ?>:</td>
226 <td><input type="text" name="title" length="50" value="<?php print $adv->title; ?>" /></td>
227 </tr>
229 <tr>
230 <td><?php print_string('authorname', 'search') ?>:</td>
231 <td><input type="text" name="author" length="50" value="<?php print $adv->author; ?>" /></td>
232 </tr>
234 <tr>
235 <td colspan="3" align="center"><br /><input type="submit" value="<?php print_string('search', 'search') ?>" /></td>
236 </tr>
238 <tr>
239 <td colspan="3" align="center">
240 <table border="0" cellpadding="0" cellspacing="0">
241 <tr>
242 <td><a href="query.php"><?php print_string('normalsearch', 'search') ?></a> |</td>
243 <td>&nbsp;<a href="stats.php"><?php print_string('statistics', 'search') ?></a></td>
244 </tr>
245 </table>
246 </td>
247 </tr>
248 </table>
249 <?php
250 print_box_end();
253 </form>
254 <br/>
256 <div align="center">
257 <?php
258 print_string('searching', 'search') . ': ';
260 if ($sq->is_valid_index()) {
261 //use cached variable to show up-to-date index size (takes deletions into account)
262 print $CFG->search_index_size;
264 else {
265 print "0";
268 print ' ';
269 print_string('documents', 'search');
270 print '.';
272 if (!$sq->is_valid_index() and isadmin()) {
273 print '<p>' . get_string('noindexmessage', 'search') . '<a href="indexersplash.php">' . get_string('createanindex', 'search')."</a></p>\n";
277 </div>
278 <?php
279 print_box_end();
281 // prints all the results in a box
282 if ($sq->is_valid()) {
283 print_box_start();
285 search_stopwatch();
286 $hit_count = $sq->count();
288 print "<br />";
290 print $hit_count.' '.get_string('resultsreturnedfor', 'search') . " '".stripslashes($query_string)."'.";
291 print "<br />";
293 if ($hit_count > 0) {
294 $page_links = $sq->page_numbers();
295 $hits = $sq->results();
297 if ($advanced) {
298 // if in advanced mode, search options are saved in the session, so
299 // we can remove the query string var from the page links, and replace
300 // it with a=1 (Advanced = on) instead
301 $page_links = preg_replace("/query_string=[^&]+/", 'a=1', $page_links);
304 print "<ol>";
306 $typestr = get_string('type', 'search');
307 $scorestr = get_string('score', 'search');
308 $authorstr = get_string('author', 'search');
309 foreach ($hits as $listing) {
310 if ($CFG->unicodedb) $listing->title = mb_convert_encoding($listing->title, 'auto', 'UTF8');
311 $title_post_processing_function = $listing->doctype.'_link_post_processing';
312 require_once "{$CFG->dirroot}/search/documents/{$listing->doctype}_document.php";
313 if (function_exists($title_post_processing_function))
314 $listing->title = $title_post_processing_function($listing->title);
315 print "<li value='".($listing->number+1)."'><a href='".str_replace('DEFAULT_POPUP_SETTINGS', DEFAULT_POPUP_SETTINGS ,$listing->url)."'>$listing->title</a><br />\n"
316 ."<em>".search_shorten_url($listing->url, 70)."</em><br />\n"
317 ."{$typestr}: ".$listing->doctype.", {$scorestr}: ".round($listing->score, 3).", {$authorstr}: ".$listing->author."\n"
318 ."</li>\n";
321 print "</ol>";
322 print $page_links;
325 print_box_end();
327 <div align="center">
328 <?php
329 print_string('ittook', 'search');
330 search_stopwatch();
331 print_string('tofetchtheseresults', 'search');
333 </div>
335 <?php
337 print_box_end();
338 print_footer();