Move storing of $db down to SearchEngine
[lhc/web/wiklou.git] / includes / search / SearchMySQL.php
index 5eaf972..b92682a 100644 (file)
@@ -1,23 +1,25 @@
 <?php
-# Copyright (C) 2004 Brion Vibber <brion@pobox.com>
-# http://www.mediawiki.org/
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with this program; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-# http://www.gnu.org/copyleft/gpl.html
-
 /**
+ * MySQL search engine
+ *
+ * Copyright (C) 2004 Brion Vibber <brion@pobox.com>
+ * http://www.mediawiki.org/
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup Search
  */
  */
 class SearchMySQL extends SearchEngine {
        var $strictMatching = true;
+       static $mMinSearchLength;
 
-       /** @todo document */
+       /**
+        * Creates an instance of this class
+        * @param $db DatabaseMysql: database object
+        */
        function __construct( $db ) {
-               $this->db = $db;
+               parent::__construct( $db );
        }
 
        /** 
@@ -79,7 +85,7 @@ class SearchMySQL extends SearchEngine {
                                // fulltext engine.
                                // For Chinese this also inserts spaces between adjacent Han characters.
                                $strippedVariants = array_map(
-                                       array( $wgContLang, 'stripForSearch' ),
+                                       array( $wgContLang, 'normalizeForSearch' ),
                                        $variants );
                                
                                // Some languages such as Chinese force all variants to a canonical
@@ -91,9 +97,10 @@ class SearchMySQL extends SearchEngine {
                                if( count( $strippedVariants) > 1 )
                                        $searchon .= '(';
                                foreach( $strippedVariants as $stripped ) {
+                                       $stripped = $this->normalizeText( $stripped );
                                        if( $nonQuoted && strpos( $stripped, ' ' ) !== false ) {
                                                // Hack for Chinese: we need to toss in quotes for
-                                               // multiple-character phrases since stripForSearch()
+                                               // multiple-character phrases since normalizeForSearch()
                                                // added spaces between them to make word breaks.
                                                $stripped = '"' . trim( $stripped ) . '"';
                                        }
@@ -162,13 +169,13 @@ class SearchMySQL extends SearchEngine {
        }
        
        protected function searchInternal( $term, $fulltext ) {
-               global $wgSearchMySQLTotalHits;
+               global $wgCountTotalSearchHits;
                
                $filteredTerm = $this->filter( $term );
                $resultSet = $this->db->query( $this->getQuery( $filteredTerm, $fulltext ) );
                
                $total = null;
-               if( $wgSearchMySQLTotalHits ) {
+               if( $wgCountTotalSearchHits ) {
                        $totalResult = $this->db->query( $this->getCountQuery( $filteredTerm, $fulltext ) );
                        $row = $totalResult->fetchObject();
                        if( $row ) {
@@ -292,8 +299,8 @@ class SearchMySQL extends SearchEngine {
                        array( 'si_page' ),
                        array(
                                'si_page' => $id,
-                               'si_title' => $title,
-                               'si_text' => $text
+                               'si_title' => $this->normalizeText( $title ),
+                               'si_text' => $this->normalizeText( $text )
                        ), __METHOD__ );
        }
 
@@ -308,18 +315,97 @@ class SearchMySQL extends SearchEngine {
                $dbw = wfGetDB( DB_MASTER );
 
                $dbw->update( 'searchindex',
-                       array( 'si_title' => $title ),
+                       array( 'si_title' => $this->normalizeText( $title ) ),
                        array( 'si_page'  => $id ),
                        __METHOD__,
                        array( $dbw->lowPriorityOption() ) );
        }
+
+       /**
+        * Converts some characters for MySQL's indexing to grok it correctly,
+        * and pads short words to overcome limitations.
+        */
+       function normalizeText( $string ) {
+               global $wgContLang;
+
+               wfProfileIn( __METHOD__ );
+               
+               $out = parent::normalizeText( $string );
+
+               // MySQL fulltext index doesn't grok utf-8, so we
+               // need to fold cases and convert to hex
+               $out = preg_replace_callback(
+                       "/([\\xc0-\\xff][\\x80-\\xbf]*)/",
+                       array( $this, 'stripForSearchCallback' ),
+                       $wgContLang->lc( $out ) );
+
+               // And to add insult to injury, the default indexing
+               // ignores short words... Pad them so we can pass them
+               // through without reconfiguring the server...
+               $minLength = $this->minSearchLength();
+               if( $minLength > 1 ) {
+                       $n = $minLength - 1;
+                       $out = preg_replace(
+                               "/\b(\w{1,$n})\b/",
+                               "$1u800",
+                               $out );
+               }
+
+               // Periods within things like hostnames and IP addresses
+               // are also important -- we want a search for "example.com"
+               // or "192.168.1.1" to work sanely.
+               //
+               // MySQL's search seems to ignore them, so you'd match on
+               // "example.wikipedia.com" and "192.168.83.1" as well.
+               $out = preg_replace(
+                       "/(\w)\.(\w|\*)/u",
+                       "$1u82e$2",
+                       $out );
+
+               wfProfileOut( __METHOD__ );
+               
+               return $out;
+       }
+
+       /**
+        * Armor a case-folded UTF-8 string to get through MySQL's
+        * fulltext search without being mucked up by funny charset
+        * settings or anything else of the sort.
+        */
+       protected function stripForSearchCallback( $matches ) {
+               return 'u8' . bin2hex( $matches[1] );
+       }
+
+       /**
+        * Check MySQL server's ft_min_word_len setting so we know
+        * if we need to pad short words...
+        * 
+        * @return int
+        */
+       protected function minSearchLength() {
+               if( is_null( self::$mMinSearchLength ) ) {
+                       $sql = "SHOW GLOBAL VARIABLES LIKE 'ft\\_min\\_word\\_len'";
+
+                       $dbr = wfGetDB( DB_SLAVE );
+                       $result = $dbr->query( $sql );
+                       $row = $result->fetchObject();
+                       $result->free();
+
+                       if( $row && $row->Variable_name == 'ft_min_word_len' ) {
+                               self::$mMinSearchLength = intval( $row->Value );
+                       } else {
+                               self::$mMinSearchLength = 0;
+                       }
+               }
+               return self::$mMinSearchLength;
+       }
 }
 
 /**
  * @ingroup Search
  */
 class MySQLSearchResultSet extends SqlSearchResultSet {
-       function MySQLSearchResultSet( $resultSet, $terms, $totalHits=null ) {
+       function __construct( $resultSet, $terms, $totalHits=null ) {
                parent::__construct( $resultSet, $terms );
                $this->mTotalHits = $totalHits;
        }
@@ -327,4 +413,4 @@ class MySQLSearchResultSet extends SqlSearchResultSet {
        function getTotalHits() {
                return $this->mTotalHits;
        }
-}
\ No newline at end of file
+}