misc style fix
[lhc/web/wiklou.git] / includes / SiteConfiguration.php
1 <?php
2 /**
3 * Configuration holder, particularly for multi-wiki sites.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 /**
24 * This is a class for holding configuration settings, particularly for
25 * multi-wiki sites.
26 *
27 * A basic synopsis:
28 *
29 * Consider a wikifarm having three sites: two production sites, one in English
30 * and one in German, and one testing site. You can assign them easy-to-remember
31 * identifiers - ISO 639 codes 'en' and 'de' for language wikis, and 'beta' for
32 * the testing wiki.
33 *
34 * You would thus initialize the site configuration by specifying the wiki
35 * identifiers:
36 *
37 * @code
38 * $conf = new SiteConfiguration;
39 * $conf->wikis = array( 'de', 'en', 'beta' );
40 * @endcode
41 *
42 * When configuring the MediaWiki global settings (the $wg variables),
43 * the identifiers will be available to specify settings on a per wiki basis.
44 *
45 * @code
46 * $conf->settings = array(
47 * 'wgSomeSetting' => array(
48 *
49 * # production:
50 * 'de' => false,
51 * 'en' => false,
52 *
53 * # test:
54 * 'beta => true,
55 * ),
56 * );
57 * @endcode
58 *
59 * With three wikis, that is easy to manage. But what about a farm with
60 * hundreds of wikis? Site configuration provides a special keyword named
61 * 'default' which is the value used when a wiki is not found. Hence
62 * the above code could be written:
63 *
64 * @code
65 * $conf->settings = array(
66 * 'wgSomeSetting' => array(
67 *
68 * 'default' => false,
69 *
70 * # Enable feature on test
71 * 'beta' => true,
72 * ),
73 * );
74 * @endcode
75 *
76 *
77 * Since settings can contain arrays, site configuration provides a way
78 * to merge an array with the default. This is very useful to avoid
79 * repeating settings again and again while still maintaining specific changes
80 * on a per wiki basis.
81 *
82 * @code
83 * $conf->settings = array(
84 * 'wgMergeSetting' = array(
85 * # Value that will be shared among all wikis:
86 * 'default' => array( NS_USER => true ),
87 *
88 * # Leading '+' means merging the array of value with the defaults
89 * '+beta' => array( NS_HELP => true ),
90 * ),
91 * );
92 *
93 * # Get configuration for the German site:
94 * $conf->get( 'wgMergeSetting', 'de' );
95 * // --> array( NS_USER => true );
96 *
97 * # Get configuration for the testing site:
98 * $conf->get( 'wgMergeSetting', 'beta' );
99 * // --> array( NS_USER => true, NS_HELP => true );
100 * @endcode
101 *
102 * Finally, to load all configuration settings, extract them in global context:
103 *
104 * @code
105 * # Name / identifier of the wiki as set in $conf->wikis
106 * $wikiID = 'beta';
107 * $globals = $conf->getAll( $wikiID );
108 * extract( $globals );
109 * @endcode
110 *
111 * TODO: give examples for,
112 * suffixes:
113 * $conf->suffixes = array( 'wiki' );
114 * localVHosts
115 * callbacks!
116 */
117 class SiteConfiguration {
118
119 /**
120 * Array of suffixes, for self::siteFromDB()
121 */
122 public $suffixes = array();
123
124 /**
125 * Array of wikis, should be the same as $wgLocalDatabases
126 */
127 public $wikis = array();
128
129 /**
130 * The whole array of settings
131 */
132 public $settings = array();
133
134 /**
135 * Array of domains that are local and can be handled by the same server
136 */
137 public $localVHosts = array();
138
139 /**
140 * Optional callback to load full configuration data.
141 * @var string|array
142 */
143 public $fullLoadCallback = null;
144
145 /** Whether or not all data has been loaded */
146 public $fullLoadDone = false;
147
148 /**
149 * A callback function that returns an array with the following keys (all
150 * optional):
151 * - suffix: site's suffix
152 * - lang: site's lang
153 * - tags: array of wiki tags
154 * - params: array of parameters to be replaced
155 * The function will receive the SiteConfiguration instance in the first
156 * argument and the wiki in the second one.
157 * if suffix and lang are passed they will be used for the return value of
158 * self::siteFromDB() and self::$suffixes will be ignored
159 *
160 * @var string|array
161 */
162 public $siteParamsCallback = null;
163
164 /**
165 * Retrieves a configuration setting for a given wiki.
166 * @param $settingName String ID of the setting name to retrieve
167 * @param $wiki String Wiki ID of the wiki in question.
168 * @param $suffix String The suffix of the wiki in question.
169 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
170 * @param $wikiTags Array The tags assigned to the wiki.
171 * @return Mixed the value of the setting requested.
172 */
173 public function get( $settingName, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
174 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
175 return $this->getSetting( $settingName, $wiki, $params );
176 }
177
178 /**
179 * Really retrieves a configuration setting for a given wiki.
180 *
181 * @param $settingName String ID of the setting name to retrieve.
182 * @param $wiki String Wiki ID of the wiki in question.
183 * @param $params Array: array of parameters.
184 * @return Mixed the value of the setting requested.
185 */
186 protected function getSetting( $settingName, $wiki, /*array*/ $params ) {
187 $retval = null;
188 if( array_key_exists( $settingName, $this->settings ) ) {
189 $thisSetting =& $this->settings[$settingName];
190 do {
191 // Do individual wiki settings
192 if( array_key_exists( $wiki, $thisSetting ) ) {
193 $retval = $thisSetting[$wiki];
194 break;
195 } elseif ( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
196 $retval = $thisSetting["+$wiki"];
197 }
198
199 // Do tag settings
200 foreach( $params['tags'] as $tag ) {
201 if( array_key_exists( $tag, $thisSetting ) ) {
202 if ( isset( $retval ) && is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
203 $retval = self::arrayMerge( $retval, $thisSetting[$tag] );
204 } else {
205 $retval = $thisSetting[$tag];
206 }
207 break 2;
208 } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) {
209 if( !isset( $retval ) ) {
210 $retval = array();
211 }
212 $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] );
213 }
214 }
215 // Do suffix settings
216 $suffix = $params['suffix'];
217 if( !is_null( $suffix ) ) {
218 if( array_key_exists( $suffix, $thisSetting ) ) {
219 if ( isset($retval) && is_array($retval) && is_array($thisSetting[$suffix]) ) {
220 $retval = self::arrayMerge( $retval, $thisSetting[$suffix] );
221 } else {
222 $retval = $thisSetting[$suffix];
223 }
224 break;
225 } elseif ( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) {
226 if ( !isset( $retval ) ) {
227 $retval = array();
228 }
229 $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] );
230 }
231 }
232
233 // Fall back to default.
234 if( array_key_exists( 'default', $thisSetting ) ) {
235 if( is_array( $retval ) && is_array( $thisSetting['default'] ) ) {
236 $retval = self::arrayMerge( $retval, $thisSetting['default'] );
237 } else {
238 $retval = $thisSetting['default'];
239 }
240 break;
241 }
242 } while ( false );
243 }
244
245 if( !is_null( $retval ) && count( $params['params'] ) ) {
246 foreach ( $params['params'] as $key => $value ) {
247 $retval = $this->doReplace( '$' . $key, $value, $retval );
248 }
249 }
250 return $retval;
251 }
252
253 /**
254 * Type-safe string replace; won't do replacements on non-strings
255 * private?
256 *
257 * @param $from
258 * @param $to
259 * @param $in
260 * @return string
261 */
262 function doReplace( $from, $to, $in ) {
263 if( is_string( $in ) ) {
264 return str_replace( $from, $to, $in );
265 } elseif( is_array( $in ) ) {
266 foreach( $in as $key => $val ) {
267 $in[$key] = $this->doReplace( $from, $to, $val );
268 }
269 return $in;
270 } else {
271 return $in;
272 }
273 }
274
275 /**
276 * Gets all settings for a wiki
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.
281 * @return Array Array of settings requested.
282 */
283 public function getAll( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
284 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
285 $localSettings = array();
286 foreach( $this->settings as $varname => $stuff ) {
287 $append = false;
288 $var = $varname;
289 if ( substr( $varname, 0, 1 ) == '+' ) {
290 $append = true;
291 $var = substr( $varname, 1 );
292 }
293
294 $value = $this->getSetting( $varname, $wiki, $params );
295 if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) ) {
296 $value = self::arrayMerge( $value, $GLOBALS[$var] );
297 }
298 if ( !is_null( $value ) ) {
299 $localSettings[$var] = $value;
300 }
301 }
302 return $localSettings;
303 }
304
305 /**
306 * Retrieves a configuration setting for a given wiki, forced to a boolean.
307 * @param $setting String ID of the setting name to retrieve
308 * @param $wiki String Wiki ID of the wiki in question.
309 * @param $suffix String The suffix of the wiki in question.
310 * @param $wikiTags Array The tags assigned to the wiki.
311 * @return bool The value of the setting requested.
312 */
313 public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) {
314 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
315 }
316
317 /**
318 * Retrieves an array of local databases
319 *
320 * @return array
321 */
322 function &getLocalDatabases() {
323 return $this->wikis;
324 }
325
326 /**
327 * Retrieves the value of a given setting, and places it in a variable passed by reference.
328 * @param $setting String ID of the setting name to retrieve
329 * @param $wiki String Wiki ID of the wiki in question.
330 * @param $suffix String The suffix of the wiki in question.
331 * @param $var array Reference The variable to insert the value into.
332 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
333 * @param $wikiTags Array The tags assigned to the wiki.
334 */
335 public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) {
336 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
337 if ( !is_null( $value ) ) {
338 $var = $value;
339 }
340 }
341
342 /**
343 * Retrieves the value of a given setting, and places it in its corresponding global variable.
344 * @param $setting String ID of the setting name to retrieve
345 * @param $wiki String Wiki ID of the wiki in question.
346 * @param $suffix String The suffix of the wiki in question.
347 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
348 * @param $wikiTags Array The tags assigned to the wiki.
349 */
350 public function extractGlobal( $setting, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
351 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
352 $this->extractGlobalSetting( $setting, $wiki, $params );
353 }
354
355 /**
356 * @param $setting string
357 * @param $wiki string
358 * @param $params array
359 */
360 public function extractGlobalSetting( $setting, $wiki, $params ) {
361 $value = $this->getSetting( $setting, $wiki, $params );
362 if ( !is_null( $value ) ) {
363 if (substr($setting,0,1) == '+' && is_array($value)) {
364 $setting = substr($setting,1);
365 if ( is_array($GLOBALS[$setting]) ) {
366 $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value );
367 } else {
368 $GLOBALS[$setting] = $value;
369 }
370 } else {
371 $GLOBALS[$setting] = $value;
372 }
373 }
374 }
375
376 /**
377 * Retrieves the values of all settings, and places them in their corresponding global variables.
378 * @param $wiki String Wiki ID of the wiki in question.
379 * @param $suffix String The suffix of the wiki in question.
380 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
381 * @param $wikiTags Array The tags assigned to the wiki.
382 */
383 public function extractAllGlobals( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
384 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
385 foreach ( $this->settings as $varName => $setting ) {
386 $this->extractGlobalSetting( $varName, $wiki, $params );
387 }
388 }
389
390 /**
391 * Return specific settings for $wiki
392 * See the documentation of self::$siteParamsCallback for more in-depth
393 * documentation about this function
394 *
395 * @param $wiki String
396 * @return array
397 */
398 protected function getWikiParams( $wiki ) {
399 static $default = array(
400 'suffix' => null,
401 'lang' => null,
402 'tags' => array(),
403 'params' => array(),
404 );
405
406 if( !is_callable( $this->siteParamsCallback ) ) {
407 return $default;
408 }
409
410 $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) );
411 # Validate the returned value
412 if( !is_array( $ret ) ) {
413 return $default;
414 }
415
416 foreach( $default as $name => $def ){
417 if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) ) {
418 $ret[$name] = $default[$name];
419 }
420 }
421
422 return $ret;
423 }
424
425 /**
426 * Merge params between the ones passed to the function and the ones given
427 * by self::$siteParamsCallback for backward compatibility
428 * Values returned by self::getWikiParams() have the priority.
429 *
430 * @param $wiki String Wiki ID of the wiki in question.
431 * @param $suffix String The suffix of the wiki in question.
432 * @param $params Array List of parameters. $.'key' is replaced by $value in
433 * all returned data.
434 * @param $wikiTags Array The tags assigned to the wiki.
435 * @return array
436 */
437 protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ) {
438 $ret = $this->getWikiParams( $wiki );
439
440 if( is_null( $ret['suffix'] ) ) {
441 $ret['suffix'] = $suffix;
442 }
443
444 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
445
446 $ret['params'] += $params;
447
448 // Automatically fill that ones if needed
449 if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) ){
450 $ret['params']['lang'] = $ret['lang'];
451 }
452 if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) ) {
453 $ret['params']['site'] = $ret['suffix'];
454 }
455
456 return $ret;
457 }
458
459 /**
460 * Work out the site and language name from a database name
461 * @param $db
462 *
463 * @return array
464 */
465 public function siteFromDB( $db ) {
466 // Allow override
467 $def = $this->getWikiParams( $db );
468 if( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) ) {
469 return array( $def['suffix'], $def['lang'] );
470 }
471
472 $site = null;
473 $lang = null;
474 foreach ( $this->suffixes as $suffix ) {
475 if ( $suffix === '' ) {
476 $site = '';
477 $lang = $db;
478 break;
479 } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
480 $site = $suffix == 'wiki' ? 'wikipedia' : $suffix;
481 $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
482 break;
483 }
484 }
485 $lang = str_replace( '_', '-', $lang );
486 return array( $site, $lang );
487 }
488
489 /**
490 * Returns true if the given vhost is handled locally.
491 * @param $vhost String
492 * @return bool
493 */
494 public function isLocalVHost( $vhost ) {
495 return in_array( $vhost, $this->localVHosts );
496 }
497
498 /**
499 * Merge multiple arrays together.
500 * On encountering duplicate keys, merge the two, but ONLY if they're arrays.
501 * PHP's array_merge_recursive() merges ANY duplicate values into arrays,
502 * which is not fun
503 *
504 * @param $array1 array
505 *
506 * @return array
507 */
508 static function arrayMerge( $array1/* ... */ ) {
509 $out = $array1;
510 for( $i = 1; $i < func_num_args(); $i++ ) {
511 foreach( func_get_arg( $i ) as $key => $value ) {
512 if ( isset($out[$key]) && is_array($out[$key]) && is_array($value) ) {
513 $out[$key] = self::arrayMerge( $out[$key], $value );
514 } elseif ( !isset($out[$key]) || !$out[$key] && !is_numeric($key) ) {
515 // Values that evaluate to true given precedence, for the primary purpose of merging permissions arrays.
516 $out[$key] = $value;
517 } elseif ( is_numeric( $key ) ) {
518 $out[] = $value;
519 }
520 }
521 }
522
523 return $out;
524 }
525
526 public function loadFullData() {
527 if ( $this->fullLoadCallback && !$this->fullLoadDone ) {
528 call_user_func( $this->fullLoadCallback, $this );
529 $this->fullLoadDone = true;
530 }
531 }
532 }