3 * Configuration holder, particularly for multi-wiki sites.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * This is a class used to hold configuration settings, particularly for multi-wiki sites.
26 class SiteConfiguration
{
29 * Array of suffixes, for self::siteFromDB()
31 public $suffixes = array();
34 * Array of wikis, should be the same as $wgLocalDatabases
36 public $wikis = array();
39 * The whole array of settings
41 public $settings = array();
44 * Array of domains that are local and can be handled by the same server
46 public $localVHosts = array();
49 * Optional callback to load full configuration data.
51 public $fullLoadCallback = null;
53 /** Whether or not all data has been loaded */
54 public $fullLoadDone = false;
57 * A callback function that returns an array with the following keys (all
59 * - suffix: site's suffix
61 * - tags: array of wiki tags
62 * - params: array of parameters to be replaced
63 * The function will receive the SiteConfiguration instance in the first
64 * argument and the wiki in the second one.
65 * if suffix and lang are passed they will be used for the return value of
66 * self::siteFromDB() and self::$suffixes will be ignored
68 public $siteParamsCallback = null;
71 * Retrieves a configuration setting for a given wiki.
72 * @param $settingName String ID of the setting name to retrieve
73 * @param $wiki String Wiki ID of the wiki in question.
74 * @param $suffix String The suffix of the wiki in question.
75 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
76 * @param $wikiTags Array The tags assigned to the wiki.
77 * @return Mixed the value of the setting requested.
79 public function get( $settingName, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
80 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
81 return $this->getSetting( $settingName, $wiki, $params );
85 * Really retrieves a configuration setting for a given wiki.
87 * @param $settingName String ID of the setting name to retrieve.
88 * @param $wiki String Wiki ID of the wiki in question.
89 * @param $params Array: array of parameters.
90 * @return Mixed the value of the setting requested.
92 protected function getSetting( $settingName, $wiki, /*array*/ $params ){
94 if( array_key_exists( $settingName, $this->settings
) ) {
95 $thisSetting =& $this->settings
[$settingName];
97 // Do individual wiki settings
98 if( array_key_exists( $wiki, $thisSetting ) ) {
99 $retval = $thisSetting[$wiki];
101 } elseif( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
102 $retval = $thisSetting["+$wiki"];
106 foreach( $params['tags'] as $tag ) {
107 if( array_key_exists( $tag, $thisSetting ) ) {
108 if ( isset( $retval ) && is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
109 $retval = self
::arrayMerge( $retval, $thisSetting[$tag] );
111 $retval = $thisSetting[$tag];
114 } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) {
115 if( !isset( $retval ) )
117 $retval = self
::arrayMerge( $retval, $thisSetting["+$tag"] );
120 // Do suffix settings
121 $suffix = $params['suffix'];
122 if( !is_null( $suffix ) ) {
123 if( array_key_exists( $suffix, $thisSetting ) ) {
124 if ( isset($retval) && is_array($retval) && is_array($thisSetting[$suffix]) ) {
125 $retval = self
::arrayMerge( $retval, $thisSetting[$suffix] );
127 $retval = $thisSetting[$suffix];
130 } elseif( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) {
133 $retval = self
::arrayMerge( $retval, $thisSetting["+$suffix"] );
137 // Fall back to default.
138 if( array_key_exists( 'default', $thisSetting ) ) {
139 if( is_array( $retval ) && is_array( $thisSetting['default'] ) ) {
140 $retval = self
::arrayMerge( $retval, $thisSetting['default'] );
142 $retval = $thisSetting['default'];
149 if( !is_null( $retval ) && count( $params['params'] ) ) {
150 foreach ( $params['params'] as $key => $value ) {
151 $retval = $this->doReplace( '$' . $key, $value, $retval );
158 * Type-safe string replace; won't do replacements on non-strings
166 function doReplace( $from, $to, $in ) {
167 if( is_string( $in ) ) {
168 return str_replace( $from, $to, $in );
169 } elseif( is_array( $in ) ) {
170 foreach( $in as $key => $val ) {
171 $in[$key] = $this->doReplace( $from, $to, $val );
180 * Gets all settings for a wiki
181 * @param $wiki String Wiki ID of the wiki in question.
182 * @param $suffix String The suffix of the wiki in question.
183 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
184 * @param $wikiTags Array The tags assigned to the wiki.
185 * @return Array Array of settings requested.
187 public function getAll( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
188 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
189 $localSettings = array();
190 foreach( $this->settings
as $varname => $stuff ) {
193 if ( substr( $varname, 0, 1 ) == '+' ) {
195 $var = substr( $varname, 1 );
198 $value = $this->getSetting( $varname, $wiki, $params );
199 if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) )
200 $value = self
::arrayMerge( $value, $GLOBALS[$var] );
201 if ( !is_null( $value ) ) {
202 $localSettings[$var] = $value;
205 return $localSettings;
209 * Retrieves a configuration setting for a given wiki, forced to a boolean.
210 * @param $setting String ID of the setting name to retrieve
211 * @param $wiki String Wiki ID of the wiki in question.
212 * @param $suffix String The suffix of the wiki in question.
213 * @param $wikiTags Array The tags assigned to the wiki.
214 * @return bool The value of the setting requested.
216 public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) {
217 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
221 * Retrieves an array of local databases
225 function &getLocalDatabases() {
230 * Retrieves the value of a given setting, and places it in a variable passed by reference.
231 * @param $setting String ID of the setting name to retrieve
232 * @param $wiki String Wiki ID of the wiki in question.
233 * @param $suffix String The suffix of the wiki in question.
234 * @param $var array Reference The variable to insert the value into.
235 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
236 * @param $wikiTags Array The tags assigned to the wiki.
238 public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) {
239 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
240 if ( !is_null( $value ) ) {
246 * Retrieves the value of a given setting, and places it in its corresponding global variable.
247 * @param $setting String ID of the setting name to retrieve
248 * @param $wiki String Wiki ID of the wiki in question.
249 * @param $suffix String The suffix of the wiki in question.
250 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
251 * @param $wikiTags Array The tags assigned to the wiki.
253 public function extractGlobal( $setting, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
254 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
255 $this->extractGlobalSetting( $setting, $wiki, $params );
259 * @param $setting string
260 * @param $wiki string
261 * @param $params array
263 public function extractGlobalSetting( $setting, $wiki, $params ) {
264 $value = $this->getSetting( $setting, $wiki, $params );
265 if ( !is_null( $value ) ) {
266 if (substr($setting,0,1) == '+' && is_array($value)) {
267 $setting = substr($setting,1);
268 if ( is_array($GLOBALS[$setting]) ) {
269 $GLOBALS[$setting] = self
::arrayMerge( $GLOBALS[$setting], $value );
271 $GLOBALS[$setting] = $value;
274 $GLOBALS[$setting] = $value;
280 * Retrieves the values of all settings, and places them in their corresponding global variables.
281 * @param $wiki String Wiki ID of the wiki in question.
282 * @param $suffix String The suffix of the wiki in question.
283 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
284 * @param $wikiTags Array The tags assigned to the wiki.
286 public function extractAllGlobals( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
287 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
288 foreach ( $this->settings
as $varName => $setting ) {
289 $this->extractGlobalSetting( $varName, $wiki, $params );
294 * Return specific settings for $wiki
295 * See the documentation of self::$siteParamsCallback for more in-depth
296 * documentation about this function
298 * @param $wiki String
301 protected function getWikiParams( $wiki ){
302 static $default = array(
309 if( !is_callable( $this->siteParamsCallback
) ) {
313 $ret = call_user_func_array( $this->siteParamsCallback
, array( $this, $wiki ) );
314 # Validate the returned value
315 if( !is_array( $ret ) ) {
319 foreach( $default as $name => $def ){
320 if( !isset( $ret[$name] ) ||
( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) )
321 $ret[$name] = $default[$name];
328 * Merge params between the ones passed to the function and the ones given
329 * by self::$siteParamsCallback for backward compatibility
330 * Values returned by self::getWikiParams() have the priority.
332 * @param $wiki String Wiki ID of the wiki in question.
333 * @param $suffix String The suffix of the wiki in question.
334 * @param $params Array List of parameters. $.'key' is replaced by $value in
336 * @param $wikiTags Array The tags assigned to the wiki.
339 protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ){
340 $ret = $this->getWikiParams( $wiki );
342 if( is_null( $ret['suffix'] ) )
343 $ret['suffix'] = $suffix;
345 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
347 $ret['params'] +
= $params;
349 // Automatically fill that ones if needed
350 if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) )
351 $ret['params']['lang'] = $ret['lang'];
352 if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) )
353 $ret['params']['site'] = $ret['suffix'];
359 * Work out the site and language name from a database name
364 public function siteFromDB( $db ) {
366 $def = $this->getWikiParams( $db );
367 if( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) )
368 return array( $def['suffix'], $def['lang'] );
372 foreach ( $this->suffixes
as $suffix ) {
373 if ( $suffix === '' ) {
377 } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
378 $site = $suffix == 'wiki' ?
'wikipedia' : $suffix;
379 $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
383 $lang = str_replace( '_', '-', $lang );
384 return array( $site, $lang );
388 * Returns true if the given vhost is handled locally.
389 * @param $vhost String
392 public function isLocalVHost( $vhost ) {
393 return in_array( $vhost, $this->localVHosts
);
397 * Merge multiple arrays together.
398 * On encountering duplicate keys, merge the two, but ONLY if they're arrays.
399 * PHP's array_merge_recursive() merges ANY duplicate values into arrays,
402 * @param $array1 array
406 static function arrayMerge( $array1/* ... */ ) {
408 for( $i = 1; $i < func_num_args(); $i++
) {
409 foreach( func_get_arg( $i ) as $key => $value ) {
410 if ( isset($out[$key]) && is_array($out[$key]) && is_array($value) ) {
411 $out[$key] = self
::arrayMerge( $out[$key], $value );
412 } elseif ( !isset($out[$key]) ||
!$out[$key] && !is_numeric($key) ) {
413 // Values that evaluate to true given precedence, for the primary purpose of merging permissions arrays.
415 } elseif ( is_numeric( $key ) ) {
424 public function loadFullData() {
425 if ($this->fullLoadCallback
&& !$this->fullLoadDone
) {
426 call_user_func( $this->fullLoadCallback
, $this );
427 $this->fullLoadDone
= true;