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

Change subject: (bug 52039) Use closures for dynamic defaults.
......................................................................


(bug 52039) Use closures for dynamic defaults.

Some default values depend on other settings that may not
have been initialized at the point when the defaults are defined.

This introduces a mechanism to allow defaults to be defined as
closures, so the calculation of the actual value can be deferred.

Change-Id: I9d22e46d9141252a9663e1bc8df6766f149291c2
---
M client/WikibaseClient.hooks.php
M client/WikibaseClient.php
M client/config/WikibaseClient.default.php
A client/tests/phpunit/ClientDefaultsTest.php
D client/tests/phpunit/ClientHooksTest.php
M lib/config/WikibaseLib.default.php
M lib/includes/Settings.php
M lib/includes/SettingsArray.php
M repo/Wikibase.php
9 files changed, 339 insertions(+), 219 deletions(-)

Approvals:
  Aude: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/client/WikibaseClient.hooks.php b/client/WikibaseClient.hooks.php
index 324d916..e016971 100644
--- a/client/WikibaseClient.hooks.php
+++ b/client/WikibaseClient.hooks.php
@@ -166,47 +166,6 @@
        }
 
        /**
-        * @since 0.4
-        *
-        * @param SettingsArray $settings
-        * @param array         $wg global settings (wgXXX).
-        * @param bool          $repoIsLocal whether this wiki is also our repo.
-        */
-       public static function applyMagicDefaultSettings( SettingsArray 
$settings, array $wg, $repoIsLocal ) {
-               // to the actual values for the local wiki.
-               if ( $repoIsLocal ) {
-                       if ( $settings->getSetting( 'repoUrl' ) === null ) {
-                               $settings->setSetting( 'repoUrl', 
$wg['wgServer'] );
-                       }
-
-                       if ( $settings->getSetting( 'repoArticlePath' ) === 
null ) {
-                               $settings->setSetting( 'repoArticlePath', 
$wg['wgArticlePath'] );
-                       }
-
-                       if ( $settings->getSetting( 'repoScriptPath' ) === null 
) {
-                               $settings->setSetting( 'repoScriptPath', 
$wg['wgScriptPath'] );
-                       }
-               }
-
-               if ( $settings->getSetting( 'siteGlobalID' ) === null ) {
-                       $settings->setSetting( 'siteGlobalID', $wg['wgDBName'] 
);
-               }
-       }
-
-       /**
-        * @since 0.4
-        *
-        * @return bool
-        */
-       public static function onSetupAfterCache() {
-               $settings = Settings::singleton();
-
-               self::applyMagicDefaultSettings( $settings, $GLOBALS, defined( 
'WB_VERSION' ) );
-
-               return true;
-       }
-
-       /**
         * Hook for injecting a message on [[Special:MovePage]]
         * @see 
https://www.mediawiki.org/wiki/Manual:Hooks/SpecialMovepageAfterMove
         *
diff --git a/client/WikibaseClient.php b/client/WikibaseClient.php
index 70500a3..0529a95 100644
--- a/client/WikibaseClient.php
+++ b/client/WikibaseClient.php
@@ -35,6 +35,8 @@
        die( "<b>Error:</b> Wikibase requires MediaWiki 1.21 alpha or above.\n" 
);
 }
 
+define( 'WBC_DIR', __DIR__ );
+
 // Include the WikibaseLib extension if that hasn't been done yet, since it's 
required for WikibaseClient to work.
 if ( !defined( 'WBL_VERSION' ) ) {
        @include_once( __DIR__ . '/../lib/WikibaseLib.php' );
@@ -147,7 +149,6 @@
        // extension hooks
        $wgHooks['WikibaseDeleteData'][]                                    = 
'\Wikibase\ClientHooks::onWikibaseDeleteData';
        $wgHooks['WikibaseRebuildData'][]                                   = 
'\Wikibase\ClientHooks::onWikibaseRebuildData';
-       $wgHooks['SetupAfterCache'][]                                   = 
'\Wikibase\ClientHooks::onSetupAfterCache';
 
        // api modules
        $wgAPIMetaModules['wikibase'] = 'Wikibase\ApiClientInfo';
diff --git a/client/config/WikibaseClient.default.php 
b/client/config/WikibaseClient.default.php
index 0c445d7..10c5fda 100644
--- a/client/config/WikibaseClient.default.php
+++ b/client/config/WikibaseClient.default.php
@@ -30,26 +30,23 @@
  */
 
 
