4 * The include paths change after this file is included from commandLine.inc,
5 * meaning that require_once() fails to detect that it is including the same
6 * file again. We use DIY C-style protection as a workaround.
9 // Hide this pattern from Doxygen, which spazzes out at it
11 if( !defined( 'SITE_CONFIGURATION' ) ){
12 define( 'SITE_CONFIGURATION', 1 );
16 * This is a class used to hold configuration settings, particularly for multi-wiki sites.
18 class SiteConfiguration
{
21 * Array of suffixes, for self::siteFromDB()
23 public $suffixes = array();
26 * Array of wikis, should be the same as $wgLocalDatabases
28 public $wikis = array();
31 * The whole array of settings
33 public $settings = array();
36 * Array of domains that are local and can be handled by the same server
38 public $localVHosts = array();
41 * Optional callback to load full configuration data.
43 public $fullLoadCallback = null;
45 /** Whether or not all data has been loaded */
46 public $fullLoadDone = false;
49 * A callback function that returns an array with the following keys (all
51 * - suffix: site's suffix
53 * - tags: array of wiki tags
54 * - params: array of parameters to be replaced
55 * The function will receive the SiteConfiguration instance in the first
56 * argument and the wiki in the second one.
57 * if suffix and lang are passed they will be used for the return value of
58 * self::siteFromDB() and self::$suffixes will be ignored
60 public $siteParamsCallback = null;
63 * Retrieves a configuration setting for a given wiki.
64 * @param $settingName String ID of the setting name to retrieve
65 * @param $wiki String Wiki ID of the wiki in question.
66 * @param $suffix String The suffix of the wiki in question.
67 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
68 * @param $wikiTags Array The tags assigned to the wiki.
69 * @return Mixed the value of the setting requested.
71 public function get( $settingName, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
72 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
73 return $this->getSetting( $settingName, $wiki, $params );
77 * Really retrieves a configuration setting for a given wiki.
79 * @param $settingName String ID of the setting name to retrieve.
80 * @param $wiki String Wiki ID of the wiki in question.
81 * @param $params Array: array of parameters.
82 * @return Mixed the value of the setting requested.
84 protected function getSetting( $settingName, $wiki, /*array*/ $params ){
86 if( array_key_exists( $settingName, $this->settings
) ) {
87 $thisSetting =& $this->settings
[$settingName];
89 // Do individual wiki settings
90 if( array_key_exists( $wiki, $thisSetting ) ) {
91 $retval = $thisSetting[$wiki];
93 } elseif( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
94 $retval = $thisSetting["+$wiki"];
98 foreach( $params['tags'] as $tag ) {
99 if( array_key_exists( $tag, $thisSetting ) ) {
100 if ( isset( $retval ) && is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
101 $retval = self
::arrayMerge( $retval, $thisSetting[$tag] );
103 $retval = $thisSetting[$tag];
106 } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) {
107 if( !isset( $retval ) )
109 $retval = self
::arrayMerge( $retval, $thisSetting["+$tag"] );
112 // Do suffix settings
113 $suffix = $params['suffix'];
114 if( !is_null( $suffix ) ) {
115 if( array_key_exists( $suffix, $thisSetting ) ) {
116 if ( isset($retval) && is_array($retval) && is_array($thisSetting[$suffix]) ) {
117 $retval = self
::arrayMerge( $retval, $thisSetting[$suffix] );
119 $retval = $thisSetting[$suffix];
122 } elseif( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) {
125 $retval = self
::arrayMerge( $retval, $thisSetting["+$suffix"] );
129 // Fall back to default.
130 if( array_key_exists( 'default', $thisSetting ) ) {
131 if( is_array( $retval ) && is_array( $thisSetting['default'] ) ) {
132 $retval = self
::arrayMerge( $retval, $thisSetting['default'] );
134 $retval = $thisSetting['default'];
141 if( !is_null( $retval ) && count( $params['params'] ) ) {
142 foreach ( $params['params'] as $key => $value ) {
143 $retval = $this->doReplace( '$' . $key, $value, $retval );
150 * Type-safe string replace; won't do replacements on non-strings
158 function doReplace( $from, $to, $in ) {
159 if( is_string( $in ) ) {
160 return str_replace( $from, $to, $in );
161 } elseif( is_array( $in ) ) {
162 foreach( $in as $key => $val ) {
163 $in[$key] = $this->doReplace( $from, $to, $val );
172 * Gets all settings for a wiki
173 * @param $wiki String Wiki ID of the wiki in question.
174 * @param $suffix String The suffix of the wiki in question.
175 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
176 * @param $wikiTags Array The tags assigned to the wiki.
177 * @return Array Array of settings requested.
179 public function getAll( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
180 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
181 $localSettings = array();
182 foreach( $this->settings
as $varname => $stuff ) {
185 if ( substr( $varname, 0, 1 ) == '+' ) {
187 $var = substr( $varname, 1 );
190 $value = $this->getSetting( $varname, $wiki, $params );
191 if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) )
192 $value = self
::arrayMerge( $value, $GLOBALS[$var] );
193 if ( !is_null( $value ) ) {
194 $localSettings[$var] = $value;
197 return $localSettings;
201 * Retrieves a configuration setting for a given wiki, forced to a boolean.
202 * @param $setting String ID of the setting name to retrieve
203 * @param $wiki String Wiki ID of the wiki in question.
204 * @param $suffix String The suffix of the wiki in question.
205 * @param $wikiTags Array The tags assigned to the wiki.
206 * @return bool The value of the setting requested.
208 public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) {
209 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
213 * Retrieves an array of local databases
217 function &getLocalDatabases() {
222 function initialise() {
226 * Retrieves the value of a given setting, and places it in a variable passed by reference.
227 * @param $setting String ID of the setting name to retrieve
228 * @param $wiki String Wiki ID of the wiki in question.
229 * @param $suffix String The suffix of the wiki in question.
230 * @param $var Reference The variable to insert the value into.
231 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
232 * @param $wikiTags Array The tags assigned to the wiki.
234 public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) {
235 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
236 if ( !is_null( $value ) ) {
242 * Retrieves the value of a given setting, and places it in its corresponding global variable.
243 * @param $setting String ID of the setting name to retrieve
244 * @param $wiki String Wiki ID of the wiki in question.
245 * @param $suffix String The suffix of the wiki in question.
246 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
247 * @param $wikiTags Array The tags assigned to the wiki.
249 public function extractGlobal( $setting, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
250 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
251 $this->extractGlobalSetting( $setting, $wiki, $params );
255 * @param $setting string
256 * @param $wiki string
257 * @param $params array
259 public function extractGlobalSetting( $setting, $wiki, $params ) {
260 $value = $this->getSetting( $setting, $wiki, $params );
261 if ( !is_null( $value ) ) {
262 if (substr($setting,0,1) == '+' && is_array($value)) {
263 $setting = substr($setting,1);
264 if ( is_array($GLOBALS[$setting]) ) {
265 $GLOBALS[$setting] = self
::arrayMerge( $GLOBALS[$setting], $value );
267 $GLOBALS[$setting] = $value;
270 $GLOBALS[$setting] = $value;
276 * Retrieves the values of all settings, and places them in their corresponding global variables.
277 * @param $wiki String Wiki ID of the wiki in question.
278 * @param $suffix String The suffix of the wiki in question.
279 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
280 * @param $wikiTags Array The tags assigned to the wiki.
282 public function extractAllGlobals( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
283 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
284 foreach ( $this->settings
as $varName => $setting ) {
285 $this->extractGlobalSetting( $varName, $wiki, $params );
290 * Return specific settings for $wiki
291 * See the documentation of self::$siteParamsCallback for more in-depth
292 * documentation about this function
294 * @param $wiki String
297 protected function getWikiParams( $wiki ){
298 static $default = array(
305 if( !is_callable( $this->siteParamsCallback
) ) {
309 $ret = call_user_func_array( $this->siteParamsCallback
, array( $this, $wiki ) );
310 # Validate the returned value
311 if( !is_array( $ret ) ) {
315 foreach( $default as $name => $def ){
316 if( !isset( $ret[$name] ) ||
( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) )
317 $ret[$name] = $default[$name];
324 * Merge params beetween the ones passed to the function and the ones given
325 * by self::$siteParamsCallback for backward compatibility
326 * Values returned by self::getWikiParams() have the priority.
328 * @param $wiki String Wiki ID of the wiki in question.
329 * @param $suffix String The suffix of the wiki in question.
330 * @param $params Array List of parameters. $.'key' is replaced by $value in
332 * @param $wikiTags Array The tags assigned to the wiki.
335 protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ){
336 $ret = $this->getWikiParams( $wiki );
338 if( is_null( $ret['suffix'] ) )
339 $ret['suffix'] = $suffix;
341 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
343 $ret['params'] +
= $params;
345 // Automatically fill that ones if needed
346 if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) )
347 $ret['params']['lang'] = $ret['lang'];
348 if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) )
349 $ret['params']['site'] = $ret['suffix'];
355 * Work out the site and language name from a database name
360 public function siteFromDB( $db ) {
362 $def = $this->getWikiParams( $db );
363 if( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) )
364 return array( $def['suffix'], $def['lang'] );
368 foreach ( $this->suffixes
as $suffix ) {
369 if ( $suffix === '' ) {
373 } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
374 $site = $suffix == 'wiki' ?
'wikipedia' : $suffix;
375 $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
379 $lang = str_replace( '_', '-', $lang );
380 return array( $site, $lang );
384 * Returns true if the given vhost is handled locally.
385 * @param $vhost String
388 public function isLocalVHost( $vhost ) {
389 return in_array( $vhost, $this->localVHosts
);
393 * Merge multiple arrays together.
394 * On encountering duplicate keys, merge the two, but ONLY if they're arrays.
395 * PHP's array_merge_recursive() merges ANY duplicate values into arrays,
398 * @param $array1 array
402 static function arrayMerge( $array1/* ... */ ) {
404 for( $i = 1; $i < func_num_args(); $i++
) {
405 foreach( func_get_arg( $i ) as $key => $value ) {
406 if ( isset($out[$key]) && is_array($out[$key]) && is_array($value) ) {
407 $out[$key] = self
::arrayMerge( $out[$key], $value );
408 } elseif ( !isset($out[$key]) ||
!$out[$key] && !is_numeric($key) ) {
409 // Values that evaluate to true given precedence, for the primary purpose of merging permissions arrays.
411 } elseif ( is_numeric( $key ) ) {
420 public function loadFullData() {
421 if ($this->fullLoadCallback
&& !$this->fullLoadDone
) {
422 call_user_func( $this->fullLoadCallback
, $this );
423 $this->fullLoadDone
= true;
427 } // End of multiple inclusion guard