author | Dan |
Thu, 15 Nov 2007 18:02:14 -0500 | |
changeset 206 | e7bbbb92385b |
parent 181 | 9237767a23ae |
child 218 | e0ec986c0af3 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/* |
|
3 |
Plugin Name: Search UI/frontend |
|
36
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
4 |
Plugin URI: http://enanocms.org/ |
0 | 5 |
Description: Provides the page Special:Search, which is a frontend to the Enano search engine. |
6 |
Author: Dan Fuhry |
|
181
9237767a23ae
Implemented cron image into Oxygen and St Patty as promised; fixed way-outdated version numbers in plugins
Dan
parents:
168
diff
changeset
|
7 |
Version: 1.0.2 |
36
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
8 |
Author URI: http://enanocms.org/ |
0 | 9 |
*/ |
10 |
||
11 |
/* |
|
12 |
* Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between |
|
181
9237767a23ae
Implemented cron image into Oxygen and St Patty as promised; fixed way-outdated version numbers in plugins
Dan
parents:
168
diff
changeset
|
13 |
* Version 1.0.2 |
0 | 14 |
* Copyright (C) 2006-2007 Dan Fuhry |
15 |
* |
|
16 |
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License |
|
17 |
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
|
18 |
* |
|
19 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
|
20 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
|
21 |
*/ |
|
22 |
||
23 |
$plugins->attachHook('base_classes_initted', ' |
|
24 |
global $paths; |
|
25 |
$paths->add_page(Array( |
|
26 |
\'name\'=>\'Rebuild search index\', |
|
27 |
\'urlname\'=>\'SearchRebuild\', |
|
28 |
\'namespace\'=>\'Special\', |
|
116
77c75179bb95
Made most special pages "visible"; fixup for non-existent special page redirect in paths.php; rewrote Special:AllPages to have pagination (WiP, Special:SpecialPages is possibly next, depending on whether paginate_array works or not)
Dan
parents:
115
diff
changeset
|
29 |
\'special\'=>0,\'visible\'=>1,\'comments_on\'=>0,\'protected\'=>1,\'delvotes\'=>0,\'delvote_ips\'=>\'\', |
0 | 30 |
)); |
31 |
||
32 |
$paths->add_page(Array( |
|
33 |
\'name\'=>\'Search\', |
|
34 |
\'urlname\'=>\'Search\', |
|
35 |
\'namespace\'=>\'Special\', |
|
36 |
\'special\'=>0,\'visible\'=>1,\'comments_on\'=>0,\'protected\'=>1,\'delvotes\'=>0,\'delvote_ips\'=>\'\', |
|
37 |
)); |
|
38 |
'); |
|
39 |
||
40 |
function page_Special_SearchRebuild() |
|
41 |
{ |
|
42 |
global $db, $session, $paths, $template, $plugins; // Common objects |
|
43 |
if(!$session->get_permissions('mod_misc')) die_friendly('Unauthorized', '<p>You need to be an administrator to rebuild the search index</p>'); |
|
44 |
$template->header(); |
|
45 |
if($paths->rebuild_search_index()) |
|
46 |
echo '<p>Index rebuilt!</p>'; |
|
47 |
else |
|
48 |
echo '<p>Index was not rebuilt due to an error.'; |
|
49 |
$template->footer(); |
|
50 |
} |
|
51 |
||
52 |
function page_Special_Search() |
|
53 |
{ |
|
54 |
global $db, $session, $paths, $template, $plugins; // Common objects |
|
55 |
if(!$q = $paths->getParam(0)) $q = ( isset($_GET['q']) ) ? $_GET['q'] : false; |
|
56 |
if(isset($_GET['words_any'])) |
|
57 |
{ |
|
58 |
$q = ''; |
|
59 |
if(!empty($_GET['words_any'])) |
|
60 |
{ |
|
61 |
$q .= $_GET['words_any'] . ' '; |
|
62 |
} |
|
63 |
if(!empty($_GET['exact_phrase'])) |
|
64 |
{ |
|
65 |
$q .= '"' . $_GET['exact_phrase'] . '" '; |
|
66 |
} |
|
67 |
if(!empty($_GET['exclude_words'])) |
|
68 |
{ |
|
69 |
$not = explode(' ', $_GET['exclude_words']); |
|
70 |
foreach ( $not as $i => $foo ) |
|
71 |
{ |
|
72 |
$not[$i] = '-' . $not[$i]; |
|
73 |
} |
|
115
261f367623af
Fixed the obnoxious issue with forms using GET and index.php?title=Foo URL scheme (this works a whole lot better than MediaWiki now
Dan
parents:
114
diff
changeset
|
74 |
$q .= implode(' ', $not) . ' '; |
0 | 75 |
} |
76 |
if(!empty($_GET['require_words'])) |
|
77 |
{ |
|
78 |
$req = explode(' ', $_GET['require_words']); |
|
79 |
foreach ( $req as $i => $foo ) |
|
80 |
{ |
|
81 |
$req[$i] = '+' . $req[$i]; |
|
82 |
} |
|
115
261f367623af
Fixed the obnoxious issue with forms using GET and index.php?title=Foo URL scheme (this works a whole lot better than MediaWiki now
Dan
parents:
114
diff
changeset
|
83 |
$q .= implode(' ', $req) . ' '; |
0 | 84 |
} |
85 |
} |
|
100
e9a685fb456f
Fixed: highlighting issue in search results renderer when the search query was comprised entirely of one quoted term
Dan
parents:
90
diff
changeset
|
86 |
$q = trim($q); |
114
47393c6619ea
Nothing special, just syncing to Scribus, several bugs have been found with GET forms and a fix is in the works
Dan
parents:
100
diff
changeset
|
87 |
|
47393c6619ea
Nothing special, just syncing to Scribus, several bugs have been found with GET forms and a fix is in the works
Dan
parents:
100
diff
changeset
|
88 |
if ( !empty($q) && !isset($_GET['search']) ) |
47393c6619ea
Nothing special, just syncing to Scribus, several bugs have been found with GET forms and a fix is in the works
Dan
parents:
100
diff
changeset
|
89 |
{ |
47393c6619ea
Nothing special, just syncing to Scribus, several bugs have been found with GET forms and a fix is in the works
Dan
parents:
100
diff
changeset
|
90 |
list($pid, $ns) = RenderMan::strToPageID($q); |
47393c6619ea
Nothing special, just syncing to Scribus, several bugs have been found with GET forms and a fix is in the works
Dan
parents:
100
diff
changeset
|
91 |
$pid = sanitize_page_id($pid); |
47393c6619ea
Nothing special, just syncing to Scribus, several bugs have been found with GET forms and a fix is in the works
Dan
parents:
100
diff
changeset
|
92 |
$key = $paths->nslist[$ns] . $pid; |
47393c6619ea
Nothing special, just syncing to Scribus, several bugs have been found with GET forms and a fix is in the works
Dan
parents:
100
diff
changeset
|
93 |
if ( isPage($key) ) |
47393c6619ea
Nothing special, just syncing to Scribus, several bugs have been found with GET forms and a fix is in the works
Dan
parents:
100
diff
changeset
|
94 |
{ |
47393c6619ea
Nothing special, just syncing to Scribus, several bugs have been found with GET forms and a fix is in the works
Dan
parents:
100
diff
changeset
|
95 |
redirect(makeUrl($key), 'Results', 'found page', 0); |
47393c6619ea
Nothing special, just syncing to Scribus, several bugs have been found with GET forms and a fix is in the works
Dan
parents:
100
diff
changeset
|
96 |
} |
47393c6619ea
Nothing special, just syncing to Scribus, several bugs have been found with GET forms and a fix is in the works
Dan
parents:
100
diff
changeset
|
97 |
} |
47393c6619ea
Nothing special, just syncing to Scribus, several bugs have been found with GET forms and a fix is in the works
Dan
parents:
100
diff
changeset
|
98 |
|
0 | 99 |
$template->header(); |
100 |
if(!empty($q)) |
|
101 |
{ |
|
36
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
102 |
// See if any pages directly match the title |
168
01bf6b1b8222
"Perhaps you were looking for" box now only shows if strlen($q) >= 4
Dan
parents:
116
diff
changeset
|
103 |
|
01bf6b1b8222
"Perhaps you were looking for" box now only shows if strlen($q) >= 4
Dan
parents:
116
diff
changeset
|
104 |
if ( strlen($q) >= 4 ) |
36
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
105 |
{ |
168
01bf6b1b8222
"Perhaps you were looking for" box now only shows if strlen($q) >= 4
Dan
parents:
116
diff
changeset
|
106 |
for ( $i = 0; $i < count ( $paths->pages ) / 2; $i++ ) |
36
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
107 |
{ |
168
01bf6b1b8222
"Perhaps you were looking for" box now only shows if strlen($q) >= 4
Dan
parents:
116
diff
changeset
|
108 |
$pg =& $paths->pages[$i]; |
01bf6b1b8222
"Perhaps you were looking for" box now only shows if strlen($q) >= 4
Dan
parents:
116
diff
changeset
|
109 |
$q_lc = strtolower( str_replace(' ', '_', $q) ); |
01bf6b1b8222
"Perhaps you were looking for" box now only shows if strlen($q) >= 4
Dan
parents:
116
diff
changeset
|
110 |
$q_tl = strtolower( str_replace('_', ' ', $q) ); |
01bf6b1b8222
"Perhaps you were looking for" box now only shows if strlen($q) >= 4
Dan
parents:
116
diff
changeset
|
111 |
$p_lc = strtolower($pg['urlname']); |
01bf6b1b8222
"Perhaps you were looking for" box now only shows if strlen($q) >= 4
Dan
parents:
116
diff
changeset
|
112 |
$p_tl = strtolower($pg['name']); |
01bf6b1b8222
"Perhaps you were looking for" box now only shows if strlen($q) >= 4
Dan
parents:
116
diff
changeset
|
113 |
if ( strstr($p_tl, $q_tl) || strstr($p_lc, $q_lc) && $pg['visible'] == 1 ) |
01bf6b1b8222
"Perhaps you were looking for" box now only shows if strlen($q) >= 4
Dan
parents:
116
diff
changeset
|
114 |
{ |
01bf6b1b8222
"Perhaps you were looking for" box now only shows if strlen($q) >= 4
Dan
parents:
116
diff
changeset
|
115 |
echo '<div class="usermessage">Perhaps you were looking for <b><a href="' . makeUrl($pg['urlname'], false, true) . '">' . htmlspecialchars($pg['name']) . '</a></b>?</div>'; |
01bf6b1b8222
"Perhaps you were looking for" box now only shows if strlen($q) >= 4
Dan
parents:
116
diff
changeset
|
116 |
break; |
01bf6b1b8222
"Perhaps you were looking for" box now only shows if strlen($q) >= 4
Dan
parents:
116
diff
changeset
|
117 |
} |
36
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
118 |
} |
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
119 |
} |
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
120 |
|
0 | 121 |
switch(SEARCH_MODE) |
122 |
{ |
|
123 |
||
124 |
case "FULLTEXT": |
|
125 |
if ( isset($_GET['offset']) ) |
|
126 |
{ |
|
127 |
$offset = intval($_GET['offset']); |
|
128 |
} |
|
129 |
else |
|
130 |
{ |
|
131 |
$offset = 0; |
|
132 |
} |
|
133 |
$sql = $db->sql_query('SELECT search_id FROM '.table_prefix.'search_cache WHERE query=\''.$db->escape($q).'\';'); |
|
134 |
if(!$sql) |
|
135 |
{ |
|
136 |
$db->_die('Error scanning search query cache'); |
|
137 |
} |
|
138 |
if($db->numrows() > 0) |
|
139 |
{ |
|
140 |
$row = $db->fetchrow(); |
|
141 |
$db->free_result(); |
|
142 |
search_fetch_fulltext_results(intval($row['search_id']), $offset); |
|
143 |
} |
|
144 |
else |
|
145 |
{ |
|
146 |
// Perform search |
|
147 |
||
148 |
$search = new MySQL_Fulltext_Search(); |
|
149 |
||
150 |
// Parse the query |
|
151 |
$parse = new Searcher(); |
|
152 |
$query = $parse->parseQuery($q); |
|
153 |
unset($parse); |
|
154 |
||
155 |
// Send query to MySQL |
|
156 |
$sql = $search->search($q); |
|
157 |
$results = Array(); |
|
158 |
if ( $row = $db->fetchrow($sql) ) |
|
159 |
{ |
|
160 |
do { |
|
161 |
$results[] = $row; |
|
162 |
} while ( $row = $db->fetchrow($sql) ); |
|
163 |
} |
|
164 |
else |
|
165 |
{ |
|
166 |
// echo '<div class="warning-box">No pages that matched your search criteria could be found.</div>'; |
|
167 |
} |
|
168 |
$texts = Array(); |
|
169 |
foreach ( $results as $result ) |
|
170 |
{ |
|
171 |
$texts[] = render_fulltext_result($result, $query); |
|
172 |
} |
|
173 |
||
174 |
// Store the result in the search cache...if someone makes the same query later we can skip searching and rendering |
|
175 |
// This cache is cleared when an affected page is saved. |
|
176 |
||
177 |
$results = serialize($texts); |
|
178 |
||
179 |
$sql = $db->sql_query('INSERT INTO '.table_prefix.'search_cache(search_time,query,results) VALUES('.time().', \''.$db->escape($q).'\', \''.$db->escape($results).'\');'); |
|
180 |
if($sql) |
|
181 |
{ |
|
182 |
search_render_fulltext_results(unserialize($results), $offset, $q); |
|
183 |
} |
|
184 |
else |
|
185 |
{ |
|
186 |
$db->_die('Error inserting search into cache'); |
|
187 |
} |
|
188 |
||
189 |
} |
|
190 |
break; |
|
191 |
||
192 |
case "BUILTIN": |
|
193 |
$titles = $paths->makeTitleSearcher(isset($_GET['match_case'])); |
|
194 |
if ( isset($_GET['offset']) ) |
|
195 |
{ |
|
196 |
$offset = intval($_GET['offset']); |
|
197 |
} |
|
198 |
else |
|
199 |
{ |
|
200 |
$offset = 0; |
|
201 |
} |
|
202 |
$sql = $db->sql_query('SELECT search_id FROM '.table_prefix.'search_cache WHERE query=\''.$db->escape($q).'\';'); |
|
203 |
if(!$sql) |
|
204 |
{ |
|
205 |
$db->_die('Error scanning search query cache'); |
|
206 |
} |
|
207 |
if($db->numrows() > 0) |
|
208 |
{ |
|
209 |
$row = $db->fetchrow(); |
|
210 |
$db->free_result(); |
|
211 |
search_show_results(intval($row['search_id']), $offset); |
|
212 |
} |
|
213 |
else |
|
214 |
{ |
|
215 |
$titles->search($q, $paths->get_page_titles()); |
|
216 |
$search = $paths->makeSearcher(isset($_GET['match_case'])); |
|
217 |
$texts = $paths->fetch_page_search_resource(); |
|
218 |
$search->searchMySQL($q, $texts); |
|
219 |
||
220 |
$results = Array(); |
|
221 |
$results['text'] = $search->results; |
|
222 |
$results['page'] = $titles->results; |
|
223 |
$results['warn'] = $search->warnings; |
|
224 |
||
225 |
$results = serialize($results); |
|
226 |
||
227 |
$sql = $db->sql_query('INSERT INTO '.table_prefix.'search_cache(search_time,query,results) VALUES('.time().', \''.$db->escape($q).'\', \''.$db->escape($results).'\');'); |
|
228 |
if($sql) |
|
229 |
{ |
|
230 |
search_render_results(unserialize($results), $offset, $q); |
|
231 |
} |
|
232 |
else |
|
233 |
{ |
|
234 |
$db->_die('Error inserting search into cache'); |
|
235 |
} |
|
236 |
} |
|
237 |
break; |
|
238 |
} |
|
239 |
$code = $plugins->setHook('search_results'); // , Array('query'=>$q)); |
|
240 |
foreach ( $code as $cmd ) |
|
241 |
{ |
|
242 |
eval($cmd); |
|
243 |
} |
|
244 |
?> |
|
245 |
<form action="<?php echo makeUrl($paths->page); ?>" method="get"> |
|
246 |
<p> |
|
114
47393c6619ea
Nothing special, just syncing to Scribus, several bugs have been found with GET forms and a fix is in the works
Dan
parents:
100
diff
changeset
|
247 |
<?php if ( $session->sid_super ): ?> |
47393c6619ea
Nothing special, just syncing to Scribus, several bugs have been found with GET forms and a fix is in the works
Dan
parents:
100
diff
changeset
|
248 |
<input type="hidden" name="auth" value="<?php echo $session->sid_super; ?>" /> |
47393c6619ea
Nothing special, just syncing to Scribus, several bugs have been found with GET forms and a fix is in the works
Dan
parents:
100
diff
changeset
|
249 |
<?php endif; ?> |
115
261f367623af
Fixed the obnoxious issue with forms using GET and index.php?title=Foo URL scheme (this works a whole lot better than MediaWiki now
Dan
parents:
114
diff
changeset
|
250 |
<?php if ( urlSeparator == '&' ): ?> |
261f367623af
Fixed the obnoxious issue with forms using GET and index.php?title=Foo URL scheme (this works a whole lot better than MediaWiki now
Dan
parents:
114
diff
changeset
|
251 |
<input type="hidden" name="title" value="<?php echo $paths->nslist['Special'] . 'Search'; ?>" /> |
261f367623af
Fixed the obnoxious issue with forms using GET and index.php?title=Foo URL scheme (this works a whole lot better than MediaWiki now
Dan
parents:
114
diff
changeset
|
252 |
<?php endif; ?> |
114
47393c6619ea
Nothing special, just syncing to Scribus, several bugs have been found with GET forms and a fix is in the works
Dan
parents:
100
diff
changeset
|
253 |
<input type="text" name="q" size="40" value="<?php echo htmlspecialchars( $q ); ?>" /> <input type="submit" value="Go" style="font-weight: bold;" /> <input name="search" type="submit" value="Search" /> <small><a href="<?php echo makeUrlNS('Special', 'Search'); ?>">Advanced Search</a></small> |
0 | 254 |
</p> |
255 |
</form> |
|
256 |
<?php |
|
257 |
} |
|
258 |
else |
|
259 |
{ |
|
260 |
?> |
|
261 |
<br /> |
|
262 |
<form action="<?php echo makeUrl($paths->page); ?>" method="get"> |
|
115
261f367623af
Fixed the obnoxious issue with forms using GET and index.php?title=Foo URL scheme (this works a whole lot better than MediaWiki now
Dan
parents:
114
diff
changeset
|
263 |
<?php if ( urlSeparator == '&' ): ?> |
261f367623af
Fixed the obnoxious issue with forms using GET and index.php?title=Foo URL scheme (this works a whole lot better than MediaWiki now
Dan
parents:
114
diff
changeset
|
264 |
<input type="hidden" name="title" value="<?php echo $paths->nslist['Special'] . 'Search'; ?>" /> |
261f367623af
Fixed the obnoxious issue with forms using GET and index.php?title=Foo URL scheme (this works a whole lot better than MediaWiki now
Dan
parents:
114
diff
changeset
|
265 |
<?php endif; ?> |
0 | 266 |
<div class="tblholder"> |
267 |
<table border="0" style="width: 100%;" cellspacing="1" cellpadding="4"> |
|
268 |
<tr><th colspan="2">Advanced Search</th></tr> |
|
269 |
<tr> |
|
270 |
<td class="row1">Search for pages with <b>any of these words</b>:</td> |
|
271 |
<td class="row1"><input type="text" name="words_any" size="40" /></td> |
|
272 |
</tr> |
|
273 |
<tr> |
|
274 |
<td class="row2">with <b>this exact phrase</b>:</td> |
|
275 |
<td class="row2"><input type="text" name="exact_phrase" size="40" /></td> |
|
276 |
</tr> |
|
277 |
<tr> |
|
278 |
<td class="row1">with <b>none of these words</b>:</td> |
|
279 |
<td class="row1"><input type="text" name="exclude_words" size="40" /></td> |
|
280 |
</tr> |
|
281 |
<tr> |
|
282 |
<td class="row2">with <b>all of these words</b>:</td> |
|
283 |
<td class="row2"><input type="text" name="require_words" size="40" /></td> |
|
284 |
</tr> |
|
285 |
<tr> |
|
286 |
<td class="row1"> |
|
287 |
<label for="chk_case">Case-sensitive search:</label> |
|
288 |
</td> |
|
289 |
<td class="row1"> |
|
290 |
<input type="checkbox" name="match_case" id="chk_case" /> |
|
291 |
</td> |
|
292 |
</tr> |
|
293 |
<tr> |
|
294 |
<th colspan="2" class="subhead"> |
|
295 |
<input type="submit" name="do_search" value="Search" /> |
|
296 |
</td> |
|
297 |
</tr> |
|
298 |
</table> |
|
299 |
</div> |
|
300 |
</form> |
|
301 |
<?php |
|
302 |
} |
|
303 |
$template->footer(); |
|
304 |
} |
|
305 |
||
306 |
function search_show_results($search_id, $start = 0) |
|
307 |
{ |
|
308 |
global $db, $session, $paths, $template, $plugins; // Common objects |
|
309 |
$q = $db->sql_query('SELECT query,results,search_time FROM '.table_prefix.'search_cache WHERE search_id='.intval($search_id).';'); |
|
310 |
if(!$q) |
|
311 |
return $db->get_error('Error selecting cached search results'); |
|
312 |
$row = $db->fetchrow(); |
|
313 |
$db->free_result(); |
|
314 |
$results = unserialize($row['results']); |
|
315 |
search_render_results($results, $start, $row['query']); |
|
316 |
} |
|
317 |
||
318 |
function search_render_results($results, $start = 0, $q = '') |
|
319 |
{ |
|
320 |
global $db, $session, $paths, $template, $plugins; // Common objects |
|
321 |
$nr1 = sizeof($results['page']); |
|
322 |
$nr2 = sizeof($results['text']); |
|
323 |
$nr = ( $nr1 > $nr2 ) ? $nr1 : $nr2; |
|
324 |
$results['page'] = array_slice($results['page'], $start, SEARCH_RESULTS_PER_PAGE); |
|
325 |
$results['text'] = array_slice($results['text'], $start, SEARCH_RESULTS_PER_PAGE); |
|
326 |
||
327 |
// Pagination |
|
328 |
$pagination = ''; |
|
329 |
if ( $nr1 > SEARCH_RESULTS_PER_PAGE || $nr2 > SEARCH_RESULTS_PER_PAGE ) |
|
330 |
{ |
|
331 |
$pagination .= '<div class="tblholder" style="padding: 0; display: table; margin: 0 0 0 auto; float: right;"> |
|
332 |
<table border="0" style="width: 100%;" cellspacing="1" cellpadding="4"> |
|
333 |
<tr> |
|
334 |
<th>Page:</th>'; |
|
335 |
$num_pages = ceil($nr / SEARCH_RESULTS_PER_PAGE); |
|
336 |
$j = 0; |
|
337 |
for ( $i = 1; $i <= $num_pages; $i++ ) |
|
338 |
{ |
|
339 |
if ($j == $start) |
|
340 |
$pagination .= '<td class="row1"><b>' . $i . '</b></td>'; |
|
341 |
else |
|
342 |
$pagination .= '<td class="row1"><a href="' . makeUrlNS('Special', 'Search', 'q=' . urlencode($q) . '&offset=' . $j, true) . '">' . $i . '</a></td>'; |
|
343 |
$j = $j + SEARCH_RESULTS_PER_PAGE; |
|
344 |
} |
|
345 |
$pagination .= '</tr></table></div>'; |
|
346 |
} |
|
347 |
||
348 |
echo $pagination; |
|
349 |
||
350 |
if ( $nr1 >= $start ) |
|
351 |
{ |
|
352 |
echo '<h3>Page title matches</h3>'; |
|
353 |
if(count($results['page']) < 1) |
|
354 |
{ |
|
355 |
echo '<div class="error-box">No pages with a title that matched your search criteria could be found.</div>'; |
|
356 |
} |
|
357 |
else |
|
358 |
{ |
|
359 |
echo '<p>'; |
|
360 |
foreach($results['page'] as $page => $text) |
|
361 |
{ |
|
362 |
echo '<a href="'.makeUrl($page).'">'.$paths->pages[$page]['name'].'</a><br />'; |
|
363 |
} |
|
364 |
echo '</p>'; |
|
365 |
} |
|
366 |
} |
|
367 |
if ( $nr2 >= $start ) |
|
368 |
{ |
|
369 |
echo '<h3>Page text matches</h3>'; |
|
370 |
if(count($results['text']) < 1) |
|
371 |
{ |
|
372 |
echo '<div class="error-box">No page text that matched your search criteria could be found.</div>'; |
|
373 |
} |
|
374 |
else |
|
375 |
{ |
|
376 |
foreach($results['text'] as $kpage => $text) |
|
377 |
{ |
|
378 |
preg_match('#^ns=('.implode('|', array_keys($paths->nslist)).');pid=(.*?)$#i', $kpage, $matches); |
|
379 |
$page = $paths->nslist[$matches[1]] . $matches[2]; |
|
380 |
echo '<p><span style="font-size: larger;"><a href="'.makeUrl($page).'">'.$paths->pages[$page]['name'].'</a></span><br />'.$text.'</p>'; |
|
381 |
} |
|
382 |
} |
|
383 |
} |
|
384 |
if(count($results['warn']) > 0) |
|
385 |
echo '<div class="warning-box"><b>Your search may not include all results.</b><br />The following errors were encountered during the search:<br /><ul><li>'.implode('</li><li>', $results['warn']).'</li></ul></div>'; |
|
386 |
echo $pagination; |
|
387 |
} |
|
388 |
||
389 |
function render_fulltext_result($result, $query) |
|
390 |
{ |
|
391 |
global $db, $session, $paths, $template, $plugins; // Common objects |
|
392 |
preg_match('#^ns=('.implode('|', array_keys($paths->nslist)).');pid=(.*?)$#i', $result['page_identifier'], $matches); |
|
393 |
$page = $paths->nslist[$matches[1]] . $matches[2]; |
|
394 |
//$score = round($result['score'] * 100, 1); |
|
395 |
$score = number_format($result['score'], 2); |
|
396 |
$char_length = $result['length']; |
|
397 |
$result_template = <<<TPLCODE |
|
398 |
<div class="search-result"> |
|
399 |
<h3><a href="{HREF}">{TITLE}</a></h3> |
|
400 |
<p>{TEXT}</p> |
|
401 |
<p> |
|
402 |
<span class="search-result-info">{NAMESPACE} - Relevance score: {SCORE} ({LENGTH} bytes)</span> |
|
403 |
</p> |
|
404 |
</div> |
|
405 |
TPLCODE; |
|
406 |
$parser = $template->makeParserText($result_template); |
|
407 |
||
408 |
$pt =& $result['page_text']; |
|
409 |
$space_chars = Array("\t", "\n", "\r", " "); |
|
410 |
||
411 |
$words = array_merge($query['any'], $query['req']); |
|
412 |
$pt = htmlspecialchars($pt); |
|
413 |
$words2 = array(); |
|
414 |
||
415 |
for ( $i = 0; $i < sizeof($words); $i++) |
|
416 |
{ |
|
417 |
if(!empty($words[$i])) |
|
418 |
$words2[] = preg_quote($words[$i]); |
|
419 |
} |
|
420 |
||
421 |
$regex = '/(' . implode('|', $words2) . ')/i'; |
|
422 |
$pt = preg_replace($regex, '<span class="search-term">\\1</span>', $pt); |
|
423 |
||
90
9d29f7e101d6
Fixed yet another minor XSS hole, this time in search results
Dan
parents:
85
diff
changeset
|
424 |
$title = preg_replace($regex, '<span class="title-search-term">\\1</span>', htmlspecialchars($paths->pages[$page]['name'])); |
0 | 425 |
|
426 |
$cut_off = false; |
|
427 |
||
428 |
foreach ( $words as $word ) |
|
429 |
{ |
|
430 |
// Boldface searched words |
|
431 |
$ptlen = strlen($pt); |
|
432 |
for ( $i = 0; $i < $ptlen; $i++ ) |
|
433 |
{ |
|
434 |
$len = strlen($word); |
|
435 |
if ( strtolower(substr($pt, $i, $len)) == strtolower($word) ) |
|
436 |
{ |
|
437 |
$chunk1 = substr($pt, 0, $i); |
|
438 |
$chunk2 = substr($pt, $i, $len); |
|
439 |
$chunk3 = substr($pt, ( $i + $len )); |
|
440 |
$pt = $chunk1 . $chunk2 . $chunk3; |
|
441 |
$ptlen = strlen($pt); |
|
442 |
// Cut off text to 150 chars or so |
|
443 |
if ( !$cut_off ) |
|
444 |
{ |
|
445 |
$cut_off = true; |
|
446 |
if ( $i - 75 > 0 ) |
|
447 |
{ |
|
448 |
// Navigate backwards until a space character is found |
|
449 |
$chunk = substr($pt, 0, ( $i - 75 )); |
|
450 |
$final_chunk = $chunk; |
|
451 |
for ( $j = strlen($chunk); $j > 0; $j = $j - 1 ) |
|
452 |
{ |
|
453 |
if ( in_array($chunk{$j}, $space_chars) ) |
|
454 |
{ |
|
455 |
$final_chunk = substr($chunk, $j + 1); |
|
456 |
break; |
|
457 |
} |
|
458 |
} |
|
459 |
$mid_chunk = substr($pt, ( $i - 75 ), 75); |
|
460 |
||
461 |
$clipped = '...' . $final_chunk . $mid_chunk . $chunk2; |
|
462 |
||
463 |
$chunk = substr($pt, ( $i + strlen($chunk2) + 75 )); |
|
464 |
$final_chunk = $chunk; |
|
465 |
for ( $j = 0; $j < strlen($chunk); $j++ ) |
|
466 |
{ |
|
467 |
if ( in_array($chunk{$j}, $space_chars) ) |
|
468 |
{ |
|
469 |
$final_chunk = substr($chunk, 0, $j); |
|
470 |
break; |
|
471 |
} |
|
472 |
} |
|
473 |
||
474 |
$end_chunk = substr($pt, ( $i + strlen($chunk2) ), 75 ); |
|
475 |
||
476 |
$clipped .= $end_chunk . $final_chunk . '...'; |
|
477 |
||
478 |
$pt = $clipped; |
|
479 |
} |
|
480 |
else if ( strlen($pt) > 200 ) |
|
481 |
{ |
|
482 |
$mid_chunk = substr($pt, ( $i - 75 ), 75); |
|
483 |
||
484 |
$clipped = $chunk1 . $chunk2; |
|
485 |
||
486 |
$chunk = substr($pt, ( $i + strlen($chunk2) + 75 )); |
|
487 |
$final_chunk = $chunk; |
|
488 |
for ( $j = 0; $j < strlen($chunk); $j++ ) |
|
489 |
{ |
|
490 |
if ( in_array($chunk{$j}, $space_chars) ) |
|
491 |
{ |
|
492 |
$final_chunk = substr($chunk, 0, $j); |
|
493 |
break; |
|
494 |
} |
|
495 |
} |
|
496 |
||
497 |
$end_chunk = substr($pt, ( $i + strlen($chunk2) ), 75 ); |
|
498 |
||
499 |
$clipped .= $end_chunk . $final_chunk . '...'; |
|
500 |
||
501 |
$pt = $clipped; |
|
502 |
||
503 |
} |
|
504 |
break 2; |
|
505 |
} |
|
506 |
} |
|
507 |
} |
|
508 |
$cut_off = false; |
|
509 |
} |
|
510 |
||
511 |
$parser->assign_vars(Array( |
|
512 |
'TITLE' => $title, |
|
513 |
'TEXT' => $pt, |
|
514 |
'NAMESPACE' => $matches[1], |
|
515 |
'SCORE' => $score, |
|
516 |
'LENGTH' => $char_length, |
|
517 |
'HREF' => makeUrl($page) |
|
518 |
)); |
|
519 |
||
520 |
return $parser->run(); |
|
521 |
||
522 |
} |
|
523 |
||
524 |
function search_fetch_fulltext_results($search_id, $offset = 0) |
|
525 |
{ |
|
526 |
global $db, $session, $paths, $template, $plugins; // Common objects |
|
527 |
$q = $db->sql_query('SELECT query,results,search_time FROM '.table_prefix.'search_cache WHERE search_id='.intval($search_id).';'); |
|
528 |
if(!$q) |
|
529 |
return $db->get_error('Error selecting cached search results'); |
|
530 |
$row = $db->fetchrow(); |
|
531 |
$db->free_result(); |
|
532 |
$results = unserialize($row['results']); |
|
533 |
search_render_fulltext_results($results, $offset, $row['query']); |
|
534 |
} |
|
535 |
||
536 |
function search_render_fulltext_results($results, $offset = 0, $query) |
|
537 |
{ |
|
538 |
$num_results = sizeof($results); |
|
539 |
$slice = array_slice($results, $offset, SEARCH_RESULTS_PER_PAGE); |
|
540 |
||
541 |
if ( $num_results < 1 ) |
|
542 |
{ |
|
36
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
543 |
echo '<div class="warning-box" style="margin-left: 0;">No page text that matched your search criteria could be found.</div>'; |
0 | 544 |
return null; |
545 |
} |
|
546 |
||
547 |
$html = paginate_array($results, sizeof($results), makeUrlNS('Special', 'Search', 'q=' . urlencode($query) . '&offset=%s'), $offset, 10); |
|
548 |
echo $html . '<br />'; |
|
549 |
||
550 |
} |
|
551 |
||
552 |
?> |