2016-09-23 10:20:07 +02:00
< ? php
2011-12-13 12:29:05 +01:00
if ( IN_serendipity !== true ) {
die ( " Don't hack! " );
}
2016-09-23 10:20:07 +02:00
@ serendipity_plugin_api :: load_language ( dirname ( __FILE__ ));
2011-12-13 12:29:05 +01:00
class serendipity_event_commentsearch extends serendipity_event
{
var $title = COMMENTSEARCH_TITLE ;
2016-09-23 10:20:07 +02:00
2011-12-13 12:29:05 +01:00
function introspect ( & $propbag )
{
global $serendipity ;
$propbag -> add ( 'name' , COMMENTSEARCH_TITLE );
$propbag -> add ( 'description' , COMMENTSEARCH_DESC );
$propbag -> add ( 'event_hooks' , array (
2016-09-23 10:20:07 +02:00
'entries_footer' => true ,
'frontend_fetchentries' => true
2011-12-13 12:29:05 +01:00
));
$propbag -> add ( 'author' , 'Garvin Hicking' );
2016-09-23 10:20:07 +02:00
$propbag -> add ( 'version' , '1.6' );
2011-12-13 12:29:05 +01:00
$propbag -> add ( 'requirements' , array (
2016-09-23 10:20:07 +02:00
'serendipity' => '1.7' ,
'smarty' => '3.1.0' ,
'php' => '5.1.0'
2011-12-13 12:29:05 +01:00
));
$propbag -> add ( 'stackable' , false );
2016-09-23 10:20:07 +02:00
$propbag -> add ( 'groups' , array ( 'FRONTEND_FEATURES' ));
2011-12-13 12:29:05 +01:00
}
2016-09-23 10:20:07 +02:00
function setupDB ()
{
2011-12-13 12:29:05 +01:00
global $serendipity ;
$built = $this -> get_config ( 'db_built' , null );
if ( empty ( $built )) {
$q = " @CREATE { FULLTEXT_MYSQL} INDEX commentbody_idx on { $serendipity [ 'dbPrefix' ] } comments (title, body); " ;
serendipity_db_schema_import ( $q );
$this -> set_config ( 'db_built' , 1 );
}
}
2016-09-23 10:20:07 +02:00
function showSearch ()
{
2011-12-13 12:29:05 +01:00
global $serendipity ;
$this -> setupDB ();
$term = serendipity_db_escape_string ( $serendipity [ 'GET' ][ 'searchTerm' ]);
if ( $serendipity [ 'dbType' ] == 'postgres' ) {
$group = '' ;
$distinct = 'DISTINCT' ;
$find_part = " (c.title ILIKE '% $term %' OR c.body ILIKE '% $term %') " ;
2016-09-23 10:20:07 +02:00
} elseif ( stristr ( $serendipity [ 'dbType' ], 'sqlite' ) !== FALSE ) {
2011-12-13 12:29:05 +01:00
$group = 'GROUP BY id' ;
$distinct = '' ;
$term = serendipity_mb ( 'strtolower' , $term );
$find_part = " (lower(c.title) LIKE '% $term %' OR lower(c.body) LIKE '% $term %') " ;
2016-09-23 10:20:07 +02:00
} else { // MYSQL
2011-12-13 12:29:05 +01:00
$group = 'GROUP BY id' ;
$distinct = '' ;
$term = str_replace ( '"' , '"' , $term );
if ( preg_match ( '@["\+\-\*~<>\(\)]+@' , $term )) {
$find_part = " MATCH(c.title,c.body) AGAINST(' $term ' IN BOOLEAN MODE) " ;
} else {
$find_part = " MATCH(c.title,c.body) AGAINST(' $term ') " ;
}
}
$querystring = " SELECT c.title AS ctitle, c.body, c.author, c.entry_id, c.timestamp AS ctimestamp, c.url, c.type,
e . id , e . title , e . timestamp
FROM { $serendipity [ 'dbPrefix' ]} comments AS c
LEFT OUTER JOIN { $serendipity [ 'dbPrefix' ]} entries AS e
ON e . id = c . entry_id
WHERE c . status = 'approved'
AND $find_part
$group
ORDER BY c . timestamp DESC " ;
$results = serendipity_db_query ( $querystring , false , 'assoc' );
if ( ! is_array ( $results )) {
if ( $results !== 1 && $results !== true ) {
2014-11-30 16:40:37 +01:00
echo ( function_exists ( 'serendipity_specialchars' ) ? serendipity_specialchars ( $results ) : htmlspecialchars ( $results , ENT_COMPAT , LANG_CHARSET ));
2011-12-13 12:29:05 +01:00
}
$results = array ();
}
2012-01-31 12:49:28 +01:00
$myAddData = array ( " from " => " serendipity_plugin_commentsearch:generate_content " );
2011-12-13 12:29:05 +01:00
foreach ( $results AS $idx => $result ) {
$results [ $idx ][ 'permalink' ] = serendipity_archiveURL ( $result [ 'id' ], $result [ 'title' ], 'baseURL' , true , $result );
2016-09-23 10:20:07 +02:00
$results [ $idx ][ 'comment' ] = $result [ 'body' ]; // escape it in the template (function_exists('serendipity_specialchars') ? serendipity_specialchars(strip_tags($result['body'])) : htmlspecialchars(strip_tags($result['body']), ENT_COMPAT, LANG_CHARSET));
2012-01-31 12:49:28 +01:00
serendipity_plugin_api :: hook_event ( 'frontend_display' , $results [ $idx ], $myAddData );
// let the template decide, if we want to have tags or not
$results [ $idx ][ 'commenthtml' ] = $results [ $idx ][ 'comment' ];
2016-09-23 10:20:07 +02:00
$results [ $idx ][ 'comment' ] = strip_tags ( $results [ $idx ][ 'comment' ]);
2011-12-13 12:29:05 +01:00
}
$serendipity [ 'smarty' ] -> assign (
array (
'comment_searchresults' => count ( $results ),
'comment_results' => $results
)
);
$filename = 'plugin_commentsearch_searchresults.tpl' ;
2016-09-23 10:20:07 +02:00
$content = $this -> parseTemplate ( $filename );
2011-12-13 12:29:05 +01:00
echo $content ;
}
2016-09-23 10:20:07 +02:00
function event_hook ( $event , & $bag , & $eventData , $addData = null )
{
2011-12-13 12:29:05 +01:00
global $serendipity ;
$hooks = & $bag -> get ( 'event_hooks' );
if ( isset ( $hooks [ $event ])) {
switch ( $event ) {
case 'frontend_fetchentries' :
if ( $serendipity [ 'GET' ][ 'action' ] == 'search' ) {
serendipity_smarty_fetch ( 'ENTRIES' , 'entries.tpl' , true );
}
break ;
case 'entries_footer' :
if ( $serendipity [ 'GET' ][ 'action' ] == 'search' ) {
$this -> showSearch ();
}
break ;
default :
return false ;
}
return true ;
}
return false ;
}
2016-09-23 10:20:07 +02:00
2011-12-13 12:29:05 +01:00
}
2016-09-23 10:20:07 +02:00
2011-12-13 12:29:05 +01:00
/* vim: set sts=4 ts=4 expandtab : */
2016-09-23 10:20:07 +02:00
?>