Fixed typo. w/s cleanup.
[lhc/web/wiklou.git] / includes / SiteConfiguration.php
1 <?php
2 /**
3 * This is a class used to hold configuration settings, particularly for multi-wiki sites.
4 */
5 class SiteConfiguration {
6
7 /**
8 * Array of suffixes, for self::siteFromDB()
9 */
10 public $suffixes = array();
11
12 /**
13 * Array of wikis, should be the same as $wgLocalDatabases
14 */
15 public $wikis = array();
16
17 /**
18 * The whole array of settings
19 */
20 public $settings = array();
21
22 /**
23 * Array of domains that are local and can be handled by the same server
24 */
25 public $localVHosts = array();
26
27 /**
28 * Optional callback to load full configuration data.
29 */
30 public $fullLoadCallback = null;
31
32 /** Whether or not all data has been loaded */
33 public $fullLoadDone = false;
34
35 /**
36 * A callback function that returns an array with the following keys (all
37 * optional):
38 * - suffix: site's suffix
39 * - lang: site's lang
40 * - tags: array of wiki tags
41 * - params: array of parameters to be replaced
42 * The function will receive the SiteConfiguration instance in the first
43 * argument and the wiki in the second one.
44 * if suffix and lang are passed they will be used for the return value of
45 * self::siteFromDB() and self::$suffixes will be ignored
46 */
47 public $siteParamsCallback = null;
48
49 /**
50 * Retrieves a configuration setting for a given wiki.
51 * @param $settingName String ID of the setting name to retrieve
52 * @param $wiki String Wiki ID of the wiki in question.
53 * @param $suffix String The suffix of the wiki in question.
54 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
55 * @param $wikiTags Array The tags assigned to the wiki.
56 * @return Mixed the value of the setting requested.
57 */
58 public function get( $settingName, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
59 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
60 return $this->getSetting( $settingName, $wiki, $params );
61 }
62
63 /**
64 * Really retrieves a configuration setting for a given wiki.
65 *
66 * @param $settingName String ID of the setting name to retrieve.
67 * @param $wiki String Wiki ID of the wiki in question.
68 * @param $params Array: array of parameters.
69 * @return Mixed the value of the setting requested.
70 */
71 protected function getSetting( $settingName, $wiki, /*array*/ $params ){
72 $retval = null;
73 if( array_key_exists( $settingName, $this->settings ) ) {
74 $thisSetting =& $this->settings[$settingName];
75 do {
76 // Do individual wiki settings
77 if( array_key_exists( $wiki, $thisSetting ) ) {
78 $retval = $thisSetting[$wiki];
79 break;
80 } elseif( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
81 $retval = $thisSetting["+$wiki"];
82 }
83
84 // Do tag settings
85 foreach( $params['tags'] as $tag ) {
86 if( array_key_exists( $tag, $thisSetting ) ) {
87 if ( isset( $retval ) && is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
88 $retval = self::arrayMerge( $retval, $thisSetting[$tag] );
89 } else {
90 $retval = $thisSetting[$tag];
91 }
92 break 2;
93 } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) {
94 if( !isset( $retval ) )
95 $retval = array();
96 $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] );
97 }
98 }
99 // Do suffix settings
100 $suffix = $params['suffix'];
101 if( !is_null( $suffix ) ) {
102 if( array_key_exists( $suffix, $thisSetting ) ) {
103 if ( isset($retval) && is_array($retval) && is_array($thisSetting[$suffix]) ) {
104 $retval = self::arrayMerge( $retval, $thisSetting[$suffix] );
105 } else {
106 $retval = $thisSetting[$suffix];
107 }
108 break;
109 } elseif( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) {
110 if (!isset($retval))
111 $retval = array();
112 $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] );
113 }
114 }
115
116 // Fall back to default.
117 if( array_key_exists( 'default', $thisSetting ) ) {
118 if( is_array( $retval ) && is_array( $thisSetting['default'] ) ) {
119 $retval = self::arrayMerge( $retval, $thisSetting['default'] );
120 } else {
121 $retval = $thisSetting['default'];
122 }
123 break;
124 }
125 } while ( false );
126 }
127
128 if( !is_null( $retval ) && count( $params['params'] ) ) {
129 foreach ( $params['params'] as $key => $value ) {
130 $retval = $this->doReplace( '$' . $key, $value, $retval );
131 }
132 }
133 return $retval;
134 }
135
136 /**
137 * Type-safe string replace; won't do replacements on non-strings
138 * private?
139 *
140 * @param $from
141 * @param $to
142 * @param $in
143 * @return string
144 */
145 function doReplace( $from, $to, $in ) {
146 if( is_string( $in ) ) {
147 return str_replace( $from, $to, $in );
148 } elseif( is_array( $in ) ) {
149 foreach( $in as $key => $val ) {
150 $in[$key] = $this->doReplace( $from, $to, $val );
151 }
152 return $in;
153 } else {
154 return $in;
155 }
156 }
157
158 /**
159 * Gets all settings for a wiki
160 * @param $wiki String Wiki ID of the wiki in question.
161 * @param $suffix String The suffix of the wiki in question.
162 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
163 * @param $wikiTags Array The tags assigned to the wiki.
164 * @return Array Array of settings requested.
165 */
166 public function getAll( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
167 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
168 $localSettings = array();
169 foreach( $this->settings as $varname => $stuff ) {
170 $append = false;
171 $var = $varname;
172 if ( substr( $varname, 0, 1 ) == '+' ) {
173 $append = true;
174 $var = substr( $varname, 1 );
175 }
176
177 $value = $this->getSetting( $varname, $wiki, $params );
178 if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) )
179 $value = self::arrayMerge( $value, $GLOBALS[$var] );
180 if ( !is_null( $value ) ) {
181 $localSettings[$var] = $value;
182 }
183 }
184 return $localSettings;
185 }
186
187 /**
188 * Retrieves a configuration setting for a given wiki, forced to a boolean.
189 * @param $setting String ID of the setting name to retrieve
190 * @param $wiki String Wiki ID of the wiki in question.
191 * @param $suffix String The suffix of the wiki in question.
192 * @param $wikiTags Array The tags assigned to the wiki.
193 * @return bool The value of the setting requested.
194 */
195 public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) {
196 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
197 }
198
199 /**
200 * Retrieves an array of local databases
201 *
202 * @return array
203 */
204 function &getLocalDatabases() {
205 return $this->wikis;
206 }
207
208 /** A no-op */
209 function initialise() {
210 }
211
212 /**
213 * Retrieves the value of a given setting, and places it in a variable passed by reference.
214 * @param $setting String ID of the setting name to retrieve
215 * @param $wiki String Wiki ID of the wiki in question.
216 * @param $suffix String The suffix of the wiki in question.
217 * @param $var Reference The variable to insert the value into.
218 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
219 * @param $wikiTags Array The tags assigned to the wiki.
220 */
221 public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) {
222 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
223 if ( !is_null( $value ) ) {
224 $var = $value;
225 }
226 }
227
228 /**
229 * Retrieves the value of a given setting, and places it in its corresponding global variable.
230 * @param $setting String ID of the setting name to retrieve
231 * @param $wiki String Wiki ID of the wiki in question.
232 * @param $suffix String The suffix of the wiki in question.
233 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
234 * @param $wikiTags Array The tags assigned to the wiki.
235 */
236 public function extractGlobal( $setting, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
237 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
238 $this->extractGlobalSetting( $setting, $wiki, $params );
239 }
240
241 /**
242 * @param $setting string
243 * @param $wiki string
244 * @param $params array
245 */
246 public function extractGlobalSetting( $setting, $wiki, $params ) {
247 $value = $this->getSetting( $setting, $wiki, $params );
248 if ( !is_null( $value ) ) {
249 if (substr($setting,0,1) == '+' && is_array($value)) {
250 $setting = substr($setting,1);
251 if ( is_array($GLOBALS[$setting]) ) {
252 $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value );
253 } else {
254 $GLOBALS[$setting] = $value;
255 }
256 } else {
257 $GLOBALS[$setting] = $value;
258 }
259 }
260 }
261
262 /**
263 * Retrieves the values of all settings, and places them in their corresponding global variables.
264 * @param $wiki String Wiki ID of the wiki in question.
265 * @param $suffix String The suffix of the wiki in question.
266 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
267 * @param $wikiTags Array The tags assigned to the wiki.
268 */
269 public function extractAllGlobals( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
270 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
271 foreach ( $this->settings as $varName => $setting ) {
272 $this->extractGlobalSetting( $varName, $wiki, $params );
273 }
274 }
275
276 /**
277 * Return specific settings for $wiki
278 * See the documentation of self::$siteParamsCallback for more in-depth
279 * documentation about this function
280 *
281 * @param $wiki String
282 * @return array
283 */
284 protected function getWikiParams( $wiki ){
285 static $default = array(
286 'suffix' => null,
287 'lang' => null,
288 'tags' => array(),
289 'params' => array(),
290 );
291
292 if( !is_callable( $this->siteParamsCallback ) ) {
293 return $default;
294 }
295
296 $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) );
297 # Validate the returned value
298 if( !is_array( $ret ) ) {
299 return $default;
300 }
301
302 foreach( $default as $name => $def ){
303 if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) )
304 $ret[$name] = $default[$name];
305 }
306
307 return $ret;
308 }
309
310 /**
311 * Merge params between the ones passed to the function and the ones given
312 * by self::$siteParamsCallback for backward compatibility
313 * Values returned by self::getWikiParams() have the priority.
314 *
315 * @param $wiki String Wiki ID of the wiki in question.
316 * @param $suffix String The suffix of the wiki in question.
317 * @param $params Array List of parameters. $.'key' is replaced by $value in
318 * all returned data.
319 * @param $wikiTags Array The tags assigned to the wiki.
320 * @return array
321 */
322 protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ){
323 $ret = $this->getWikiParams( $wiki );
324
325 if( is_null( $ret['suffix'] ) )
326 $ret['suffix'] = $suffix;
327
328 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
329
330 $ret['params'] += $params;
331
332 // Automatically fill that ones if needed
333 if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) )
334 $ret['params']['lang'] = $ret['lang'];
335 if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) )
336 $ret['params']['site'] = $ret['suffix'];
337
338 return $ret;
339 }
340
341 /**
342 * Work out the site and language name from a database name
343 * @param $db
344 *
345 * @return array
346 */
347 public function siteFromDB( $db ) {
348 // Allow override
349 $def = $this->getWikiParams( $db );
350 if( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) )
351 return array( $def['suffix'], $def['lang'] );
352
353 $site = null;
354 $lang = null;
355 foreach ( $this->suffixes as $suffix ) {
356 if ( $suffix === '' ) {
357 $site = '';
358 $lang = $db;
359 break;
360 } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
361 $site = $suffix == 'wiki' ? 'wikipedia' : $suffix;
362 $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
363 break;
364 }
365 }
366 $lang = str_replace( '_', '-', $lang );
367 return array( $site, $lang );
368 }
369
370 /**
371 * Returns true if the given vhost is handled locally.
372 * @param $vhost String
373 * @return bool
374 */
375 public function isLocalVHost( $vhost ) {
376 return in_array( $vhost, $this->localVHosts );
377 }
378
379 /**
380 * Merge multiple arrays together.
381 * On encountering duplicate keys, merge the two, but ONLY if they're arrays.
382 * PHP's array_merge_recursive() merges ANY duplicate values into arrays,
383 * which is not fun
384 *
385 * @param $array1 array
386 *
387 * @return array
388 */
389 static function arrayMerge( $array1/* ... */ ) {
390 $out = $array1;
391 for( $i = 1; $i < func_num_args(); $i++ ) {
392 foreach( func_get_arg( $i ) as $key => $value ) {
393 if ( isset($out[$key]) && is_array($out[$key]) && is_array($value) ) {
394 $out[$key] = self::arrayMerge( $out[$key], $value );
395 } elseif ( !isset($out[$key]) || !$out[$key] && !is_numeric($key) ) {
396 // Values that evaluate to true given precedence, for the primary purpose of merging permissions arrays.
397 $out[$key] = $value;
398 } elseif ( is_numeric( $key ) ) {
399 $out[] = $value;
400 }
401 }
402 }
403
404 return $out;
405 }
406
407 public function loadFullData() {
408 if ($this->fullLoadCallback && !$this->fullLoadDone) {
409 call_user_func( $this->fullLoadCallback, $this );
410 $this->fullLoadDone = true;
411 }
412 }
413 }