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