From fbfe789b987b4cfd060c429d474a7529739fd0d3 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Thu, 23 Jan 2014 17:45:11 -0800 Subject: [PATCH] Config: Add Config and GlobalConfig classes Allows configuration options to be fetched from context. Only one implementation, GlobalConfig, is provided, which simply returns $GLOBALS[$name]. There can be more classes in the future, possibly a database-based one. For convinience the "wg" prefix is automatically added. Ironically, this adds the $wgConfigClass global variable which is used to determine which implementation of Config to use by default. The ContextSource getConfig and setConfig methods were introduced in I23194d1ba (1.23), but have no uses in Gerrit, so they can safely be re-purposed. Change-Id: I13baec0b6d4ea7badf20b9c5f9b40846348838e4 --- includes/AutoLoader.php | 4 ++ includes/DefaultSettings.php | 7 +++ includes/config/Config.php | 59 +++++++++++++++++++ includes/config/GlobalConfig.php | 44 ++++++++++++++ includes/context/ContextSource.php | 4 +- includes/context/DerivativeContext.php | 4 +- includes/context/IContextSource.php | 2 +- includes/context/RequestContext.php | 15 +++-- .../includes/config/GlobalConfigTest.php | 38 ++++++++++++ 9 files changed, 164 insertions(+), 13 deletions(-) create mode 100644 includes/config/Config.php create mode 100644 includes/config/GlobalConfig.php create mode 100644 tests/phpunit/includes/config/GlobalConfigTest.php diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 91fa55fb44..d4b2aa5ae2 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -405,6 +405,10 @@ $wgAutoloadLocalClasses = array( 'RedisConnectionPool' => 'includes/clientpool/RedisConnectionPool.php', 'RedisConnRef' => 'includes/clientpool/RedisConnectionPool.php', + # includes/config + 'Config' => 'includes/config/Config.php', + 'GlobalConfig' => 'includes/config/GlobalConfig.php', + # includes/content 'AbstractContent' => 'includes/content/AbstractContent.php', 'ContentHandler' => 'includes/content/ContentHandler.php', diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 11b4ef68b7..96cfdfeac4 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -59,6 +59,13 @@ if ( !defined( 'MEDIAWIKI' ) ) { */ $wgConf = new SiteConfiguration; +/** + * Class name to use for accessing Config. + * Currently only 'GlobalConfig' is available + * @since 1.23 + */ +$wgConfigClass = 'GlobalConfig'; + /** * MediaWiki version number * @since 1.2 diff --git a/includes/config/Config.php b/includes/config/Config.php new file mode 100644 index 0000000000..067b1e4527 --- /dev/null +++ b/includes/config/Config.php @@ -0,0 +1,59 @@ +getContext()->getConfig(); diff --git a/includes/context/DerivativeContext.php b/includes/context/DerivativeContext.php index 2137757ec2..f5616e04d3 100644 --- a/includes/context/DerivativeContext.php +++ b/includes/context/DerivativeContext.php @@ -88,9 +88,9 @@ class DerivativeContext extends ContextSource { } /** - * Get the SiteConfiguration object + * Get the Config object * - * @return SiteConfiguration + * @return Config */ public function getConfig() { if ( !is_null( $this->config ) ) { diff --git a/includes/context/IContextSource.php b/includes/context/IContextSource.php index 6c5c0a23af..eb513723a6 100644 --- a/includes/context/IContextSource.php +++ b/includes/context/IContextSource.php @@ -103,7 +103,7 @@ interface IContextSource { * Get the site configuration * * @since 1.23 - * @return SiteConfiguration + * @return Config */ public function getConfig(); diff --git a/includes/context/RequestContext.php b/includes/context/RequestContext.php index 5f917316a0..978ef03c3f 100644 --- a/includes/context/RequestContext.php +++ b/includes/context/RequestContext.php @@ -64,28 +64,27 @@ class RequestContext implements IContextSource { private $skin; /** - * @var SiteConfiguration + * @var Config */ private $config; /** - * Set the SiteConfiguration object + * Set the Config object * - * @param SiteConfiguration $c + * @param Config $c */ - public function setConfig( SiteConfiguration $c ) { + public function setConfig( Config $c ) { $this->config = $c; } /** - * Get the SiteConfiguration object + * Get the Config object * - * @return SiteConfiguration + * @return Config */ public function getConfig() { if ( $this->config === null ) { - global $wgConf; - $this->config = $wgConf; + $this->config = Config::factory(); } return $this->config; } diff --git a/tests/phpunit/includes/config/GlobalConfigTest.php b/tests/phpunit/includes/config/GlobalConfigTest.php new file mode 100644 index 0000000000..b605a46eb2 --- /dev/null +++ b/tests/phpunit/includes/config/GlobalConfigTest.php @@ -0,0 +1,38 @@ +config = new GlobalConfig; + } + + public static function provideGet() { + return array( + array( 'wgSitename', array( 'Sitename' ) ), + array( 'wgFoo', array( 'Foo' ) ), + array( 'efVariable', array( 'Variable', 'ef' ) ), + array( 'Foo', array( 'Foo', '' ) ), + ); + } + + /** + * @param string $name + * @param array $params + * @dataProvider provideGet + * @covers GlobalConfig::get + */ + public function testGet( $name, $params ) { + $rand = wfRandom(); + $old = isset( $GLOBALS[$name] ) ? $GLOBALS[$name] : null; + $GLOBALS[$name] = $rand; + $out = call_user_func_array( array( $this->config, 'get' ), $params ); + $this->assertEquals( $rand, $out ); + if ( $old ) { + $GLOBALS[$name] = $old; + } + } +} -- 2.20.1