2004-05-23 15:00:46 +00:00
|
|
|
<?php
|
2012-05-10 15:51:44 +00:00
|
|
|
/**
|
|
|
|
|
* Configuration holder, particularly for multi-wiki sites.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
2018-01-19 01:44:03 +00:00
|
|
|
use MediaWiki\Shell\Shell;
|
|
|
|
|
|
2007-04-24 06:53:31 +00:00
|
|
|
/**
|
2012-05-28 21:06:57 +00:00
|
|
|
* This is a class for holding configuration settings, particularly for
|
|
|
|
|
* multi-wiki sites.
|
|
|
|
|
*
|
|
|
|
|
* A basic synopsis:
|
|
|
|
|
*
|
|
|
|
|
* Consider a wikifarm having three sites: two production sites, one in English
|
|
|
|
|
* and one in German, and one testing site. You can assign them easy-to-remember
|
|
|
|
|
* identifiers - ISO 639 codes 'en' and 'de' for language wikis, and 'beta' for
|
|
|
|
|
* the testing wiki.
|
|
|
|
|
*
|
|
|
|
|
* You would thus initialize the site configuration by specifying the wiki
|
|
|
|
|
* identifiers:
|
|
|
|
|
*
|
|
|
|
|
* @code
|
|
|
|
|
* $conf = new SiteConfiguration;
|
2016-07-25 01:25:09 +00:00
|
|
|
* $conf->wikis = [ 'de', 'en', 'beta' ];
|
2012-05-28 21:06:57 +00:00
|
|
|
* @endcode
|
|
|
|
|
*
|
|
|
|
|
* When configuring the MediaWiki global settings (the $wg variables),
|
|
|
|
|
* the identifiers will be available to specify settings on a per wiki basis.
|
|
|
|
|
*
|
|
|
|
|
* @code
|
2016-07-25 01:25:09 +00:00
|
|
|
* $conf->settings = [
|
|
|
|
|
* 'wgSomeSetting' => [
|
2012-05-28 21:06:57 +00:00
|
|
|
*
|
|
|
|
|
* # production:
|
|
|
|
|
* 'de' => false,
|
|
|
|
|
* 'en' => false,
|
|
|
|
|
*
|
|
|
|
|
* # test:
|
|
|
|
|
* 'beta => true,
|
2016-07-25 01:25:09 +00:00
|
|
|
* ],
|
|
|
|
|
* ];
|
2012-05-28 21:06:57 +00:00
|
|
|
* @endcode
|
|
|
|
|
*
|
|
|
|
|
* With three wikis, that is easy to manage. But what about a farm with
|
|
|
|
|
* hundreds of wikis? Site configuration provides a special keyword named
|
|
|
|
|
* 'default' which is the value used when a wiki is not found. Hence
|
|
|
|
|
* the above code could be written:
|
|
|
|
|
*
|
|
|
|
|
* @code
|
2016-07-25 01:25:09 +00:00
|
|
|
* $conf->settings = [
|
|
|
|
|
* 'wgSomeSetting' => [
|
2012-05-28 21:06:57 +00:00
|
|
|
*
|
|
|
|
|
* 'default' => false,
|
|
|
|
|
*
|
|
|
|
|
* # Enable feature on test
|
|
|
|
|
* 'beta' => true,
|
2016-07-25 01:25:09 +00:00
|
|
|
* ],
|
|
|
|
|
* ];
|
2012-05-28 21:06:57 +00:00
|
|
|
* @endcode
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* Since settings can contain arrays, site configuration provides a way
|
|
|
|
|
* to merge an array with the default. This is very useful to avoid
|
|
|
|
|
* repeating settings again and again while still maintaining specific changes
|
|
|
|
|
* on a per wiki basis.
|
|
|
|
|
*
|
|
|
|
|
* @code
|
2016-07-25 01:25:09 +00:00
|
|
|
* $conf->settings = [
|
|
|
|
|
* 'wgMergeSetting' = [
|
2012-05-28 21:06:57 +00:00
|
|
|
* # Value that will be shared among all wikis:
|
2016-07-25 01:25:09 +00:00
|
|
|
* 'default' => [ NS_USER => true ],
|
2012-05-28 21:06:57 +00:00
|
|
|
*
|
|
|
|
|
* # Leading '+' means merging the array of value with the defaults
|
2016-07-25 01:25:09 +00:00
|
|
|
* '+beta' => [ NS_HELP => true ],
|
|
|
|
|
* ],
|
|
|
|
|
* ];
|
2012-05-28 21:06:57 +00:00
|
|
|
*
|
|
|
|
|
* # Get configuration for the German site:
|
|
|
|
|
* $conf->get( 'wgMergeSetting', 'de' );
|
2016-07-25 01:25:09 +00:00
|
|
|
* // --> [ NS_USER => true ];
|
2012-05-28 21:06:57 +00:00
|
|
|
*
|
|
|
|
|
* # Get configuration for the testing site:
|
|
|
|
|
* $conf->get( 'wgMergeSetting', 'beta' );
|
2016-07-25 01:25:09 +00:00
|
|
|
* // --> [ NS_USER => true, NS_HELP => true ];
|
2012-05-28 21:06:57 +00:00
|
|
|
* @endcode
|
|
|
|
|
*
|
|
|
|
|
* Finally, to load all configuration settings, extract them in global context:
|
|
|
|
|
*
|
|
|
|
|
* @code
|
|
|
|
|
* # Name / identifier of the wiki as set in $conf->wikis
|
|
|
|
|
* $wikiID = 'beta';
|
|
|
|
|
* $globals = $conf->getAll( $wikiID );
|
|
|
|
|
* extract( $globals );
|
|
|
|
|
* @endcode
|
|
|
|
|
*
|
2016-07-06 18:01:03 +00:00
|
|
|
* @note For WikiMap to function, the configuration must define string values for
|
|
|
|
|
* $wgServer (or $wgCanonicalServer) and $wgArticlePath, even if these are the
|
|
|
|
|
* same for all wikis or can be correctly determined by the logic in
|
|
|
|
|
* Setup.php.
|
|
|
|
|
*
|
2014-05-11 15:33:33 +00:00
|
|
|
* @todo Give examples for,
|
2012-05-28 21:06:57 +00:00
|
|
|
* suffixes:
|
2016-07-25 01:25:09 +00:00
|
|
|
* $conf->suffixes = [ 'wiki' ];
|
2012-05-28 21:06:57 +00:00
|
|
|
* localVHosts
|
|
|
|
|
* callbacks!
|
2007-04-24 06:53:31 +00:00
|
|
|
*/
|
2004-05-23 15:00:46 +00:00
|
|
|
class SiteConfiguration {
|
2008-08-26 15:10:12 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Array of suffixes, for self::siteFromDB()
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
public $suffixes = [];
|
2008-08-26 15:10:12 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Array of wikis, should be the same as $wgLocalDatabases
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
public $wikis = [];
|
2008-08-26 15:10:12 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The whole array of settings
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
public $settings = [];
|
2008-08-26 15:10:12 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Array of domains that are local and can be handled by the same server
|
2014-09-01 20:26:36 +00:00
|
|
|
*
|
|
|
|
|
* @deprecated since 1.25; use $wgLocalVirtualHosts instead.
|
2008-08-26 15:10:12 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
public $localVHosts = [];
|
2011-07-28 07:27:40 +00:00
|
|
|
|
2009-06-02 09:49:31 +00:00
|
|
|
/**
|
|
|
|
|
* Optional callback to load full configuration data.
|
2012-07-05 22:53:57 +00:00
|
|
|
* @var string|array
|
2009-06-02 09:49:31 +00:00
|
|
|
*/
|
|
|
|
|
public $fullLoadCallback = null;
|
2011-07-28 07:27:40 +00:00
|
|
|
|
2009-06-02 09:49:31 +00:00
|
|
|
/** Whether or not all data has been loaded */
|
|
|
|
|
public $fullLoadDone = false;
|
2008-08-26 15:10:12 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A callback function that returns an array with the following keys (all
|
|
|
|
|
* optional):
|
|
|
|
|
* - suffix: site's suffix
|
|
|
|
|
* - lang: site's lang
|
|
|
|
|
* - tags: array of wiki tags
|
|
|
|
|
* - params: array of parameters to be replaced
|
|
|
|
|
* The function will receive the SiteConfiguration instance in the first
|
|
|
|
|
* argument and the wiki in the second one.
|
|
|
|
|
* if suffix and lang are passed they will be used for the return value of
|
|
|
|
|
* self::siteFromDB() and self::$suffixes will be ignored
|
2012-07-05 22:53:57 +00:00
|
|
|
*
|
|
|
|
|
* @var string|array
|
2008-08-26 15:10:12 +00:00
|
|
|
*/
|
|
|
|
|
public $siteParamsCallback = null;
|
2005-07-05 21:22:25 +00:00
|
|
|
|
2013-02-04 00:28:52 +00:00
|
|
|
/**
|
|
|
|
|
* Configuration cache for getConfig()
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $cfgCache = [];
|
2013-02-04 00:28:52 +00:00
|
|
|
|
2008-08-18 09:26:48 +00:00
|
|
|
/**
|
|
|
|
|
* Retrieves a configuration setting for a given wiki.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $settingName ID of the setting name to retrieve
|
|
|
|
|
* @param string $wiki Wiki ID of the wiki in question.
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param string|null $suffix The suffix of the wiki in question.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params List of parameters. $.'key' is replaced by $value in all returned data.
|
|
|
|
|
* @param array $wikiTags The tags assigned to the wiki.
|
2014-04-22 11:07:02 +00:00
|
|
|
* @return mixed The value of the setting requested.
|
2008-08-18 09:26:48 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
public function get( $settingName, $wiki, $suffix = null, $params = [],
|
|
|
|
|
$wikiTags = []
|
2014-05-11 15:33:33 +00:00
|
|
|
) {
|
2008-08-26 15:10:12 +00:00
|
|
|
$params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
|
|
|
|
|
return $this->getSetting( $settingName, $wiki, $params );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Really retrieves a configuration setting for a given wiki.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $settingName ID of the setting name to retrieve.
|
|
|
|
|
* @param string $wiki Wiki ID of the wiki in question.
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param array $params Array of parameters.
|
|
|
|
|
* @return mixed The value of the setting requested.
|
2008-08-26 15:10:12 +00:00
|
|
|
*/
|
2014-07-03 19:48:08 +00:00
|
|
|
protected function getSetting( $settingName, $wiki, array $params ) {
|
2008-08-26 15:10:12 +00:00
|
|
|
$retval = null;
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( array_key_exists( $settingName, $this->settings ) ) {
|
2008-02-06 15:56:25 +00:00
|
|
|
$thisSetting =& $this->settings[$settingName];
|
|
|
|
|
do {
|
2008-08-18 09:26:48 +00:00
|
|
|
// Do individual wiki settings
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( array_key_exists( $wiki, $thisSetting ) ) {
|
2008-02-06 15:56:25 +00:00
|
|
|
$retval = $thisSetting[$wiki];
|
|
|
|
|
break;
|
2012-07-05 22:53:57 +00:00
|
|
|
} elseif ( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
|
2008-08-18 09:26:48 +00:00
|
|
|
$retval = $thisSetting["+$wiki"];
|
2008-02-06 15:56:25 +00:00
|
|
|
}
|
2008-08-26 15:10:12 +00:00
|
|
|
|
2008-08-18 09:26:48 +00:00
|
|
|
// Do tag settings
|
2013-04-20 22:49:30 +00:00
|
|
|
foreach ( $params['tags'] as $tag ) {
|
|
|
|
|
if ( array_key_exists( $tag, $thisSetting ) ) {
|
2014-07-04 19:20:22 +00:00
|
|
|
if ( is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
|
2008-08-26 15:10:12 +00:00
|
|
|
$retval = self::arrayMerge( $retval, $thisSetting[$tag] );
|
2008-08-18 09:26:48 +00:00
|
|
|
} else {
|
|
|
|
|
$retval = $thisSetting[$tag];
|
|
|
|
|
}
|
2008-02-06 15:56:25 +00:00
|
|
|
break 2;
|
2013-04-20 22:49:30 +00:00
|
|
|
} elseif ( array_key_exists( "+$tag", $thisSetting ) && is_array( $thisSetting["+$tag"] ) ) {
|
2014-07-04 19:20:22 +00:00
|
|
|
if ( $retval === null ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$retval = [];
|
2012-07-05 22:53:57 +00:00
|
|
|
}
|
2008-08-26 15:10:12 +00:00
|
|
|
$retval = self::arrayMerge( $retval, $thisSetting["+$tag"] );
|
2008-02-06 15:56:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-08-18 09:26:48 +00:00
|
|
|
// Do suffix settings
|
2008-08-26 15:10:12 +00:00
|
|
|
$suffix = $params['suffix'];
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( !is_null( $suffix ) ) {
|
|
|
|
|
if ( array_key_exists( $suffix, $thisSetting ) ) {
|
2014-07-04 19:20:22 +00:00
|
|
|
if ( is_array( $retval ) && is_array( $thisSetting[$suffix] ) ) {
|
2008-08-26 15:10:12 +00:00
|
|
|
$retval = self::arrayMerge( $retval, $thisSetting[$suffix] );
|
|
|
|
|
} else {
|
|
|
|
|
$retval = $thisSetting[$suffix];
|
|
|
|
|
}
|
|
|
|
|
break;
|
2014-05-11 15:33:33 +00:00
|
|
|
} elseif ( array_key_exists( "+$suffix", $thisSetting )
|
|
|
|
|
&& is_array( $thisSetting["+$suffix"] )
|
|
|
|
|
) {
|
2014-07-04 19:20:22 +00:00
|
|
|
if ( $retval === null ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$retval = [];
|
2012-07-05 22:53:57 +00:00
|
|
|
}
|
2008-08-26 15:10:12 +00:00
|
|
|
$retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] );
|
2008-08-18 09:26:48 +00:00
|
|
|
}
|
2008-02-06 15:56:25 +00:00
|
|
|
}
|
2008-08-26 15:10:12 +00:00
|
|
|
|
2008-08-18 09:26:48 +00:00
|
|
|
// Fall back to default.
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( array_key_exists( 'default', $thisSetting ) ) {
|
|
|
|
|
if ( is_array( $retval ) && is_array( $thisSetting['default'] ) ) {
|
2008-08-26 15:10:12 +00:00
|
|
|
$retval = self::arrayMerge( $retval, $thisSetting['default'] );
|
2008-08-18 09:26:48 +00:00
|
|
|
} else {
|
|
|
|
|
$retval = $thisSetting['default'];
|
|
|
|
|
}
|
2008-02-06 15:56:25 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} while ( false );
|
2004-05-23 15:00:46 +00:00
|
|
|
}
|
2005-08-14 07:22:36 +00:00
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( !is_null( $retval ) && count( $params['params'] ) ) {
|
2008-08-26 15:10:12 +00:00
|
|
|
foreach ( $params['params'] as $key => $value ) {
|
2008-01-19 06:06:49 +00:00
|
|
|
$retval = $this->doReplace( '$' . $key, $value, $retval );
|
2004-05-23 15:00:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-05-24 03:59:38 +00:00
|
|
|
return $retval;
|
2004-05-23 15:00:46 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-08-26 15:10:12 +00:00
|
|
|
/**
|
|
|
|
|
* Type-safe string replace; won't do replacements on non-strings
|
|
|
|
|
* private?
|
2011-05-21 20:06:57 +00:00
|
|
|
*
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $from
|
|
|
|
|
* @param string $to
|
|
|
|
|
* @param string|array $in
|
2016-12-08 05:04:53 +00:00
|
|
|
* @return string|array
|
2008-08-26 15:10:12 +00:00
|
|
|
*/
|
2008-01-19 06:06:49 +00:00
|
|
|
function doReplace( $from, $to, $in ) {
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( is_string( $in ) ) {
|
2008-01-19 06:06:49 +00:00
|
|
|
return str_replace( $from, $to, $in );
|
2013-04-20 22:49:30 +00:00
|
|
|
} elseif ( is_array( $in ) ) {
|
|
|
|
|
foreach ( $in as $key => $val ) {
|
2008-01-19 06:26:39 +00:00
|
|
|
$in[$key] = $this->doReplace( $from, $to, $val );
|
2008-01-19 06:06:49 +00:00
|
|
|
}
|
|
|
|
|
return $in;
|
|
|
|
|
} else {
|
|
|
|
|
return $in;
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-05-23 15:00:46 +00:00
|
|
|
|
2008-08-18 09:26:48 +00:00
|
|
|
/**
|
|
|
|
|
* Gets all settings for a wiki
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $wiki Wiki ID of the wiki in question.
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param string|null $suffix The suffix of the wiki in question.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params List of parameters. $.'key' is replaced by $value in all returned data.
|
|
|
|
|
* @param array $wikiTags The tags assigned to the wiki.
|
2014-04-22 11:07:02 +00:00
|
|
|
* @return array Array of settings requested.
|
2008-08-18 09:26:48 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
public function getAll( $wiki, $suffix = null, $params = [], $wikiTags = [] ) {
|
2008-08-26 15:10:12 +00:00
|
|
|
$params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
|
2016-02-17 09:09:32 +00:00
|
|
|
$localSettings = [];
|
2013-04-20 22:49:30 +00:00
|
|
|
foreach ( $this->settings as $varname => $stuff ) {
|
2008-08-20 14:12:52 +00:00
|
|
|
$append = false;
|
|
|
|
|
$var = $varname;
|
|
|
|
|
if ( substr( $varname, 0, 1 ) == '+' ) {
|
|
|
|
|
$append = true;
|
|
|
|
|
$var = substr( $varname, 1 );
|
|
|
|
|
}
|
2008-08-26 15:10:12 +00:00
|
|
|
|
|
|
|
|
$value = $this->getSetting( $varname, $wiki, $params );
|
2012-07-05 22:53:57 +00:00
|
|
|
if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) ) {
|
2008-08-26 15:10:12 +00:00
|
|
|
$value = self::arrayMerge( $value, $GLOBALS[$var] );
|
2012-07-05 22:53:57 +00:00
|
|
|
}
|
2006-01-07 13:09:30 +00:00
|
|
|
if ( !is_null( $value ) ) {
|
2008-08-20 12:56:59 +00:00
|
|
|
$localSettings[$var] = $value;
|
2006-01-04 22:48:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $localSettings;
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-18 09:26:48 +00:00
|
|
|
/**
|
|
|
|
|
* Retrieves a configuration setting for a given wiki, forced to a boolean.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $setting ID of the setting name to retrieve
|
|
|
|
|
* @param string $wiki Wiki ID of the wiki in question.
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param string|null $suffix The suffix of the wiki in question.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $wikiTags The tags assigned to the wiki.
|
2008-08-18 09:26:48 +00:00
|
|
|
* @return bool The value of the setting requested.
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
public function getBool( $setting, $wiki, $suffix = null, $wikiTags = [] ) {
|
|
|
|
|
return (bool)$this->get( $setting, $wiki, $suffix, [], $wikiTags );
|
2004-05-23 15:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-21 20:06:57 +00:00
|
|
|
/**
|
|
|
|
|
* Retrieves an array of local databases
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2004-05-23 15:00:46 +00:00
|
|
|
function &getLocalDatabases() {
|
2006-01-04 22:48:35 +00:00
|
|
|
return $this->wikis;
|
2004-05-23 15:00:46 +00:00
|
|
|
}
|
2005-07-05 21:22:25 +00:00
|
|
|
|
2008-08-18 09:26:48 +00:00
|
|
|
/**
|
|
|
|
|
* Retrieves the value of a given setting, and places it in a variable passed by reference.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $setting ID of the setting name to retrieve
|
|
|
|
|
* @param string $wiki Wiki ID of the wiki in question.
|
|
|
|
|
* @param string $suffix The suffix of the wiki in question.
|
2017-08-11 00:23:16 +00:00
|
|
|
* @param array &$var Reference The variable to insert the value into.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params List of parameters. $.'key' is replaced by $value in all returned data.
|
|
|
|
|
* @param array $wikiTags The tags assigned to the wiki.
|
2008-08-18 09:26:48 +00:00
|
|
|
*/
|
2014-05-11 15:33:33 +00:00
|
|
|
public function extractVar( $setting, $wiki, $suffix, &$var,
|
2016-02-17 09:09:32 +00:00
|
|
|
$params = [], $wikiTags = []
|
2014-05-11 15:33:33 +00:00
|
|
|
) {
|
2008-02-06 15:56:25 +00:00
|
|
|
$value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
|
2004-05-23 15:00:46 +00:00
|
|
|
if ( !is_null( $value ) ) {
|
|
|
|
|
$var = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-07-05 21:22:25 +00:00
|
|
|
|
2008-08-18 09:26:48 +00:00
|
|
|
/**
|
|
|
|
|
* Retrieves the value of a given setting, and places it in its corresponding global variable.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $setting ID of the setting name to retrieve
|
|
|
|
|
* @param string $wiki Wiki ID of the wiki in question.
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param string|null $suffix The suffix of the wiki in question.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params List of parameters. $.'key' is replaced by $value in all returned data.
|
|
|
|
|
* @param array $wikiTags The tags assigned to the wiki.
|
2008-08-18 09:26:48 +00:00
|
|
|
*/
|
2014-05-11 15:33:33 +00:00
|
|
|
public function extractGlobal( $setting, $wiki, $suffix = null,
|
2016-02-17 09:09:32 +00:00
|
|
|
$params = [], $wikiTags = []
|
2014-05-11 15:33:33 +00:00
|
|
|
) {
|
2008-08-26 15:10:12 +00:00
|
|
|
$params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
|
|
|
|
|
$this->extractGlobalSetting( $setting, $wiki, $params );
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-21 20:06:57 +00:00
|
|
|
/**
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $setting
|
|
|
|
|
* @param string $wiki
|
|
|
|
|
* @param array $params
|
2011-05-21 20:06:57 +00:00
|
|
|
*/
|
2008-08-26 15:10:12 +00:00
|
|
|
public function extractGlobalSetting( $setting, $wiki, $params ) {
|
|
|
|
|
$value = $this->getSetting( $setting, $wiki, $params );
|
2004-05-23 15:00:46 +00:00
|
|
|
if ( !is_null( $value ) ) {
|
2013-01-26 18:32:03 +00:00
|
|
|
if ( substr( $setting, 0, 1 ) == '+' && is_array( $value ) ) {
|
|
|
|
|
$setting = substr( $setting, 1 );
|
|
|
|
|
if ( is_array( $GLOBALS[$setting] ) ) {
|
2008-08-26 15:10:12 +00:00
|
|
|
$GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value );
|
2008-08-20 14:12:52 +00:00
|
|
|
} else {
|
|
|
|
|
$GLOBALS[$setting] = $value;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$GLOBALS[$setting] = $value;
|
|
|
|
|
}
|
2004-05-23 15:00:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-18 09:26:48 +00:00
|
|
|
/**
|
|
|
|
|
* Retrieves the values of all settings, and places them in their corresponding global variables.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $wiki Wiki ID of the wiki in question.
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param string|null $suffix The suffix of the wiki in question.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params List of parameters. $.'key' is replaced by $value in all returned data.
|
|
|
|
|
* @param array $wikiTags The tags assigned to the wiki.
|
2008-08-18 09:26:48 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
public function extractAllGlobals( $wiki, $suffix = null, $params = [],
|
|
|
|
|
$wikiTags = []
|
2014-05-11 15:33:33 +00:00
|
|
|
) {
|
2008-08-26 15:10:12 +00:00
|
|
|
$params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
|
2004-05-24 03:59:38 +00:00
|
|
|
foreach ( $this->settings as $varName => $setting ) {
|
2008-08-26 15:10:12 +00:00
|
|
|
$this->extractGlobalSetting( $varName, $wiki, $params );
|
2004-05-23 15:00:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-08-14 06:03:10 +00:00
|
|
|
|
2008-08-26 15:10:12 +00:00
|
|
|
/**
|
|
|
|
|
* Return specific settings for $wiki
|
|
|
|
|
* See the documentation of self::$siteParamsCallback for more in-depth
|
|
|
|
|
* documentation about this function
|
|
|
|
|
*
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $wiki
|
2008-08-26 15:10:12 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2012-12-20 15:09:25 +00:00
|
|
|
protected function getWikiParams( $wiki ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
static $default = [
|
2008-08-26 15:10:12 +00:00
|
|
|
'suffix' => null,
|
|
|
|
|
'lang' => null,
|
2016-02-17 09:09:32 +00:00
|
|
|
'tags' => [],
|
|
|
|
|
'params' => [],
|
|
|
|
|
];
|
2008-08-26 15:10:12 +00:00
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( !is_callable( $this->siteParamsCallback ) ) {
|
2008-08-26 15:10:12 +00:00
|
|
|
return $default;
|
2011-05-21 20:06:57 +00:00
|
|
|
}
|
2008-08-26 15:10:12 +00:00
|
|
|
|
2018-06-09 23:26:32 +00:00
|
|
|
$ret = ( $this->siteParamsCallback )( $this, $wiki );
|
2008-08-26 15:10:12 +00:00
|
|
|
# Validate the returned value
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( !is_array( $ret ) ) {
|
2008-08-26 15:10:12 +00:00
|
|
|
return $default;
|
2010-11-30 18:44:50 +00:00
|
|
|
}
|
2008-08-26 15:10:12 +00:00
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
foreach ( $default as $name => $def ) {
|
2019-12-29 11:14:00 +00:00
|
|
|
if ( !isset( $ret[$name] ) || ( is_array( $def ) && !is_array( $ret[$name] ) ) ) {
|
|
|
|
|
$ret[$name] = $def;
|
2012-07-05 22:53:57 +00:00
|
|
|
}
|
2008-08-26 15:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-07-28 07:27:40 +00:00
|
|
|
* Merge params between the ones passed to the function and the ones given
|
2008-08-26 15:10:12 +00:00
|
|
|
* by self::$siteParamsCallback for backward compatibility
|
|
|
|
|
* Values returned by self::getWikiParams() have the priority.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $wiki Wiki ID of the wiki in question.
|
|
|
|
|
* @param string $suffix The suffix of the wiki in question.
|
|
|
|
|
* @param array $params List of parameters. $.'key' is replaced by $value in
|
2014-04-22 11:07:02 +00:00
|
|
|
* all returned data.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $wikiTags The tags assigned to the wiki.
|
2008-08-26 15:10:12 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2014-07-03 19:48:08 +00:00
|
|
|
protected function mergeParams( $wiki, $suffix, array $params, array $wikiTags ) {
|
2008-08-26 15:10:12 +00:00
|
|
|
$ret = $this->getWikiParams( $wiki );
|
|
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( is_null( $ret['suffix'] ) ) {
|
2008-08-26 15:10:12 +00:00
|
|
|
$ret['suffix'] = $suffix;
|
2012-07-05 22:53:57 +00:00
|
|
|
}
|
2008-08-26 15:10:12 +00:00
|
|
|
|
|
|
|
|
$ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
|
|
|
|
|
|
|
|
|
|
$ret['params'] += $params;
|
|
|
|
|
|
|
|
|
|
// Automatically fill that ones if needed
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) ) {
|
2008-08-26 15:10:12 +00:00
|
|
|
$ret['params']['lang'] = $ret['lang'];
|
2012-07-05 22:53:57 +00:00
|
|
|
}
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) ) {
|
2008-08-26 15:10:12 +00:00
|
|
|
$ret['params']['site'] = $ret['suffix'];
|
2012-07-05 22:53:57 +00:00
|
|
|
}
|
2008-08-26 15:10:12 +00:00
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Work out the site and language name from a database name
|
2018-10-13 07:29:23 +00:00
|
|
|
* @param string $wiki Wiki ID
|
2011-05-21 20:06:57 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2018-10-13 07:29:23 +00:00
|
|
|
public function siteFromDB( $wiki ) {
|
2008-08-26 15:10:12 +00:00
|
|
|
// Allow override
|
2018-10-13 07:29:23 +00:00
|
|
|
$def = $this->getWikiParams( $wiki );
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $def['suffix'], $def['lang'] ];
|
2012-07-05 22:53:57 +00:00
|
|
|
}
|
2008-08-26 15:10:12 +00:00
|
|
|
|
|
|
|
|
$site = null;
|
|
|
|
|
$lang = null;
|
2013-06-01 15:52:26 +00:00
|
|
|
foreach ( $this->suffixes as $altSite => $suffix ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $suffix === '' ) {
|
|
|
|
|
$site = '';
|
2018-10-13 07:29:23 +00:00
|
|
|
$lang = $wiki;
|
2008-03-30 09:48:15 +00:00
|
|
|
break;
|
2018-10-13 07:29:23 +00:00
|
|
|
} elseif ( substr( $wiki, -strlen( $suffix ) ) == $suffix ) {
|
2013-06-01 15:52:26 +00:00
|
|
|
$site = is_numeric( $altSite ) ? $suffix : $altSite;
|
2018-10-13 07:29:23 +00:00
|
|
|
$lang = substr( $wiki, 0, strlen( $wiki ) - strlen( $suffix ) );
|
2004-08-14 06:03:10 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-07-11 00:47:00 +00:00
|
|
|
$lang = str_replace( '_', '-', $lang );
|
2018-10-13 07:29:23 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $site, $lang ];
|
2004-08-14 06:03:10 +00:00
|
|
|
}
|
2005-06-26 06:34:13 +00:00
|
|
|
|
2013-02-04 00:28:52 +00:00
|
|
|
/**
|
|
|
|
|
* Get the resolved (post-setup) configuration of a potentially foreign wiki.
|
|
|
|
|
* For foreign wikis, this is expensive, and only works if maintenance
|
|
|
|
|
* scripts are setup to handle the --wiki parameter such as in wiki farms.
|
|
|
|
|
*
|
|
|
|
|
* @param string $wiki
|
|
|
|
|
* @param array|string $settings A setting name or array of setting names
|
2014-04-23 11:39:49 +00:00
|
|
|
* @return mixed|mixed[] Array if $settings is an array, otherwise the value
|
2013-02-04 00:28:52 +00:00
|
|
|
* @throws MWException
|
|
|
|
|
* @since 1.21
|
|
|
|
|
*/
|
|
|
|
|
public function getConfig( $wiki, $settings ) {
|
|
|
|
|
global $IP;
|
|
|
|
|
|
|
|
|
|
$multi = is_array( $settings );
|
|
|
|
|
$settings = (array)$settings;
|
2018-10-16 02:18:16 +00:00
|
|
|
if ( WikiMap::isCurrentWikiId( $wiki ) ) { // $wiki is this wiki
|
2016-02-17 09:09:32 +00:00
|
|
|
$res = [];
|
2013-02-04 00:28:52 +00:00
|
|
|
foreach ( $settings as $name ) {
|
|
|
|
|
if ( !preg_match( '/^wg[A-Z]/', $name ) ) {
|
|
|
|
|
throw new MWException( "Variable '$name' does start with 'wg'." );
|
|
|
|
|
} elseif ( !isset( $GLOBALS[$name] ) ) {
|
|
|
|
|
throw new MWException( "Variable '$name' is not set." );
|
|
|
|
|
}
|
|
|
|
|
$res[$name] = $GLOBALS[$name];
|
|
|
|
|
}
|
|
|
|
|
} else { // $wiki is a foreign wiki
|
|
|
|
|
if ( isset( $this->cfgCache[$wiki] ) ) {
|
|
|
|
|
$res = array_intersect_key( $this->cfgCache[$wiki], array_flip( $settings ) );
|
|
|
|
|
if ( count( $res ) == count( $settings ) ) {
|
2014-09-06 21:42:02 +00:00
|
|
|
return $multi ? $res : current( $res ); // cache hit
|
2013-02-04 00:28:52 +00:00
|
|
|
}
|
|
|
|
|
} elseif ( !in_array( $wiki, $this->wikis ) ) {
|
|
|
|
|
throw new MWException( "No such wiki '$wiki'." );
|
|
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->cfgCache[$wiki] = [];
|
2013-02-04 00:28:52 +00:00
|
|
|
}
|
2018-01-19 01:44:03 +00:00
|
|
|
$result = Shell::makeScriptCommand(
|
2013-02-04 00:28:52 +00:00
|
|
|
"$IP/maintenance/getConfiguration.php",
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2013-02-04 00:28:52 +00:00
|
|
|
'--wiki', $wiki,
|
|
|
|
|
'--settings', implode( ' ', $settings ),
|
2018-01-19 01:44:03 +00:00
|
|
|
'--format', 'PHP',
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
2018-01-19 01:44:03 +00:00
|
|
|
)
|
|
|
|
|
// limit.sh breaks this call
|
|
|
|
|
->limits( [ 'memory' => 0, 'filesize' => 0 ] )
|
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
|
|
$data = trim( $result->getStdout() );
|
2019-03-27 10:13:08 +00:00
|
|
|
if ( $result->getExitCode() || $data === '' ) {
|
2018-01-19 01:44:03 +00:00
|
|
|
throw new MWException( "Failed to run getConfiguration.php: {$result->getStdout()}" );
|
2013-02-04 00:28:52 +00:00
|
|
|
}
|
|
|
|
|
$res = unserialize( $data );
|
|
|
|
|
if ( !is_array( $res ) ) {
|
|
|
|
|
throw new MWException( "Failed to unserialize configuration array." );
|
|
|
|
|
}
|
|
|
|
|
$this->cfgCache[$wiki] = $this->cfgCache[$wiki] + $res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $multi ? $res : current( $res );
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-26 15:10:12 +00:00
|
|
|
/**
|
|
|
|
|
* Merge multiple arrays together.
|
|
|
|
|
* On encountering duplicate keys, merge the two, but ONLY if they're arrays.
|
|
|
|
|
* PHP's array_merge_recursive() merges ANY duplicate values into arrays,
|
|
|
|
|
* which is not fun
|
2011-05-21 20:06:57 +00:00
|
|
|
*
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param array $array1
|
Get rid of unnecessary func_get_args() and friends
HHVM does not support variadic arguments with type hints. This is
mostly not a big problem, because we can just drop the type hint, but
for some reason PHPUnit adds a type hint of "array" when it creates
mocks, so a class with a variadic method can't be mocked (at least in
some cases). As such, I left alone all the classes that seem like
someone might like to mock them, like Title and User. If anyone wants
to mock them in the future, they'll have to switch back to
func_get_args(). Some of the changes are definitely safe, like
functions and test classes.
In most cases, func_get_args() (and/or func_get_arg(), func_num_args() )
were only present because the code was written before we required PHP
5.6, and writing them as variadic functions is strictly superior. In
some cases I left them alone, aside from HHVM compatibility:
* Forwarding all arguments to another function. It's useful to keep
func_get_args() here where we want to keep the list of expected
arguments and their meanings in the function signature line for
documentation purposes, but don't want to copy-paste a long line of
argument names.
* Handling deprecated calling conventions.
* One or two miscellaneous cases where we're basically using the
arguments individually but want to use them as an array as well for
some reason.
Change-Id: I066ec95a7beb7c0665146195a08e7cce1222c788
2018-10-08 14:10:45 +00:00
|
|
|
* @param array ...$arrays
|
2011-05-21 20:06:57 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
2008-08-26 15:10:12 +00:00
|
|
|
*/
|
Get rid of unnecessary func_get_args() and friends
HHVM does not support variadic arguments with type hints. This is
mostly not a big problem, because we can just drop the type hint, but
for some reason PHPUnit adds a type hint of "array" when it creates
mocks, so a class with a variadic method can't be mocked (at least in
some cases). As such, I left alone all the classes that seem like
someone might like to mock them, like Title and User. If anyone wants
to mock them in the future, they'll have to switch back to
func_get_args(). Some of the changes are definitely safe, like
functions and test classes.
In most cases, func_get_args() (and/or func_get_arg(), func_num_args() )
were only present because the code was written before we required PHP
5.6, and writing them as variadic functions is strictly superior. In
some cases I left them alone, aside from HHVM compatibility:
* Forwarding all arguments to another function. It's useful to keep
func_get_args() here where we want to keep the list of expected
arguments and their meanings in the function signature line for
documentation purposes, but don't want to copy-paste a long line of
argument names.
* Handling deprecated calling conventions.
* One or two miscellaneous cases where we're basically using the
arguments individually but want to use them as an array as well for
some reason.
Change-Id: I066ec95a7beb7c0665146195a08e7cce1222c788
2018-10-08 14:10:45 +00:00
|
|
|
static function arrayMerge( array $array1, ...$arrays ) {
|
2008-08-26 15:10:12 +00:00
|
|
|
$out = $array1;
|
Get rid of unnecessary func_get_args() and friends
HHVM does not support variadic arguments with type hints. This is
mostly not a big problem, because we can just drop the type hint, but
for some reason PHPUnit adds a type hint of "array" when it creates
mocks, so a class with a variadic method can't be mocked (at least in
some cases). As such, I left alone all the classes that seem like
someone might like to mock them, like Title and User. If anyone wants
to mock them in the future, they'll have to switch back to
func_get_args(). Some of the changes are definitely safe, like
functions and test classes.
In most cases, func_get_args() (and/or func_get_arg(), func_num_args() )
were only present because the code was written before we required PHP
5.6, and writing them as variadic functions is strictly superior. In
some cases I left them alone, aside from HHVM compatibility:
* Forwarding all arguments to another function. It's useful to keep
func_get_args() here where we want to keep the list of expected
arguments and their meanings in the function signature line for
documentation purposes, but don't want to copy-paste a long line of
argument names.
* Handling deprecated calling conventions.
* One or two miscellaneous cases where we're basically using the
arguments individually but want to use them as an array as well for
some reason.
Change-Id: I066ec95a7beb7c0665146195a08e7cce1222c788
2018-10-08 14:10:45 +00:00
|
|
|
foreach ( $arrays as $array ) {
|
|
|
|
|
foreach ( $array as $key => $value ) {
|
2013-02-03 20:05:24 +00:00
|
|
|
if ( isset( $out[$key] ) && is_array( $out[$key] ) && is_array( $value ) ) {
|
2008-08-26 15:10:12 +00:00
|
|
|
$out[$key] = self::arrayMerge( $out[$key], $value );
|
2013-02-03 20:05:24 +00:00
|
|
|
} elseif ( !isset( $out[$key] ) || !$out[$key] && !is_numeric( $key ) ) {
|
2014-05-11 15:33:33 +00:00
|
|
|
// Values that evaluate to true given precedence, for the
|
|
|
|
|
// primary purpose of merging permissions arrays.
|
2008-08-26 15:10:12 +00:00
|
|
|
$out[$key] = $value;
|
|
|
|
|
} elseif ( is_numeric( $key ) ) {
|
|
|
|
|
$out[] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
2011-05-21 20:06:57 +00:00
|
|
|
|
2009-06-02 09:49:31 +00:00
|
|
|
public function loadFullData() {
|
2012-07-05 22:53:57 +00:00
|
|
|
if ( $this->fullLoadCallback && !$this->fullLoadDone ) {
|
2018-06-09 23:26:32 +00:00
|
|
|
( $this->fullLoadCallback )( $this );
|
2009-06-02 09:49:31 +00:00
|
|
|
$this->fullLoadDone = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-05-23 15:00:46 +00:00
|
|
|
}
|