= 4 ) $this->full[] = $word; else $this->small[] = $word; } include_once dirname( dirname( __FILE__ ) ).'/models/search-module.php'; $modules = Search_Module_Factory::running(); $and = array(); $have_comments = false; foreach ( (array)$modules AS $module ) { if ( $module->is_post() ) $prefix = $wpdb->prefix.'search_post'; else { $have_comments = true; $prefix = $wpdb->prefix.'search_comment'; } if ( isset( $_GET[$module->field_name()] ) ) { $value = $module->field_value( $_GET[$module->field_name()] ); if ( $value !== false ) $and[] = $prefix.'.'.$module->field_name()." LIKE '%".$wpdb->escape( $value )."%'"; } } if ( $have_comments ) $this->fields .= ",{$wpdb->prefix}search_comment.comment_id"; // Any fulltext searches? $priorities = $fields = array(); if ( count( $this->full ) > 0 ) { $term = implode( ' ', $this->full ); $term = trim( $term ); $this->terms = $this->full; $scores = array(); foreach ( (array)$modules AS $module ) { if ( $module->is_post() ) $item = $wpdb->prefix.'search_post.'.$module->field_name(); else $item = $wpdb->prefix.'search_comment.'.$module->field_name(); $fields[] = $item; $scores[] = sprintf( "(%2.2f * (MATCH(%s) AGAINST ('%s' IN BOOLEAN MODE)))", $module->priority, $item, $wpdb->escape( $term ) ); } $this->fields .= ',MAX('.implode( ' + ', $scores ).') AS score'; $this->orderby = 'score DESC,'.$this->orderby; } // Form SQL $sql = "SELECT DISTINCT SQL_CALC_FOUND_ROWS ".$this->fields." FROM {$wpdb->posts} LEFT JOIN {$wpdb->prefix}search_post ON {$wpdb->posts}.ID={$wpdb->prefix}search_post.post_id "; if ( $have_comments ) $sql .= " LEFT JOIN {$wpdb->prefix}search_comment ON {$wpdb->posts}.ID={$wpdb->prefix}search_comment.post_id "; $sql .= ' WHERE 1=1 '; $sql .= $this->get_restricted_posts(); if ( count( $and ) > 0 ) $sql .= ' AND '.implode( ' AND ', $and ); // Add small words foreach ( (array)$this->small AS $small ) { $this->terms[] = $small; foreach ( (array)$modules AS $module ) { if ( $module->is_post() ) $prefix = $wpdb->prefix.'search_post'; else $prefix = $wpdb->prefix.'search_comment'; $priorities[] = $prefix.'.'.$module->field_name()." LIKE '%".$wpdb->escape( $small )."%'"; } } if ( count( $this->full ) > 0 ) $priorities[] = sprintf( "(MATCH(".implode( ',', $fields ).") AGAINST ('%s' IN BOOLEAN MODE))", $wpdb->escape( $term ) ); $sql .= ' AND ('.implode( ' OR ', $priorities ).') '; if ( count( $this->full ) > 0 ) $sql .= " GROUP BY {$wpdb->posts}.ID HAVING score "; $sql .= "ORDER BY ".$this->orderby.' '; $sql .= $this->limits; return $sql; } /** * Convert 'x AND y' into FULLTEXT * * @param array $matches Regex match array * @return string Replacement string **/ function logical_and( $matches ) { $this->full[] = '+'.$matches[1].' +'.$matches[2]; return ''; } /** * Record search phrases in quotes * * @param array $matches Regex match array * @return string Replacement string **/ function exact_words( $matches ) { $this->small[] = $matches[1]; return ''; } /** * Return the search terms as an array of regular expressions * * @return array Search terms **/ function get_terms() { $terms = array(); // Convert words into proper patterns foreach ( (array)$this->terms AS $word ) { if ( substr( $word, 0, 1 ) != '-' ) { $word = preg_quote( $word, '/' ); $word = str_replace( '\\*', '\w*', $word ); if ( trim( $word ) != '' ) $terms[] = '\b'.$word.'\b'; } } return $terms; } /** * 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 ); } } }