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

Change subject: Allow boost templates configuration with config vars
......................................................................


Allow boost templates configuration with config vars

This would greatly help testing boost templates with rel forge and
it's also needed for running an A/B test when on wiki configuration is
not suited with a new query builder, different similarity...

By default on wiki config overrides by config boost-templates.

- $wgCirrusSearchBoostTemplates allows to set the list of boosted
  templates
- $wgCirrusSearchIgnoreOnWikiBoostTemplates allows to ignore on wiki
  config useful with the UserTesting framework to force particular
  templates

Change-Id: If4402cf1da0167ab95c3e9d7c6c96f670a0325f7
---
M CirrusSearch.php
M includes/Util.php
M tests/unit/UtilTest.php
3 files changed, 148 insertions(+), 20 deletions(-)

Approvals:
  Smalyshev: Looks good to me, but someone else must approve
  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/CirrusSearch.php b/CirrusSearch.php
index 0be45b8..49ceaa1 100644
--- a/CirrusSearch.php
+++ b/CirrusSearch.php
@@ -955,6 +955,22 @@
  */
 $wgCirrusSearchExtraBackendLatency = 0;
 
+/**
+ * Configure default boost-templates
+ * Can be overridden on wiki and System messages.
+ *
+ * $wgCirrusSearchBoostTemplates = [
+ *     'Template:Featured article' => 2.0,
+ * ];
+ */
+$wgCirrusSearchBoostTemplates = [];
+
+/**
+ * Disable customization of boot templates on wiki
+ * Set to true to disable onwiki config.
+ */
+$wgCirrusSearchIgnoreOnWikiBoostTemplates = false;
+
 $includes = __DIR__ . "/includes/";
 $apiDir = $includes . 'Api/';
 $buildDocument = $includes . 'BuildDocument/';
diff --git a/includes/Util.php b/includes/Util.php
index 0cc60a6..d8685b5 100644
--- a/includes/Util.php
+++ b/includes/Util.php
@@ -374,20 +374,46 @@
         * @return \float[]
         */
        public static function getDefaultBoostTemplates( SearchConfig $config = 
null ) {
-               // are we on the same wiki?
                if ( is_null( $config ) ) {
                        $config = 
MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 
'CirrusSearch' );
-                       $local = true;
-               } else {
-                       $local = ( $config->getWikiId() == wfWikiID() );
                }
 
