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
23 use MediaWiki\Shell\Shell
;
26 * This is a class for holding configuration settings, particularly for
31 * Consider a wikifarm having three sites: two production sites, one in English
32 * and one in German, and one testing site. You can assign them easy-to-remember
33 * identifiers - ISO 639 codes 'en' and 'de' for language wikis, and 'beta' for
36 * You would thus initialize the site configuration by specifying the wiki
40 * $conf = new SiteConfiguration;
41 * $conf->wikis = [ 'de', 'en', 'beta' ];
44 * When configuring the MediaWiki global settings (the $wg variables),
45 * the identifiers will be available to specify settings on a per wiki basis.
49 * 'wgSomeSetting' => [
61 * With three wikis, that is easy to manage. But what about a farm with
62 * hundreds of wikis? Site configuration provides a special keyword named
63 * 'default' which is the value used when a wiki is not found. Hence
64 * the above code could be written:
68 * 'wgSomeSetting' => [
72 * # Enable feature on test
79 * Since settings can contain arrays, site configuration provides a way
80 * to merge an array with the default. This is very useful to avoid
81 * repeating settings again and again while still maintaining specific changes
82 * on a per wiki basis.
86 * 'wgMergeSetting' = [
87 * # Value that will be shared among all wikis:
88 * 'default' => [ NS_USER => true ],
90 * # Leading '+' means merging the array of value with the defaults
91 * '+beta' => [ NS_HELP => true ],
95 * # Get configuration for the German site:
96 * $conf->get( 'wgMergeSetting', 'de' );
97 * // --> [ NS_USER => true ];
99 * # Get configuration for the testing site:
100 * $conf->get( 'wgMergeSetting', 'beta' );
101 * // --> [ NS_USER => true, NS_HELP => true ];
104 * Finally, to load all configuration settings, extract them in global context:
107 * # Name / identifier of the wiki as set in $conf->wikis
109 * $globals = $conf->getAll( $wikiID );
110 * extract( $globals );
113 * @note For WikiMap to function, the configuration must define string values for
114 * $wgServer (or $wgCanonicalServer) and $wgArticlePath, even if these are the
115 * same for all wikis or can be correctly determined by the logic in
118 * @todo Give examples for,
120 * $conf->suffixes = [ 'wiki' ];
124 class SiteConfiguration
{
127 * Array of suffixes, for self::siteFromDB()
129 public $suffixes = [];
132 * Array of wikis, should be the same as $wgLocalDatabases
137 * The whole array of settings
139 public $settings = [];
142 * Array of domains that are local and can be handled by the same server
144 * @deprecated since 1.25; use $wgLocalVirtualHosts instead.
146 public $localVHosts = [];
149 * Optional callback to load full configuration data.
152 public $fullLoadCallback = null;
154 /** Whether or not all data has been loaded */
155 public $fullLoadDone = false;
158 * A callback function that returns an array with the following keys (all
160 * - suffix: site's suffix
161 * - lang: site's lang
162 * - tags: array of wiki tags
163 * - params: array of parameters to be replaced
164 * The function will receive the SiteConfiguration instance in the first
165 * argument and the wiki in the second one.
166 * if suffix and lang are passed they will be used for the return value of
167 * self::siteFromDB() and self::$suffixes will be ignored
171 public $siteParamsCallback = null;
174 * Configuration cache for getConfig()
177 protected $cfgCache = [];
180 * Retrieves a configuration setting for a given wiki.
181 * @param string $settingName ID of the setting name to retrieve
182 * @param string $wiki Wiki ID of the wiki in question.
183 * @param string|null $suffix The suffix of the wiki in question.
184 * @param array $params List of parameters. $.'key' is replaced by $value in all returned data.
185 * @param array $wikiTags The tags assigned to the wiki.
186 * @return mixed The value of the setting requested.
188 public function get( $settingName, $wiki, $suffix = null, $params = [],
191 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
192 return $this->getSetting( $settingName, $wiki, $params );
196 * Really retrieves a configuration setting for a given wiki.
198 * @param string $settingName ID of the setting name to retrieve.
199 * @param string $wiki Wiki ID of the wiki in question.
200 * @param array $params Array of parameters.
201 * @return mixed The value of the setting requested.
203 protected function getSetting( $settingName, $wiki, array $params ) {
205 if ( array_key_exists( $settingName, $this->settings
) ) {
206 $thisSetting =& $this->settings
[$settingName];
208 // Do individual wiki settings
209 if ( array_key_exists( $wiki, $thisSetting ) ) {
210 $retval = $thisSetting[$wiki];
212 } elseif ( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
213 $retval = $thisSetting["+$wiki"];
217 foreach ( $params['tags'] as $tag ) {
218 if ( array_key_exists( $tag, $thisSetting ) ) {
219 if ( is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
220 $retval = self
::arrayMerge( $retval, $thisSetting[$tag] );
222 $retval = $thisSetting[$tag];
225 } elseif ( array_key_exists( "+$tag", $thisSetting ) && is_array( $thisSetting["+$tag"] ) ) {
226 if ( $retval === null ) {
229 $retval = self
::arrayMerge( $retval, $thisSetting["+$tag"] );
232 // Do suffix settings
233 $suffix = $params['suffix'];
234 if ( !is_null( $suffix ) ) {
235 if ( array_key_exists( $suffix, $thisSetting ) ) {
236 if ( is_array( $retval ) && is_array( $thisSetting[$suffix] ) ) {
237 $retval = self
::arrayMerge( $retval, $thisSetting[$suffix] );
239 $retval = $thisSetting[$suffix];
242 } elseif ( array_key_exists( "+$suffix", $thisSetting )
243 && is_array( $thisSetting["+$suffix"] )
245 if ( $retval === null ) {
248 $retval = self
::arrayMerge( $retval, $thisSetting["+$suffix"] );
252 // Fall back to default.
253 if ( array_key_exists( 'default', $thisSetting ) ) {
254 if ( is_array( $retval ) && is_array( $thisSetting['default'] ) ) {
255 $retval = self
::arrayMerge( $retval, $thisSetting['default'] );
257 $retval = $thisSetting['default'];
264 if ( !is_null( $retval ) && count( $params['params'] ) ) {
265 foreach ( $params['params'] as $key => $value ) {
266 $retval = $this->doReplace( '$' . $key, $value, $retval );
273 * Type-safe string replace; won't do replacements on non-strings
276 * @param string $from
278 * @param string|array $in
279 * @return string|array
281 function doReplace( $from, $to, $in ) {
282 if ( is_string( $in ) ) {
283 return str_replace( $from, $to, $in );
284 } elseif ( is_array( $in ) ) {
285 foreach ( $in as $key => $val ) {
286 $in[$key] = $this->doReplace( $from, $to, $val );
295 * Gets all settings for a wiki
296 * @param string $wiki Wiki ID of the wiki in question.
297 * @param string|null $suffix The suffix of the wiki in question.
298 * @param array $params List of parameters. $.'key' is replaced by $value in all returned data.
299 * @param array $wikiTags The tags assigned to the wiki.
300 * @return array Array of settings requested.
302 public function getAll( $wiki, $suffix = null, $params = [], $wikiTags = [] ) {
303 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
305 foreach ( $this->settings
as $varname => $stuff ) {
308 if ( substr( $varname, 0, 1 ) == '+' ) {
310 $var = substr( $varname, 1 );
313 $value = $this->getSetting( $varname, $wiki, $params );
314 if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) ) {
315 $value = self
::arrayMerge( $value, $GLOBALS[$var] );
317 if ( !is_null( $value ) ) {
318 $localSettings[$var] = $value;
321 return $localSettings;
325 * Retrieves a configuration setting for a given wiki, forced to a boolean.
326 * @param string $setting ID of the setting name to retrieve
327 * @param string $wiki Wiki ID of the wiki in question.
328 * @param string|null $suffix The suffix of the wiki in question.
329 * @param array $wikiTags The tags assigned to the wiki.
330 * @return bool The value of the setting requested.
332 public function getBool( $setting, $wiki, $suffix = null, $wikiTags = [] ) {
333 return (bool)$this->get( $setting, $wiki, $suffix, [], $wikiTags );
337 * Retrieves an array of local databases
341 function &getLocalDatabases() {
346 * Retrieves the value of a given setting, and places it in a variable passed by reference.
347 * @param string $setting ID of the setting name to retrieve
348 * @param string $wiki Wiki ID of the wiki in question.
349 * @param string $suffix The suffix of the wiki in question.
350 * @param array &$var Reference The variable to insert the value into.
351 * @param array $params List of parameters. $.'key' is replaced by $value in all returned data.
352 * @param array $wikiTags The tags assigned to the wiki.
354 public function extractVar( $setting, $wiki, $suffix, &$var,
355 $params = [], $wikiTags = []
357 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
358 if ( !is_null( $value ) ) {
364 * Retrieves the value of a given setting, and places it in its corresponding global variable.
365 * @param string $setting ID of the setting name to retrieve
366 * @param string $wiki Wiki ID of the wiki in question.
367 * @param string|null $suffix The suffix of the wiki in question.
368 * @param array $params List of parameters. $.'key' is replaced by $value in all returned data.
369 * @param array $wikiTags The tags assigned to the wiki.
371 public function extractGlobal( $setting, $wiki, $suffix = null,
372 $params = [], $wikiTags = []
374 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
375 $this->extractGlobalSetting( $setting, $wiki, $params );
379 * @param string $setting
380 * @param string $wiki
381 * @param array $params
383 public function extractGlobalSetting( $setting, $wiki, $params ) {
384 $value = $this->getSetting( $setting, $wiki, $params );
385 if ( !is_null( $value ) ) {
386 if ( substr( $setting, 0, 1 ) == '+' && is_array( $value ) ) {
387 $setting = substr( $setting, 1 );
388 if ( is_array( $GLOBALS[$setting] ) ) {
389 $GLOBALS[$setting] = self
::arrayMerge( $GLOBALS[$setting], $value );
391 $GLOBALS[$setting] = $value;
394 $GLOBALS[$setting] = $value;
400 * Retrieves the values of all settings, and places them in their corresponding global variables.
401 * @param string $wiki Wiki ID of the wiki in question.
402 * @param string|null $suffix The suffix of the wiki in question.
403 * @param array $params List of parameters. $.'key' is replaced by $value in all returned data.
404 * @param array $wikiTags The tags assigned to the wiki.
406 public function extractAllGlobals( $wiki, $suffix = null, $params = [],
409 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
410 foreach ( $this->settings
as $varName => $setting ) {
411 $this->extractGlobalSetting( $varName, $wiki, $params );
416 * Return specific settings for $wiki
417 * See the documentation of self::$siteParamsCallback for more in-depth
418 * documentation about this function
420 * @param string $wiki
423 protected function getWikiParams( $wiki ) {
431 if ( !is_callable( $this->siteParamsCallback
) ) {
435 $ret = ( $this->siteParamsCallback
)( $this, $wiki );
436 # Validate the returned value
437 if ( !is_array( $ret ) ) {
441 foreach ( $default as $name => $def ) {
442 if ( !isset( $ret[$name] ) ||
( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) ) {
443 $ret[$name] = $default[$name];
451 * Merge params between the ones passed to the function and the ones given
452 * by self::$siteParamsCallback for backward compatibility
453 * Values returned by self::getWikiParams() have the priority.
455 * @param string $wiki Wiki ID of the wiki in question.
456 * @param string $suffix The suffix of the wiki in question.
457 * @param array $params List of parameters. $.'key' is replaced by $value in
459 * @param array $wikiTags The tags assigned to the wiki.
462 protected function mergeParams( $wiki, $suffix, array $params, array $wikiTags ) {
463 $ret = $this->getWikiParams( $wiki );
465 if ( is_null( $ret['suffix'] ) ) {
466 $ret['suffix'] = $suffix;
469 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
471 $ret['params'] +
= $params;
473 // Automatically fill that ones if needed
474 if ( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) ) {
475 $ret['params']['lang'] = $ret['lang'];
477 if ( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) ) {
478 $ret['params']['site'] = $ret['suffix'];
485 * Work out the site and language name from a database name
486 * @param string $wiki Wiki ID
490 public function siteFromDB( $wiki ) {
492 $def = $this->getWikiParams( $wiki );
493 if ( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) ) {
494 return [ $def['suffix'], $def['lang'] ];
499 foreach ( $this->suffixes
as $altSite => $suffix ) {
500 if ( $suffix === '' ) {
504 } elseif ( substr( $wiki, -strlen( $suffix ) ) == $suffix ) {
505 $site = is_numeric( $altSite ) ?
$suffix : $altSite;
506 $lang = substr( $wiki, 0, strlen( $wiki ) - strlen( $suffix ) );
510 $lang = str_replace( '_', '-', $lang );
512 return [ $site, $lang ];
516 * Get the resolved (post-setup) configuration of a potentially foreign wiki.
517 * For foreign wikis, this is expensive, and only works if maintenance
518 * scripts are setup to handle the --wiki parameter such as in wiki farms.
520 * @param string $wiki
521 * @param array|string $settings A setting name or array of setting names
522 * @return mixed|mixed[] Array if $settings is an array, otherwise the value
523 * @throws MWException
526 public function getConfig( $wiki, $settings ) {
529 $multi = is_array( $settings );
530 $settings = (array)$settings;
531 if ( WikiMap
::isCurrentWikiId( $wiki ) ) { // $wiki is this wiki
533 foreach ( $settings as $name ) {
534 if ( !preg_match( '/^wg[A-Z]/', $name ) ) {
535 throw new MWException( "Variable '$name' does start with 'wg'." );
536 } elseif ( !isset( $GLOBALS[$name] ) ) {
537 throw new MWException( "Variable '$name' is not set." );
539 $res[$name] = $GLOBALS[$name];
541 } else { // $wiki is a foreign wiki
542 if ( isset( $this->cfgCache
[$wiki] ) ) {
543 $res = array_intersect_key( $this->cfgCache
[$wiki], array_flip( $settings ) );
544 if ( count( $res ) == count( $settings ) ) {
545 return $multi ?
$res : current( $res ); // cache hit
547 } elseif ( !in_array( $wiki, $this->wikis
) ) {
548 throw new MWException( "No such wiki '$wiki'." );
550 $this->cfgCache
[$wiki] = [];
552 $result = Shell
::makeScriptCommand(
553 "$IP/maintenance/getConfiguration.php",
556 '--settings', implode( ' ', $settings ),
560 // limit.sh breaks this call
561 ->limits( [ 'memory' => 0, 'filesize' => 0 ] )
564 $data = trim( $result->getStdout() );
565 if ( $result->getExitCode() ||
$data === '' ) {
566 throw new MWException( "Failed to run getConfiguration.php: {$result->getStdout()}" );
568 $res = unserialize( $data );
569 if ( !is_array( $res ) ) {
570 throw new MWException( "Failed to unserialize configuration array." );
572 $this->cfgCache
[$wiki] = $this->cfgCache
[$wiki] +
$res;
575 return $multi ?
$res : current( $res );
579 * Merge multiple arrays together.
580 * On encountering duplicate keys, merge the two, but ONLY if they're arrays.
581 * PHP's array_merge_recursive() merges ANY duplicate values into arrays,
584 * @param array $array1
585 * @param array ...$arrays
589 static function arrayMerge( array $array1, ...$arrays ) {
591 foreach ( $arrays as $array ) {
592 foreach ( $array as $key => $value ) {
593 if ( isset( $out[$key] ) && is_array( $out[$key] ) && is_array( $value ) ) {
594 $out[$key] = self
::arrayMerge( $out[$key], $value );
595 } elseif ( !isset( $out[$key] ) ||
!$out[$key] && !is_numeric( $key ) ) {
596 // Values that evaluate to true given precedence, for the
597 // primary purpose of merging permissions arrays.
599 } elseif ( is_numeric( $key ) ) {
608 public function loadFullData() {
609 if ( $this->fullLoadCallback
&& !$this->fullLoadDone
) {
610 ( $this->fullLoadCallback
)( $this );
611 $this->fullLoadDone
= true;