jenkins-bot has submitted this change and it was merged.

Change subject: Do not fetch results from commons if running from TextCat
......................................................................


Do not fetch results from commons if running from TextCat

Results from commons are likely to be displayed from the local
wiki search, running a query on detected language wiki + commons
will return duplicated results.

Force a local search in such case.

Bug: T145628
Change-Id: Iaecabd70caa251172834ce824cde84de49cb5427
---
M includes/CirrusSearch.php
M tests/unit/LanguageDetectTest.php
2 files changed, 42 insertions(+), 3 deletions(-)

Approvals:
  Cindy-the-browser-test-bot: Looks good to me, but someone else must approve
  EBernhardson: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/CirrusSearch.php b/includes/CirrusSearch.php
index ce43cd0..84ebe33 100644
--- a/includes/CirrusSearch.php
+++ b/includes/CirrusSearch.php
@@ -304,7 +304,7 @@
                                $config = null;
                        }
                        if ( $config ) {
-                               $matches = $this->searchTextReal( $term, 
$config );
+                               $matches = $this->searchTextReal( $term, 
$config, true );
                                if ( $matches instanceof ResultSet ) {
                                        $numRows = $matches->numRows();
                                        
$this->extraSearchMetrics['wgCirrusSearchAltLanguageNumResults'] = $numRows;
@@ -327,9 +327,11 @@
         * Do the hard part of the searching - actual Searcher invocation
         * @param string $term
         * @param SearchConfig $config
+        * @param boolean $forceLocal set to true to force searching on the
+        *        local wiki (e.g. avoid searching on commons)
         * @return null|Status|ResultSet
         */
-       private function searchTextReal( $term, SearchConfig $config = null ) {
+       protected function searchTextReal( $term, SearchConfig $config = null, 
$forceLocal = false ) {
                global $wgCirrusSearchInterwikiSources;
 
                // Convert the unicode character 'ideographic whitespace' into 
standard
@@ -353,6 +355,8 @@
                        $searcher->addSuggestPrefix( '~' );
                }
 
+               $searcher->getSearchContext()->setLimitSearchToLocalWiki( 
$forceLocal );
+
                // TODO remove this when we no longer have to support core 
versions without
                // Ie946150c6796139201221dfa6f7750c210e97166
                if ( method_exists( $this, 'getSort' ) ) {
diff --git a/tests/unit/LanguageDetectTest.php 
b/tests/unit/LanguageDetectTest.php
index a84298b..3439bc0 100644
--- a/tests/unit/LanguageDetectTest.php
+++ b/tests/unit/LanguageDetectTest.php
@@ -23,7 +23,7 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
  */
-class LanguageDetectTest extends \PHPUnit_Framework_TestCase {
+class LanguageDetectTest extends \MediaWikiTestCase {
 
        /**
         * @var \CirrusSearch
@@ -45,6 +45,7 @@
        }
 
        public function setUp() {
+               parent::setUp();
                $this->cirrus = new \CirrusSearch();
                global $wgCirrusSearchTextcatModel;
                if (empty( $wgCirrusSearchTextcatModel ) ) {
@@ -71,6 +72,31 @@
                $detector = new TextCat();
                $detect = $detector->detect($this->cirrus, "volviendose malo");
                $this->assertEquals("en", $detect);
+       }
+
+       /**
+        * Simply test the searchTextReal $forceLocal boolean flag.
+        * Testing the full chain seems hard so we just test that
+        * the $forceLocal flag is running a search on the local
+        * wiki.
+        */
+       public function testLocalSearch() {
+               \RequestContext::getMain()->setRequest( new \FauxRequest( [
+                       'cirrusDumpQuery' => 1,
+               ] ) );
+               $this->setMwGlobals( [
+                       'wgCirrusSearchIndexBaseName' => 'mywiki',
+                       'wgCirrusSearchExtraIndexes' => [NS_FILE => 
['externalwiki_file']],
+               ] );
+               $cirrus = new MyCirrusSearch();
+               $cirrus->setNamespaces( [NS_FILE] );
+               $cirrus->setDumpAndDie( false );
+               $result = $cirrus->mySearchTextReal( 'hello', 
$cirrus->getConfig(), true );
+               $result = json_decode( $result, true );
+               $this->assertEquals( 'mywiki_general/page/_search', 
$result['path'] );
+               $result = $cirrus->mySearchTextReal( 'hello', 
$cirrus->getConfig() );
+               $result = json_decode( $result, true );
+               $this->assertEquals( 
'mywiki_general,externalwiki_file/page/_search', $result['path'] );
        }
 
        public function getHttpLangs() {
@@ -106,3 +132,12 @@
                $this->httpLang = $http;
        }
 }
+
+/**
+ * Just a simple wrapper to access the protected method searchTextReal
+ */
+class MyCirrusSearch extends \CirrusSearch {
+       public function mySearchTextReal( $term, SearchConfig $config = null, 
$forceLocal = false ) {
+               return $this->searchTextReal( $term, $config, $forceLocal );
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/314312
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Iaecabd70caa251172834ce824cde84de49cb5427
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/CirrusSearch
Gerrit-Branch: master
Gerrit-Owner: DCausse <dcau...@wikimedia.org>
Gerrit-Reviewer: Cindy-the-browser-test-bot <bernhardsone...@gmail.com>
Gerrit-Reviewer: EBernhardson <ebernhard...@wikimedia.org>
Gerrit-Reviewer: Gehel <gleder...@wikimedia.org>
Gerrit-Reviewer: Manybubbles <never...@wikimedia.org>
Gerrit-Reviewer: Smalyshev <smalys...@wikimedia.org>
Gerrit-Reviewer: Tjones <tjo...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to