3 * The query page - accepts a user-entered query string and returns results.
5 * Queries are boolean-aware, e.g.:
8 * '-' term must not be present
9 * '' (no modifier) term's presence increases rank, but isn't required
10 * 'field:' search this field
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
) {
32 if (empty($CFG->enableglobalsearch
)) {
33 error('Global searching is not enabled.');
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);
68 //parse the advanced variables into a query string
69 //TODO: move out to external query class (QueryParse?)
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
));
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
));
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
));
92 //add module restriction
93 if ($adv->module
!= 'All') {
94 $query_string .= ' +doctype:'.$adv->module
;
97 //create title search string
98 if (strlen(trim($adv->title
)) > 0) {
99 $query_string .= ' +title:'.implode(' +title:', preg_split("/[\s,;]+/", $adv->title
));
102 //create author search string
103 if (strlen(trim($adv->author
)) > 0) {
104 $query_string .= ' +author:'.implode(' +author:', preg_split("/[\s,;]+/", $adv->author
));
107 //save our options if the query is valid
108 if (!empty($query_string)) {
109 $_SESSION['search_advanced_query'] = serialize($adv);
113 //normalise page number
114 if ($page_number < 1) {
118 //run the query against the index
119 $sq = new SearchQuery($query_string, $page_number, 10, false);
122 if (!$site = get_site()) {
123 redirect("index.php");
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
134 print_heading(search_check_php5(true));
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);
147 foreach ($vars as $key => $value) {
148 $adv->$key = stripslashes(htmlentities($value));
153 <form id
="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  
;<input type
="submit" value
="Search" />  
;
157 <a href
="query.php?a=1">Advanced search
</a
> |
158 <a href
="stats.php">Statistics
</a
>
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">
167 <td width
="240">These words must appear
:</td
>
168 <td
><input type
="text" name
="mustappear" length
="50" value
="<?php print $adv->mustappear; ?>" /></td
>
172 <td
>These words must not appear
:</td
>
173 <td
><input type
="text" name
="notappear" length
="50" value
="<?php print $adv->notappear; ?>" /></td
>
177 <td
>These words help improve rank
:</td
>
178 <td
><input type
="text" name
="canappear" length
="50" value
="<?php print $adv->canappear; ?>" /></td
>
182 <td
>Which modules to search?
:</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";
189 print "<option value='$mod'>$mod</option>\n";
197 <td
>Words in title
:</td
>
198 <td
><input type
="text" name
="title" length
="50" value
="<?php print $adv->title; ?>" /></td
>
202 <td
>Author name
:</td
>
203 <td
><input type
="text" name
="author" length
="50" value
="<?php print $adv->author; ?>" /></td
>
207 <td colspan
="3" align
="center"><br
/><input type
="submit" value
="Search" /></td
>
211 <td colspan
="3" align
="center">
212 <table border
="0" cellpadding
="0" cellspacing
="0">
214 <td
><a href
="query.php">Normal search
</a
> |
</td
>
215 <td
> 
;<a href
="stats.php">Statistics
</a
></td
>
222 print_simple_box_end();
231 print '<div align="center">';
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
;
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";
249 print_simple_box_end();
251 if ($sq->is_valid()) {
252 print_simple_box_start('center', '50%', 'white', 10);
255 $hit_count = $sq->count();
259 print $hit_count." results returned for '".stripslashes($query_string)."'.";
262 if ($hit_count > 0) {
263 $page_links = $sq->page_numbers();
264 $hits = $sq->results();
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);
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"
286 print_simple_box_end();
290 It took
<?php
search_stopwatch(); ?
> to fetch these results
.
296 print_simple_box_end();