3 * This is a class used to hold configuration settings, particularly for multi-wiki sites.
5 class SiteConfiguration
{
8 * Array of suffixes, for self::siteFromDB()
10 public $suffixes = array();
13 * Array of wikis, should be the same as $wgLocalDatabases
15 public $wikis = array();
18 * The whole array of settings
20 public $settings = array();
23 * Array of domains that are local and can be handled by the same server
25 public $localVHosts = array();
28 * Optional callback to load full configuration data.
30 public $fullLoadCallback = null;
32 /** Whether or not all data has been loaded */
33 public $fullLoadDone = false;
36 * A callback function that returns an array with the following keys (all
38 * - suffix: site's suffix
40 * - tags: array of wiki tags
41 * - params: array of parameters to be replaced
42 * The function will receive the SiteConfiguration instance in the first
43 * argument and the wiki in the second one.
44 * if suffix and lang are passed they will be used for the return value of
45 * self::siteFromDB() and self::$suffixes will be ignored
47 public $siteParamsCallback = null;
50 * Retrieves a configuration setting for a given wiki.
51 * @param $settingName String ID of the setting name to retrieve
52 * @param $wiki String Wiki ID of the wiki in question.
53 * @param $suffix String The suffix of the wiki in question.
54 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
55 * @param $wikiTags Array The tags assigned to the wiki.
56 * @return Mixed the value of the setting requested.
58 public function get( $settingName, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
59 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
60 return $this->getSetting( $settingName, $wiki, $params );
64 * Really retrieves a configuration setting for a given wiki.
66 * @param $settingName String ID of the setting name to retrieve.
67 * @param $wiki String Wiki ID of the wiki in question.
68 * @param $params Array: array of parameters.
69 * @return Mixed the value of the setting requested.
71 protected function getSetting( $settingName, $wiki, /*array*/ $params ){
73 if( array_key_exists( $settingName, $this->settings
) ) {
74 $thisSetting =& $this->settings
[$settingName];
76 // Do individual wiki settings
77 if( array_key_exists( $wiki, $thisSetting ) ) {
78 $retval = $thisSetting[$wiki];
80 } elseif( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
81 $retval = $thisSetting["+$wiki"];
85 foreach( $params['tags'] as $tag ) {
86 if( array_key_exists( $tag, $thisSetting ) ) {
87 if ( isset( $retval ) && is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
88 $retval = self
::arrayMerge( $retval, $thisSetting[$tag] );
90 $retval = $thisSetting[$tag];
93 } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) {
94 if( !isset( $retval ) )
96 $retval = self
::arrayMerge( $retval, $thisSetting["+$tag"] );
100 $suffix = $params['suffix'];
101 if( !is_null( $suffix ) ) {
102 if( array_key_exists( $suffix, $thisSetting ) ) {
103 if ( isset($retval) && is_array($retval) && is_array($thisSetting[$suffix]) ) {
104 $retval = self
::arrayMerge( $retval, $thisSetting[$suffix] );
106 $retval = $thisSetting[$suffix];
109 } elseif( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) {
112 $retval = self
::arrayMerge( $retval, $thisSetting["+$suffix"] );
116 // Fall back to default.
117 if( array_key_exists( 'default', $thisSetting ) ) {
118 if( is_array( $retval ) && is_array( $thisSetting['default'] ) ) {
119 $retval = self
::arrayMerge( $retval, $thisSetting['default'] );
121 $retval = $thisSetting['default'];
128 if( !is_null( $retval ) && count( $params['params'] ) ) {
129 foreach ( $params['params'] as $key => $value ) {
130 $retval = $this->doReplace( '$' . $key, $value, $retval );
137 * Type-safe string replace; won't do replacements on non-strings
145 function doReplace( $from, $to, $in ) {
146 if( is_string( $in ) ) {
147 return str_replace( $from, $to, $in );
148 } elseif( is_array( $in ) ) {
149 foreach( $in as $key => $val ) {
150 $in[$key] = $this->doReplace( $from, $to, $val );
159 * Gets all settings for a wiki
160 * @param $wiki String Wiki ID of the wiki in question.
161 * @param $suffix String The suffix of the wiki in question.
162 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
163 * @param $wikiTags Array The tags assigned to the wiki.
164 * @return Array Array of settings requested.
166 public function getAll( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
167 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
168 $localSettings = array();
169 foreach( $this->settings
as $varname => $stuff ) {
172 if ( substr( $varname, 0, 1 ) == '+' ) {
174 $var = substr( $varname, 1 );
177 $value = $this->getSetting( $varname, $wiki, $params );
178 if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) )
179 $value = self
::arrayMerge( $value, $GLOBALS[$var] );
180 if ( !is_null( $value ) ) {
181 $localSettings[$var] = $value;
184 return $localSettings;
188 * Retrieves a configuration setting for a given wiki, forced to a boolean.
189 * @param $setting String ID of the setting name to retrieve
190 * @param $wiki String Wiki ID of the wiki in question.
191 * @param $suffix String The suffix of the wiki in question.
192 * @param $wikiTags Array The tags assigned to the wiki.
193 * @return bool The value of the setting requested.
195 public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) {
196 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
200 * Retrieves an array of local databases
204 function &getLocalDatabases() {
209 function initialise() {
213 * Retrieves the value of a given setting, and places it in a variable passed by reference.
214 * @param $setting String ID of the setting name to retrieve
215 * @param $wiki String Wiki ID of the wiki in question.
216 * @param $suffix String The suffix of the wiki in question.
217 * @param $var Reference The variable to insert the value into.
218 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
219 * @param $wikiTags Array The tags assigned to the wiki.
221 public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) {
222 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
223 if ( !is_null( $value ) ) {
229 * Retrieves the value of a given setting, and places it in its corresponding global variable.
230 * @param $setting String ID of the setting name to retrieve
231 * @param $wiki String Wiki ID of the wiki in question.
232 * @param $suffix String The suffix of the wiki in question.
233 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
234 * @param $wikiTags Array The tags assigned to the wiki.
236 public function extractGlobal( $setting, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
237 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
238 $this->extractGlobalSetting( $setting, $wiki, $params );
242 * @param $setting string
243 * @param $wiki string
244 * @param $params array
246 public function extractGlobalSetting( $setting, $wiki, $params ) {
247 $value = $this->getSetting( $setting, $wiki, $params );
248 if ( !is_null( $value ) ) {
249 if (substr($setting,0,1) == '+' && is_array($value)) {
250 $setting = substr($setting,1);
251 if ( is_array($GLOBALS[$setting]) ) {
252 $GLOBALS[$setting] = self
::arrayMerge( $GLOBALS[$setting], $value );
254 $GLOBALS[$setting] = $value;
257 $GLOBALS[$setting] = $value;
263 * Retrieves the values of all settings, and places them in their corresponding global variables.
264 * @param $wiki String Wiki ID of the wiki in question.
265 * @param $suffix String The suffix of the wiki in question.
266 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
267 * @param $wikiTags Array The tags assigned to the wiki.
269 public function extractAllGlobals( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
270 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
271 foreach ( $this->settings
as $varName => $setting ) {
272 $this->extractGlobalSetting( $varName, $wiki, $params );
277 * Return specific settings for $wiki
278 * See the documentation of self::$siteParamsCallback for more in-depth
279 * documentation about this function
281 * @param $wiki String
284 protected function getWikiParams( $wiki ){
285 static $default = array(
292 if( !is_callable( $this->siteParamsCallback
) ) {
296 $ret = call_user_func_array( $this->siteParamsCallback
, array( $this, $wiki ) );
297 # Validate the returned value
298 if( !is_array( $ret ) ) {
302 foreach( $default as $name => $def ){
303 if( !isset( $ret[$name] ) ||
( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) )
304 $ret[$name] = $default[$name];
311 * Merge params beetween the ones passed to the function and the ones given
312 * by self::$siteParamsCallback for backward compatibility
313 * Values returned by self::getWikiParams() have the priority.
315 * @param $wiki String Wiki ID of the wiki in question.
316 * @param $suffix String The suffix of the wiki in question.
317 * @param $params Array List of parameters. $.'key' is replaced by $value in
319 * @param $wikiTags Array The tags assigned to the wiki.
322 protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ){
323 $ret = $this->getWikiParams( $wiki );
325 if( is_null( $ret['suffix'] ) )
326 $ret['suffix'] = $suffix;
328 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
330 $ret['params'] +
= $params;
332 // Automatically fill that ones if needed
333 if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) )
334 $ret['params']['lang'] = $ret['lang'];
335 if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) )
336 $ret['params']['site'] = $ret['suffix'];
342 * Work out the site and language name from a database name
347 public function siteFromDB( $db ) {
349 $def = $this->getWikiParams( $db );
350 if( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) )
351 return array( $def['suffix'], $def['lang'] );
355 foreach ( $this->suffixes
as $suffix ) {
356 if ( $suffix === '' ) {
360 } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
361 $site = $suffix == 'wiki' ?
'wikipedia' : $suffix;
362 $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
366 $lang = str_replace( '_', '-', $lang );
367 return array( $site, $lang );
371 * Returns true if the given vhost is handled locally.
372 * @param $vhost String
375 public function isLocalVHost( $vhost ) {
376 return in_array( $vhost, $this->localVHosts
);
380 * Merge multiple arrays together.
381 * On encountering duplicate keys, merge the two, but ONLY if they're arrays.
382 * PHP's array_merge_recursive() merges ANY duplicate values into arrays,
385 * @param $array1 array
389 static function arrayMerge( $array1/* ... */ ) {
391 for( $i = 1; $i < func_num_args(); $i++
) {
392 foreach( func_get_arg( $i ) as $key => $value ) {
393 if ( isset($out[$key]) && is_array($out[$key]) && is_array($value) ) {
394 $out[$key] = self
::arrayMerge( $out[$key], $value );
395 } elseif ( !isset($out[$key]) ||
!$out[$key] && !is_numeric($key) ) {
396 // Values that evaluate to true given precedence, for the primary purpose of merging permissions arrays.
398 } elseif ( is_numeric( $key ) ) {
407 public function loadFullData() {
408 if ($this->fullLoadCallback
&& !$this->fullLoadDone
) {
409 call_user_func( $this->fullLoadCallback
, $this );
410 $this->fullLoadDone
= true;