+use Wikibase\SettingsArray;
+
 return call_user_func( function() {
        global $wgLanguageCode, $wgDBname;
 
        $defaults = array(
                'namespaces' => array(), // by default, include all namespaces; 
deprecated as of 0.4
                'excludeNamespaces' => array(),
-               'repoUrl' => '//www.wikidata.org',
-               'repoScriptPath' => '/w',
-               'repoArticlePath' => '/wiki/$1',
                'sort' => 'code',
                'sortPrepend' => array(),
                'alwaysSort' => false,
-               'siteGlobalID' => $wgDBname,
                // @todo would be great to just get this from the sites stuff
                // but we will need to make sure the caching works good enough
                'siteLocalID' => $wgLanguageCode,
                'injectRecentChanges' => true,
                'showExternalRecentChanges' => true,
                'defaultClientStore' => null,
-               'repoDatabase' => null, // note: "false" means "local"!
                // default for repo items in main namespace
                'repoNamespaces' => array(
                        'wikibase-item' => '',
@@ -181,16 +178,82 @@
                ),
        );
 
-       // Repository is active on the local wiki, change defaults accordingly
-       if ( defined( 'WB_VERSION' ) ) {
-               $defaults['repoUrl'] = null; // initialized later
-               $defaults['repoArticlePath'] = null; // initialized later
-               $defaults['repoScriptPath'] = null; // initialized later
+       // Some defaults depend on information not available at this time.
+       // Especially, if the repository may be active on the local wiki, and
+       // we need to adjust some defaults accordingly.
+       // We use Closures to calculate such settings on the fly, the first 
time they
+       // are used. See SettingsArray::setSetting() for details.
 
-               // use the local database for direct repo access
-               $defaults['repoDatabase'] = false;
-               $defaults['changesDatabase'] = false;
-       }
+       //NOTE: when this is executed, WB_VERSION may not yet be defined, 
because
+       //      the repo extension has not yet been initialized. We need to 
defer the
+       //      check and do it inside the closures.
+       //      We use the pseudo-setting thisWikiIsTheRepo to store this 
information.
+       //      thisWikiIsTheRepo should really never be overwritten, except 
for testing.
+
+       $defaults['thisWikiIsTheRepo'] = function ( SettingsArray $settings ) {
+               // determine whether the repo extension is present
+               return defined( 'WB_VERSION' );
+       };
+
+       $defaults['repoUrl'] = function ( SettingsArray $settings ) {
+               // use $wgServer if this wiki is the repo, otherwise default to 
wikidata.org
+               return $settings->getSetting( 'thisWikiIsTheRepo' ) ? 
$GLOBALS['wgServer'] : '//www.wikidata.org';
+       };
+
+       $defaults['repoArticlePath'] = function ( SettingsArray $settings ) {
+               // use $wgArticlePath if this wiki is the repo, otherwise 
default to /wiki/$1
+               return $settings->getSetting( 'thisWikiIsTheRepo' ) ? 
$GLOBALS['wgArticlePath'] : '/wiki/$1';
+       };
+
+       $defaults['repoScriptPath'] = function ( SettingsArray $settings ) {
+               // use $wgScriptPath if this wiki is the repo, otherwise 
default to /w
+               return $settings->getSetting( 'thisWikiIsTheRepo' ) ? 
$GLOBALS['wgScriptPath'] : '/w';
+       };
+
+       $defaults['repoDatabase'] = function ( SettingsArray $settings ) {
+               // Use false (meaning the local wiki's database) if this wiki 
is the repo,
+               // otherwise default to null (meaning we can't access the 
repo's DB directly).
+               return $settings->getSetting( 'thisWikiIsTheRepo' ) ? false : 
null;
+       };
+
+       $defaults['changesDatabase'] = function ( SettingsArray $settings ) {
+               // Per default, the database for tracking changes is the repo's 
database.
+               // Note that the value for the repoDatabase setting may be 
calculated dynamically,
+               // see above.
+               return $settings->getSetting( 'repoDatabase' );
+       };
+
+       $defaults['siteGlobalID'] = function ( SettingsArray $settings ) {
+               // The database name is a sane default for the site ID.
+               // On Wikimedia sites, this is always correct.
+               return $GLOBALS['wgDBname'];
+       };
+
+       // Prefix to use for cache keys that should be shared among
+       // a wikibase repo and all its clients.
+       // In order to share caches between clients (and the repo)
+       // the default includes WBL_VERSION and the repo's DB name.
+       $defaults['sharedCacheKeyPrefix'] = function ( SettingsArray $settings 
) {
+               // Per default, the database for tracking changes is the repo's 
database.
+               // Note that the value for the repoDatabase setting may be 
calculated dynamically,
+               // see above.
+
+               //NOTE: keep in sync with the default value for 
sharedCacheKeyPrefix defined
+               //      in WikibaseLib.default.php, since that's the key the 
repo is using per
+               //      default.
+
+               $repoId = $settings->getSetting( 'repoDatabase' );
+
+               if ( $repoId === false ) {
+                       // false means the local wiki's DB (this is the case 
when the local wiki is the repo)
+                       $repoId = $GLOBALS['wgDBname'];
+               } elseif ( $repoId === null ) {
+                       // null means no direct access to the repo's DB, so use 
the repo's URL instead
+                       $repoId = 'repoUrl:' . $settings->getSetting( 'repoUrl' 
);
+               }
+
+               return $repoId . ':WBL/' . WBL_VERSION;
+       };
 
        return $defaults;
 } );
