/** * Return array of active search modules * * @return array Array of modules **/ function running() { global $search_spider; $modules = array(); $options = $search_spider->get_options(); foreach ($options['active'] as $field) { $modules[$field] = Search_Module_Factory::get($field); } return array_filter($modules); }
/** * Constructor. Extracts options and sets up SQL * * @param array $options Search Unleashed options * @return void **/ function SearchSpider( $options ) { global $wpdb; $this->options = $options; $this->exclude_cats = array_filter( explode( ',', $options['exclude_cat'] ) ); $this->exclude = array_filter( explode( ',', $options['exclude'] ) ); $this->exclude[] = 0; $type = $sql = array(); if ( $options['pages'] ) $type[] = "{$wpdb->posts}.post_type='page'"; if ( $options['posts'] ) $type[] = "{$wpdb->posts}.post_type='post'"; if ( count( $type ) > 0 ) $sql[] = implode( ' OR ', $type ); $this->post_sql = $this->comment_sql = ' WHERE '; if ( count( $this->exclude_cats ) > 0 ) { $this->post_sql = " LEFT JOIN {$wpdb->term_relationships} AS rel ON ({$wpdb->posts}.ID=rel.object_id) LEFT JOIN {$wpdb->term_taxonomy} AS tax ON (tax.taxonomy='category' AND rel.term_taxonomy_id=tax.term_taxonomy_id) LEFT JOIN {$wpdb->terms} AS term ON (tax.term_id=term.term_id) WHERE "; $this->post_sql .= 'tax.term_id NOT IN ('.implode( ',', $this->exclude_cats ).") AND "; $this->comment_sql = str_replace( "{$wpdb->posts}.ID", "{$wpdb->comments}.comment_post_ID", $this->post_sql ); } $this->post_sql .= ' ('.implode( ') AND (', $sql ).") AND {$wpdb->posts}.ID NOT IN (".implode( ', ', $this->exclude ).") AND {$wpdb->posts}.post_type IN ( 'post', 'page' )"; $this->comment_sql .= "{$wpdb->comments}.comment_type='' AND {$wpdb->comments}.comment_approved='1' AND {$wpdb->comments}.comment_post_ID NOT IN(".implode( ', ', $this->exclude ).")"; $this->modules = Search_Module_Factory::running(); if ( $this->options['private'] == false ) $this->post_sql .= " AND {$wpdb->posts}.post_status!='private'"; if ( $this->options['draft'] == false ) $this->post_sql .= " AND {$wpdb->posts}.post_status!='draft'"; if ( $this->options['protected'] == false ) $this->post_sql .= " AND {$wpdb->posts}.post_password=''"; $this->blog_url = get_option( 'home' ); }
/** * Total number of items that have been indexed * * @return integer Count **/ function total() { global $wpdb; include_once dirname( dirname( __FILE__ ) ).'/models/search-module.php'; $modules = Search_Module_Factory::running(); $have_comments = false; foreach ( (array)$modules AS $module ) { if ( $module->is_comment() ) $have_comments = true; } $count = $wpdb->get_var( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_status!='revision' AND post_status!='attachment'" ); if ( $have_comments ) $count += $wpdb->get_var( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved=1 AND comment_type=''" ); return $count; }
/** * Hook the posts 'where' and search Lucene * We modify the search SQL to return the posts found from Lucene * * @param string $where WordPress SQL * @return string SQL **/ function posts_where( $where ) { $_GET['s'] = get_query_var( 's' ); $this->terms = array( $_GET['s'] ); $lucene = $this->open(); if ( $lucene ) { $modules = Search_Module_Factory::running(); $where = 'AND 1=2'; // Nothing was found try { $this->terms = array_filter( preg_split( '/[\s,]+/', trim( get_query_var( 's' ) ) ) ); $this->query = new Zend_Search_Lucene_Search_Query_Boolean(); $have_comments = false; // Add queries for all the modules foreach ( (array)$modules AS $module ) { if ( $module->is_comment() ) $have_comments = true; $sub = Zend_Search_Lucene_Search_QueryParser::parse( get_query_var( 's' ), get_option( 'blog_charset' ) ); $this->query->addSubquery( $sub, true ); if ( isset( $_GET[$module->field_name()] ) ) { $value = $module->field_value( $_GET[$module->field_name()] ); // XXX remove any braces from and value if ( $value !== false ) { $sub = Zend_Search_Lucene_Search_QueryParser::parse( $module->field_name().':('.$value.')', get_option( 'blog_charset' ) ); $this->query->addSubquery( $sub, true ); } } } // Add restrictions for status $this->query->addSubquery( Zend_Search_Lucene_Search_QueryParser::parse( $this->get_restricted_posts() ), true ); // Do the Lucene query $hits = new ArrayObject( $lucene->find( $this->query ) ); if ( count( $hits ) > 0 ) { global $wpdb; $page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) - 1 : 0; $start = get_query_var( 'posts_per_page' ) * ( $page ); $this->total_hits = count( $hits ); if ( $have_comments ) $hits = new LimitIterator( $hits->getIterator(), 0 ); else $hits = new LimitIterator( $hits->getIterator(), $start ); foreach ( $hits AS $hit ) { $this->post_ids[] = $hit->post_id; if ( !$have_comments && count( $this->post_ids ) >= get_query_var( 'posts_per_page' ) ) break; } if ( $have_comments ) { $this->post_ids = array_unique( $this->post_ids ); $this->total_hits = count( $this->post_ids ); $this->post_ids = array_slice( $this->post_ids, $start, get_query_var( 'posts_per_page' ) ); } $where = "AND ID IN (".implode( ',', $this->post_ids ).')'; add_filter( 'the_posts', array( &$this, 'the_posts' ) ); } } catch( Zend_Search_Lucene_Exception $e ) { } } return $where; }
/** * Main administration page * * @return void **/ function admin_spider() { if (current_user_can('administrator')) { include_once dirname(__FILE__) . '/models/spider.php'; $sub = $this->submenu(); if ($sub == '') { $this->admin_index(); } elseif ($sub == 'log') { $this->admin_log(); } elseif ($sub == 'options') { $this->admin_options(); } elseif ($sub == 'modules') { $this->render_admin('modules', array('types' => Search_Module_Factory::available(), 'options' => $this->get_options())); } elseif ($sub == 'filters') { $this->admin_filters(); } elseif ($sub == 'support') { $this->render_admin('support'); } } else { $this->render_message(__('You are not allowed access to this resource', 'search-unleashed')); } }
/** * Display edit box for module * module ID - $_POST['id] * * @return void **/ function su_module_edit() { if ( current_user_can( 'administrator' ) && check_ajax_referer( 'searchunleashed-module' ) ) { require dirname( __FILE__ ).'/models/search-module.php'; $this->render_admin( 'module_edit', array( 'module' =>Search_Module_Factory::get( $_GET['id'] ) ) ); die(); } }
/** * Install the FULLTEXT DB tables * * @return void **/ function install_engine() { global $wpdb; $this->remove_engine(); include_once dirname( dirname( __FILE__ ) ).'/models/search-module.php'; $modules = Search_Module_Factory::running(); $post = "CREATE TABLE `{$wpdb->prefix}search_post` (`post_id` int(11) unsigned NOT NULL,"; $comment = "CREATE TABLE `{$wpdb->prefix}search_comment` (`post_id` int(11) unsigned NOT NULL,`comment_id` int(11) unsigned NOT NULL,"; $post_sql = $comment_sql = array(); foreach ( $modules AS $module ) { if ( $module->is_post() ) $post_sql[] = "`".$module->field_name()."` text DEFAULT NULL"; else $comment_sql[] = "`".$module->field_name()."` text DEFAULT NULL"; } $post .= implode( ",\n", $post_sql ); $post .= ",PRIMARY KEY (`post_id`)"; if ( count( $post_sql ) > 0 ) $post .= ",\nFULLTEXT KEY `fulltext` (".str_replace( ' text DEFAULT NULL', '', implode( ",", $post_sql ) ).")"; $post .= ') ENGINE=MyISAM'; $post = str_replace( ',,', ',', $post ); $wpdb->query( $post ); if ( count( $comment_sql ) > 0 ) { $comment .= implode( ",\n", $comment_sql ); $comment .= ",PRIMARY KEY (`post_id`,`comment_id`),\n"; $comment .= "FULLTEXT KEY `fulltext` (".str_replace( ' text DEFAULT NULL', '', implode( ",", $comment_sql ) ).")) ENGINE=MyISAM"; $wpdb->query( $comment ); } }
/** * Modules highlight appropriate content for a post * * @param Object $post WP post object * @param string $content Post content * @return void **/ function highlight($post, $content) { $modules = Search_Module_Factory::running(); $ordered = array(); foreach ($modules as $module) { if ($module->id() == strtolower('Search_Post_Content')) { array_unshift($ordered, $module); } else { $ordered[] = $module; } } $text = ''; foreach ($ordered as $module) { $text .= $module->highlight($post, $this->get_terms(), $content); } return $text; }
/** * Install the WP database tables * * @return void **/ function install_engine() { global $wpdb; $this->remove_engine(); include_once dirname(dirname(__FILE__)) . '/models/search-module.php'; $modules = Search_Module_Factory::running(); $post = "CREATE TABLE `{$wpdb->prefix}search_post` (`post_id` int(11) unsigned NOT NULL,"; $comment = "CREATE TABLE `{$wpdb->prefix}search_comment` (`post_id` int(11) unsigned NOT NULL,`comment_id` int(11) unsigned NOT NULL,"; foreach ($modules as $module) { if ($module->is_post()) { $post .= "`" . $module->field_name() . "` text DEFAULT NULL,\n"; } else { $comment .= "`" . $module->field_name() . "` text DEFAULT NULL,\n"; } } $post .= "PRIMARY KEY (`post_id`))"; $comment .= "PRIMARY KEY (`post_id`,`comment_id`))"; $wpdb->query($post); $wpdb->query($comment); }