178 public function get( $settingName, $wiki, $suffix =
null, $params = [],
181 $params = $this->
mergeParams( $wiki, $suffix, $params, $wikiTags );
182 $overrides = $this->settings[$settingName] ??
null;
183 return $overrides ? $this->
processSetting( $overrides, $wiki, $params ) :
null;
220 if ( array_key_exists( $wiki, $thisSetting ) ) {
222 $retval = $thisSetting[$wiki];
224 if ( array_key_exists(
"+$wiki", $thisSetting ) && is_array( $thisSetting[
"+$wiki"] ) ) {
227 $retval = $thisSetting[
"+$wiki"];
231 foreach ( $params[
'tags'] as $tag ) {
232 if ( array_key_exists( $tag, $thisSetting ) ) {
233 if ( is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
241 $retval = $thisSetting[$tag];
245 } elseif ( array_key_exists(
"+$tag", $thisSetting ) && is_array( $thisSetting[
"+$tag"] ) ) {
249 if ( $retval ===
null ) {
256 if ( !$done && array_key_exists(
'default', $thisSetting ) ) {
257 if ( is_array( $retval ) && is_array( $thisSetting[
'default'] ) ) {
266 $retval = $thisSetting[
'default'];
272 if ( is_string( $retval ) ) {
273 $retval = strtr( $retval, $params[
'replacements'] );
274 } elseif ( is_array( $retval ) ) {
275 foreach ( $retval as $key => $val ) {
276 if ( is_string( $val ) ) {
277 $retval[$key] = strtr( $val, $params[
'replacements'] );
293 public function getAll( $wiki, $suffix =
null, $params = [], $wikiTags = [] ) {
294 $params = $this->
mergeParams( $wiki, $suffix, $params, $wikiTags );
296 foreach ( $this->settings as $varname => $overrides ) {
299 if ( substr( $varname, 0, 1 ) ==
'+' ) {
301 $var = substr( $varname, 1 );
305 if ( $append && is_array( $value ) && is_array(
$GLOBALS[$var] ) ) {
308 if ( $value !==
null ) {
309 $localSettings[$var] = $value;
312 return $localSettings;
323 public function getBool( $setting, $wiki, $suffix =
null, $wikiTags = [] ) {
324 return (
bool)$this->
get( $setting, $wiki, $suffix, [], $wikiTags );
346 $params = [], $wikiTags = []
348 $value = $this->
get( $setting, $wiki, $suffix, $params, $wikiTags );
349 if ( $value !==
null ) {
363 $params = [], $wikiTags = []
365 $params = $this->
mergeParams( $wiki, $suffix, $params, $wikiTags );
375 $overrides = $this->settings[$setting] ??
null;
376 $value = $overrides ? $this->
processSetting( $overrides, $wiki, $params ) :
null;
377 if ( $value !==
null ) {
378 if ( substr( $setting, 0, 1 ) ==
'+' && is_array( $value ) ) {
379 $setting = substr( $setting, 1 );
380 if ( is_array(
$GLOBALS[$setting] ) ) {
401 $params = $this->
mergeParams( $wiki, $suffix, $params, $wikiTags );
402 foreach ( $this->settings as $varName => $setting ) {
423 if ( !is_callable( $this->siteParamsCallback ) ) {
428 # Validate the returned value
429 if ( !is_array( $ret ) ) {
433 foreach ( $default as $name => $def ) {
434 if ( !isset( $ret[$name] ) || ( is_array( $def ) && !is_array( $ret[$name] ) ) ) {
454 protected function mergeParams( $wiki, $suffix, array $params, array $wikiTags ) {
457 if ( $ret[
'suffix'] ===
null ) {
458 $ret[
'suffix'] = $suffix;
464 $wikiTags[] = $ret[
'suffix'];
466 $ret[
'tags'] = array_unique( array_merge( $ret[
'tags'], $wikiTags ) );
468 $ret[
'params'] += $params;
472 if ( !isset( $ret[
'params'][
'lang'] ) && $ret[
'lang'] !==
null ) {
473 $ret[
'params'][
'lang'] = $ret[
'lang'];
475 if ( !isset( $ret[
'params'][
'site'] ) && $ret[
'suffix'] !==
null ) {
476 $ret[
'params'][
'site'] = $ret[
'suffix'];
481 $ret[
'replacements'] = [];
482 foreach ( $ret[
'params'] as $key => $value ) {
483 $ret[
'replacements'][
'$' . $key ] = $value;
498 if ( $def[
'suffix'] !==
null && $def[
'lang'] !==
null ) {
499 return [ $def[
'suffix'], $def[
'lang'] ];
504 foreach ( $this->suffixes as $altSite => $suffix ) {
505 if ( $suffix ===
'' ) {
509 } elseif ( substr( $wiki, -strlen( $suffix ) ) == $suffix ) {
510 $site = is_numeric( $altSite ) ? $suffix : $altSite;
511 $lang = substr( $wiki, 0, strlen( $wiki ) - strlen( $suffix ) );
515 if (
$lang !==
null ) {
519 return [ $site,
$lang ];
538 if ( WikiMap::isCurrentWikiId( $wiki ) ) {
541 if ( !preg_match(
'/^wg[A-Z]/', $name ) ) {
542 throw new MWException(
"Variable '$name' does start with 'wg'." );
543 } elseif ( !isset(
$GLOBALS[$name] ) ) {
544 throw new MWException(
"Variable '$name' is not set." );
549 if ( isset( $this->cfgCache[$wiki] ) ) {
550 $res = array_intersect_key( $this->cfgCache[$wiki], array_flip(
$settings ) );
552 return $multi ?
$res : current(
$res );
554 } elseif ( !in_array( $wiki, $this->wikis ) ) {
557 $this->cfgCache[$wiki] = [];
559 $result = Shell::makeScriptCommand(
560 "$IP/maintenance/getConfiguration.php",
568 ->limits( [
'memory' => 0,
'filesize' => 0 ] )
571 $data = trim( $result->getStdout() );
572 if ( $result->getExitCode() || $data ===
'' ) {
573 throw new MWException(
"Failed to run getConfiguration.php: {$result->getStdout()}" );
576 if ( !is_array(
$res ) ) {
577 throw new MWException(
"Failed to unserialize configuration array." );
579 $this->cfgCache[$wiki] +=
$res;
582 return $multi ?
$res : current(
$res );
595 private static function arrayMerge( array $array1, array $array2 ) {
597 foreach ( $array2 as $key => $value ) {
598 if ( isset( $out[$key] ) ) {
599 if ( is_array( $out[$key] ) && is_array( $value ) ) {
602 } elseif ( is_numeric( $key ) ) {
610 } elseif ( $out[$key] ===
false ) {
628 if ( $this->fullLoadCallback && !$this->fullLoadDone ) {
630 $this->fullLoadDone =
true;
unserialize( $serialized)
This is a class for holding configuration settings, particularly for multi-wiki sites.
getConfig( $wiki, $settings)
Get the resolved (post-setup) configuration of a potentially foreign wiki.
extractAllGlobals( $wiki, $suffix=null, $params=[], $wikiTags=[])
Retrieves the values of all settings, and places them in their corresponding global variables.
getLocalDatabases()
Retrieves an array of local databases.
$wikis
Array of wikis, should be the same as $wgLocalDatabases.
mergeParams( $wiki, $suffix, array $params, array $wikiTags)
Merge params between the ones passed to the function and the ones given by self::$siteParamsCallback ...
siteFromDB( $wiki)
Work out the site and language name from a database name.
array $cfgCache
Configuration cache for getConfig()
string array $fullLoadCallback
Optional callback to load full configuration data.
extractGlobal( $setting, $wiki, $suffix=null, $params=[], $wikiTags=[])
Retrieves the value of a given setting, and places it in its corresponding global variable.
string array $siteParamsCallback
A callback function that returns an array with the following keys (all optional):
extractGlobalSetting( $setting, $wiki, $params)
getAll( $wiki, $suffix=null, $params=[], $wikiTags=[])
Gets all settings for a wiki.
extractVar( $setting, $wiki, $suffix, &$var, $params=[], $wikiTags=[])
Retrieves the value of a given setting, and places it in a variable passed by reference.
getWikiParams( $wiki)
Return specific settings for $wiki See the documentation of self::$siteParamsCallback for more in-dep...
getBool( $setting, $wiki, $suffix=null, $wikiTags=[])
Retrieves a configuration setting for a given wiki, forced to a boolean.
$settings
The whole array of settings.
processSetting(array $thisSetting, $wiki, array $params)
Retrieve the configuration setting for a given wiki, based on an overrides array.
static arrayMerge(array $array1, array $array2)
Merge multiple arrays together.
$fullLoadDone
Whether or not all data has been loaded.
$suffixes
Array of suffixes, for self::siteFromDB()
if(!isset( $args[0])) $lang