187 public function get( $settingName, $wiki, $site =
null, $params = [],
190 $params = $this->
mergeParams( $wiki, $site, $params, $wikiTags );
191 $overrides = $this->settings[$settingName] ??
null;
192 $value = $overrides ? $this->processSetting( $overrides, $wiki, $params[
'tags'] ) :
null;
193 if ( !array_key_exists(
'@replaceableSettings', $this->settings )
194 || in_array( $settingName, $this->settings[
'@replaceableSettings'] )
196 $this->doReplacements( $value, $params[
'replacements'] );
232 private function processSetting( $thisSetting, $wiki, $tags ) {
238 if ( array_key_exists( $wiki, $thisSetting ) ) {
240 $retval = $thisSetting[$wiki];
242 if ( array_key_exists(
"+$wiki", $thisSetting ) ) {
245 $retval = $thisSetting[
"+$wiki"];
248 foreach ( $tags as $tag ) {
249 if ( array_key_exists( $tag, $thisSetting ) ) {
250 if ( is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
253 $retval = self::arrayMerge( $retval, $thisSetting[$tag] );
258 $retval = $thisSetting[$tag];
261 } elseif ( array_key_exists(
"+$tag", $thisSetting ) ) {
265 $retval = self::arrayMerge( $retval ?? [], $thisSetting[
"+$tag"] );
269 if ( array_key_exists(
'default', $thisSetting ) ) {
270 if ( is_array( $retval ) && is_array( $thisSetting[
'default'] ) ) {
273 $retval = self::arrayMerge( $retval, $thisSetting[
'default'] );
279 $retval = $thisSetting[
'default'];
292 private function doReplacements( &$value, $replacements ) {
296 if ( is_string( $value ) ) {
297 $value = strtr( $value, $replacements );
298 } elseif ( is_array( $value ) ) {
299 foreach ( $value as &$val ) {
300 if ( is_string( $val ) ) {
301 $val = strtr( $val, $replacements );
315 public function getAll( $wiki, $site =
null, $params = [], $wikiTags = [] ) {
316 $params = $this->
mergeParams( $wiki, $site, $params, $wikiTags );
317 $tags = $params[
'tags'];
319 foreach ( $this->settings as $varname => $overrides ) {
320 $value = $this->processSetting( $overrides, $wiki, $tags );
321 if ( $varname[0] ===
'+' ) {
322 $varname = substr( $varname, 1 );
323 if ( is_array( $value ) && is_array( $GLOBALS[$varname] ) ) {
324 $value = self::arrayMerge( $value, $GLOBALS[$varname] );
327 if ( $value !==
null ) {
328 $localSettings[$varname] = $value;
332 $replacements = $params[
'replacements'];
333 if ( array_key_exists(
'@replaceableSettings', $this->settings ) ) {
334 foreach ( $this->settings[
'@replaceableSettings'] as $varname ) {
335 if ( array_key_exists( $varname, $localSettings ) ) {
336 $this->doReplacements( $localSettings[$varname], $replacements );
340 foreach ( $localSettings as &$value ) {
341 $this->doReplacements( $value, $replacements );
344 return $localSettings;
355 public function getBool( $setting, $wiki, $site =
null, $wikiTags = [] ) {
356 return (
bool)$this->
get( $setting, $wiki, $site, [], $wikiTags );
378 $params = [], $wikiTags = []
380 $value = $this->
get( $setting, $wiki, $site, $params, $wikiTags );
381 if ( $value !==
null ) {
395 $params = [], $wikiTags = []
397 $params = $this->
mergeParams( $wiki, $site, $params, $wikiTags );
407 $overrides = $this->settings[$setting] ??
null;
408 $value = $overrides ? $this->processSetting( $overrides, $wiki, $params[
'tags'] ) :
null;
409 if ( !array_key_exists(
'@replaceableSettings', $this->settings )
410 || in_array( $setting, $this->settings[
'@replaceableSettings'] )
412 $this->doReplacements( $value, $params[
'replacements'] );
414 if ( $value !==
null ) {
415 if ( substr( $setting, 0, 1 ) ==
'+' && is_array( $value ) ) {
416 $setting = substr( $setting, 1 );
417 if ( is_array( $GLOBALS[$setting] ) ) {
418 $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value );
420 $GLOBALS[$setting] = $value;
423 $GLOBALS[$setting] = $value;
438 $params = $this->
mergeParams( $wiki, $site, $params, $wikiTags );
439 foreach ( $this->settings as $varName => $setting ) {
460 if ( !is_callable( $this->siteParamsCallback ) ) {
465 # Validate the returned value
466 if ( !is_array( $ret ) ) {
470 foreach ( $default as $name => $def ) {
471 if ( !isset( $ret[$name] ) || ( is_array( $def ) && !is_array( $ret[$name] ) ) ) {
491 protected function mergeParams( $wiki, $site, array $params, array $wikiTags ) {
494 $ret[
'suffix'] ??= $site;
499 $wikiTags[] = $ret[
'suffix'];
501 $ret[
'tags'] = array_unique( array_merge( $ret[
'tags'], $wikiTags ) );
503 $ret[
'params'] += $params;
507 if ( !isset( $ret[
'params'][
'lang'] ) && $ret[
'lang'] !==
null ) {
508 $ret[
'params'][
'lang'] = $ret[
'lang'];
510 if ( !isset( $ret[
'params'][
'site'] ) && $ret[
'suffix'] !==
null ) {
511 $ret[
'params'][
'site'] = $ret[
'suffix'];
516 $ret[
'replacements'] = [];
518 foreach ( $ret[
'params'] as $key => $value ) {
519 $ret[
'replacements'][
'$' . $key ] = $value;
534 if ( $def[
'suffix'] !==
null && $def[
'lang'] !==
null ) {
535 return [ $def[
'suffix'], $def[
'lang'] ];
538 $languageCode = str_replace(
'_',
'-', $wiki );
539 foreach ( $this->suffixes as $altSite => $suffix ) {
540 if ( $suffix ===
'' ) {
541 return [
'', $languageCode ];
542 } elseif ( str_ends_with( $wiki, $suffix ) ) {
543 $site = is_string( $altSite ) ? $altSite : $suffix;
544 $languageCode = substr( $languageCode, 0, -strlen( $suffix ) );
545 return [ $site, $languageCode ];
549 return [
null, null ];
566 if ( WikiMap::isCurrentWikiId( $wiki ) ) {
569 if ( !preg_match(
'/^wg[A-Z]/', $name ) ) {
570 throw new MWException(
"Variable '$name' does start with 'wg'." );
571 } elseif ( !isset( $GLOBALS[$name] ) ) {
572 throw new MWException(
"Variable '$name' is not set." );
574 $res[$name] = $GLOBALS[$name];
577 if ( isset( $this->cfgCache[$wiki] ) ) {
578 $res = array_intersect_key( $this->cfgCache[$wiki],
581 return $multi ?
$res : current(
$res );
583 } elseif ( !in_array( $wiki, $this->wikis ) ) {
586 $this->cfgCache[$wiki] = [];
588 $result = Shell::makeScriptCommand(
589 MW_INSTALL_PATH .
'/maintenance/getConfiguration.php',
597 ->limits( [
'memory' => 0,
'filesize' => 0 ] )
600 $data = trim( $result->getStdout() );
601 if ( $result->getExitCode() || $data ===
'' ) {
602 throw new MWException(
"Failed to run getConfiguration.php: {$result->getStdout()}" );
604 $res = unserialize( $data );
605 if ( !is_array(
$res ) ) {
606 throw new MWException(
"Failed to unserialize configuration array." );
608 $this->cfgCache[$wiki] +=
$res;
611 return $multi ?
$res : current(
$res );
624 private static function arrayMerge( array $array1, array $array2 ) {
626 foreach ( $array2 as $key => $value ) {
627 if ( isset( $out[$key] ) ) {
628 if ( is_array( $out[$key] ) && is_array( $value ) ) {
630 $out[$key] = self::arrayMerge( $out[$key], $value );
631 } elseif ( is_numeric( $key ) ) {
639 } elseif ( $out[$key] ===
false ) {
657 if ( $this->fullLoadCallback && !$this->fullLoadDone ) {
659 $this->fullLoadDone =
true;
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()