(bug 31565) Option to use group members on Special:UserRights
[mediawiki.git] / includes / SiteConfiguration.php
blob6a861d8e885e028bc3353f96eca26e76d34b3d18
1 <?php
2 /**
3 * Configuration holder, particularly for multi-wiki sites.
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.
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.
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
20 * @file
23 /**
24 * This is a class for holding configuration settings, particularly for
25 * multi-wiki sites.
27 * A basic synopsis:
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.
34 * You would thus initialize the site configuration by specifying the wiki
35 * identifiers:
37 * @code
38 * $conf = new SiteConfiguration;
39 * $conf->wikis = array( 'de', 'en', 'beta' );
40 * @endcode
42 * When configuring the MediaWiki global settings (the $wg variables),
43 * the identifiers will be available to specify settings on a per wiki basis.
45 * @code
46 * $conf->settings = array(
47 * 'wgSomeSetting' => array(
49 * # production:
50 * 'de' => false,
51 * 'en' => false,
53 * # test:
54 * 'beta => true,
55 * ),
56 * );
57 * @endcode
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:
64 * @code
65 * $conf->settings = array(
66 * 'wgSomeSetting' => array(
68 * 'default' => false,
70 * # Enable feature on test
71 * 'beta' => true,
72 * ),
73 * );
74 * @endcode
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.
82 * @code
83 * $conf->settings = array(
84 * 'wgMergeSetting' = array(
85 * # Value that will be shared among all wikis:
86 * 'default' => array( NS_USER => true ),
88 * # Leading '+' means merging the array of value with the defaults
89 * '+beta' => array( NS_HELP => true ),
90 * ),
91 * );
93 * # Get configuration for the German site:
94 * $conf->get( 'wgMergeSetting', 'de' );
95 * // --> array( NS_USER => true );
97 * # Get configuration for the testing site:
98 * $conf->get( 'wgMergeSetting', 'beta' );
99 * // --> array( NS_USER => true, NS_HELP => true );
100 * @endcode
102 * Finally, to load all configuration settings, extract them in global context:
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
111 * TODO: give examples for,
112 * suffixes:
113 * $conf->suffixes = array( 'wiki' );
114 * localVHosts
115 * callbacks!
117 class SiteConfiguration {
120 * Array of suffixes, for self::siteFromDB()
122 public $suffixes = array();
125 * Array of wikis, should be the same as $wgLocalDatabases
127 public $wikis = array();
130 * The whole array of settings
132 public $settings = array();
135 * Array of domains that are local and can be handled by the same server
137 public $localVHosts = array();
140 * Optional callback to load full configuration data.
141 * @var string|array
143 public $fullLoadCallback = null;
145 /** Whether or not all data has been loaded */
146 public $fullLoadDone = false;
149 * A callback function that returns an array with the following keys (all
150 * optional):
151 * - suffix: site's suffix
152 * - lang: site's lang
153 * - tags: array of wiki tags
154 * - params: array of parameters to be replaced
155 * The function will receive the SiteConfiguration instance in the first
156 * argument and the wiki in the second one.
157 * if suffix and lang are passed they will be used for the return value of
158 * self::siteFromDB() and self::$suffixes will be ignored
160 * @var string|array
162 public $siteParamsCallback = null;
165 * Retrieves a configuration setting for a given wiki.
166 * @param $settingName String ID of the setting name to retrieve
167 * @param $wiki String Wiki ID of the wiki in question.
168 * @param $suffix String The suffix of the wiki in question.
169 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
170 * @param $wikiTags Array The tags assigned to the wiki.
171 * @return Mixed the value of the setting requested.
173 public function get( $settingName, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
174 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
175 return $this->getSetting( $settingName, $wiki, $params );
179 * Really retrieves a configuration setting for a given wiki.
181 * @param $settingName String ID of the setting name to retrieve.
182 * @param $wiki String Wiki ID of the wiki in question.
183 * @param $params Array: array of parameters.
184 * @return Mixed the value of the setting requested.
186 protected function getSetting( $settingName, $wiki, /*array*/ $params ){
187 $retval = null;
188 if( array_key_exists( $settingName, $this->settings ) ) {
189 $thisSetting =& $this->settings[$settingName];
190 do {
191 // Do individual wiki settings
192 if( array_key_exists( $wiki, $thisSetting ) ) {
193 $retval = $thisSetting[$wiki];
194 break;
195 } elseif ( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
196 $retval = $thisSetting["+$wiki"];
199 // Do tag settings
200 foreach( $params['tags'] as $tag ) {
201 if( array_key_exists( $tag, $thisSetting ) ) {
202 if ( isset( $retval ) && is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
203 $retval = self::arrayMerge( $retval, $thisSetting[$tag] );
204 } else {
205 $retval = $thisSetting[$tag];
207 break 2;
208 } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) {
209 if( !isset( $retval ) ) {
210 $retval = array();
212 $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] );
215 // Do suffix settings
216 $suffix = $params['suffix'];
217 if( !is_null( $suffix ) ) {
218 if( array_key_exists( $suffix, $thisSetting ) ) {
219 if ( isset($retval) && is_array($retval) && is_array($thisSetting[$suffix]) ) {
220 $retval = self::arrayMerge( $retval, $thisSetting[$suffix] );
221 } else {
222 $retval = $thisSetting[$suffix];
224 break;
225 } elseif ( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) {
226 if ( !isset( $retval ) ) {
227 $retval = array();
229 $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] );
233 // Fall back to default.
234 if( array_key_exists( 'default', $thisSetting ) ) {
235 if( is_array( $retval ) && is_array( $thisSetting['default'] ) ) {
236 $retval = self::arrayMerge( $retval, $thisSetting['default'] );
237 } else {
238 $retval = $thisSetting['default'];
240 break;
242 } while ( false );
245 if( !is_null( $retval ) && count( $params['params'] ) ) {
246 foreach ( $params['params'] as $key => $value ) {
247 $retval = $this->doReplace( '$' . $key, $value, $retval );
250 return $retval;
254 * Type-safe string replace; won't do replacements on non-strings
255 * private?
257 * @param $from
258 * @param $to
259 * @param $in
260 * @return string
262 function doReplace( $from, $to, $in ) {
263 if( is_string( $in ) ) {
264 return str_replace( $from, $to, $in );
265 } elseif( is_array( $in ) ) {
266 foreach( $in as $key => $val ) {
267 $in[$key] = $this->doReplace( $from, $to, $val );
269 return $in;
270 } else {
271 return $in;
276 * Gets all settings for a wiki
277 * @param $wiki String Wiki ID of the wiki in question.
278 * @param $suffix String The suffix of the wiki in question.
279 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
280 * @param $wikiTags Array The tags assigned to the wiki.
281 * @return Array Array of settings requested.
283 public function getAll( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
284 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
285 $localSettings = array();
286 foreach( $this->settings as $varname => $stuff ) {
287 $append = false;
288 $var = $varname;
289 if ( substr( $varname, 0, 1 ) == '+' ) {
290 $append = true;
291 $var = substr( $varname, 1 );
294 $value = $this->getSetting( $varname, $wiki, $params );
295 if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) ) {
296 $value = self::arrayMerge( $value, $GLOBALS[$var] );
298 if ( !is_null( $value ) ) {
299 $localSettings[$var] = $value;
302 return $localSettings;
306 * Retrieves a configuration setting for a given wiki, forced to a boolean.
307 * @param $setting String ID of the setting name to retrieve
308 * @param $wiki String Wiki ID of the wiki in question.
309 * @param $suffix String The suffix of the wiki in question.
310 * @param $wikiTags Array The tags assigned to the wiki.
311 * @return bool The value of the setting requested.
313 public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) {
314 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
318 * Retrieves an array of local databases
320 * @return array
322 function &getLocalDatabases() {
323 return $this->wikis;
327 * Retrieves the value of a given setting, and places it in a variable passed by reference.
328 * @param $setting String ID of the setting name to retrieve
329 * @param $wiki String Wiki ID of the wiki in question.
330 * @param $suffix String The suffix of the wiki in question.
331 * @param $var array Reference The variable to insert the value into.
332 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
333 * @param $wikiTags Array The tags assigned to the wiki.
335 public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) {
336 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
337 if ( !is_null( $value ) ) {
338 $var = $value;
343 * Retrieves the value of a given setting, and places it in its corresponding global variable.
344 * @param $setting String ID of the setting name to retrieve
345 * @param $wiki String Wiki ID of the wiki in question.
346 * @param $suffix String The suffix of the wiki in question.
347 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
348 * @param $wikiTags Array The tags assigned to the wiki.
350 public function extractGlobal( $setting, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
351 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
352 $this->extractGlobalSetting( $setting, $wiki, $params );
356 * @param $setting string
357 * @param $wiki string
358 * @param $params array
360 public function extractGlobalSetting( $setting, $wiki, $params ) {
361 $value = $this->getSetting( $setting, $wiki, $params );
362 if ( !is_null( $value ) ) {
363 if (substr($setting,0,1) == '+' && is_array($value)) {
364 $setting = substr($setting,1);
365 if ( is_array($GLOBALS[$setting]) ) {
366 $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value );
367 } else {
368 $GLOBALS[$setting] = $value;
370 } else {
371 $GLOBALS[$setting] = $value;
377 * Retrieves the values of all settings, and places them in their corresponding global variables.
378 * @param $wiki String Wiki ID of the wiki in question.
379 * @param $suffix String The suffix of the wiki in question.
380 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
381 * @param $wikiTags Array The tags assigned to the wiki.
383 public function extractAllGlobals( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
384 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
385 foreach ( $this->settings as $varName => $setting ) {
386 $this->extractGlobalSetting( $varName, $wiki, $params );
391 * Return specific settings for $wiki
392 * See the documentation of self::$siteParamsCallback for more in-depth
393 * documentation about this function
395 * @param $wiki String
396 * @return array
398 protected function getWikiParams( $wiki ){
399 static $default = array(
400 'suffix' => null,
401 'lang' => null,
402 'tags' => array(),
403 'params' => array(),
406 if( !is_callable( $this->siteParamsCallback ) ) {
407 return $default;
410 $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) );
411 # Validate the returned value
412 if( !is_array( $ret ) ) {
413 return $default;
416 foreach( $default as $name => $def ){
417 if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) ) {
418 $ret[$name] = $default[$name];
422 return $ret;
426 * Merge params between the ones passed to the function and the ones given
427 * by self::$siteParamsCallback for backward compatibility
428 * Values returned by self::getWikiParams() have the priority.
430 * @param $wiki String Wiki ID of the wiki in question.
431 * @param $suffix String The suffix of the wiki in question.
432 * @param $params Array List of parameters. $.'key' is replaced by $value in
433 * all returned data.
434 * @param $wikiTags Array The tags assigned to the wiki.
435 * @return array
437 protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ){
438 $ret = $this->getWikiParams( $wiki );
440 if( is_null( $ret['suffix'] ) ) {
441 $ret['suffix'] = $suffix;
444 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
446 $ret['params'] += $params;
448 // Automatically fill that ones if needed
449 if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) ){
450 $ret['params']['lang'] = $ret['lang'];
452 if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) ) {
453 $ret['params']['site'] = $ret['suffix'];
456 return $ret;
460 * Work out the site and language name from a database name
461 * @param $db
463 * @return array
465 public function siteFromDB( $db ) {
466 // Allow override
467 $def = $this->getWikiParams( $db );
468 if( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) ) {
469 return array( $def['suffix'], $def['lang'] );
472 $site = null;
473 $lang = null;
474 foreach ( $this->suffixes as $suffix ) {
475 if ( $suffix === '' ) {
476 $site = '';
477 $lang = $db;
478 break;
479 } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
480 $site = $suffix == 'wiki' ? 'wikipedia' : $suffix;
481 $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
482 break;
485 $lang = str_replace( '_', '-', $lang );
486 return array( $site, $lang );
490 * Returns true if the given vhost is handled locally.
491 * @param $vhost String
492 * @return bool
494 public function isLocalVHost( $vhost ) {
495 return in_array( $vhost, $this->localVHosts );
499 * Merge multiple arrays together.
500 * On encountering duplicate keys, merge the two, but ONLY if they're arrays.
501 * PHP's array_merge_recursive() merges ANY duplicate values into arrays,
502 * which is not fun
504 * @param $array1 array
506 * @return array
508 static function arrayMerge( $array1/* ... */ ) {
509 $out = $array1;
510 for( $i = 1; $i < func_num_args(); $i++ ) {
511 foreach( func_get_arg( $i ) as $key => $value ) {
512 if ( isset($out[$key]) && is_array($out[$key]) && is_array($value) ) {
513 $out[$key] = self::arrayMerge( $out[$key], $value );
514 } elseif ( !isset($out[$key]) || !$out[$key] && !is_numeric($key) ) {
515 // Values that evaluate to true given precedence, for the primary purpose of merging permissions arrays.
516 $out[$key] = $value;
517 } elseif ( is_numeric( $key ) ) {
518 $out[] = $value;
523 return $out;
526 public function loadFullData() {
527 if ( $this->fullLoadCallback && !$this->fullLoadDone ) {
528 call_user_func( $this->fullLoadCallback, $this );
529 $this->fullLoadDone = true;