Pass objects to methods accepting them instead of relying on global objects.
[lhc/web/wiklou.git] / includes / SiteConfiguration.php
1 <?php
2 /**
3 * Configuration holder, particularly for multi-wiki sites.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * This is a class for holding configuration settings, particularly for
25 * multi-wiki sites.
26 *
27 * A basic synopsis:
28 *
29 * Consider a wikifarm having three sites: two production sites, one in English
30 * and one in German, and one testing site. You can assign them easy-to-remember
31 * identifiers - ISO 639 codes 'en' and 'de' for language wikis, and 'beta' for
32 * the testing wiki.
33 *
34 * You would thus initialize the site configuration by specifying the wiki
35 * identifiers:
36 *
37 * @code
38 * $conf = new SiteConfiguration;
39 * $conf->wikis = array( 'de', 'en', 'beta' );
40 * @endcode
41 *
42 * When configuring the MediaWiki global settings (the $wg variables),
43 * the identifiers will be available to specify settings on a per wiki basis.
44 *
45 * @code
46 * $conf->settings = array(
47 * 'wgSomeSetting' => array(
48 *
49 * # production:
50 * 'de' => false,
51 * 'en' => false,
52 *
53 * # test:
54 * 'beta => true,
55 * ),
56 * );
57 * @endcode
58 *
59 * With three wikis, that is easy to manage. But what about a farm with
60 * hundreds of wikis? Site configuration provides a special keyword named
61 * 'default' which is the value used when a wiki is not found. Hence
62 * the above code could be written:
63 *
64 * @code
65 * $conf->settings = array(
66 * 'wgSomeSetting' => array(
67 *
68 * 'default' => false,
69 *
70 * # Enable feature on test
71 * 'beta' => true,
72 * ),
73 * );
74 * @endcode
75 *
76 *
77 * Since settings can contain arrays, site configuration provides a way
78 * to merge an array with the default. This is very useful to avoid
79 * repeating settings again and again while still maintaining specific changes
80 * on a per wiki basis.
81 *
82 * @code
83 * $conf->settings = array(
84 * 'wgMergeSetting' = array(
85 * # Value that will be shared among all wikis:
86 * 'default' => array( NS_USER => true ),
87 *
88 * # Leading '+' means merging the array of value with the defaults
89 * '+beta' => array( NS_HELP => true ),
90 * ),
91 * );
92 *
93 * # Get configuration for the German site:
94 * $conf->get( 'wgMergeSetting', 'de' );
95 * // --> array( NS_USER => true );
96 *
97 * # Get configuration for the testing site:
98 * $conf->get( 'wgMergeSetting', 'beta' );
99 * // --> array( NS_USER => true, NS_HELP => true );
100 * @endcode
101 *
102 * Finally, to load all configuration settings, extract them in global context:
103 *
104 * @code
105 * # Name / identifier of the wiki as set in $conf->wikis
106 * $wikiID = 'beta';
107 * $globals = $conf->getAll( $wikiID );
108 * extract( $globals );
109 * @endcode
110 *
111 * TODO: give examples for,
112 * suffixes:
113 * $conf->suffixes = array( 'wiki' );
114 * localVHosts
115 * callbacks!
116 */
117 class SiteConfiguration {
118
119 /**
120 * Array of suffixes, for self::siteFromDB()
121 */
122 public $suffixes = array();
123
124 /**
125 * Array of wikis, should be the same as $wgLocalDatabases
126 */
127 public $wikis = array();
128
129 /**
130 * The whole array of settings
131 */
132 public $settings = array();
133
134 /**
135 * Array of domains that are local and can be handled by the same server
136 */
137 public $localVHosts = array();
138
139 /**
140 * Optional callback to load full configuration data.
141 */
142 public $fullLoadCallback = null;
143
144 /** Whether or not all data has been loaded */
145 public $fullLoadDone = false;
146
147 /**
148 * A callback function that returns an array with the following keys (all
149 * optional):
150 * - suffix: site's suffix
151 * - lang: site's lang
152 * - tags: array of wiki tags
153 * - params: array of parameters to be replaced
154 * The function will receive the SiteConfiguration instance in the first
155 * argument and the wiki in the second one.
156 * if suffix and lang are passed they will be used for the return value of
157 * self::siteFromDB() and self::$suffixes will be ignored
158 */
159 public $siteParamsCallback = null;
160
161 /**
162 * Retrieves a configuration setting for a given wiki.
163 * @param $settingName String ID of the setting name to retrieve
164 * @param $wiki String Wiki ID of the wiki in question.
165 * @param $suffix String The suffix of the wiki in question.
166 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
167 * @param $wikiTags Array The tags assigned to the wiki.
168 * @return Mixed the value of the setting requested.
169 */
170 public function get( $settingName, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
171 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
172 return $this->getSetting( $settingName, $wiki, $params );
173 }
174
175 /**
176 * Really retrieves a configuration setting for a given wiki.
177 *
178 * @param $settingName String ID of the setting name to retrieve.
179 * @param $wiki String Wiki ID of the wiki in question.
180 * @param $params Array: array of parameters.
181 * @return Mixed the value of the setting requested.
182 */
183 protected function getSetting( $settingName, $wiki, /*array*/ $params ){
184 $retval = null;
185 if( array_key_exists( $settingName, $this->settings ) ) {
186 $thisSetting =& $this->settings[$settingName];
187 do {
188 // Do individual wiki settings
189 if( array_key_exists( $wiki, $thisSetting ) ) {
190 $retval = $thisSetting[$wiki];
191 break;
192 } elseif( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
193 $retval = $thisSetting["+$wiki"];
194 }
195
196 // Do tag settings
197 foreach( $params['tags'] as $tag ) {
198 if( array_key_exists( $tag, $thisSetting ) ) {
199 if ( isset( $retval ) && is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
200 $retval = self::arrayMerge( $retval, $thisSetting[$tag] );
201 } else {
202 $retval = $thisSetting[$tag];
203 }
204 break 2;
205 } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) {
206 if( !isset( $retval ) )
207 $retval = array();
208 $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] );
209 }
210 }
211 // Do suffix settings
212 $suffix = $params['suffix'];
213 if( !is_null( $suffix ) ) {
214 if( array_key_exists( $suffix, $thisSetting ) ) {
215 if ( isset($retval) && is_array($retval) && is_array($thisSetting[$suffix]) ) {
216 $retval = self::arrayMerge( $retval, $thisSetting[$suffix] );
217 } else {
218 $retval = $thisSetting[$suffix];
219 }
220 break;
221 } elseif( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) {
222 if (!isset($retval))
223 $retval = array();
224 $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] );
225 }
226 }
227
228 // Fall back to default.
229 if( array_key_exists( 'default', $thisSetting ) ) {
230 if( is_array( $retval ) && is_array( $thisSetting['default'] ) ) {
231 $retval = self::arrayMerge( $retval, $thisSetting['default'] );
232 } else {
233 $retval = $thisSetting['default'];
234 }
235 break;
236 }
237 } while ( false );
238 }
239
240 if( !is_null( $retval ) && count( $params['params'] ) ) {
241 foreach ( $params['params'] as $key => $value ) {
242 $retval = $this->doReplace( '$' . $key, $value, $retval );
243 }
244 }
245 return $retval;
246 }
247
248 /**
249 * Type-safe string replace; won't do replacements on non-strings
250 * private?
251 *
252 * @param $from
253 * @param $to
254 * @param $in
255 * @return string
256 */
257 function doReplace( $from, $to, $in ) {
258 if( is_string( $in ) ) {
259 return str_replace( $from, $to, $in );
260 } elseif( is_array( $in ) ) {
261 foreach( $in as $key => $val ) {
262 $in[$key] = $this->doReplace( $from, $to, $val );
263 }
264 return $in;
265 } else {
266 return $in;
267 }
268 }
269
270 /**
271 * Gets all settings for a wiki
272 * @param $wiki String Wiki ID of the wiki in question.
273 * @param $suffix String The suffix of the wiki in question.
274 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
275 * @param $wikiTags Array The tags assigned to the wiki.
276 * @return Array Array of settings requested.
277 */
278 public function getAll( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
279 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
280 $localSettings = array();
281 foreach( $this->settings as $varname => $stuff ) {
282 $append = false;
283 $var = $varname;
284 if ( substr( $varname, 0, 1 ) == '+' ) {
285 $append = true;
286 $var = substr( $varname, 1 );
287 }
288
289 $value = $this->getSetting( $varname, $wiki, $params );
290 if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) )
291 $value = self::arrayMerge( $value, $GLOBALS[$var] );
292 if ( !is_null( $value ) ) {
293 $localSettings[$var] = $value;
294 }
295 }
296 return $localSettings;
297 }
298
299 /**
300 * Retrieves a configuration setting for a given wiki, forced to a boolean.
301 * @param $setting String ID of the setting name to retrieve
302 * @param $wiki String Wiki ID of the wiki in question.
303 * @param $suffix String The suffix of the wiki in question.
304 * @param $wikiTags Array The tags assigned to the wiki.
305 * @return bool The value of the setting requested.
306 */
307 public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) {
308 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
309 }
310
311 /**
312 * Retrieves an array of local databases
313 *
314 * @return array
315 */
316 function &getLocalDatabases() {
317 return $this->wikis;
318 }
319
320 /**
321 * Retrieves the value of a given setting, and places it in a variable passed by reference.
322 * @param $setting String ID of the setting name to retrieve
323 * @param $wiki String Wiki ID of the wiki in question.
324 * @param $suffix String The suffix of the wiki in question.
325 * @param $var array Reference The variable to insert the value into.
326 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
327 * @param $wikiTags Array The tags assigned to the wiki.
328 */
329 public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) {
330 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
331 if ( !is_null( $value ) ) {
332 $var = $value;
333 }
334 }
335
336 /**
337 * Retrieves the value of a given setting, and places it in its corresponding global variable.
338 * @param $setting String ID of the setting name to retrieve
339 * @param $wiki String Wiki ID of the wiki in question.
340 * @param $suffix String The suffix of the wiki in question.
341 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
342 * @param $wikiTags Array The tags assigned to the wiki.
343 */
344 public function extractGlobal( $setting, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
345 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
346 $this->extractGlobalSetting( $setting, $wiki, $params );
347 }
348
349 /**
350 * @param $setting string
351 * @param $wiki string
352 * @param $params array
353 */
354 public function extractGlobalSetting( $setting, $wiki, $params ) {
355 $value = $this->getSetting( $setting, $wiki, $params );
356 if ( !is_null( $value ) ) {
357 if (substr($setting,0,1) == '+' && is_array($value)) {
358 $setting = substr($setting,1);
359 if ( is_array($GLOBALS[$setting]) ) {
360 $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value );
361 } else {
362 $GLOBALS[$setting] = $value;
363 }
364 } else {
365 $GLOBALS[$setting] = $value;
366 }
367 }
368 }
369
370 /**
371 * Retrieves the values of all settings, and places them in their corresponding global variables.
372 * @param $wiki String Wiki ID of the wiki in question.
373 * @param $suffix String The suffix of the wiki in question.
374 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
375 * @param $wikiTags Array The tags assigned to the wiki.
376 */
377 public function extractAllGlobals( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
378 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
379 foreach ( $this->settings as $varName => $setting ) {
380 $this->extractGlobalSetting( $varName, $wiki, $params );
381 }
382 }
383
384 /**
385 * Return specific settings for $wiki
386 * See the documentation of self::$siteParamsCallback for more in-depth
387 * documentation about this function
388 *
389 * @param $wiki String
390 * @return array
391 */
392 protected function getWikiParams( $wiki ){
393 static $default = array(
394 'suffix' => null,
395 'lang' => null,
396 'tags' => array(),
397 'params' => array(),
398 );
399
400 if( !is_callable( $this->siteParamsCallback ) ) {
401 return $default;
402 }
403
404 $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) );
405 # Validate the returned value
406 if( !is_array( $ret ) ) {
407 return $default;
408 }
409
410 foreach( $default as $name => $def ){
411 if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) )
412 $ret[$name] = $default[$name];
413 }
414
415 return $ret;
416 }
417
418 /**
419 * Merge params between the ones passed to the function and the ones given
420 * by self::$siteParamsCallback for backward compatibility
421 * Values returned by self::getWikiParams() have the priority.
422 *
423 * @param $wiki String Wiki ID of the wiki in question.
424 * @param $suffix String The suffix of the wiki in question.
425 * @param $params Array List of parameters. $.'key' is replaced by $value in
426 * all returned data.
427 * @param $wikiTags Array The tags assigned to the wiki.
428 * @return array
429 */
430 protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ){
431 $ret = $this->getWikiParams( $wiki );
432
433 if( is_null( $ret['suffix'] ) )
434 $ret['suffix'] = $suffix;
435
436 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
437
438 $ret['params'] += $params;
439
440 // Automatically fill that ones if needed
441 if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) )
442 $ret['params']['lang'] = $ret['lang'];
443 if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) )
444 $ret['params']['site'] = $ret['suffix'];
445
446 return $ret;
447 }
448
449 /**
450 * Work out the site and language name from a database name
451 * @param $db
452 *
453 * @return array
454 */
455 public function siteFromDB( $db ) {
456 // Allow override
457 $def = $this->getWikiParams( $db );
458 if( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) )
459 return array( $def['suffix'], $def['lang'] );
460
461 $site = null;
462 $lang = null;
463 foreach ( $this->suffixes as $suffix ) {
464 if ( $suffix === '' ) {
465 $site = '';
466 $lang = $db;
467 break;
468 } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
469 $site = $suffix == 'wiki' ? 'wikipedia' : $suffix;
470 $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
471 break;
472 }
473 }
474 $lang = str_replace( '_', '-', $lang );
475 return array( $site, $lang );
476 }
477
478 /**
479 * Returns true if the given vhost is handled locally.
480 * @param $vhost String
481 * @return bool
482 */
483 public function isLocalVHost( $vhost ) {
484 return in_array( $vhost, $this->localVHosts );
485 }
486
487 /**
488 * Merge multiple arrays together.
489 * On encountering duplicate keys, merge the two, but ONLY if they're arrays.
490 * PHP's array_merge_recursive() merges ANY duplicate values into arrays,
491 * which is not fun
492 *
493 * @param $array1 array
494 *
495 * @return array
496 */
497 static function arrayMerge( $array1/* ... */ ) {
498 $out = $array1;
499 for( $i = 1; $i < func_num_args(); $i++ ) {
500 foreach( func_get_arg( $i ) as $key => $value ) {
501 if ( isset($out[$key]) && is_array($out[$key]) && is_array($value) ) {
502 $out[$key] = self::arrayMerge( $out[$key], $value );
503 } elseif ( !isset($out[$key]) || !$out[$key] && !is_numeric($key) ) {
504 // Values that evaluate to true given precedence, for the primary purpose of merging permissions arrays.
505 $out[$key] = $value;
506 } elseif ( is_numeric( $key ) ) {
507 $out[] = $value;
508 }
509 }
510 }
511
512 return $out;
513 }
514
515 public function loadFullData() {
516 if ($this->fullLoadCallback && !$this->fullLoadDone) {
517 call_user_func( $this->fullLoadCallback, $this );
518 $this->fullLoadDone = true;
519 }
520 }
521 }