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
32 * A special value to return when default config items do not exist. Use
33 * this to differentiate from 'null' which may be a valid config value.
35 * Please don't ever make this a default (or accepted) value for your
36 * configuration. It's liable to Break Something.
38 const NO_SUCH_DEFAULT_CONFIG
= 'mw-no-such-default-config';
41 * The Wiki ID (usually $wgDBname)
50 private static $__instance;
53 * Stores of the core defaults, extension defaults and wiki overrides
57 protected $defaults, $extensionDefaults, $values = array();
60 * Constructor. Children should call this if implementing.
61 * @param $confConfig Array of config vars
63 protected function __construct( $confConfig ) {
64 $this->wikiId
= $confConfig['wikiId'];
65 $this->defaults
= (array)(new DefaultSettings
);
66 // @todo implement this:
67 // $this->initExtensionDefaults();
68 $this->initChangedSettings();
69 if( isset( $confConfig['exposeGlobals'] ) ) {
70 $this->exposeGlobals();
75 * Expose all config variables as globals for back-compat. Ewwww.
77 private function exposeGlobals() {
78 $allVars = $this->defaults +
$this->extensionDefaults +
$this->values
;
79 foreach( $allVars as $name => $value ) {
80 $var = 'wg' . ucfirst( $name );
81 $GLOBALS[$var] = $value;
86 * Load customized settings from whatever the data store is
88 abstract protected function initChangedSettings();
91 * Apply a setting to the backend store
92 * @param $name String Name of the setting
93 * @param $value mixed Value to store
95 abstract protected function writeSetting( $name, $value );
98 * Initialize a new child class based on a configuration array
99 * @param $conf Array of configuration settings, see $wgConfiguration
103 private static function newFromSettings( $conf ) {
104 $class = ucfirst( $conf['type'] ) . 'Conf';
105 if( !class_exists( $class ) ) {
106 throw new MWException( '$wgConfiguration misconfigured with invalid "type"' );
108 return new $class( $conf );
112 * Get the singleton if we don't want a specific wiki
113 * @param $wiki String An id for a remote wiki
116 public static function load( $wiki = false ) {
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
;