3 * Base configuration class.
5 * Get some configuration variable:
6 * $mySetting = Conf::get( 'mySetting' );
8 * Copyright © 2011 Chad Horohoe <chadh@wikimedia.org>
9 * http://www.mediawiki.org/
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
27 * @defgroup Config Config
31 * A special value to return when default config items do not exist. Use
32 * this to differentiate from 'null' which may be a valid config value.
34 * Please don't ever make this a default (or accepted) value for your
35 * configuration. It's liable to Break Something.
37 const NO_SUCH_DEFAULT_CONFIG
= 'mw-no-such-default-config';
40 * The Wiki ID (usually $wgDBname)
49 private static $__instance;
52 * Stores of the core defaults, extension defaults and wiki overrides
56 protected $defaults, $extensionDefaults, $values = array();
59 * Constructor. Children should call this if implementing.
60 * @param $confConfig Array of config vars
62 protected function __construct( $confConfig ) {
63 $this->wikiId
= $confConfig['wikiId'];
64 $this->defaults
= (array)(new DefaultSettings
);
65 // @todo implement this:
66 // $this->initExtensionDefaults();
67 $this->initChangedSettings();
68 if( isset( $confConfig['exposeGlobals'] ) ) {
69 $this->exposeGlobals();
74 * Expose all config variables as globals for back-compat. Ewwww.
76 private function exposeGlobals() {
77 $allVars = $this->defaults +
$this->extensionDefaults +
$this->values
;
78 foreach( $allVars as $name => $value ) {
79 $var = 'wg' . ucfirst( $name );
80 $GLOBALS[$var] = $value;
85 * Load customized settings from whatever the data store is
87 abstract protected function initChangedSettings();
90 * Apply a setting to the backend store
91 * @param $name String Name of the setting
92 * @param $value mixed Value to store
94 abstract protected function writeSetting( $name, $value );
97 * Initialize a new child class based on a configuration array
98 * @param $conf Array of configuration settings, see $wgConfiguration
102 private static function newFromSettings( $conf ) {
103 $class = ucfirst( $conf['type'] ) . 'Conf';
104 if( !class_exists( $class ) ) {
105 throw new MWException( '$wgConfiguration misconfigured with invalid "type"' );
107 return new $class( $conf );
111 * Get the singleton if we don't want a specific wiki
112 * @param $wiki String An id for a remote wiki
115 public static function load( $wiki = false ) {
116 throw new MWException( "Not working yet, don't attempt to use this" );
117 if( !self
::$__instance ) {
118 /**global $wgConfiguration;
119 self::$__instance = self::newFromSettings( $wgConfiguration );*/
121 if( $wiki && $wiki != self
::$__instance->getWikiId() ) {
122 // Load configuration for a different wiki, not sure how
123 // we're gonna do this yet
126 return self
::$__instance;
130 * Get a property from the configuration database, falling back
131 * to DefaultSettings if undefined
132 * @param $name String Name of setting to retrieve.
133 * @param $wiki String An id for a remote wiki
136 public static function get( $name, $wiki = false ) {
137 return self
::load( $wiki )->retrieveSetting( $name );
141 * Actually get the setting, checking overrides, extensions, then core.
143 * @param $name String Name of setting to get
146 public function retrieveSetting( $name ) {
147 // isset() is ok here, because the default is to return null anyway.
148 if( isset( $this->values
[$name] ) ) {
149 return $this->values
[$name];
150 } elseif( isset( $this->extensionDefaults
[$name] ) ) {
151 return $this->extensionDefaults
[$name];
152 } elseif( isset( $this->defaults
[$name] ) ) {
153 return $this->defaults
[$name];
155 wfDebug( __METHOD__
. " called for unknown configuration item '$name'\n" );
161 * Apply a setting to the configuration object.
162 * @param $name String Name of the config item
163 * @param $value mixed Any value to use for the key
164 * @param $write bool Whether to write to the static copy (db, file, etc)
166 public function applySetting( $name, $value, $write = false ) {
167 $this->values
[$name] = $value;
168 if( $write && ( $value !== $this->getDefaultSetting( $name ) ) ) {
169 $this->writeSetting( $name, $value );
174 * Get the default for a given setting name. Check core and then extensions.
175 * Will return NO_SUCH_DEFAULT_CONFIG if the config item does not exist.
177 * @param $name String Name of setting
180 public function getDefaultSetting( $name ) {
181 // Use array_key_exists() here, to make sure we return a default
182 // that's really set to null.
183 if( array_key_exists( $name, $this->defaults
) ) {
184 return $this->defaults
[$name];
185 } elseif( array_key_exists( $name, $this->extensionDefaults
) ) {
186 return $this->extensionDefaults
[$name];
188 wfDebug( __METHOD__
. " called for unknown configuration item '$name'\n" );
189 return self
::NO_SUCH_DEFAULT_CONFIG
;
194 * What is the wiki ID for this site?
197 public function getWikiId() {
198 return $this->wikiId
;