Merge "Add link to protect log to action=info"
[lhc/web/wiklou.git] / includes / search / SearchMySQL.php
index 2810bce..9f10697 100644 (file)
@@ -24,6 +24,8 @@
  * @ingroup Search
  */
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * Search engine hook for MySQL 4+
  * @ingroup Search
@@ -34,17 +36,15 @@ class SearchMySQL extends SearchDatabase {
        private static $mMinSearchLength;
 
        /**
-        * Parse the user's query and transform it into an SQL fragment which will
-        * become part of a WHERE clause
+        * Parse the user's query and transform it into two SQL fragments:
+        * a WHERE condition and an ORDER BY expression
         *
         * @param string $filteredText
         * @param string $fulltext
         *
-        * @return string
+        * @return array
         */
-       function parseQuery( $filteredText, $fulltext ) {
-               global $wgContLang;
-
+       private function parseQuery( $filteredText, $fulltext ) {
                $lc = $this->legalSearchChars( self::CHARS_NO_SYNTAX ); // Minus syntax chars (" and *)
                $searchon = '';
                $this->searchTerms = [];
@@ -54,9 +54,9 @@ class SearchMySQL extends SearchDatabase {
                if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
                                $filteredText, $m, PREG_SET_ORDER ) ) {
                        foreach ( $m as $bits ) {
-                               MediaWiki\suppressWarnings();
+                               Wikimedia\suppressWarnings();
                                list( /* all */, $modifier, $term, $nonQuoted, $wildcard ) = $bits;
-                               MediaWiki\restoreWarnings();
+                               Wikimedia\restoreWarnings();
 
                                if ( $nonQuoted != '' ) {
                                        $term = $nonQuoted;
@@ -76,7 +76,8 @@ class SearchMySQL extends SearchDatabase {
 
                                // Some languages such as Serbian store the input form in the search index,
                                // so we may need to search for matches in multiple writing system variants.
-                               $convertedVariants = $wgContLang->autoConvertToAllVariants( $term );
+                               $contLang = MediaWikiServices::getInstance()->getContentLanguage();
+                               $convertedVariants = $contLang->autoConvertToAllVariants( $term );
                                if ( is_array( $convertedVariants ) ) {
                                        $variants = array_unique( array_values( $convertedVariants ) );
                                } else {
@@ -87,9 +88,7 @@ class SearchMySQL extends SearchDatabase {
                                // around problems with minimum lengths and encoding in MySQL's
                                // fulltext engine.
                                // For Chinese this also inserts spaces between adjacent Han characters.
-                               $strippedVariants = array_map(
-                                       [ $wgContLang, 'normalizeForSearch' ],
-                                       $variants );
+                               $strippedVariants = array_map( [ $contLang, 'normalizeForSearch' ], $variants );
 
                                // Some languages such as Chinese force all variants to a canonical
                                // form when stripping to the low-level search index, so to be sure
@@ -127,14 +126,15 @@ class SearchMySQL extends SearchDatabase {
 
                $searchon = $this->db->addQuotes( $searchon );
                $field = $this->getIndexField( $fulltext );
-               return " MATCH($field) AGAINST($searchon IN BOOLEAN MODE) ";
+               return [
+                       " MATCH($field) AGAINST($searchon IN BOOLEAN MODE) ",
+                       " MATCH($field) AGAINST($searchon IN NATURAL LANGUAGE MODE) DESC "
+               ];
        }
 
-       function regexTerm( $string, $wildcard ) {
-               global $wgContLang;
-
+       private function regexTerm( $string, $wildcard ) {
                $regex = preg_quote( $string, '/' );
-               if ( $wgContLang->hasWordBreaks() ) {
+               if ( MediaWikiServices::getInstance()->getContentLanguage()->hasWordBreaks() ) {
                        if ( $wildcard ) {
                                // Don't cut off the final bit!
                                $regex = "\b$regex";
@@ -144,7 +144,7 @@ class SearchMySQL extends SearchDatabase {
                } else {
                        // For Chinese, words may legitimately abut other words in the text literal.
                        // Don't add \b boundary checks... note this could cause false positives
-                       // for latin chars.
+                       // for Latin chars.
                }
                return $regex;
        }
@@ -164,7 +164,7 @@ class SearchMySQL extends SearchDatabase {
         * @param string $term Raw search term
         * @return SqlSearchResultSet
         */
-       function searchText( $term ) {
+       protected function doSearchTextInDB( $term ) {
                return $this->searchInternal( $term, true );
        }
 
@@ -174,7 +174,7 @@ class SearchMySQL extends SearchDatabase {
         * @param string $term Raw search term
         * @return SqlSearchResultSet
         */
-       function searchTitle( $term ) {
+       protected function doSearchTitleInDB( $term ) {
                return $this->searchInternal( $term, false );
        }
 
@@ -261,7 +261,7 @@ class SearchMySQL extends SearchDatabase {
         * @return array
         * @since 1.18 (changed)
         */
-       function getQuery( $filteredTerm, $fulltext ) {
+       private function getQuery( $filteredTerm, $fulltext ) {
                $query = [
                        'tables' => [],
                        'fields' => [],
@@ -283,7 +283,7 @@ class SearchMySQL extends SearchDatabase {
         * @param bool $fulltext
         * @return string
         */
-       function getIndexField( $fulltext ) {
+       private function getIndexField( $fulltext ) {
                return $fulltext ? 'si_text' : 'si_title';
        }
 
@@ -295,7 +295,7 @@ class SearchMySQL extends SearchDatabase {
         * @param bool $fulltext
         * @since 1.18 (changed)
         */
-       function queryMain( &$query, $filteredTerm, $fulltext ) {
+       private function queryMain( &$query, $filteredTerm, $fulltext ) {
                $match = $this->parseQuery( $filteredTerm, $fulltext );
                $query['tables'][] = 'page';
                $query['tables'][] = 'searchindex';
@@ -303,7 +303,8 @@ class SearchMySQL extends SearchDatabase {
                $query['fields'][] = 'page_namespace';
                $query['fields'][] = 'page_title';
                $query['conds'][] = 'page_id=si_page';
-               $query['conds'][] = $match;
+               $query['conds'][] = $match[0];
+               $query['options']['ORDER BY'] = $match[1];
        }
 
        /**
@@ -312,13 +313,13 @@ class SearchMySQL extends SearchDatabase {
         * @param bool $fulltext
         * @return array
         */
-       function getCountQuery( $filteredTerm, $fulltext ) {
+       private function getCountQuery( $filteredTerm, $fulltext ) {
                $match = $this->parseQuery( $filteredTerm, $fulltext );
 
                $query = [
                        'tables' => [ 'page', 'searchindex' ],
                        'fields' => [ 'COUNT(*) as c' ],
-                       'conds' => [ 'page_id=si_page', $match ],
+                       'conds' => [ 'page_id=si_page', $match[0] ],
                        'options' => [],
                        'joins' => [],
                ];
@@ -385,8 +386,6 @@ class SearchMySQL extends SearchDatabase {
         * @return mixed|string
         */
        function normalizeText( $string ) {
-               global $wgContLang;
-
                $out = parent::normalizeText( $string );
 
                // MySQL fulltext index doesn't grok utf-8, so we
@@ -394,7 +393,7 @@ class SearchMySQL extends SearchDatabase {
                $out = preg_replace_callback(
                        "/([\\xc0-\\xff][\\x80-\\xbf]*)/",
                        [ $this, 'stripForSearchCallback' ],
-                       $wgContLang->lc( $out ) );
+                       MediaWikiServices::getInstance()->getContentLanguage()->lc( $out ) );
 
                // And to add insult to injury, the default indexing
                // ignores short words... Pad them so we can pass them