diff --git a/client/tests/phpunit/ClientDefaultsTest.php 
b/client/tests/phpunit/ClientDefaultsTest.php
new file mode 100644
index 0000000..e158da6
--- /dev/null
+++ b/client/tests/phpunit/ClientDefaultsTest.php
@@ -0,0 +1,228 @@
+<?php
+ /**
+ *
+ * Copyright © 02.07.13 by the authors listed below.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @license GPL 2+
+ * @file
+ *
+ * @ingroup WikibaseClient
+ * @ingroup Test
+ *
+ * @group Wikibase
+ * @group WikibaseClient
+ *
+ * @author Daniel Kinzler
+ */
+
+
+namespace Wikibase\Test;
+
+
+use Wikibase\SettingsArray;
+
+/**
+ * Class ClientDefaultsTest
+ * @package Wikibase\Test
+ */
+class ClientDefaultsTest extends \MediaWikiTestCase {
+
+       public static function settingsProvider() {
+               //TODO: repoDatabase
+               //TODO: changesDatabase
+               //TODO: sharedCacheKeyPrefix
+
+               return array(
+                       array( // #0: no local repo, all values set
+                               array( // $settings
+                                       'repoUrl' => 'http://acme.com',
+                                       'repoArticlePath' => '/wiki',
+                                       'repoScriptPath' => '/w',
+                                       'siteGlobalID' => 'mywiki',
+                                       'repoDatabase' => 'foo',
+                                       'changesDatabase' => 'doo',
+                                       'sharedCacheKeyPrefix' => 'xoo:WBL/' . 
WBL_VERSION,
+                               ),
+                               array( // $wg
+                                       'wgServer' => 'http://www.acme.com',
+                                       'wgArticlePath' => '/mywiki',
+                                       'wgScriptPath' => '/mediawiki',
+                                       'wgDBname' => 'mw_mywiki',
+                               ),
+                               false, // $repoIsLocal
+                               array( // $expected
+                                       'repoUrl' => 'http://acme.com',
+                                       'repoArticlePath' => '/wiki',
+                                       'repoScriptPath' => '/w',
+                                       'siteGlobalID' => 'mywiki',
+                                       'repoDatabase' => 'foo',
+                                       'changesDatabase' => 'doo',
+                                       'sharedCacheKeyPrefix' => 'xoo:WBL/' . 
WBL_VERSION,
+                               )
+                       ),
+
+                       array( // #1: no local repo, no values set
+                               array( // $settings
+                               ),
+                               array( // $wg
+                                       'wgServer' => 'http://www.acme.com',
+                                       'wgArticlePath' => '/mywiki',
+                                       'wgScriptPath' => '/mediawiki',
+                                       'wgDBname' => 'mw_mywiki',
+                               ),
+                               false, // $repoIsLocal
+                               array( // $expected
+                                       'repoUrl' => '//www.wikidata.org',   // 
hardcoded default
+                                       'repoArticlePath' => '/wiki/$1', // 
hardcoded default
+                                       'repoScriptPath' => '/w', // hardcoded 
default
+                                       'siteGlobalID' => 'mw_mywiki',
+                                       'repoDatabase' => null,
+                                       'changesDatabase' => null,
+                                       'sharedCacheKeyPrefix' => 
'repoUrl://www.wikidata.org:WBL/' . WBL_VERSION,
+                               )
+                       ),
+
+                       array( // #2: local repo, all values set
+                               array( // $settings
+                                       'repoUrl' => 'http://acme.com',
+                                       'repoArticlePath' => '/wiki',
+                                       'repoScriptPath' => '/w',
+                                       'siteGlobalID' => 'mywiki',
+                                       'repoDatabase' => 'foo',
+                                       'changesDatabase' => 'doo',
+                                       'sharedCacheKeyPrefix' => 'foo:WBL/' . 
WBL_VERSION,
+                               ),
+                               array( // $wg
+                                       'wgServer' => 'http://www.acme.com',
+                                       'wgArticlePath' => '/mywiki',
+                                       'wgScriptPath' => '/mediawiki',
+                                       'wgDBname' => 'mw_mywiki',
+                               ),
+                               true, // $repoIsLocal
+                               array( // $expected
+                                       'repoUrl' => 'http://acme.com',
+                                       'repoArticlePath' => '/wiki',
+                                       'repoScriptPath' => '/w',
+                                       'siteGlobalID' => 'mywiki',
+                                       'repoDatabase' => 'foo',
+                                       'changesDatabase' => 'doo',
+                                       'sharedCacheKeyPrefix' => 'foo:WBL/' . 
WBL_VERSION,
+                               )
+                       ),
+
+                       array( // #3: local repo, no values set
+                               array( // $settings
+                               ),
+                               array( // $wg
+                                       'wgServer' => 'http://www.acme.com',
+                                       'wgArticlePath' => '/mywiki',
+                                       'wgScriptPath' => '/mediawiki',
+                                       'wgDBname' => 'mw_mywiki',
+                               ),
+                               true, // $repoIsLocal
+                               array( // $expected
+                                       'repoUrl' => 'http://www.acme.com',
+                                       'repoArticlePath' => '/mywiki',
+                                       'repoScriptPath' => '/mediawiki',
+                                       'siteGlobalID' => 'mw_mywiki',
+                                       'repoDatabase' => false,
+                                       'changesDatabase' => false,
+                                       'sharedCacheKeyPrefix' => 
'mw_mywiki:WBL/' . WBL_VERSION,
+                               )
+                       ),
+
+                       array( // #4: derive changesDatabase
+                               array( // $settings
+                                       'repoDatabase' => 'mw_foowiki',
+                               ),
+                               array( // $wg
+                               ),
+                               false, // $repoIsLocal
+                               array( // $expected
+                                       'repoDatabase' => 'mw_foowiki',
+                                       'changesDatabase' => 'mw_foowiki',
+                               )
+                       ),
+
+                       array( // #5: derive sharedCacheKeyPrefix from 
repoDatabase value
+                               array( // $settings
+                                       'repoDatabase' => 'mw_foowiki',
+                                       'repoUrl' => 'http://www.acme.com',
+                               ),
+                               array( // $wg
+                                       'wgDBname' => 'mw_mywiki',
+                               ),
+                               false, // $repoIsLocal
+                               array( // $expected
+                                       'sharedCacheKeyPrefix' => 
'mw_foowiki:WBL/' . WBL_VERSION,
+                               )
+                       ),
+
+                       array( // #6: derive sharedCacheKeyPrefix from repoUrl
+                               array( // $settings
+                                       'repoDatabase' => null,
+                                       'repoUrl' => 'http://www.acme.com',
+                               ),
+                               array( // $wg
+                                       'wgDBname' => 'mw_mywiki',
+                               ),
+                               false, // $repoIsLocal
+                               array( // $expected
+                                       'sharedCacheKeyPrefix' => 
'repoUrl:http://www.acme.com:WBL/' . WBL_VERSION,
+                               )
+                       ),
+
+                       array( // #7: derive sharedCacheKeyPrefix from wgDBname
+                               array( // $settings
+                                       'repoDatabase' => false,
+                                       'repoUrl' => 'http://www.acme.com',
+                               ),
+                               array( // $wg
+                                       'wgDBname' => 'mw_mywiki',
+                               ),
+                               false, // $repoIsLocal
+                               array( // $expected
+                                       'sharedCacheKeyPrefix' => 
'mw_mywiki:WBL/' . WBL_VERSION,
+                               )
+                       ),
+               );
+       }
+
+       /**
+        * @dataProvider settingsProvider
+        */
+       public function testDefaults( array $settings, array $wg, $repoIsLocal, 
$expected ) {
+               $this->setMwGlobals( $wg );
+
+               $defaults = include( WBC_DIR . 
'/config/WikibaseClient.default.php' );
+
+               $settings = array_merge( $defaults, $settings );
+               $settings = new SettingsArray( $settings );
+
+               //NOTE: thisWikiIsTheRepo us used by some "magic" (dynamic) 
defaults
+               //      to decide how to behave. Normally, this is true if and 
only if
+               //      WB_VERSION is defined.
+               $settings->setSetting( 'thisWikiIsTheRepo', $repoIsLocal );
+
+               foreach ( $expected as $key => $exp ) {
+                       $actual = $settings->getSetting( $key );
+                       $this->assertSame( $exp, $actual, "Setting $key" );
+               }
+       }
+
+}
\ No newline at end of file
diff --git a/client/tests/phpunit/ClientHooksTest.php 
b/client/tests/phpunit/ClientHooksTest.php
deleted file mode 100644
index a6c177b..0000000
--- a/client/tests/phpunit/ClientHooksTest.php
+++ /dev/null
@@ -1,150 +0,0 @@
-<?php
- /**
- *
- * Copyright © 02.07.13 by the authors listed below.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @license GPL 2+
- * @file
- *
- * @ingroup WikibaseClient
- * @ingroup Test
- *
- * @group WikibaseClient
- *
- * @author Daniel Kinzler
- */
-
-
-namespace Wikibase\Test;
-
-
-use Wikibase\ClientHooks;
-use Wikibase\SettingsArray;
-
-/**
- * Class ClientHooksTest
- * @covers Wikibase\ClientHooks
- * @package Wikibase\Test
- */
-class ClientHooksTest extends \PHPUnit_Framework_TestCase {
-
-       public static function provideOnSetupAfterCache() {
-               return array(
-                       array( // #0: no local repo, all values set
-                               array( // $settings
-                                       'repoUrl' => 'http://acme.com',
-                                       'repoArticlePath' => '/wiki',
-                                       'repoScriptPath' => '/w',
-                                       'siteGlobalID' => 'mywiki',
-                               ),
-                               array( // $wg
-                                       'wgServer' => 'http://www.acme.com',
-                                       'wgArticlePath' => '/mywiki',
-                                       'wgScriptPath' => '/mediawiki',
-                                       'wgDBName' => 'mw_mywiki',
-                               ),
-                               false, // $repoIsLocal
-                               array( // $expected
-                                       'repoUrl' => 'http://acme.com',
-                                       'repoArticlePath' => '/wiki',
-                                       'repoScriptPath' => '/w',
-                                       'siteGlobalID' => 'mywiki',
-                               )
-                       ),
-
-                       array( // #1: no local repo, no values set
-                               array( // $settings
-                                       'repoUrl' => null,
-                                       'repoArticlePath' => null,
-                                       'repoScriptPath' => null,
-                                       'siteGlobalID' => null,
-                               ),
-                               array( // $wg
-                                       'wgServer' => 'http://www.acme.com',
-                                       'wgArticlePath' => '/mywiki',
-                                       'wgScriptPath' => '/mediawiki',
-                                       'wgDBName' => 'mw_mywiki',
-                               ),
-                               false, // $repoIsLocal
-                               array( // $expected
-                                       'repoUrl' => null,
-                                       'repoArticlePath' => null,
-                                       'repoScriptPath' => null,
-                                       'siteGlobalID' => 'mw_mywiki',
-                               )
-                       ),
-
-                       array( // #2: local repo, all values set
-                               array( // $settings
-                                       'repoUrl' => 'http://acme.com',
-                                       'repoArticlePath' => '/wiki',
-                                       'repoScriptPath' => '/w',
-                                       'siteGlobalID' => 'mywiki',
-                               ),
-                               array( // $wg
-                                       'wgServer' => 'http://www.acme.com',
-                                       'wgArticlePath' => '/mywiki',
-                                       'wgScriptPath' => '/mediawiki',
-                                       'wgDBName' => 'mw_mywiki',
-                               ),
-                               true, // $repoIsLocal
-                               array( // $expected
-                                       'repoUrl' => 'http://acme.com',
-                                       'repoArticlePath' => '/wiki',
-                                       'repoScriptPath' => '/w',
-                                       'siteGlobalID' => 'mywiki',
-                               )
-                       ),
-
-                       array( // #3: local repo, no values set
-                               array( // $settings
-                                       'repoUrl' => null,
-                                       'repoArticlePath' => null,
-                                       'repoScriptPath' => null,
-                                       'siteGlobalID' => null,
-                               ),
-                               array( // $wg
-                                       'wgServer' => 'http://www.acme.com',
-                                       'wgArticlePath' => '/mywiki',
-                                       'wgScriptPath' => '/mediawiki',
-                                       'wgDBName' => 'mw_mywiki',
-                               ),
-                               true, // $repoIsLocal
-                               array( // $expected
-                                       'repoUrl' => 'http://www.acme.com',
-                                       'repoArticlePath' => '/mywiki',
-                                       'repoScriptPath' => '/mediawiki',
-                                       'siteGlobalID' => 'mw_mywiki',
-                               )
-                       ),
-               );
-       }
-
-       /**
-        * @dataProvider provideOnSetupAfterCache
-        */
-       public function testOnSetupAfterCache( array $settings, array $wg, 
$repoIsLocal, $expected ) {
-               $settings = new SettingsArray( $settings );
-               ClientHooks::applyMagicDefaultSettings( $settings, $wg, 
$repoIsLocal );
-
-               foreach ( $expected as $key => $value ) {
-                       $this->assertSame( $value, $settings->getSetting( $key 
), "Setting $key" );
-               }
-       }
-
-}
\ No newline at end of file
diff --git a/lib/config/WikibaseLib.default.php 
b/lib/config/WikibaseLib.default.php
index aea5e48..7a10fce 100644
--- a/lib/config/WikibaseLib.default.php
+++ b/lib/config/WikibaseLib.default.php
@@ -68,7 +68,8 @@
                // In order to share caches between clients (and the repo),
                // set a prefix based on the repo's name and WBL_VERSION
                // or a similar version ID.
