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
'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',
*/
$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
--- /dev/null
+<?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;
+ }
+}
--- /dev/null
+<?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();
+ }
+}
}
/**
- * Get the SiteConfiguration object
+ * Get the Config object
*
* @since 1.23
- * @return SiteConfiguration
+ * @return Config
*/
public function getConfig() {
return $this->getContext()->getConfig();
}
/**
- * Get the SiteConfiguration object
+ * Get the Config object
*
- * @return SiteConfiguration
+ * @return Config
*/
public function getConfig() {
if ( !is_null( $this->config ) ) {
* Get the site configuration
*
* @since 1.23
- * @return SiteConfiguration
+ * @return Config
*/
public function getConfig();
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;
}
--- /dev/null
+<?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;
+ }
+ }
+}