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