(Bug 41352) Provide tests for edit conflicts.
[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 * @throws MWException
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 bool|string $wiki An id for a remote wiki
114 * @throws MWException
115 * @return Conf child
116 */
117 public static function load( $wiki = false ) {
118 throw new MWException( "Not working yet, don't attempt to use this" );
119 if( !self::$__instance ) {
120 /**global $wgConfiguration;
121 self::$__instance = self::newFromSettings( $wgConfiguration );*/
122 }
123 if( $wiki && $wiki != self::$__instance->getWikiId() ) {
124 // Load configuration for a different wiki, not sure how
125 // we're gonna do this yet
126 return null;
127 }
128 return self::$__instance;
129 }
130
131 /**
132 * Get a property from the configuration database, falling back
133 * to DefaultSettings if undefined
134 * @param $name String Name of setting to retrieve.
135 * @param $wiki String An id for a remote wiki
136 * @return mixed
137 */
138 public static function get( $name, $wiki = false ) {
139 return self::load( $wiki )->retrieveSetting( $name );
140 }
141
142 /**
143 * Actually get the setting, checking overrides, extensions, then core.
144 *
145 * @param $name String Name of setting to get
146 * @return mixed
147 */
148 public function retrieveSetting( $name ) {
149 // isset() is ok here, because the default is to return null anyway.
150 if( isset( $this->values[$name] ) ) {
151 return $this->values[$name];
152 } elseif( isset( $this->extensionDefaults[$name] ) ) {
153 return $this->extensionDefaults[$name];
154 } elseif( isset( $this->defaults[$name] ) ) {
155 return $this->defaults[$name];
156 } else {
157 wfDebug( __METHOD__ . " called for unknown configuration item '$name'\n" );
158 return null;
159 }
160 }
161
162 /**
163 * Apply a setting to the configuration object.
164 * @param $name String Name of the config item
165 * @param $value mixed Any value to use for the key
166 * @param $write bool Whether to write to the static copy (db, file, etc)
167 */
168 public function applySetting( $name, $value, $write = false ) {
169 $this->values[$name] = $value;
170 if( $write && ( $value !== $this->getDefaultSetting( $name ) ) ) {
171 $this->writeSetting( $name, $value );
172 }
173 }
174
175 /**
176 * Get the default for a given setting name. Check core and then extensions.
177 * Will return NO_SUCH_DEFAULT_CONFIG if the config item does not exist.
178 *
179 * @param $name String Name of setting
180 * @return mixed
181 */
182 public function getDefaultSetting( $name ) {
183 // Use array_key_exists() here, to make sure we return a default
184 // that's really set to null.
185 if( array_key_exists( $name, $this->defaults ) ) {
186 return $this->defaults[$name];
187 } elseif( array_key_exists( $name, $this->extensionDefaults ) ) {
188 return $this->extensionDefaults[$name];
189 } else {
190 wfDebug( __METHOD__ . " called for unknown configuration item '$name'\n" );
191 return self::NO_SUCH_DEFAULT_CONFIG;
192 }
193 }
194
195 /**
196 * What is the wiki ID for this site?
197 * @return String
198 */
199 public function getWikiId() {
200 return $this->wikiId;
201 }
202 }