Merge "Fix highlighting for phrase queries"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Tue, 27 Jun 2017 23:44:27 +0000 (23:44 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 27 Jun 2017 23:44:27 +0000 (23:44 +0000)
includes/deferred/SearchUpdate.php
includes/search/SearchEngine.php
includes/search/SearchMySQL.php
includes/search/SearchOracle.php
includes/search/SearchSqlite.php
tests/phpunit/includes/search/SearchEngineTest.php

index b9a259b..c94ae2a 100644 (file)
@@ -124,7 +124,7 @@ class SearchUpdate implements DeferrableUpdate {
                # Language-specific strip/conversion
                $text = $wgContLang->normalizeForSearch( $text );
                $se = $se ?: MediaWikiServices::getInstance()->newSearchEngine();
-               $lc = $se->legalSearchChars() . '&#;';
+               $lc = $se->legalSearchCharsForUpdate() . '&#;';
 
                $text = preg_replace( "/<\\/?\\s*[A-Za-z][^>]*?>/",
                        ' ', $wgContLang->lc( " " . $text . " " ) ); # Strip HTML markup
@@ -207,7 +207,7 @@ class SearchUpdate implements DeferrableUpdate {
                $ns = $this->title->getNamespace();
                $title = $this->title->getText();
 
-               $lc = $search->legalSearchChars() . '&#;';
+               $lc = $search->legalSearchCharsForUpdate() . '&#;';
                $t = $wgContLang->normalizeForSearch( $title );
                $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
                $t = $wgContLang->lc( $t );
index ac95e7c..7d05265 100644 (file)
@@ -206,7 +206,7 @@ abstract class SearchEngine {
        }
 
        /**
-        * Get chars legal for search.
+        * Get chars legal for search (at query time).
         * NOTE: usage as static is deprecated and preserved only as BC measure
         * @return string
         */
@@ -214,6 +214,16 @@ abstract class SearchEngine {
                return "A-Za-z_'.0-9\\x80-\\xFF\\-";
        }
 
+       /**
+        * Get chars legal for search (at index time).
+        *
+        * @since 1.30
+        * @return string
+        */
+       public function legalSearchCharsForUpdate() {
+               return static::legalSearchChars();
+       }
+
        /**
         * Set the maximum number of results to return
         * and how many to skip before returning the first.
index 36cbbaa..2c7feeb 100644 (file)
@@ -149,8 +149,8 @@ class SearchMySQL extends SearchDatabase {
                return $regex;
        }
 
-       public static function legalSearchChars() {
-               return "\"*" . parent::legalSearchChars();
+       public function legalSearchCharsForUpdate() {
+               return "\"*" . parent::legalSearchCharsForUpdate();
        }
 
        /**
index c5a5ef1..2e6cb84 100644 (file)
@@ -266,7 +266,7 @@ class SearchOracle extends SearchDatabase {
                        [] );
        }
 
-       public static function legalSearchChars() {
-               return "\"" . parent::legalSearchChars();
+       public function legalSearchCharsForUpdate() {
+               return "\"" . parent::legalSearchCharsForUpdate();
        }
 }
index b40e1aa..5a8995d 100644 (file)
@@ -141,8 +141,8 @@ class SearchSqlite extends SearchDatabase {
                return $regex;
        }
 
-       public static function legalSearchChars() {
-               return "\"*" . parent::legalSearchChars();
+       public function legalSearchCharsForUpdate() {
+               return "\"*" . parent::legalSearchCharsForUpdate();
        }
 
        /**
index c74c893..6e00e53 100644 (file)
@@ -124,6 +124,24 @@ class SearchEngineTest extends MediaWikiLangTestCase {
                        "Plain search failed" );
        }
 
+       public function testPhraseSearch() {
+               $res = $this->search->searchText( '"smithee is one who smiths"' );
+               $this->assertEquals(
+                       [ 'Smithee' ],
+                       $this->fetchIds( $res ),
+                       "Phrase search failed" );
+               $res = $this->search->searchText( '"smithee is one who smiths"' );
+               $match = $res->next();
+               $terms = [ 'smithee', 'is', 'one', 'who', 'smiths' ];
+               $snippet = "";
+               foreach ( $terms as $term ) {
+                       $snippet .= " <span class='searchmatch'>" . $term . "</span>";
+               }
+               $this->assertRegexp( '/' . preg_quote( $snippet, '/' ) . '/',
+                       $match->getTextSnippet( $res->termMatches() ),
+                       "Phrase search failed to highlight" );
+       }
+
        public function testTextPowerSearch() {
                $this->search->setNamespaces( [ 0, 1, 4 ] );
                $this->assertEquals(