Config: Add Config and GlobalConfig classes
authorKunal Mehta <legoktm@gmail.com>
Fri, 24 Jan 2014 01:45:11 +0000 (17:45 -0800)
committerKunal Mehta <legoktm@gmail.com>
Tue, 28 Jan 2014 05:10:30 +0000 (21:10 -0800)
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
includes/DefaultSettings.php
includes/config/Config.php [new file with mode: 0644]
includes/config/GlobalConfig.php [new file with mode: 0644]
includes/context/ContextSource.php
includes/context/DerivativeContext.php
includes/context/IContextSource.php
includes/context/RequestContext.php
tests/phpunit/includes/config/GlobalConfigTest.php [new file with mode: 0644]

index 91fa55f..d4b2aa5 100644 (file)
@@ -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',
index 11b4ef6..96cfdfe 100644 (file)
@@ -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 (file)
index 0000000..067b1e4
--- /dev/null
@@ -0,0 +1,59 @@
+<?php
+/**
+ * Copyright 2014
+ *
+ * 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
+ *
+ * @file
+ */
+
+/**
+ * Abstract class for get settings for
+ *
+ * @since 1.23
+ */
+
+abstract class Config {
+
+       /**
+        * @param string $name configuration variable name without prefix
+        * @param string $prefix of the variable name
+        * @return mixed
+        */
+       abstract public function get( $name, $prefix = 'wg' );
+
+       /**
+        * @param string $name configuration variable name without prefix
+        * @param mixed $value to set
+        * @param string $prefix of the variable name
+        * @return Status object indicating success or failure
+        */
+       abstract public function set( $name, $value, $prefix = 'wg' );
+
+       /**
+        * @param string|null $type class name for Config object,
+        *        uses $wgConfigClass if not provided
+        * @return Config
+        */
+       public static function factory( $type = null ) {
+               if ( !$type ) {
+                       global $wgConfigClass;
+                       $type = $wgConfigClass;
+               }
+
+               return new $type;
+       }
+}
diff --git a/includes/config/GlobalConfig.php b/includes/config/GlobalConfig.php
new file mode 100644 (file)
index 0000000..1b1cd89
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+/**
+ * Copyright 2014
+ *
+ * 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
+ *
+ * @file
+ */
+
+/**
+ * Accesses configuration settings from $GLOBALS
+ *
+ * @since 1.23
+ */
+class GlobalConfig extends Config {
+
+       /**
+        * @see Config::get
+        */
+       public function get( $name, $prefix = 'wg' ) {
+               return $GLOBALS[$prefix . $name];
+       }
+
+       /**
+        * @see Config::set
+        */
+       public function set( $name, $value, $prefix = 'wg' ) {
+               $GLOBALS[$prefix . $name] = $value;
+               return Status::newGood();
+       }
+}
index 186b8e6..9bc416d 100644 (file)
@@ -60,10 +60,10 @@ abstract class ContextSource implements IContextSource {
        }
 
        /**
-        * Get the SiteConfiguration object
+        * Get the Config object
         *
         * @since 1.23
-        * @return SiteConfiguration
+        * @return Config
         */
        public function getConfig() {
                return $this->getContext()->getConfig();
index 2137757..f5616e0 100644 (file)
@@ -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 ) ) {
index 6c5c0a2..eb51372 100644 (file)
@@ -103,7 +103,7 @@ interface IContextSource {
         * Get the site configuration
         *
         * @since 1.23
-        * @return SiteConfiguration
+        * @return Config
         */
        public function getConfig();
 
index 5f91731..978ef03 100644 (file)
@@ -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 (file)
index 0000000..b605a46
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+class GlobalConfigTest extends MediaWikiTestCase {
+
+       /** @var GlobalConfig $config */
+       protected $config;
+
+       protected function setUp() {
+               parent::setUp();
+               $this->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;
+               }
+       }
+}