Fix Bug 33384 - database drivers cannot be provided by extension
[lhc/web/wiklou.git] / includes / conf / Conf.php
1 <?php
2 /**
3 * Base configuration class.
4 *
5 * Get some configuration variable:
6 * $mySetting = Conf::get( 'mySetting' );
7 *
8 * Copyright © 2011 Chad Horohoe <chadh@wikimedia.org>
9 * http://www.mediawiki.org/
10 *
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.
15 *
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.
20 *
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
25 *
26 * @file
27 * @defgroup Config Config
28 * @ingroup Config
29 */
30 abstract class Conf {
31 /**
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.
34 *
35 * Please don't ever make this a default (or accepted) value for your
36 * configuration. It's liable to Break Something.
37 */
38 const NO_SUCH_DEFAULT_CONFIG = 'mw-no-such-default-config';
39
40 /**
41 * The Wiki ID (usually $wgDBname)
42 * @var String
43 */
44 private $wikiId;
45
46 /**
47 * Singleton
48 * @var Conf
49 */
50 private static $__instance;
51
52 /**
53 * Stores of the core defaults, extension defaults and wiki overrides
54 *
55 * @var array
56 */
57 protected $defaults, $extensionDefaults, $values = array();
58
59 /**
60 * Constructor. Children should call this if implementing.
61 * @param $confConfig Array of config vars
62 */
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();
71 }
72 }
73
74 /**
75 * Expose all config variables as globals for back-compat. Ewwww.
76 */
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;
82 }
83 }
84
85 /**
86 * Load customized settings from whatever the data store is
87 */
88 abstract protected function initChangedSettings();
89
90 /**
91 * Apply a setting to the backend store
92 * @param $name String Name of the setting
93 * @param $value mixed Value to store
94 */
95 abstract protected function writeSetting( $name, $value );
96
97 /**
98 * Initialize a new child class based on a configuration array
99 * @param $conf Array of configuration settings, see $wgConfiguration
100 * for details
101 * @return Conf
102 */
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"' );
107 }
108 return new $class( $conf );
109 }
110
111 /**
112 * Get the singleton if we don't want a specific wiki
113 * @param $wiki String An id for a remote wiki
114 * @return Conf child
115 */
116 public static function load( $wiki = false ) {
117 throw new MWException( "Not working yet, don't attempt to use this" );
118 if( !self::$__instance ) {
119 /**global $wgConfiguration;
120 self::$__instance = self::newFromSettings( $wgConfiguration );*/
121 }
122 if( $wiki && $wiki != self::$__instance->getWikiId() ) {
123 // Load configuration for a different wiki, not sure how
124 // we're gonna do this yet
125 return null;
126 }
127 return self::$__instance;
128 }
129
130 /**
131 * Get a property from the configuration database, falling back
132 * to DefaultSettings if undefined
133 * @param $name String Name of setting to retrieve.
134 * @param $wiki String An id for a remote wiki
135 * @return mixed
136 */
137 public static function get( $name, $wiki = false ) {
138 return self::load( $wiki )->retrieveSetting( $name );
139 }
140
141 /**
142 * Actually get the setting, checking overrides, extensions, then core.
143 *
144 * @param $name String Name of setting to get
145 * @return mixed
146 */
147 public function retrieveSetting( $name ) {
148 // isset() is ok here, because the default is to return null anyway.
149 if( isset( $this->values[$name] ) ) {
150 return $this->values[$name];
151 } elseif( isset( $this->extensionDefaults[$name] ) ) {
152 return $this->extensionDefaults[$name];
153 } elseif( isset( $this->defaults[$name] ) ) {
154 return $this->defaults[$name];
155 } else {
156 wfDebug( __METHOD__ . " called for unknown configuration item '$name'\n" );
157 return null;
158 }
159 }
160
161 /**
162 * Apply a setting to the configuration object.
163 * @param $name String Name of the config item
164 * @param $value mixed Any value to use for the key
165 * @param $write bool Whether to write to the static copy (db, file, etc)
166 */
167 public function applySetting( $name, $value, $write = false ) {
168 $this->values[$name] = $value;
169 if( $write && ( $value !== $this->getDefaultSetting( $name ) ) ) {
170 $this->writeSetting( $name, $value );
171 }
172 }
173
174 /**
175 * Get the default for a given setting name. Check core and then extensions.
176 * Will return NO_SUCH_DEFAULT_CONFIG if the config item does not exist.
177 *
178 * @param $name String Name of setting
179 * @return mixed
180 */
181 public function getDefaultSetting( $name ) {
182 // Use array_key_exists() here, to make sure we return a default
183 // that's really set to null.
184 if( array_key_exists( $name, $this->defaults ) ) {
185 return $this->defaults[$name];
186 } elseif( array_key_exists( $name, $this->extensionDefaults ) ) {
187 return $this->extensionDefaults[$name];
188 } else {
189 wfDebug( __METHOD__ . " called for unknown configuration item '$name'\n" );
190 return self::NO_SUCH_DEFAULT_CONFIG;
191 }
192 }
193
194 /**
195 * What is the wiki ID for this site?
196 * @return String
197 */
198 public function getWikiId() {
199 return $this->wikiId;
200 }
201 }