SI standards for time units
[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 */
29 abstract class Conf {
30 /**
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.
33 *
34 * Please don't ever make this a default (or accepted) value for your
35 * configuration. It's liable to Break Something.
36 */
37 const NO_SUCH_DEFAULT_CONFIG = 'mw-no-such-default-config';
38
39 /**
40 * The Wiki ID (usually $wgDBname)
41 * @var String
42 */
43 private $wikiId;
44
45 /**
46 * Singleton
47 * @var Conf
48 */
49 private static $__instance;
50
51 /**
52 * Stores of the core defaults, extension defaults and wiki overrides
53 *
54 * @var array
55 */
56 protected $defaults, $extensionDefaults, $values = array();
57
58 /**
59 * Constructor. Children should call this if implementing.
60 * @param $confConfig Array of config vars
61 */
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();
70 }
71 }
72
73 /**
74 * Expose all config variables as globals for back-compat. Ewwww.
75 */
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;
81 }
82 }
83
84 /**
85 * Load customized settings from whatever the data store is
86 */
87 abstract protected function initChangedSettings();
88
89 /**
90 * Apply a setting to the backend store
91 * @param $name String Name of the setting
92 * @param $value mixed Value to store
93 */
94 abstract protected function writeSetting( $name, $value );
95
96 /**
97 * Initialize a new child class based on a configuration array
98 * @param $conf Array of configuration settings, see $wgConfiguration
99 * for details
100 * @return Conf
101 */
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"' );
106 }
107 return new $class( $conf );
108 }
109
110 /**
111 * Get the singleton if we don't want a specific wiki
112 * @param $wiki String An id for a remote wiki
113 * @return Conf child
114 */
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 );*/
120 }
121 if( $wiki && $wiki != self::$__instance->getWikiId() ) {
122 // Load configuration for a different wiki, not sure how
123 // we're gonna do this yet
124 return null;
125 }
126 return self::$__instance;
127 }
128
129 /**
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
134 * @return mixed
135 */
136 public static function get( $name, $wiki = false ) {
137 return self::load( $wiki )->retrieveSetting( $name );
138 }
139
140 /**
141 * Actually get the setting, checking overrides, extensions, then core.
142 *
143 * @param $name String Name of setting to get
144 * @return mixed
145 */
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];
154 } else {
155 wfDebug( __METHOD__ . " called for unknown configuration item '$name'\n" );
156 return null;
157 }
158 }
159
160 /**
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)
165 */
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 );
170 }
171 }
172
173 /**
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.
176 *
177 * @param $name String Name of setting
178 * @return mixed
179 */
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];
187 } else {
188 wfDebug( __METHOD__ . " called for unknown configuration item '$name'\n" );
189 return self::NO_SUCH_DEFAULT_CONFIG;
190 }
191 }
192
193 /**
194 * What is the wiki ID for this site?
195 * @return String
196 */
197 public function getWikiId() {
198 return $this->wikiId;
199 }
200 }