More documentation updates and additions
[lhc/web/wiklou.git] / includes / SiteConfiguration.php
1 <?php
2
3 /**
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.
7 */
8
9 // Hide this pattern from Doxygen, which spazzes out at it
10 /// @cond
11 if( !defined( 'SITE_CONFIGURATION' ) ){
12 define( 'SITE_CONFIGURATION', 1 );
13 /// @endcond
14
15 /**
16 * This is a class used to hold configuration settings, particularly for multi-wiki sites.
17 */
18 class SiteConfiguration {
19
20 /**
21 * Array of suffixes, for self::siteFromDB()
22 */
23 public $suffixes = array();
24
25 /**
26 * Array of wikis, should be the same as $wgLocalDatabases
27 */
28 public $wikis = array();
29
30 /**
31 * The whole array of settings
32 */
33 public $settings = array();
34
35 /**
36 * Array of domains that are local and can be handled by the same server
37 */
38 public $localVHosts = array();
39
40 /**
41 * Optional callback to load full configuration data.
42 */
43 public $fullLoadCallback = null;
44
45 /** Whether or not all data has been loaded */
46 public $fullLoadDone = false;
47
48 /**
49 * A callback function that returns an array with the following keys (all
50 * optional):
51 * - suffix: site's suffix
52 * - lang: site's lang
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
59 */
60 public $siteParamsCallback = null;
61
62 /**
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.
70 */
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 );
74 }
75
76 /**
77 * Really retrieves a configuration setting for a given wiki.
78 *
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.
83 */
84 protected function getSetting( $settingName, $wiki, /*array*/ $params ){
85 $retval = null;
86 if( array_key_exists( $settingName, $this->settings ) ) {
87 $thisSetting =& $this->settings[$settingName];
88 do {
89 // Do individual wiki settings
90 if( array_key_exists( $wiki, $thisSetting ) ) {
91 $retval = $thisSetting[$wiki];
92 break;
93 } elseif( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
94 $retval = $thisSetting["+$wiki"];
95 }
96
97 // Do tag settings
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] );
102 } else {
103 $retval = $thisSetting[$tag];
104 }
105 break 2;
106 } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) {
107 if( !isset( $retval ) )
108 $retval = array();
109 $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] );
110 }
111 }
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] );
118 } else {
119 $retval = $thisSetting[$suffix];
120 }
121 break;
122 } elseif( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) {
123 if (!isset($retval))
124 $retval = array();
125 $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] );
126 }
127 }
128
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'] );
133 } else {
134 $retval = $thisSetting['default'];
135 }
136 break;
137 }
138 } while ( false );
139 }
140
141 if( !is_null( $retval ) && count( $params['params'] ) ) {
142 foreach ( $params['params'] as $key => $value ) {
143 $retval = $this->doReplace( '$' . $key, $value, $retval );
144 }
145 }
146 return $retval;
147 }
148
149 /**
150 * Type-safe string replace; won't do replacements on non-strings
151 * private?
152 *
153 * @param $from
154 * @param $to
155 * @param $in
156 * @return string
157 */
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 );
164 }
165 return $in;
166 } else {
167 return $in;
168 }
169 }
170
171 /**
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.
178 */
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 ) {
183 $append = false;
184 $var = $varname;
185 if ( substr( $varname, 0, 1 ) == '+' ) {
186 $append = true;
187 $var = substr( $varname, 1 );
188 }
189
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;
195 }
196 }
197 return $localSettings;
198 }
199
200 /**
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.
207 */
208 public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) {
209 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
210 }
211
212 /**
213 * Retrieves an array of local databases
214 *
215 * @return array
216 */
217 function &getLocalDatabases() {
218 return $this->wikis;
219 }
220
221 /** A no-op */
222 function initialise() {
223 }
224
225 /**
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.
233 */
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 ) ) {
237 $var = $value;
238 }
239 }
240
241 /**
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.
248 */
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 );
252 }
253
254 /**
255 * @param $setting string
256 * @param $wiki string
257 * @param $params array
258 */
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 );
266 } else {
267 $GLOBALS[$setting] = $value;
268 }
269 } else {
270 $GLOBALS[$setting] = $value;
271 }
272 }
273 }
274
275 /**
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.
281 */
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 );
286 }
287 }
288
289 /**
290 * Return specific settings for $wiki
291 * See the documentation of self::$siteParamsCallback for more in-depth
292 * documentation about this function
293 *
294 * @param $wiki String
295 * @return array
296 */
297 protected function getWikiParams( $wiki ){
298 static $default = array(
299 'suffix' => null,
300 'lang' => null,
301 'tags' => array(),
302 'params' => array(),
303 );
304
305 if( !is_callable( $this->siteParamsCallback ) ) {
306 return $default;
307 }
308
309 $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) );
310 # Validate the returned value
311 if( !is_array( $ret ) ) {
312 return $default;
313 }
314
315 foreach( $default as $name => $def ){
316 if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) )
317 $ret[$name] = $default[$name];
318 }
319
320 return $ret;
321 }
322
323 /**
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.
327 *
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
331 * all returned data.
332 * @param $wikiTags Array The tags assigned to the wiki.
333 * @return array
334 */
335 protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ){
336 $ret = $this->getWikiParams( $wiki );
337
338 if( is_null( $ret['suffix'] ) )
339 $ret['suffix'] = $suffix;
340
341 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
342
343 $ret['params'] += $params;
344
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'];
350
351 return $ret;
352 }
353
354 /**
355 * Work out the site and language name from a database name
356 * @param $db
357 *
358 * @return array
359 */
360 public function siteFromDB( $db ) {
361 // Allow override
362 $def = $this->getWikiParams( $db );
363 if( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) )
364 return array( $def['suffix'], $def['lang'] );
365
366 $site = null;
367 $lang = null;
368 foreach ( $this->suffixes as $suffix ) {
369 if ( $suffix === '' ) {
370 $site = '';
371 $lang = $db;
372 break;
373 } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
374 $site = $suffix == 'wiki' ? 'wikipedia' : $suffix;
375 $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
376 break;
377 }
378 }
379 $lang = str_replace( '_', '-', $lang );
380 return array( $site, $lang );
381 }
382
383 /**
384 * Returns true if the given vhost is handled locally.
385 * @param $vhost String
386 * @return bool
387 */
388 public function isLocalVHost( $vhost ) {
389 return in_array( $vhost, $this->localVHosts );
390 }
391
392 /**
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,
396 * which is not fun
397 *
398 * @param $array1 array
399 *
400 * @return array
401 */
402 static function arrayMerge( $array1/* ... */ ) {
403 $out = $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.
410 $out[$key] = $value;
411 } elseif ( is_numeric( $key ) ) {
412 $out[] = $value;
413 }
414 }
415 }
416
417 return $out;
418 }
419
420 public function loadFullData() {
421 if ($this->fullLoadCallback && !$this->fullLoadDone) {
422 call_user_func( $this->fullLoadCallback, $this );
423 $this->fullLoadDone = true;
424 }
425 }
426 }
427 } // End of multiple inclusion guard