search: refactor DatabaseSearch to take a load balancer instance
[lhc/web/wiklou.git] / includes / search / SearchMssql.php
index 43bd3be..6a23bb3 100644 (file)
@@ -21,6 +21,9 @@
  * @ingroup Search
  */
 
+use MediaWiki\MediaWikiServices;
+use Wikimedia\Rdbms\IResultWrapper;
+
 /**
  * Search engine hook base class for Mssql (ConText).
  * @ingroup Search
@@ -32,8 +35,10 @@ class SearchMssql extends SearchDatabase {
         * @param string $term Raw search term
         * @return SqlSearchResultSet
         */
-       protected function doSearchText( $term ) {
-               $resultSet = $this->db->query( $this->getQuery( $this->filter( $term ), true ) );
+       protected function doSearchTextInDB( $term ) {
+               $dbr = $this->lb->getConnectionRef( DB_REPLICA );
+               $resultSet = $dbr->query( $this->getQuery( $this->filter( $term ), true ) );
+
                return new SqlSearchResultSet( $resultSet, $this->searchTerms );
        }
 
@@ -43,8 +48,10 @@ class SearchMssql extends SearchDatabase {
         * @param string $term Raw search term
         * @return SqlSearchResultSet
         */
-       protected function doSearchTitle( $term ) {
-               $resultSet = $this->db->query( $this->getQuery( $this->filter( $term ), false ) );
+       protected function doSearchTitleInDB( $term ) {
+               $dbr = $this->lb->getConnectionRef( DB_REPLICA );
+               $resultSet = $dbr->query( $this->getQuery( $this->filter( $term ), false ) );
+
                return new SqlSearchResultSet( $resultSet, $this->searchTerms );
        }
 
@@ -69,7 +76,9 @@ class SearchMssql extends SearchDatabase {
         * @return string
         */
        private function queryLimit( $sql ) {
-               return $this->db->limitResult( $sql, $this->limit, $this->offset );
+               $dbr = $this->lb->getConnectionRef( DB_REPLICA );
+
+               return $dbr->limitResult( $sql, $this->limit, $this->offset );
        }
 
        /**
@@ -117,8 +126,9 @@ class SearchMssql extends SearchDatabase {
         */
        private function queryMain( $filteredTerm, $fulltext ) {
                $match = $this->parseQuery( $filteredTerm, $fulltext );
-               $page = $this->db->tableName( 'page' );
-               $searchindex = $this->db->tableName( 'searchindex' );
+               $dbr = $this->lb->getMaintenanceConnectionRef( DB_REPLICA );
+               $page = $dbr->tableName( 'page' );
+               $searchindex = $dbr->tableName( 'searchindex' );
 
                return 'SELECT page_id, page_namespace, page_title, ftindex.[RANK]' .
                        "FROM $page,FREETEXTTABLE($searchindex , $match, LANGUAGE 'English') as ftindex " .
@@ -131,7 +141,6 @@ class SearchMssql extends SearchDatabase {
         * @return string
         */
        private function parseQuery( $filteredText, $fulltext ) {
-               global $wgContLang;
                $lc = $this->legalSearchChars( self::CHARS_NO_SYNTAX );
                $this->searchTerms = [];
 
@@ -142,7 +151,8 @@ class SearchMssql extends SearchDatabase {
                if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
                        $filteredText, $m, PREG_SET_ORDER ) ) {
                        foreach ( $m as $terms ) {
-                               $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] );
+                               $q[] = $terms[1] . MediaWikiServices::getInstance()->getContentLanguage()->
+                                       normalizeForSearch( $terms[2] );
 
                                if ( !empty( $terms[3] ) ) {
                                        $regexp = preg_quote( $terms[3], '/' );
@@ -156,8 +166,10 @@ class SearchMssql extends SearchDatabase {
                        }
                }
 
-               $searchon = $this->db->addQuotes( implode( ',', $q ) );
+               $dbr = $this->lb->getConnectionRef( DB_REPLICA );
+               $searchon = $dbr->addQuotes( implode( ',', $q ) );
                $field = $this->getIndexField( $fulltext );
+
                return "$field, $searchon";
        }
 
@@ -168,7 +180,7 @@ class SearchMssql extends SearchDatabase {
         * @param int $id
         * @param string $title
         * @param string $text
-        * @return bool|ResultWrapper
+        * @return bool|IResultWrapper
         */
        function update( $id, $title, $text ) {
                // We store the column data as UTF-8 byte order marked binary stream
@@ -176,13 +188,14 @@ class SearchMssql extends SearchDatabase {
                // to properly decode the stream as UTF-8.  SQL doesn't support UTF8 as a data type
                // but the indexer will correctly handle it by this method.  Since all we are doing
                // is passing this data to the indexer and never retrieving it via PHP, this will save space
-               $table = $this->db->tableName( 'searchindex' );
+               $dbr = $this->lb->getMaintenanceConnectionRef( DB_MASTER );
+               $table = $dbr->tableName( 'searchindex' );
                $utf8bom = '0xEFBBBF';
                $si_title = $utf8bom . bin2hex( $title );
                $si_text = $utf8bom . bin2hex( $text );
                $sql = "DELETE FROM $table WHERE si_page = $id;";
                $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)";
-               return $this->db->query( $sql, 'SearchMssql::update' );
+               return $dbr->query( $sql, 'SearchMssql::update' );
        }
 
        /**
@@ -191,16 +204,17 @@ class SearchMssql extends SearchDatabase {
         *
         * @param int $id
         * @param string $title
-        * @return bool|ResultWrapper
+        * @return bool|IResultWrapper
         */
        function updateTitle( $id, $title ) {
-               $table = $this->db->tableName( 'searchindex' );
+               $dbr = $this->lb->getMaintenanceConnectionRef( DB_MASTER );
+               $table = $dbr->tableName( 'searchindex' );
 
                // see update for why we are using the utf8bom
                $utf8bom = '0xEFBBBF';
                $si_title = $utf8bom . bin2hex( $title );
                $sql = "DELETE FROM $table WHERE si_page = $id;";
                $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)";
-               return $this->db->query( $sql, 'SearchMssql::updateTitle' );
+               return $dbr->query( $sql, 'SearchMssql::updateTitle' );
        }
 }