[Evocms-plugins-commits] SF.net SVN: evocms-plugins: [326] am_search_cloud_plugin/trunk/ _am_search
Brought to you by:
blueyed
From: <ya...@us...> - 2007-04-29 16:26:14
|
Revision: 326 http://svn.sourceforge.net/evocms-plugins/?rev=326&view=rev Author: yabs Date: 2007-04-29 09:26:09 -0700 (Sun, 29 Apr 2007) Log Message: ----------- added cache added search term filter added a few more settings Modified Paths: -------------- am_search_cloud_plugin/trunk/_am_search_cloud.plugin.php Modified: am_search_cloud_plugin/trunk/_am_search_cloud.plugin.php =================================================================== --- am_search_cloud_plugin/trunk/_am_search_cloud.plugin.php 2007-04-29 16:24:05 UTC (rev 325) +++ am_search_cloud_plugin/trunk/_am_search_cloud.plugin.php 2007-04-29 16:26:09 UTC (rev 326) @@ -25,7 +25,7 @@ var $name = 'AM Search Cloud'; var $code = 'am_srchcloud'; var $priority = 100; - var $version = '1.0'; + var $version = '1.1'; var $author = 'AstonishMe'; var $group = 'AstonishMe'; var $help_url = 'http://b2evo.astonishme.co.uk/'; @@ -35,6 +35,14 @@ // internal - have we displayed the search cloud? var $has_shown = false; + + // 1.8.x + function am_search_cloud_plugin() + { + return $this->PluginInit(); + } + + // 1.9.x function PluginInit() { $this->short_desc = T_( 'Add a search cloud to your blog' ); @@ -42,6 +50,40 @@ } + function GetDefaultSettings() + { + return array( + 'cache' => array( + 'label' => T_('Cache lifetime'), + 'defaultvalue' => 1440, + 'note' => T_('This is the time in minutes that the results will be cached for'), + 'type' => 'integer', + 'size' => 4 + ), + 'max_length' => array( + 'label' => T_('Max length'), + 'defaultvalue' => 0, + 'note' => T_('This is the maximum length for a search term ( 0 = no restrictions )'), + 'type' => 'integer', + 'size' => 4 + ), + 'filter_replace' => array( + 'label' => T_('Filter character'), + 'defaultvalue' => '*', + 'note' => T_('This is the character that filtered words will be replaced with'), + 'type' => 'text', + 'size' => 1 + ), + 'filter_list' => array( + 'label' => T_('filter_words'), + 'defaultvalue' => '', + 'note' => T_('This is a comma seperated list of words to filter'), + 'type' => 'text', + 'size' => 40 + ) + ); + } + function SkinBeginHtmlHead() { echo '<link href="'.$this->get_plugin_url().'cloud.css" type="text/css" rel="stylesheet" />'."\n\r"; @@ -55,9 +97,9 @@ global $blog, $DB, $BlogCache; // set all the parameters - $the_limit = ( isset( $params[ 'limit' ] ) ? $params[ 'limit' ] : 0 ); - $search_blog = ( empty( $params['blog' ] ) ? $blog : $params[ 'blog' ] ); - $search_order = ( empty( $params['order' ] ) || !in_array( $params[ 'order' ], array( 'asc', 'desc') ) ? 'rand' : $params[ 'order' ] ); + $params[ 'limit' ] = ( isset( $params[ 'limit' ] ) ? $params[ 'limit' ] : 0 ); + $params[ 'blog' ] = ( empty( $params['blog' ] ) ? $blog : $params[ 'blog' ] ); + $params[ 'order' ] = ( empty( $params['order' ] ) || !in_array( $params[ 'order' ], array( 'asc', 'desc') ) ? 'rand' : $params[ 'order' ] ); $params[ 'before_all' ] = ( empty( $params['before_all' ] ) ? '<div id="search_cloud">' : $params[ 'before_all' ] ); $params[ 'after_all' ] = ( empty( $params['after_all' ] ) ? '</div>' : $params[ 'after_all' ] ); $params[ 'title' ] = ( empty( $params['title' ] ) ? '<h3>'.$this->T_( 'Search Cloud' ).'</h3>' : $params[ 'title' ] ); @@ -67,14 +109,39 @@ $params[ 'after_each' ] = ( empty( $params['after_each' ] ) ? '</li>' : $params[ 'after_each' ] ); $params[ 'hover_title' ] = ( empty( $params['hover_title' ] ) ? 'Searched #count# times - Read it!' : $params[ 'hover_title' ] ); - $sql = 'select hit_uri, hit_referer, hit_blog_ID from T_hitlog where hit_referer_type = \'search\''.( $search_blog == 1 ? '' : ' and hit_blog_ID = '.$search_blog ); + // lets check if we have a current cached version for these settings + $sql = 'select searchcloud_id, searchcloud_cache_time, searchcloud_cache_data from '.$this->get_sql_table( 'cache' ).' where searchcloud_params = \''.$DB->escape( serialize( $params ) ).'\''; + if( $cache_results = $DB->get_row( $sql ) ) + { // we have a cache for these settings + //echo '<p>'.$cache_results->searchcloud_cache_time.'<br />'.( time() - ( $this->Settings->get( 'cache' ) * 60 ) ).'</p>'; + if( $cache_results->searchcloud_cache_time > ( time() - ( $this->Settings->get( 'cache' ) * 60 ) ) ) + { // the cache is current + echo unserialize( $cache_results->searchcloud_cache_data ); + return; + } + } + + // either we don't have a cache or the cache has expired, lets meander through the hitlog instead + $sql = 'select hit_uri, hit_referer, hit_blog_ID from T_hitlog where hit_referer_type = \'search\''.( $params[ 'blog' ] == 1 ? '' : ' and hit_blog_ID = '.$params[ 'blog' ] ); $all_hits = $DB->get_results( $sql ); $max = 0; + $max_length = $this->Settings->get( 'max_length' ); + $filter_words = explode( ',', $this->Settings->get( 'filter_list' ) ); + $search_list = array(); + $replace_list = array(); + foreach( $filter_words as $filter_word ) + { + $replace_list[] = str_repeat( $this->Settings->get( 'filter_replace' ), strlen( $filter_word ) ); + $search_list[] = '#'.preg_quote( $filter_word ).'#i'; + } + foreach( $all_hits as $a_hit ) { $term = strtolower( trim( stats_search_keywords( $a_hit->hit_referer ) ) ); if( $term != '['.T_('no query string found').']' && $term != '['.T_('not a query - no params!').']' ) { + if( !empty( $replace_list ) ) $term = preg_replace( $search_list, $replace_list, $term ); + $term = ( $max_length ? substr( $term, 0, $max_length ).'...' : $term ); $search_stats[ $term ]->count = ( empty( $search_stats[ $term ]->count ) ? 1 : $search_stats[ $term ]->count + 1 ); $search_stats[ $term ]->url = $a_hit->hit_uri; $search_stats[ $term ]->blog_ID = $a_hit->hit_blog_ID; @@ -87,7 +154,7 @@ $the_top[ $stat->count ][$words] = $stat; krsort( $the_top ); - if( $search_order == 'rand' ) shuffle( $the_top ); +// if( $params[ 'order' ] == 'rand' ) shuffle( $the_top ); $search_stats = array(); foreach( $the_top as $a_group ) @@ -95,12 +162,12 @@ foreach( $a_group as $my_name => $a_term ) { $search_stats[ $my_name ] = $a_term; - if( !empty( $the_limit ) && sizeof( $search_stats ) == $the_limit ) break; + if( !empty( $params[ 'limit' ] ) && sizeof( $search_stats ) == $params[ 'limit' ] ) break; } - if( !empty( $the_limit ) && sizeof( $search_stats ) == $the_limit ) break; + if( !empty( $params[ 'limit' ] ) && sizeof( $search_stats ) == $params[ 'limit' ] ) break; } - switch( $search_order ) + switch( $params[ 'order' ] ) { case 'asc' : ksort( $search_stats ); @@ -128,7 +195,31 @@ $output .= $params[ 'before_each'].'<a href="'.$full_url.'" title=" '.str_replace ('#count#', $search_stat->count, $this->T_( $params[ 'hover_title' ] ) ).' " class="search_cloud_'.$class.'">'.str_replace( array(' ','-'), array( ' ','‑'), $search_stat->term ).'</a>'.$params[ 'after_each' ]."\n"; } $output = ( empty( $output ) ? '' : $params[ 'before_all' ].$params[ 'title' ].$params[ 'block_start' ].$output.$params[ 'block_end' ].$params[ 'after_all' ] ); - echo $output; + + // lets cache the results + if( empty( $cache_results->searchcloud_id ) ) + { // this is a brand new cloud + $sql = 'insert into '.$this->get_sql_table( 'cache' ).' ( searchcloud_cache_time, searchcloud_cache_data, searchcloud_params ) values( '.time().', \''.$DB->escape( serialize( $output ) ).'\', \''.$DB->escape( serialize( $params ) ).'\')'; + } + else + { // we're updating an existing cloud + $sql = 'update '.$this->get_sql_table( 'cache' ).' set searchcloud_cache_time = '.time().', searchcloud_cache_data = \''.$DB->escape( serialize( '<!-- cached search cloud -->'.$output ) ).'\', searchcloud_params = \''.$DB->escape( serialize( $params ) ).'\' where searchcloud_id = '.$cache_results->searchcloud_id; + } + $DB->query( $sql ); + echo '<!-- new search cloud -->'.$output; } + + function GetDbLayout() + { + return array( + 'CREATE TABLE IF NOT EXISTS '.$this->get_sql_table('cache').' ( + searchcloud_id int(10) unsigned NOT NULL auto_increment, + searchcloud_cache_time int(10) NOT NULL, + searchcloud_cache_data text NULL DEFAULT NULL, + searchcloud_params text NULL DEFAULT NULL, + PRIMARY KEY (searchcloud_id) + )' + ); + } } ?> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |