Deprecate SearchEngine::replacePrefixes
authorDavid Causse <dcausse@wikimedia.org>
Tue, 10 Jul 2018 12:10:09 +0000 (14:10 +0200)
committerEBernhardson <ebernhardson@wikimedia.org>
Tue, 17 Jul 2018 21:56:14 +0000 (21:56 +0000)
This should be handled internally by SearchEngine implementations.

Bug: T198860
Change-Id: Ifbfd0fcb81fcacf5228bd2ffcac7b80fca872b2a
Depends-On: I7d4ff9498fa1f4ea66835c634b8931f4c29993fb

RELEASE-NOTES-1.32
includes/api/ApiQuerySearch.php
includes/search/SearchDatabase.php
includes/search/SearchEngine.php
includes/search/SearchMssql.php
includes/search/SearchMySQL.php
includes/search/SearchOracle.php
includes/search/SearchPostgres.php
includes/search/SearchSqlite.php
includes/specials/SpecialSearch.php

index 4f1cd79..c2dbd9d 100644 (file)
@@ -273,6 +273,8 @@ because of Phabricator reports.
   logic as it should be handled internally by SearchEngine implementations
   supporting this feature. SearchEngine implementations should no longer
   override this methods.
+* SearchEngine::replacePrefixes( $query ) should no longer be called prior
+  to running searchText/searchTitle.
 
 === Other changes in 1.32 ===
 * (T198811) The following tables have had their UNIQUE indexes turned into
index 2153fdc..f5461e0 100644 (file)
@@ -72,8 +72,13 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                        wfDeprecated( 'SearchEngine::transformSearchTerm() (overridden by ' .
                                get_class( $search ) . ')', '1.32' );
                }
-               $query = $search->replacePrefixes( $query );
 
+               $nquery = $search->replacePrefixes( $query );
+               if ( $nquery !== $query ) {
+                       $query = $nquery;
+                       wfDeprecated( 'SearchEngine::replacePrefixes() (overridden by ' .
+                                                 get_class( $search ) . ')', '1.32' );
+               }
                // Perform the actual search
                if ( $what == 'text' ) {
                        $matches = $search->searchText( $query );
index f2929a3..93f8d23 100644 (file)
@@ -28,7 +28,7 @@ use Wikimedia\Rdbms\IDatabase;
  * @ingroup Search
  * @since 1.23
  */
-class SearchDatabase extends SearchEngine {
+abstract class SearchDatabase extends SearchEngine {
        /**
         * @var IDatabase Slave database for reading from for results
         */
@@ -45,6 +45,38 @@ class SearchDatabase extends SearchEngine {
                }
        }
 
+       /**
+        * @param string $term
+        * @return SearchResultSet|Status|null
+        */
+       final public function doSearchText( $term ) {
+               return $this->doSearchTextInDB( $this->extractNamespacePrefix( $term ) );
+       }
+
+       /**
+        * Perform a full text search query and return a result set.
+        *
+        * @param string $term Raw search term
+        * @return SqlSearchResultSet
+        */
+       abstract protected function doSearchTextInDB( $term );
+
+       /**
+        * @param string $term
+        * @return SearchResultSet|null
+        */
+       final public function doSearchTitle( $term ) {
+               return $this->doSearchTitleInDB( $this->extractNamespacePrefix( $term ) );
+       }
+
+       /**
+        * Perform a title-only search query and return a result set.
+        *
+        * @param string $term Raw search term
+        * @return SqlSearchResultSet
+        */
+       abstract protected function doSearchTitleInDB( $term );
+
        /**
         * Return a 'cleaned up' search string
         *
@@ -58,4 +90,19 @@ class SearchDatabase extends SearchEngine {
                $lc = $this->legalSearchChars( self::CHARS_ALL );
                return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
        }
+
+       /**
+        * Extract the optional namespace prefix and set self::namespaces
+        * accordingly and return the query string
+        * @param string $term
+        * @return string the query string without any namespace prefix
+        */
+       final protected function extractNamespacePrefix( $term ) {
+               $queryAndNs = self::parseNamespacePrefixes( $term );
+               if ( $queryAndNs === false ) {
+                       return $term;
+               }
+               $this->namespaces = $queryAndNs[1];
+               return $queryAndNs[0];
+       }
 }
index d46918f..30c2271 100644 (file)
@@ -387,16 +387,12 @@ abstract class SearchEngine {
         * or namespace names and set the list of namespaces
         * of this class accordingly.
         *
+        * @deprecated since 1.32; should be handled internally by the search engine
         * @param string $query
         * @return string
         */
        function replacePrefixes( $query ) {
-               $queryAndNs = self::parseNamespacePrefixes( $query );
-               if ( $queryAndNs === false ) {
-                       return $query;
-               }
-               $this->namespaces = $queryAndNs[1];
-               return $queryAndNs[0];
+               return $query;
        }
 
        /**
index 30ac92d..289f925 100644 (file)
@@ -34,7 +34,7 @@ class SearchMssql extends SearchDatabase {
         * @param string $term Raw search term
         * @return SqlSearchResultSet
         */
-       protected function doSearchText( $term ) {
+       protected function doSearchTextInDB( $term ) {
                $resultSet = $this->db->query( $this->getQuery( $this->filter( $term ), true ) );
                return new SqlSearchResultSet( $resultSet, $this->searchTerms );
        }
@@ -45,7 +45,7 @@ class SearchMssql extends SearchDatabase {
         * @param string $term Raw search term
         * @return SqlSearchResultSet
         */
-       protected function doSearchTitle( $term ) {
+       protected function doSearchTitleInDB( $term ) {
                $resultSet = $this->db->query( $this->getQuery( $this->filter( $term ), false ) );
                return new SqlSearchResultSet( $resultSet, $this->searchTerms );
        }
index 9a03ebe..6253b55 100644 (file)
@@ -167,7 +167,7 @@ class SearchMySQL extends SearchDatabase {
         * @param string $term Raw search term
         * @return SqlSearchResultSet
         */
-       protected function doSearchText( $term ) {
+       protected function doSearchTextInDB( $term ) {
                return $this->searchInternal( $term, true );
        }
 
@@ -177,7 +177,7 @@ class SearchMySQL extends SearchDatabase {
         * @param string $term Raw search term
         * @return SqlSearchResultSet
         */
-       protected function doSearchTitle( $term ) {
+       protected function doSearchTitleInDB( $term ) {
                return $this->searchInternal( $term, false );
        }
 
index 7fe5b53..6d7e988 100644 (file)
@@ -64,7 +64,7 @@ class SearchOracle extends SearchDatabase {
         * @param string $term Raw search term
         * @return SqlSearchResultSet
         */
-       protected function doSearchText( $term ) {
+       protected function doSearchTextInDB( $term ) {
                if ( $term == '' ) {
                        return new SqlSearchResultSet( false, '' );
                }
@@ -79,7 +79,7 @@ class SearchOracle extends SearchDatabase {
         * @param string $term Raw search term
         * @return SqlSearchResultSet
         */
-       protected function doSearchTitle( $term ) {
+       protected function doSearchTitleInDB( $term ) {
                if ( $term == '' ) {
                        return new SqlSearchResultSet( false, '' );
                }
index 729e528..6d5f117 100644 (file)
@@ -37,7 +37,7 @@ class SearchPostgres extends SearchDatabase {
         * @param string $term Raw search term
         * @return SqlSearchResultSet
         */
-       protected function doSearchTitle( $term ) {
+       protected function doSearchTitleInDB( $term ) {
                $q = $this->searchQuery( $term, 'titlevector', 'page_title' );
                $olderror = error_reporting( E_ERROR );
                $resultSet = $this->db->query( $q, 'SearchPostgres', true );
@@ -45,7 +45,7 @@ class SearchPostgres extends SearchDatabase {
                return new SqlSearchResultSet( $resultSet, $this->searchTerms );
        }
 
-       protected function doSearchText( $term ) {
+       protected function doSearchTextInDB( $term ) {
                $q = $this->searchQuery( $term, 'textvector', 'old_text' );
                $olderror = error_reporting( E_ERROR );
                $resultSet = $this->db->query( $q, 'SearchPostgres', true );
index 1dc37d2..0ed477a 100644 (file)
@@ -156,7 +156,7 @@ class SearchSqlite extends SearchDatabase {
         * @param string $term Raw search term
         * @return SqlSearchResultSet
         */
-       protected function doSearchText( $term ) {
+       protected function doSearchTextInDB( $term ) {
                return $this->searchInternal( $term, true );
        }
 
@@ -166,7 +166,7 @@ class SearchSqlite extends SearchDatabase {
         * @param string $term Raw search term
         * @return SqlSearchResultSet
         */
-       protected function doSearchTitle( $term ) {
+       protected function doSearchTitleInDB( $term ) {
                return $this->searchInternal( $term, false );
        }
 
index 15349b8..2cff90e 100644 (file)
@@ -314,16 +314,20 @@ class SpecialSearch extends SpecialPage {
                $showSuggestion = $title === null || !$title->isKnown();
                $search->setShowSuggestion( $showSuggestion );
 
-               $nterm = $search->transformSearchTerm( $term );
-               if ( $nterm !== $term ) {
-                       $term = $nterm;
+               $rewritten = $search->transformSearchTerm( $term );
+               if ( $rewritten !== $term ) {
+                       $term = $rewritten;
                        wfDeprecated( 'SearchEngine::transformSearchTerm() (overridden by ' .
                                get_class( $search ) . ')', '1.32' );
                }
 
-               // fetch search results
                $rewritten = $search->replacePrefixes( $term );
+               if ( $rewritten !== $term ) {
+                       wfDeprecated( 'SearchEngine::replacePrefixes() (overridden by ' .
+                                                 get_class( $search ) . ')', '1.32' );
+               }
 
+               // fetch search results
                $titleMatches = $search->searchTitle( $rewritten );
                $textMatches = $search->searchText( $rewritten );