186 public function get( $settingName, $wiki, $site =
null, $params = [],
189 $params = $this->
mergeParams( $wiki, $site, $params, $wikiTags );
190 $overrides = $this->settings[$settingName] ??
null;
191 $value = $overrides ? $this->processSetting( $overrides, $wiki, $params[
'tags'] ) :
null;
192 if ( !array_key_exists(
'@replaceableSettings', $this->settings )
193 || in_array( $settingName, $this->settings[
'@replaceableSettings'] )
195 $this->doReplacements( $value, $params[
'replacements'] );
231 private function processSetting( $thisSetting, $wiki, $tags ) {
237 if ( array_key_exists( $wiki, $thisSetting ) ) {
239 $retval = $thisSetting[$wiki];
241 if ( array_key_exists(
"+$wiki", $thisSetting ) ) {
244 $retval = $thisSetting[
"+$wiki"];
247 foreach ( $tags as $tag ) {
248 if ( array_key_exists( $tag, $thisSetting ) ) {
249 if ( is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
252 $retval = self::arrayMerge( $retval, $thisSetting[$tag] );
257 $retval = $thisSetting[$tag];
260 } elseif ( array_key_exists(
"+$tag", $thisSetting ) ) {
264 $retval = self::arrayMerge( $retval ?? [], $thisSetting[
"+$tag"] );
268 if ( array_key_exists(
'default', $thisSetting ) ) {
269 if ( is_array( $retval ) && is_array( $thisSetting[
'default'] ) ) {
272 $retval = self::arrayMerge( $retval, $thisSetting[
'default'] );
278 $retval = $thisSetting[
'default'];
291 private function doReplacements( &$value, $replacements ) {
295 if ( is_string( $value ) ) {
296 $value = strtr( $value, $replacements );
297 } elseif ( is_array( $value ) ) {
298 foreach ( $value as &$val ) {
299 if ( is_string( $val ) ) {
300 $val = strtr( $val, $replacements );
314 public function getAll( $wiki, $site =
null, $params = [], $wikiTags = [] ) {
315 $params = $this->
mergeParams( $wiki, $site, $params, $wikiTags );
316 $tags = $params[
'tags'];
318 foreach ( $this->settings as $varname => $overrides ) {
319 $value = $this->processSetting( $overrides, $wiki, $tags );
320 if ( $varname[0] ===
'+' ) {
321 $varname = substr( $varname, 1 );
322 if ( is_array( $value ) && is_array( $GLOBALS[$varname] ) ) {
323 $value = self::arrayMerge( $value, $GLOBALS[$varname] );
326 if ( $value !==
null ) {
327 $localSettings[$varname] = $value;
331 $replacements = $params[
'replacements'];
332 if ( array_key_exists(
'@replaceableSettings', $this->settings ) ) {
333 foreach ( $this->settings[
'@replaceableSettings'] as $varname ) {
334 if ( array_key_exists( $varname, $localSettings ) ) {
335 $this->doReplacements( $localSettings[$varname], $replacements );
339 foreach ( $localSettings as &$value ) {
340 $this->doReplacements( $value, $replacements );
343 return $localSettings;
354 public function getBool( $setting, $wiki, $site =
null, $wikiTags = [] ) {
355 return (
bool)$this->
get( $setting, $wiki, $site, [], $wikiTags );
377 $params = [], $wikiTags = []
379 $value = $this->
get( $setting, $wiki, $site, $params, $wikiTags );
380 if ( $value !==
null ) {
394 $params = [], $wikiTags = []
396 $params = $this->
mergeParams( $wiki, $site, $params, $wikiTags );
406 $overrides = $this->settings[$setting] ??
null;
407 $value = $overrides ? $this->processSetting( $overrides, $wiki, $params[
'tags'] ) :
null;
408 if ( !array_key_exists(
'@replaceableSettings', $this->settings )
409 || in_array( $setting, $this->settings[
'@replaceableSettings'] )
411 $this->doReplacements( $value, $params[
'replacements'] );
413 if ( $value !==
null ) {
414 if ( substr( $setting, 0, 1 ) ==
'+' && is_array( $value ) ) {
415 $setting = substr( $setting, 1 );
416 if ( is_array( $GLOBALS[$setting] ) ) {
417 $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value );
419 $GLOBALS[$setting] = $value;
422 $GLOBALS[$setting] = $value;
437 $params = $this->
mergeParams( $wiki, $site, $params, $wikiTags );
438 foreach ( $this->settings as $varName => $setting ) {
459 if ( !is_callable( $this->siteParamsCallback ) ) {
464 # Validate the returned value
465 if ( !is_array( $ret ) ) {
469 foreach ( $default as $name => $def ) {
470 if ( !isset( $ret[$name] ) || ( is_array( $def ) && !is_array( $ret[$name] ) ) ) {
490 protected function mergeParams( $wiki, $site, array $params, array $wikiTags ) {
493 if ( $ret[
'suffix'] ===
null ) {
494 $ret[
'suffix'] = $site;
500 $wikiTags[] = $ret[
'suffix'];
502 $ret[
'tags'] = array_unique( array_merge( $ret[
'tags'], $wikiTags ) );
504 $ret[
'params'] += $params;
508 if ( !isset( $ret[
'params'][
'lang'] ) && $ret[
'lang'] !==
null ) {
509 $ret[
'params'][
'lang'] = $ret[
'lang'];
511 if ( !isset( $ret[
'params'][
'site'] ) && $ret[
'suffix'] !==
null ) {
512 $ret[
'params'][
'site'] = $ret[
'suffix'];
517 $ret[
'replacements'] = [];
519 foreach ( $ret[
'params'] as $key => $value ) {
520 $ret[
'replacements'][
'$' . $key ] = $value;
535 if ( $def[
'suffix'] !==
null && $def[
'lang'] !==
null ) {
536 return [ $def[
'suffix'], $def[
'lang'] ];
539 $languageCode = str_replace(
'_',
'-', $wiki );
540 foreach ( $this->suffixes as $altSite => $suffix ) {
541 if ( $suffix ===
'' ) {
542 return [
'', $languageCode ];
543 } elseif ( str_ends_with( $wiki, $suffix ) ) {
544 $site = is_string( $altSite ) ? $altSite : $suffix;
545 $languageCode = substr( $languageCode, 0, -strlen( $suffix ) );
546 return [ $site, $languageCode ];
550 return [
null, null ];
567 if ( WikiMap::isCurrentWikiId( $wiki ) ) {
570 if ( !preg_match(
'/^wg[A-Z]/', $name ) ) {
571 throw new MWException(
"Variable '$name' does start with 'wg'." );
572 } elseif ( !isset( $GLOBALS[$name] ) ) {
573 throw new MWException(
"Variable '$name' is not set." );
575 $res[$name] = $GLOBALS[$name];
578 if ( isset( $this->cfgCache[$wiki] ) ) {
579 $res = array_intersect_key( $this->cfgCache[$wiki],
582 return $multi ?
$res : current(
$res );
584 } elseif ( !in_array( $wiki, $this->wikis ) ) {
587 $this->cfgCache[$wiki] = [];
589 $result = Shell::makeScriptCommand(
590 MW_INSTALL_PATH .
'/maintenance/getConfiguration.php',
598 ->limits( [
'memory' => 0,
'filesize' => 0 ] )
601 $data = trim( $result->getStdout() );
602 if ( $result->getExitCode() || $data ===
'' ) {
603 throw new MWException(
"Failed to run getConfiguration.php: {$result->getStdout()}" );
606 if ( !is_array(
$res ) ) {
607 throw new MWException(
"Failed to unserialize configuration array." );
609 $this->cfgCache[$wiki] +=
$res;
612 return $multi ?
$res : current(
$res );
625 private static function arrayMerge( array $array1, array $array2 ) {
627 foreach ( $array2 as $key => $value ) {
628 if ( isset( $out[$key] ) ) {
629 if ( is_array( $out[$key] ) && is_array( $value ) ) {
631 $out[$key] = self::arrayMerge( $out[$key], $value );
632 } elseif ( is_numeric( $key ) ) {
640 } elseif ( $out[$key] ===
false ) {
658 if ( $this->fullLoadCallback && !$this->fullLoadDone ) {
660 $this->fullLoadDone =
true;
unserialize( $serialized)
Configuration holder, particularly for multi-wiki sites.
getConfig( $wiki, $settings)
Get the resolved (post-setup) configuration of a potentially foreign wiki.
getLocalDatabases()
Retrieves an array of local databases.
$wikis
Array of wikis, should be the same as $wgLocalDatabases.
extractAllGlobals( $wiki, $site=null, $params=[], $wikiTags=[])
Retrieves the values of all settings, and places them in their corresponding global variables.
callable null $siteParamsCallback
A callback function that returns an array with the following keys (all optional):
siteFromDB( $wiki)
Work out the site and language name from a database name.
getBool( $setting, $wiki, $site=null, $wikiTags=[])
Retrieves a configuration setting for a given wiki, forced to a boolean.
extractGlobal( $setting, $wiki, $site=null, $params=[], $wikiTags=[])
Retrieves the value of a given setting, and places it in its corresponding global variable.
array $cfgCache
Configuration cache for getConfig()
string array $fullLoadCallback
Optional callback to load full configuration data.
extractVar( $setting, $wiki, $site, &$var, $params=[], $wikiTags=[])
Retrieves the value of a given setting, and places it in a variable passed by reference.
extractGlobalSetting( $setting, $wiki, $params)
getWikiParams( $wiki)
Return specific settings for $wiki See the documentation of self::$siteParamsCallback for more in-dep...
mergeParams( $wiki, $site, array $params, array $wikiTags)
Merge params between the ones passed to the function and the ones given by self::$siteParamsCallback ...
$settings
The whole array of settings.
getAll( $wiki, $site=null, $params=[], $wikiTags=[])
Gets all settings for a wiki.
$fullLoadDone
Whether or not all data has been loaded.
$suffixes
Array of suffixes, for self::siteFromDB()