author | Dan |
Mon, 01 Oct 2007 23:52:25 -0400 | |
changeset 160 | 87a988ca4ff4 |
parent 116 | 77c75179bb95 |
child 119 | 9de392a67964 |
child 169 | 01bf6b1b8222 |
permissions | -rwxr-xr-x |
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 |
|
85
7c68a18a27be
AJAX comments are now paginated; plugin manager can now show system plugins; typo in installer corrected; links in oxygen/stpatty/admin footers changed to "About Enano" page; 1.0.1 release candidate
Dan
parents:
36
diff
changeset
|
7 |
Version: 1.0.1 |
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 |
|
13 |
* Version 1.0 release candidate 2 |
|
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 |
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
103 |
|
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
104 |
for ( $i = 0; $i < count ( $paths->pages ) / 2; $i++ ) |
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
105 |
{ |
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
106 |
$pg =& $paths->pages[$i]; |
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
107 |
$q_lc = strtolower( str_replace(' ', '_', $q) ); |
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
108 |
$q_tl = strtolower( str_replace('_', ' ', $q) ); |
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
109 |
$p_lc = strtolower($pg['urlname']); |
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
110 |
$p_tl = strtolower($pg['name']); |
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
|
111 |
if ( strstr($p_tl, $q_tl) || strstr($p_lc, $q_lc) && $pg['visible'] == 1 ) |
36
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
112 |
{ |
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
113 |
echo '<div class="usermessage">Perhaps you were looking for <b><a href="' . makeUrl($pg['urlname'], false, true) . '">' . htmlspecialchars($pg['name']) . '</a></b>?</div>'; |
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
114 |
break; |
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
115 |
} |
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
116 |
} |
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
117 |
|
0 | 118 |
switch(SEARCH_MODE) |
119 |
{ |
|
120 |
||
121 |
case "FULLTEXT": |
|
122 |
if ( isset($_GET['offset']) ) |
|
123 |
{ |
|
124 |
$offset = intval($_GET['offset']); |
|
125 |
} |
|
126 |
else |
|
127 |
{ |
|
128 |
$offset = 0; |
|
129 |
} |
|
130 |
$sql = $db->sql_query('SELECT search_id FROM '.table_prefix.'search_cache WHERE query=\''.$db->escape($q).'\';'); |
|
131 |
if(!$sql) |
|
132 |
{ |
|
133 |
$db->_die('Error scanning search query cache'); |
|
134 |
} |
|
135 |
if($db->numrows() > 0) |
|
136 |
{ |
|
137 |
$row = $db->fetchrow(); |
|
138 |
$db->free_result(); |
|
139 |
search_fetch_fulltext_results(intval($row['search_id']), $offset); |
|
140 |
} |
|
141 |
else |
|
142 |
{ |
|
143 |
// Perform search |
|
144 |
||
145 |
$search = new MySQL_Fulltext_Search(); |
|
146 |
||
147 |
// Parse the query |
|
148 |
$parse = new Searcher(); |
|
149 |
$query = $parse->parseQuery($q); |
|
150 |
unset($parse); |
|
151 |
||
152 |
// Send query to MySQL |
|
153 |
$sql = $search->search($q); |
|
154 |
$results = Array(); |
|
155 |
if ( $row = $db->fetchrow($sql) ) |
|
156 |
{ |
|
157 |
do { |
|
158 |
$results[] = $row; |
|
159 |
} while ( $row = $db->fetchrow($sql) ); |
|
160 |
} |
|
161 |
else |
|
162 |
{ |
|
163 |
// echo '<div class="warning-box">No pages that matched your search criteria could be found.</div>'; |
|
164 |
} |
|
165 |
$texts = Array(); |
|
166 |
foreach ( $results as $result ) |
|
167 |
{ |
|
168 |
$texts[] = render_fulltext_result($result, $query); |
|
169 |
} |
|
170 |
||
171 |
// Store the result in the search cache...if someone makes the same query later we can skip searching and rendering |
|
172 |
// This cache is cleared when an affected page is saved. |
|
173 |
||
174 |
$results = serialize($texts); |
|
175 |
||
176 |
$sql = $db->sql_query('INSERT INTO '.table_prefix.'search_cache(search_time,query,results) VALUES('.time().', \''.$db->escape($q).'\', \''.$db->escape($results).'\');'); |
|
177 |
if($sql) |
|
178 |
{ |
|
179 |
search_render_fulltext_results(unserialize($results), $offset, $q); |
|
180 |
} |
|
181 |
else |
|
182 |
{ |
|
183 |
$db->_die('Error inserting search into cache'); |
|
184 |
} |
|
185 |
||
186 |
} |
|
187 |
break; |
|
188 |
||
189 |
case "BUILTIN": |
|
190 |
$titles = $paths->makeTitleSearcher(isset($_GET['match_case'])); |
|
191 |
if ( isset($_GET['offset']) ) |
|
192 |
{ |
|
193 |
$offset = intval($_GET['offset']); |
|
194 |
} |
|
195 |
else |
|
196 |
{ |
|
197 |
$offset = 0; |
|
198 |
} |
|
199 |
$sql = $db->sql_query('SELECT search_id FROM '.table_prefix.'search_cache WHERE query=\''.$db->escape($q).'\';'); |
|
200 |
if(!$sql) |
|
201 |
{ |
|
202 |
$db->_die('Error scanning search query cache'); |
|
203 |
} |
|
204 |
if($db->numrows() > 0) |
|
205 |
{ |
|
206 |
$row = $db->fetchrow(); |
|
207 |
$db->free_result(); |
|
208 |
search_show_results(intval($row['search_id']), $offset); |
|
209 |
} |
|
210 |
else |
|
211 |
{ |
|
212 |
$titles->search($q, $paths->get_page_titles()); |
|
213 |
$search = $paths->makeSearcher(isset($_GET['match_case'])); |
|
214 |
$texts = $paths->fetch_page_search_resource(); |
|
215 |
$search->searchMySQL($q, $texts); |
|
216 |
||
217 |
$results = Array(); |
|
218 |
$results['text'] = $search->results; |
|
219 |
$results['page'] = $titles->results; |
|
220 |
$results['warn'] = $search->warnings; |
|
221 |
||
222 |
$results = serialize($results); |
|
223 |
||
224 |
$sql = $db->sql_query('INSERT INTO '.table_prefix.'search_cache(search_time,query,results) VALUES('.time().', \''.$db->escape($q).'\', \''.$db->escape($results).'\');'); |
|
225 |
if($sql) |
|
226 |
{ |
|
227 |
search_render_results(unserialize($results), $offset, $q); |
|
228 |
} |
|
229 |
else |
|
230 |
{ |
|
231 |
$db->_die('Error inserting search into cache'); |
|
232 |
} |
|
233 |
} |
|
234 |
break; |
|
235 |
} |
|
236 |
$code = $plugins->setHook('search_results'); // , Array('query'=>$q)); |
|
237 |
foreach ( $code as $cmd ) |
|
238 |
{ |
|
239 |
eval($cmd); |
|
240 |
} |
|
241 |
?> |
|
242 |
<form action="<?php echo makeUrl($paths->page); ?>" method="get"> |
|
243 |
<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
|
244 |
<?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
|
245 |
<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
|
246 |
<?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
|
247 |
<?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
|
248 |
<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
|
249 |
<?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
|
250 |
<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 | 251 |
</p> |
252 |
</form> |
|
253 |
<?php |
|
254 |
} |
|
255 |
else |
|
256 |
{ |
|
257 |
?> |
|
258 |
<br /> |
|
259 |
<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
|
260 |
<?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
|
261 |
<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
|
262 |
<?php endif; ?> |
0 | 263 |
<div class="tblholder"> |
264 |
<table border="0" style="width: 100%;" cellspacing="1" cellpadding="4"> |
|
265 |
<tr><th colspan="2">Advanced Search</th></tr> |
|
266 |
<tr> |
|
267 |
<td class="row1">Search for pages with <b>any of these words</b>:</td> |
|
268 |
<td class="row1"><input type="text" name="words_any" size="40" /></td> |
|
269 |
</tr> |
|
270 |
<tr> |
|
271 |
<td class="row2">with <b>this exact phrase</b>:</td> |
|
272 |
<td class="row2"><input type="text" name="exact_phrase" size="40" /></td> |
|
273 |
</tr> |
|
274 |
<tr> |
|
275 |
<td class="row1">with <b>none of these words</b>:</td> |
|
276 |
<td class="row1"><input type="text" name="exclude_words" size="40" /></td> |
|
277 |
</tr> |
|
278 |
<tr> |
|
279 |
<td class="row2">with <b>all of these words</b>:</td> |
|
280 |
<td class="row2"><input type="text" name="require_words" size="40" /></td> |
|
281 |
</tr> |
|
282 |
<tr> |
|
283 |
<td class="row1"> |
|
284 |
<label for="chk_case">Case-sensitive search:</label> |
|
285 |
</td> |
|
286 |
<td class="row1"> |
|
287 |
<input type="checkbox" name="match_case" id="chk_case" /> |
|
288 |
</td> |
|
289 |
</tr> |
|
290 |
<tr> |
|
291 |
<th colspan="2" class="subhead"> |
|
292 |
<input type="submit" name="do_search" value="Search" /> |
|
293 |
</td> |
|
294 |
</tr> |
|
295 |
</table> |
|
296 |
</div> |
|
297 |
</form> |
|
298 |
<?php |
|
299 |
} |
|
300 |
$template->footer(); |
|
301 |
} |
|
302 |
||
303 |
function search_show_results($search_id, $start = 0) |
|
304 |
{ |
|
305 |
global $db, $session, $paths, $template, $plugins; // Common objects |
|
306 |
$q = $db->sql_query('SELECT query,results,search_time FROM '.table_prefix.'search_cache WHERE search_id='.intval($search_id).';'); |
|
307 |
if(!$q) |
|
308 |
return $db->get_error('Error selecting cached search results'); |
|
309 |
$row = $db->fetchrow(); |
|
310 |
$db->free_result(); |
|
311 |
$results = unserialize($row['results']); |
|
312 |
search_render_results($results, $start, $row['query']); |
|
313 |
} |
|
314 |
||
315 |
function search_render_results($results, $start = 0, $q = '') |
|
316 |
{ |
|
317 |
global $db, $session, $paths, $template, $plugins; // Common objects |
|
318 |
$nr1 = sizeof($results['page']); |
|
319 |
$nr2 = sizeof($results['text']); |
|
320 |
$nr = ( $nr1 > $nr2 ) ? $nr1 : $nr2; |
|
321 |
$results['page'] = array_slice($results['page'], $start, SEARCH_RESULTS_PER_PAGE); |
|
322 |
$results['text'] = array_slice($results['text'], $start, SEARCH_RESULTS_PER_PAGE); |
|
323 |
||
324 |
// Pagination |
|
325 |
$pagination = ''; |
|
326 |
if ( $nr1 > SEARCH_RESULTS_PER_PAGE || $nr2 > SEARCH_RESULTS_PER_PAGE ) |
|
327 |
{ |
|
328 |
$pagination .= '<div class="tblholder" style="padding: 0; display: table; margin: 0 0 0 auto; float: right;"> |
|
329 |
<table border="0" style="width: 100%;" cellspacing="1" cellpadding="4"> |
|
330 |
<tr> |
|
331 |
<th>Page:</th>'; |
|
332 |
$num_pages = ceil($nr / SEARCH_RESULTS_PER_PAGE); |
|
333 |
$j = 0; |
|
334 |
for ( $i = 1; $i <= $num_pages; $i++ ) |
|
335 |
{ |
|
336 |
if ($j == $start) |
|
337 |
$pagination .= '<td class="row1"><b>' . $i . '</b></td>'; |
|
338 |
else |
|
339 |
$pagination .= '<td class="row1"><a href="' . makeUrlNS('Special', 'Search', 'q=' . urlencode($q) . '&offset=' . $j, true) . '">' . $i . '</a></td>'; |
|
340 |
$j = $j + SEARCH_RESULTS_PER_PAGE; |
|
341 |
} |
|
342 |
$pagination .= '</tr></table></div>'; |
|
343 |
} |
|
344 |
||
345 |
echo $pagination; |
|
346 |
||
347 |
if ( $nr1 >= $start ) |
|
348 |
{ |
|
349 |
echo '<h3>Page title matches</h3>'; |
|
350 |
if(count($results['page']) < 1) |
|
351 |
{ |
|
352 |
echo '<div class="error-box">No pages with a title that matched your search criteria could be found.</div>'; |
|
353 |
} |
|
354 |
else |
|
355 |
{ |
|
356 |
echo '<p>'; |
|
357 |
foreach($results['page'] as $page => $text) |
|
358 |
{ |
|
359 |
echo '<a href="'.makeUrl($page).'">'.$paths->pages[$page]['name'].'</a><br />'; |
|
360 |
} |
|
361 |
echo '</p>'; |
|
362 |
} |
|
363 |
} |
|
364 |
if ( $nr2 >= $start ) |
|
365 |
{ |
|
366 |
echo '<h3>Page text matches</h3>'; |
|
367 |
if(count($results['text']) < 1) |
|
368 |
{ |
|
369 |
echo '<div class="error-box">No page text that matched your search criteria could be found.</div>'; |
|
370 |
} |
|
371 |
else |
|
372 |
{ |
|
373 |
foreach($results['text'] as $kpage => $text) |
|
374 |
{ |
|
375 |
preg_match('#^ns=('.implode('|', array_keys($paths->nslist)).');pid=(.*?)$#i', $kpage, $matches); |
|
376 |
$page = $paths->nslist[$matches[1]] . $matches[2]; |
|
377 |
echo '<p><span style="font-size: larger;"><a href="'.makeUrl($page).'">'.$paths->pages[$page]['name'].'</a></span><br />'.$text.'</p>'; |
|
378 |
} |
|
379 |
} |
|
380 |
} |
|
381 |
if(count($results['warn']) > 0) |
|
382 |
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>'; |
|
383 |
echo $pagination; |
|
384 |
} |
|
385 |
||
386 |
function render_fulltext_result($result, $query) |
|
387 |
{ |
|
388 |
global $db, $session, $paths, $template, $plugins; // Common objects |
|
389 |
preg_match('#^ns=('.implode('|', array_keys($paths->nslist)).');pid=(.*?)$#i', $result['page_identifier'], $matches); |
|
390 |
$page = $paths->nslist[$matches[1]] . $matches[2]; |
|
391 |
//$score = round($result['score'] * 100, 1); |
|
392 |
$score = number_format($result['score'], 2); |
|
393 |
$char_length = $result['length']; |
|
394 |
$result_template = <<<TPLCODE |
|
395 |
<div class="search-result"> |
|
396 |
<h3><a href="{HREF}">{TITLE}</a></h3> |
|
397 |
<p>{TEXT}</p> |
|
398 |
<p> |
|
399 |
<span class="search-result-info">{NAMESPACE} - Relevance score: {SCORE} ({LENGTH} bytes)</span> |
|
400 |
</p> |
|
401 |
</div> |
|
402 |
TPLCODE; |
|
403 |
$parser = $template->makeParserText($result_template); |
|
404 |
||
405 |
$pt =& $result['page_text']; |
|
406 |
$space_chars = Array("\t", "\n", "\r", " "); |
|
407 |
||
408 |
$words = array_merge($query['any'], $query['req']); |
|
409 |
$pt = htmlspecialchars($pt); |
|
410 |
$words2 = array(); |
|
411 |
||
412 |
for ( $i = 0; $i < sizeof($words); $i++) |
|
413 |
{ |
|
414 |
if(!empty($words[$i])) |
|
415 |
$words2[] = preg_quote($words[$i]); |
|
416 |
} |
|
417 |
||
418 |
$regex = '/(' . implode('|', $words2) . ')/i'; |
|
419 |
$pt = preg_replace($regex, '<span class="search-term">\\1</span>', $pt); |
|
420 |
||
90
9d29f7e101d6
Fixed yet another minor XSS hole, this time in search results
Dan
parents:
85
diff
changeset
|
421 |
$title = preg_replace($regex, '<span class="title-search-term">\\1</span>', htmlspecialchars($paths->pages[$page]['name'])); |
0 | 422 |
|
423 |
$cut_off = false; |
|
424 |
||
425 |
foreach ( $words as $word ) |
|
426 |
{ |
|
427 |
// Boldface searched words |
|
428 |
$ptlen = strlen($pt); |
|
429 |
for ( $i = 0; $i < $ptlen; $i++ ) |
|
430 |
{ |
|
431 |
$len = strlen($word); |
|
432 |
if ( strtolower(substr($pt, $i, $len)) == strtolower($word) ) |
|
433 |
{ |
|
434 |
$chunk1 = substr($pt, 0, $i); |
|
435 |
$chunk2 = substr($pt, $i, $len); |
|
436 |
$chunk3 = substr($pt, ( $i + $len )); |
|
437 |
$pt = $chunk1 . $chunk2 . $chunk3; |
|
438 |
$ptlen = strlen($pt); |
|
439 |
// Cut off text to 150 chars or so |
|
440 |
if ( !$cut_off ) |
|
441 |
{ |
|
442 |
$cut_off = true; |
|
443 |
if ( $i - 75 > 0 ) |
|
444 |
{ |
|
445 |
// Navigate backwards until a space character is found |
|
446 |
$chunk = substr($pt, 0, ( $i - 75 )); |
|
447 |
$final_chunk = $chunk; |
|
448 |
for ( $j = strlen($chunk); $j > 0; $j = $j - 1 ) |
|
449 |
{ |
|
450 |
if ( in_array($chunk{$j}, $space_chars) ) |
|
451 |
{ |
|
452 |
$final_chunk = substr($chunk, $j + 1); |
|
453 |
break; |
|
454 |
} |
|
455 |
} |
|
456 |
$mid_chunk = substr($pt, ( $i - 75 ), 75); |
|
457 |
||
458 |
$clipped = '...' . $final_chunk . $mid_chunk . $chunk2; |
|
459 |
||
460 |
$chunk = substr($pt, ( $i + strlen($chunk2) + 75 )); |
|
461 |
$final_chunk = $chunk; |
|
462 |
for ( $j = 0; $j < strlen($chunk); $j++ ) |
|
463 |
{ |
|
464 |
if ( in_array($chunk{$j}, $space_chars) ) |
|
465 |
{ |
|
466 |
$final_chunk = substr($chunk, 0, $j); |
|
467 |
break; |
|
468 |
} |
|
469 |
} |
|
470 |
||
471 |
$end_chunk = substr($pt, ( $i + strlen($chunk2) ), 75 ); |
|
472 |
||
473 |
$clipped .= $end_chunk . $final_chunk . '...'; |
|
474 |
||
475 |
$pt = $clipped; |
|
476 |
} |
|
477 |
else if ( strlen($pt) > 200 ) |
|
478 |
{ |
|
479 |
$mid_chunk = substr($pt, ( $i - 75 ), 75); |
|
480 |
||
481 |
$clipped = $chunk1 . $chunk2; |
|
482 |
||
483 |
$chunk = substr($pt, ( $i + strlen($chunk2) + 75 )); |
|
484 |
$final_chunk = $chunk; |
|
485 |
for ( $j = 0; $j < strlen($chunk); $j++ ) |
|
486 |
{ |
|
487 |
if ( in_array($chunk{$j}, $space_chars) ) |
|
488 |
{ |
|
489 |
$final_chunk = substr($chunk, 0, $j); |
|
490 |
break; |
|
491 |
} |
|
492 |
} |
|
493 |
||
494 |
$end_chunk = substr($pt, ( $i + strlen($chunk2) ), 75 ); |
|
495 |
||
496 |
$clipped .= $end_chunk . $final_chunk . '...'; |
|
497 |
||
498 |
$pt = $clipped; |
|
499 |
||
500 |
} |
|
501 |
break 2; |
|
502 |
} |
|
503 |
} |
|
504 |
} |
|
505 |
$cut_off = false; |
|
506 |
} |
|
507 |
||
508 |
$parser->assign_vars(Array( |
|
509 |
'TITLE' => $title, |
|
510 |
'TEXT' => $pt, |
|
511 |
'NAMESPACE' => $matches[1], |
|
512 |
'SCORE' => $score, |
|
513 |
'LENGTH' => $char_length, |
|
514 |
'HREF' => makeUrl($page) |
|
515 |
)); |
|
516 |
||
517 |
return $parser->run(); |
|
518 |
||
519 |
} |
|
520 |
||
521 |
function search_fetch_fulltext_results($search_id, $offset = 0) |
|
522 |
{ |
|
523 |
global $db, $session, $paths, $template, $plugins; // Common objects |
|
524 |
$q = $db->sql_query('SELECT query,results,search_time FROM '.table_prefix.'search_cache WHERE search_id='.intval($search_id).';'); |
|
525 |
if(!$q) |
|
526 |
return $db->get_error('Error selecting cached search results'); |
|
527 |
$row = $db->fetchrow(); |
|
528 |
$db->free_result(); |
|
529 |
$results = unserialize($row['results']); |
|
530 |
search_render_fulltext_results($results, $offset, $row['query']); |
|
531 |
} |
|
532 |
||
533 |
function search_render_fulltext_results($results, $offset = 0, $query) |
|
534 |
{ |
|
535 |
$num_results = sizeof($results); |
|
536 |
$slice = array_slice($results, $offset, SEARCH_RESULTS_PER_PAGE); |
|
537 |
||
538 |
if ( $num_results < 1 ) |
|
539 |
{ |
|
36
425261984266
Added "page hint" on search page; deprecated "www." on EnanoCMS.org links
Dan
parents:
0
diff
changeset
|
540 |
echo '<div class="warning-box" style="margin-left: 0;">No page text that matched your search criteria could be found.</div>'; |
0 | 541 |
return null; |
542 |
} |
|
543 |
||
544 |
$html = paginate_array($results, sizeof($results), makeUrlNS('Special', 'Search', 'q=' . urlencode($query) . '&offset=%s'), $offset, 10); |
|
545 |
echo $html . '<br />'; |
|
546 |
||
547 |
} |
|
548 |
||
549 |
?> |