+               $fromConfig = $config->get( 'CirrusSearchBoostTemplates' );
+               if ( $config->get( 'CirrusSearchIgnoreOnWikiBoostTemplates' ) ) 
{
+                       // on wiki messages disabled, we can return this config
+                       // directly
+                       return $fromConfig;
+               }
+
+               $fromMessage = self::getOnWikiBoostTemplates( $config );
+               if ( empty( $fromMessage ) ) {
+                       // the onwiki config is empty (or unknown for non-local
+                       // config), we can fallback to templates from config
+                       return $fromConfig;
+               }
+               return $fromMessage;
+       }
+
+       /**
+        * Load and cache boost templates configured on wiki via the system
+        * message 'cirrussearch-boost-templates'.
+        * If called from the local wiki the message will be cached.
+        * If called from a non local wiki an attempt to fetch this data from 
the cache is made.
+        * If an empty array is returned it means that no config is available 
on wiki
+        * or the value possibly unknown if run from a non local wiki.
+        *
+        * @param SearchConfig $config
+        * @return \float[] indexed by template name
+        */
+       private static function getOnWikiBoostTemplates( SearchConfig $config ) 
{
                $cache = \ObjectCache::getLocalClusterInstance();
                $cacheKey = $cache->makeGlobalKey( 
'cirrussearch-boost-templates', $config->getWikiId() );
-
-               if ( $local ) {
-                       // if we're dealing with local templates
+               if ( $config->getWikiId() == wfWikiID() ) {
+                       // Local wiki we can fetch boost templates from system
+                       // message
                        if ( self::$defaultBoostTemplates !== null ) {
+                               // This static cache is never set with non-local
+                               // wiki data.
                                return self::$defaultBoostTemplates;
                        }
 
@@ -407,15 +433,15 @@
                        self::$defaultBoostTemplates = $templates;
                        return $templates;
                }
-
                // Here we're dealing with boost template from other wiki, try 
to fetch it if it exists
                // otherwise, don't bother.
-               // We won't use static cache for non-local keys.
-               $templates = $cache->get( $cacheKey );
-               if ( !$templates ) {
+               $nonLocalCache = $cache->get( $cacheKey );
+               if ( !is_array( $nonLocalCache ) ) {
+                       // not yet in cache, value is unknown
+                       // return empty array
                        return [];
                }
-               return $templates;
+               return $nonLocalCache;
        }
 
        /**
diff --git a/tests/unit/UtilTest.php b/tests/unit/UtilTest.php
index 12d1df8..c315947 100644
--- a/tests/unit/UtilTest.php
+++ b/tests/unit/UtilTest.php
@@ -312,13 +312,19 @@
 
        /**
         * Create test hash config for a wiki.
-        * @param $wiki
+        * @param string $wiki
+        * @param mixed[] $moreData additional config
         * @return HashSearchConfig
         */
-       private function getHashConfig( $wiki ) {
-               $config = new HashSearchConfig([
-                       '_wikiID' => $wiki
-               ]);
+       private function getHashConfig( $wiki, array $moreData = array() ) {
+               if ( !isset( $moreData['CirrusSearchBoostTemplates'] ) ) {
+                       $moreData['CirrusSearchBoostTemplates'] = [];
+               }
+               if ( !isset( 
$moreData['CirrusSearchIgnoreOnWikiBoostTemplates'] ) ) {
+                       $moreData['CirrusSearchIgnoreOnWikiBoostTemplates'] = 
false;
+               }
+               $moreData[ '_wikiID' ] = $wiki;
+               $config = new HashSearchConfig( $moreData );
                return $config;
        }
 
@@ -329,7 +335,7 @@
         */
        private function putDataIntoCache( \BagOStuff $cache, $wiki ) {
                $key = $cache->makeGlobalKey( 'cirrussearch-boost-templates', 
$wiki );
-               $cache->set( $key, "Data for $wiki" );
+               $cache->set( $key, ["Data for $wiki" => 2] );
        }
 
        /**
@@ -364,16 +370,96 @@
 
        }
 
+       /**
+        * @covers Utils::getDefaultBoostTemplates
+        */
+       public function testCustomizeBoostTemplatesByConfig() {
+               $configValues = [
+                       'CirrusSearchBoostTemplates' => [
+                               'Featured' => 2,
+                       ],
+               ];
+               $config = $this->getHashConfig( 'ruwiki', $configValues );
+               $ru = Util::getDefaultBoostTemplates( $config );
+               $this->assertArrayEquals( 
$configValues['CirrusSearchBoostTemplates'], $ru );
+       }
+
+       /**
+        * @covers Utils::getDefaultBoostTemplates
+        */
+       public function testOverrideBoostTemplatesWithOnWikiConfig() {
+               $configValues = [
+                       'CirrusSearchBoostTemplates' => [
+                               'Featured' => 2,
+                       ],
+               ];
+               $config = $this->getHashConfig( 'ruwiki', $configValues );
+
+               // On wiki config should override config templates
+               $cache = $this->makeLocalCache();
+               $this->putDataIntoCache( $cache, 'ruwiki' );
+               $ru = Util::getDefaultBoostTemplates( $config );
+               $this->assertNotEquals( 
$configValues['CirrusSearchBoostTemplates'], $ru );
+       }
+
+       /**
+        * @covers Utils::getDefaultBoostTemplates
+        */
+       public function testOverrideBoostTemplatesWithOnCurrentWikiConfig() {
+               $configValues = [
+                       'CirrusSearchBoostTemplates' => [
+                               'Featured' => 2,
+                       ],
+               ];
+               $config = $this->getHashConfig( wfWikiID(), $configValues );
+
+               // On wiki config should override config templates
+               $cache = $this->makeLocalCache();
+               $this->putDataIntoCache( $cache, wfWikiID() );
+
+               $ru = Util::getDefaultBoostTemplates( $config );
+               $this->assertNotEquals( 
$configValues['CirrusSearchBoostTemplates'], $ru );
+       }
+
+       /**
+        * @covers Utils::getDefaultBoostTemplates
+        */
+       public function testDisableOverrideBoostTemplatesWithOnWikiConfig() {
+               $configValues = [
+                       'CirrusSearchBoostTemplates' => [
+                               'Featured' => 2,
+                       ],
+                       // we can disable on wiki customization
+                       'CirrusSearchIgnoreOnWikiBoostTemplates' => true,
+               ];
+               $config = $this->getHashConfig( 'ruwiki', $configValues );
+
+               $cache = $this->makeLocalCache();
+               $this->putDataIntoCache( $cache, 'ruwiki' );
+
+               $ru = Util::getDefaultBoostTemplates( $this->getHashConfig( 
'ruwiki' ) );
+               $this->assertArrayEquals( 
$configValues['CirrusSearchBoostTemplates'], $ru );
+       }
+
        public function testgetDefaultBoostTemplatesLocal() {
                global $wgContLang;
                $this->setPrivateVar( \MessageCache::class, 'instance', 
$this->getMockCache() );
                $this->setPrivateVar( Util::class, 'defaultBoostTemplates', 
null );
 
                $cache = $this->makeLocalCache();
-               $config = 
MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 
'CirrusSearch' );
+               $config = $this->getHashConfig( wfWikiID() );
                $key = $cache->makeGlobalKey( 'cirrussearch-boost-templates', 
$config->getWikiId() );
 
-               $cur = Util::getDefaultBoostTemplates();
+               // FIXME: we cannot really test the default value for $config
+               // with Util::getDefaultBoostTemplates(). It looks like
+               // MediaWikiServices initializes the current wiki SearchConfig
+               // when wfWikiID() == 'wiki' and then it's cached, the test
+               // framework seems to update the wiki name to wiki-unittest_
+               // making it impossible to test if we are running on the local
+               // wiki.
+               // resetting MediaWikiServices would be nice but it does not
+               // seem to be trivial.
+               $cur = Util::getDefaultBoostTemplates( $config );
                reset( $cur );
                $this->assertContains( ' in ' . $wgContLang->getCode(), key( 
$cur ) );
 

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

Gerrit-MessageType: merged
Gerrit-Change-Id: If4402cf1da0167ab95c3e9d7c6c96f670a0325f7
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/CirrusSearch
Gerrit-Branch: master
Gerrit-Owner: DCausse <[email protected]>
Gerrit-Reviewer: Cindy-the-browser-test-bot <[email protected]>
Gerrit-Reviewer: DCausse <[email protected]>
Gerrit-Reviewer: EBernhardson <[email protected]>
Gerrit-Reviewer: Gehel <[email protected]>
Gerrit-Reviewer: Manybubbles <[email protected]>
Gerrit-Reviewer: Smalyshev <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to