-               // @todo: generate the default programmatically, so it can 
automatically use the right repo ID.
+               // NOTE: WikibaseClient.default.php overrides this to depend
+               // on repoDatabase dynamically.
                'sharedCacheKeyPrefix' => $GLOBALS['wgDBname'] . ':WBL/' . 
WBL_VERSION,
 
                // The duration of the object cache, in seconds.
diff --git a/lib/includes/Settings.php b/lib/includes/Settings.php
index c719dce..0c762b6 100644
--- a/lib/includes/Settings.php
+++ b/lib/includes/Settings.php
@@ -89,7 +89,7 @@
         * @return mixed
         */
        public static function get( $settingName ) {
-               return static::singleton()->offsetGet( $settingName );
+               return static::singleton()->getSetting( $settingName );
        }
 
 }
diff --git a/lib/includes/SettingsArray.php b/lib/includes/SettingsArray.php
index ab78d9b..ae992b7 100644
--- a/lib/includes/SettingsArray.php
+++ b/lib/includes/SettingsArray.php
@@ -2,10 +2,15 @@
 
 namespace Wikibase;
 
+use Closure;
 use OutOfBoundsException;
 
 /**
  * Class representing a collection of settings.
+ *
+ * @note: settings can be dynamic: if a setting is given as a closure, the 
closure will be
+ *        called to get the actual setting value the first time this setting 
is retrieved
+ *        using getSetting(). The closure is called with the SettingsArray as 
the only argument.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -28,6 +33,7 @@
  *
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
+ * @author Daniel Kinzler
  */
 class SettingsArray extends \ArrayObject {
 
@@ -46,7 +52,26 @@
                        throw new OutOfBoundsException( 'Attempt to get 
non-existing setting "' . $settingName . '"' );
                }
 
-               return $this[$settingName];
+               $value = $this[$settingName];
+
+               // Allow closures to be used for deferred evaluation
+               // of "magic" (dynamic) defaults.
+               if ( $value instanceof Closure ) {
+                       $value = $value( $this );
+
+                       if ( is_object( $value ) ) {
+                               $logValue = 'instance of ' . get_class( $value 
);
+                       } else {
+                               $logValue = var_export( $value, true );
+                       }
+
+                       wfDebugLog( __CLASS__, __FUNCTION__ . ': setting ' . 
$settingName . 'was given as a closure, resolve it to ' . $logValue );
+
+                       // only eval once, then remember the value
+                       $this->setSetting( $settingName, $value );
+               }
+
+               return $value;
        }
 
        /**
@@ -55,7 +80,9 @@
         * @since 0.1
         *
         * @param string $settingName
-        * @param mixed $settingValue
+        * @param mixed $settingValue The desired value. If this is a closure, 
the closure will be
+        *        called to get the actual setting value the first time this 
setting is retrieved
+        *        using getSetting(). The closure is called with this 
SettingsArray as the only argument.
         */
        public function setSetting( $settingName, $settingValue ) {
                $this[$settingName] = $settingValue;
diff --git a/repo/Wikibase.php b/repo/Wikibase.php
index 4f86b09..f6a1d00 100644
--- a/repo/Wikibase.php
+++ b/repo/Wikibase.php
@@ -46,15 +46,6 @@
        die( "<b>Error:</b> Wikibase requires MediaWiki 1.20 or above.\n" );
 }
 
-if ( defined( 'WBC_VERSION' ) ) {
-       // TODO: needed because WikibaseClient.defaults.php depends on 
WB_VERSION to know whether the
-       // repo is configured. Should be removed as soon as we change Settings 
to apply defaults
-       // "late", that is, when merging settings, after setup is complete.
-
-       throw new Exception( "Bad initialization order: When running the 
Wikibase repository extension and the "
-               . "WikibaseClient extension on the same wiki, WikibaseClient 
has to be included AFTER the repository." );
-}
-
 // Include the WikibaseLib extension if that hasn't been done yet, since it's 
required for Wikibase to work.
 if ( !defined( 'WBL_VERSION' ) ) {
        @include_once( __DIR__ . '/../lib/WikibaseLib.php' );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I9d22e46d9141252a9663e1bc8df6766f149291c2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Werner <[email protected]>
Gerrit-Reviewer: Denny Vrandecic <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to