Automatic installer.php lang files by installer_builder (20080623)
[moodle.git] / search / query.php
blob6667201e90e33afb9bb6db968cb0b6121d0e4a29
1 <?php
2 /**
3 * The query page - accepts a user-entered query string and returns results.
5 * Queries are boolean-aware, e.g.:
7 * '+' term required
8 * '-' term must not be present
9 * '' (no modifier) term's presence increases rank, but isn't required
10 * 'field:' search this field
12 * Examples:
14 * 'earthquake +author:michael'
15 * Searches for documents written by 'michael' that contain 'earthquake'
17 * 'earthquake +doctype:wiki'
18 * Search all wiki pages for 'earthquake'
20 * '+author:helen +author:foster'
21 * All articles written by Helen Foster
25 require_once('../config.php');
26 require_once("$CFG->dirroot/search/lib.php");
28 if ($CFG->forcelogin) {
29 require_login();
32 if (empty($CFG->enableglobalsearch)) {
33 error('Global searching is not enabled.');
36 $adv = new Object();
38 //check for php5, but don't die yet (see line 52)
39 if ($check = search_check_php5()) {
40 require_once("$CFG->dirroot/search/querylib.php");
42 $page_number = optional_param('page', -1, PARAM_INT);
43 $pages = ($page_number == -1) ? false : true;
44 $advanced = (optional_param('a', '0', PARAM_INT) == '1') ? true : false;
45 $query_string = optional_param('query_string', '', PARAM_CLEAN);
47 if ($pages && isset($_SESSION['search_advanced_query'])) {
48 //if both are set, then we are busy browsing through the result pages of an advanced query
49 $adv = unserialize($_SESSION['search_advanced_query']);
50 } else if ($advanced) {
51 //otherwise we are dealing with a new advanced query
52 unset($_SESSION['search_advanced_query']);
53 session_unregister('search_advanced_query');
55 //chars to strip from strings (whitespace)
56 $chars = " \t\n\r\0\x0B,-+";
58 //retrieve advanced query variables
59 $adv->mustappear = trim(optional_param('mustappear', '', PARAM_CLEAN), $chars);
60 $adv->notappear = trim(optional_param('notappear', '', PARAM_CLEAN), $chars);
61 $adv->canappear = trim(optional_param('canappear', '', PARAM_CLEAN), $chars);
62 $adv->module = optional_param('module', '', PARAM_CLEAN);
63 $adv->title = trim(optional_param('title', '', PARAM_CLEAN), $chars);
64 $adv->author = trim(optional_param('author', '', PARAM_CLEAN), $chars);
65 } //else
67 if ($advanced) {
68 //parse the advanced variables into a query string
69 //TODO: move out to external query class (QueryParse?)
71 $query_string = '';
73 //get all available module types
74 $module_types = array_merge(array('All'), array_values(search_get_document_types()));
75 $adv->module = in_array($adv->module, $module_types) ? $adv->module : 'All';
77 //convert '1 2' into '+1 +2' for required words field
78 if (strlen(trim($adv->mustappear)) > 0) {
79 $query_string = ' +'.implode(' +', preg_split("/[\s,;]+/", $adv->mustappear));
80 } //if
82 //convert '1 2' into '-1 -2' for not wanted words field
83 if (strlen(trim($adv->notappear)) > 0) {
84 $query_string .= ' -'.implode(' -', preg_split("/[\s,;]+/", $adv->notappear));
85 } //if
87 //this field is left untouched, apart from whitespace being stripped
88 if (strlen(trim($adv->canappear)) > 0) {
89 $query_string .= ' '.implode(' ', preg_split("/[\s,;]+/", $adv->canappear));
90 } //if
92 //add module restriction
93 if ($adv->module != 'All') {
94 $query_string .= ' +doctype:'.$adv->module;
95 } //if
97 //create title search string
98 if (strlen(trim($adv->title)) > 0) {
99 $query_string .= ' +title:'.implode(' +title:', preg_split("/[\s,;]+/", $adv->title));
100 } //if
102 //create author search string
103 if (strlen(trim($adv->author)) > 0) {
104 $query_string .= ' +author:'.implode(' +author:', preg_split("/[\s,;]+/", $adv->author));
105 } //if
107 //save our options if the query is valid
108 if (!empty($query_string)) {
109 $_SESSION['search_advanced_query'] = serialize($adv);
110 } //if
111 } //if
113 //normalise page number
114 if ($page_number < 1) {
115 $page_number = 1;
116 } //if
118 //run the query against the index
119 $sq = new SearchQuery($query_string, $page_number, 10, false);
120 } //if
122 if (!$site = get_site()) {
123 redirect("index.php");
124 } //if
126 $strsearch = "Search"; //get_string();
127 $strquery = "Enter your search query"; //get_string();
129 print_header("$site->shortname: $strsearch: $strquery", "$site->fullname",
130 "<a href=\"index.php\">$strsearch</a> -> $strquery");
132 //keep things pretty, even if php5 isn't available
133 if (!$check) {
134 print_heading(search_check_php5(true));
135 print_footer();
136 exit(0);
137 } //if
139 print_simple_box_start('center', '100%', '', 20);
140 print_heading($strquery);
142 print_simple_box_start('center', '', '', 20);
144 $vars = get_object_vars($adv);
146 if (isset($vars)) {
147 foreach ($vars as $key => $value) {
148 $adv->$key = stripslashes(htmlentities($value));
149 } //foreach
153 <form name="query" method="get" action="query.php">
154 <?php if (!$advanced) { ?>
155 <input type="text" name="query_string" length="50" value="<?php print stripslashes(htmlentities($query_string)) ?>" />
156 &nbsp;<input type="submit" value="Search" /> &nbsp;
157 <a href="query.php?a=1">Advanced search</a> |
158 <a href="stats.php">Statistics</a>
159 <?php } else {
160 print_simple_box_start('center', '', 'white', 10);
162 <input type="hidden" name="a" value="<?php print $advanced; ?>"/>
164 <table border="0" cellpadding="3" cellspacing="3">
166 <tr>
167 <td width="240">These words must appear:</td>
168 <td><input type="text" name="mustappear" length="50" value="<?php print $adv->mustappear; ?>" /></td>
169 </tr>
171 <tr>
172 <td>These words must not appear:</td>
173 <td><input type="text" name="notappear" length="50" value="<?php print $adv->notappear; ?>" /></td>
174 </tr>
176 <tr>
177 <td>These words help improve rank:</td>
178 <td><input type="text" name="canappear" length="50" value="<?php print $adv->canappear; ?>" /></td>
179 </tr>
181 <tr>
182 <td>Which modules to search?:</td>
183 <td>
184 <select name="module">
185 <?php foreach($module_types as $mod) {
186 if ($mod == $adv->module) {
187 print "<option value='$mod' selected>$mod</option>\n";
188 } else {
189 print "<option value='$mod'>$mod</option>\n";
190 } //else
191 } ?>
192 </select>
193 </td>
194 </tr>
196 <tr>
197 <td>Words in title:</td>
198 <td><input type="text" name="title" length="50" value="<?php print $adv->title; ?>" /></td>
199 </tr>
201 <tr>
202 <td>Author name:</td>
203 <td><input type="text" name="author" length="50" value="<?php print $adv->author; ?>" /></td>
204 </tr>
206 <tr>
207 <td colspan="3" align="center"><br /><input type="submit" value="Search" /></td>
208 </tr>
210 <tr>
211 <td colspan="3" align="center">
212 <table border="0" cellpadding="0" cellspacing="0">
213 <tr>
214 <td><a href="query.php">Normal search</a> |</td>
215 <td>&nbsp;<a href="stats.php">Statistics</a></td>
216 </tr>
217 </table>
218 </td>
219 </tr>
220 </table>
221 <?php
222 print_simple_box_end();
223 } //if
225 </form>
227 <br>
229 <?php
231 print '<div align="center">';
232 print 'Searching: ';
234 if ($sq->is_valid_index()) {
235 //use cached variable to show up-to-date index size (takes deletions into account)
236 print $CFG->search_index_size;
237 } else {
238 print "0";
239 } //else
241 print ' documents.';
243 if (!$sq->is_valid_index() and isadmin()) {
244 print "<p>Admin: There appears to be no search index. Please <a href='indexersplash.php'>create an index</a>.</p>\n";
245 } //if
247 print '</div>';
249 print_simple_box_end();
251 if ($sq->is_valid()) {
252 print_simple_box_start('center', '50%', 'white', 10);
254 search_stopwatch();
255 $hit_count = $sq->count();
257 print "<br />";
259 print $hit_count." results returned for '".stripslashes($query_string)."'.";
260 print "<br />";
262 if ($hit_count > 0) {
263 $page_links = $sq->page_numbers();
264 $hits = $sq->results();
266 if ($advanced) {
267 //if in advanced mode, search options are saved in the session, so
268 //we can remove the query string var from the page links, and replace
269 //it with a=1 (Advanced = on) instead
270 $page_links = preg_replace("/query_string=[^&]+/", 'a=1', $page_links);
271 } //if
273 print "<ol>";
275 foreach ($hits as $listing) {
276 print "<li value='".($listing->number+1)."'><a href='".$listing->url."'>$listing->title</a><br />\n"
277 ."<em>".search_shorten_url($listing->url, 70)."</em><br />\n"
278 ."Type: ".$listing->doctype.", score: ".round($listing->score, 3).", author: ".$listing->author."\n"
279 ."</li>\n";
280 } //for
282 print "</ol>";
283 print $page_links;
284 } //if
286 print_simple_box_end();
289 <div align="center">
290 It took <?php search_stopwatch(); ?> to fetch these results.
291 </div>
293 <?php
294 } //if (sq is valid)
296 print_simple_box_end();
297 print_footer();