Merge "selenium: Remove "RunJobs" wait from specialrecentchanges test"
[lhc/web/wiklou.git] / includes / search / SearchMySQL.php
index 6253b55..ff21367 100644 (file)
@@ -24,6 +24,8 @@
  * @ingroup Search
  */
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * Search engine hook for MySQL 4+
  * @ingroup Search
@@ -43,8 +45,6 @@ class SearchMySQL extends SearchDatabase {
         * @return array
         */
        private function parseQuery( $filteredText, $fulltext ) {
-               global $wgContLang;
-
                $lc = $this->legalSearchChars( self::CHARS_NO_SYNTAX ); // Minus syntax chars (" and *)
                $searchon = '';
                $this->searchTerms = [];
@@ -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
@@ -125,7 +124,8 @@ class SearchMySQL extends SearchDatabase {
                        wfDebug( __METHOD__ . ": Can't understand search query '{$filteredText}'\n" );
                }
 
-               $searchon = $this->db->addQuotes( $searchon );
+               $dbr = $this->lb->getConnectionRef( DB_REPLICA );
+               $searchon = $dbr->addQuotes( $searchon );
                $field = $this->getIndexField( $fulltext );
                return [
                        " MATCH($field) AGAINST($searchon IN BOOLEAN MODE) ",
@@ -134,10 +134,8 @@ class SearchMySQL extends SearchDatabase {
        }
 
        private function regexTerm( $string, $wildcard ) {
-               global $wgContLang;
-
                $regex = preg_quote( $string, '/' );
-               if ( $wgContLang->hasWordBreaks() ) {
+               if ( MediaWikiServices::getInstance()->getContentLanguage()->hasWordBreaks() ) {
                        if ( $wildcard ) {
                                // Don't cut off the final bit!
                                $regex = "\b$regex";
@@ -147,12 +145,12 @@ 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;
        }
 
-       public static function legalSearchChars( $type = self::CHARS_ALL ) {
+       public function legalSearchChars( $type = self::CHARS_ALL ) {
                $searchChars = parent::legalSearchChars( $type );
                if ( $type === self::CHARS_ALL ) {
                        // " for phrase, * for wildcard
@@ -165,7 +163,7 @@ class SearchMySQL extends SearchDatabase {
         * Perform a full text search query and return a result set.
         *
         * @param string $term Raw search term
-        * @return SqlSearchResultSet
+        * @return SqlSearchResultSet|null
         */
        protected function doSearchTextInDB( $term ) {
                return $this->searchInternal( $term, true );
@@ -175,7 +173,7 @@ class SearchMySQL extends SearchDatabase {
         * Perform a title-only search query and return a result set.
         *
         * @param string $term Raw search term
-        * @return SqlSearchResultSet
+        * @return SqlSearchResultSet|null
         */
        protected function doSearchTitleInDB( $term ) {
                return $this->searchInternal( $term, false );
@@ -189,14 +187,15 @@ class SearchMySQL extends SearchDatabase {
 
                $filteredTerm = $this->filter( $term );
                $query = $this->getQuery( $filteredTerm, $fulltext );
-               $resultSet = $this->db->select(
+               $dbr = $this->lb->getConnectionRef( DB_REPLICA );
+               $resultSet = $dbr->select(
                        $query['tables'], $query['fields'], $query['conds'],
                        __METHOD__, $query['options'], $query['joins']
                );
 
                $total = null;
                $query = $this->getCountQuery( $filteredTerm, $fulltext );
-               $totalResult = $this->db->select(
+               $totalResult = $dbr->select(
                        $query['tables'], $query['fields'], $query['conds'],
                        __METHOD__, $query['options'], $query['joins']
                );
@@ -227,7 +226,8 @@ class SearchMySQL extends SearchDatabase {
        protected function queryFeatures( &$query ) {
                foreach ( $this->features as $feature => $value ) {
                        if ( $feature === 'title-suffix-filter' && $value ) {
-                               $query['conds'][] = 'page_title' . $this->db->buildLike( $this->db->anyString(), $value );
+                               $dbr = $this->lb->getConnectionRef( DB_REPLICA );
+                               $query['conds'][] = 'page_title' . $dbr->buildLike( $dbr->anyString(), $value );
                        }
                }
        }
@@ -342,7 +342,7 @@ class SearchMySQL extends SearchDatabase {
         * @param string $text
         */
        function update( $id, $title, $text ) {
-               $dbw = wfGetDB( DB_MASTER );
+               $dbw = $this->lb->getConnectionRef( DB_MASTER );
                $dbw->replace( 'searchindex',
                        [ 'si_page' ],
                        [
@@ -360,13 +360,12 @@ class SearchMySQL extends SearchDatabase {
         * @param string $title
         */
        function updateTitle( $id, $title ) {
-               $dbw = wfGetDB( DB_MASTER );
-
+               $dbw = $this->lb->getConnectionRef( DB_MASTER );
                $dbw->update( 'searchindex',
                        [ 'si_title' => $this->normalizeText( $title ) ],
                        [ 'si_page' => $id ],
-                       __METHOD__,
-                       [ $dbw->lowPriorityOption() ] );
+                       __METHOD__
+               );
        }
 
        /**
@@ -377,8 +376,7 @@ class SearchMySQL extends SearchDatabase {
         * @param string $title Title of page that was deleted
         */
        function delete( $id, $title ) {
-               $dbw = wfGetDB( DB_MASTER );
-
+               $dbw = $this->lb->getConnectionRef( DB_MASTER );
                $dbw->delete( 'searchindex', [ 'si_page' => $id ], __METHOD__ );
        }
 
@@ -389,8 +387,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
@@ -398,7 +394,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
@@ -446,7 +442,7 @@ class SearchMySQL extends SearchDatabase {
                if ( is_null( self::$mMinSearchLength ) ) {
                        $sql = "SHOW GLOBAL VARIABLES LIKE 'ft\\_min\\_word\\_len'";
 
-                       $dbr = wfGetDB( DB_REPLICA );
+                       $dbr = $this->lb->getConnectionRef( DB_REPLICA );
                        $result = $dbr->query( $sql, __METHOD__ );
                        $row = $result->fetchObject();
                        $result->free();