Add a way for packagers to override some installation details
[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 used to hold configuration settings, particularly for multi-wiki sites.
25 */
26 class SiteConfiguration {
27
28 /**
29 * Array of suffixes, for self::siteFromDB()
30 */
31 public $suffixes = array();
32
33 /**
34 * Array of wikis, should be the same as $wgLocalDatabases
35 */
36 public $wikis = array();
37
38 /**
39 * The whole array of settings
40 */
41 public $settings = array();
42
43 /**
44 * Array of domains that are local and can be handled by the same server
45 */
46 public $localVHosts = array();
47
48 /**
49 * Optional callback to load full configuration data.
50 */
51 public $fullLoadCallback = null;
52
53 /** Whether or not all data has been loaded */
54 public $fullLoadDone = false;
55
56 /**
57 * A callback function that returns an array with the following keys (all
58 * optional):
59 * - suffix: site's suffix
60 * - lang: site's lang
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
67 */
68 public $siteParamsCallback = null;
69
70 /**
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.
78 */
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 );
82 }
83
84 /**
85 * Really retrieves a configuration setting for a given wiki.
86 *
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.
91 */
92 protected function getSetting( $settingName, $wiki, /*array*/ $params ){
93 $retval = null;
94 if( array_key_exists( $settingName, $this->settings ) ) {
95 $thisSetting =& $this->settings[$settingName];
96 do {
97 // Do individual wiki settings
98 if( array_key_exists( $wiki, $thisSetting ) ) {
99 $retval = $thisSetting[$wiki];
100 break;
101 } elseif( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
102 $retval = $thisSetting["+$wiki"];
103 }
104
105 // Do tag settings
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] );
110 } else {
111 $retval = $thisSetting[$tag];
112 }
113 break 2;
114 } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) {
115 if( !isset( $retval ) )
116 $retval = array();
117 $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] );
118 }
119 }
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] );
126 } else {
127 $retval = $thisSetting[$suffix];
128 }
129 break;
130 } elseif( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) {
131 if (!isset($retval))
132 $retval = array();
133 $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] );
134 }
135 }
136
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'] );
141 } else {
142 $retval = $thisSetting['default'];
143 }
144 break;
145 }
146 } while ( false );
147 }
148
149 if( !is_null( $retval ) && count( $params['params'] ) ) {
150 foreach ( $params['params'] as $key => $value ) {
151 $retval = $this->doReplace( '$' . $key, $value, $retval );
152 }
153 }
154 return $retval;
155 }
156
157 /**
158 * Type-safe string replace; won't do replacements on non-strings
159 * private?
160 *
161 * @param $from
162 * @param $to
163 * @param $in
164 * @return string
165 */
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 );
172 }
173 return $in;
174 } else {
175 return $in;
176 }
177 }
178
179 /**
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.
186 */
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 ) {
191 $append = false;
192 $var = $varname;
193 if ( substr( $varname, 0, 1 ) == '+' ) {
194 $append = true;
195 $var = substr( $varname, 1 );
196 }
197
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;
203 }
204 }
205 return $localSettings;
206 }
207
208 /**
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.
215 */
216 public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) {
217 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
218 }
219
220 /**
221 * Retrieves an array of local databases
222 *
223 * @return array
224 */
225 function &getLocalDatabases() {
226 return $this->wikis;
227 }
228
229 /**
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.
237 */
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 ) ) {
241 $var = $value;
242 }
243 }
244
245 /**
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.
252 */
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 );
256 }
257
258 /**
259 * @param $setting string
260 * @param $wiki string
261 * @param $params array
262 */
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 );
270 } else {
271 $GLOBALS[$setting] = $value;
272 }
273 } else {
274 $GLOBALS[$setting] = $value;
275 }
276 }
277 }
278
279 /**
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.
285 */
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 );
290 }
291 }
292
293 /**
294 * Return specific settings for $wiki
295 * See the documentation of self::$siteParamsCallback for more in-depth
296 * documentation about this function
297 *
298 * @param $wiki String
299 * @return array
300 */
301 protected function getWikiParams( $wiki ){
302 static $default = array(
303 'suffix' => null,
304 'lang' => null,
305 'tags' => array(),
306 'params' => array(),
307 );
308
309 if( !is_callable( $this->siteParamsCallback ) ) {
310 return $default;
311 }
312
313 $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) );
314 # Validate the returned value
315 if( !is_array( $ret ) ) {
316 return $default;
317 }
318
319 foreach( $default as $name => $def ){
320 if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) )
321 $ret[$name] = $default[$name];
322 }
323
324 return $ret;
325 }
326
327 /**
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.
331 *
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
335 * all returned data.
336 * @param $wikiTags Array The tags assigned to the wiki.
337 * @return array
338 */
339 protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ){
340 $ret = $this->getWikiParams( $wiki );
341
342 if( is_null( $ret['suffix'] ) )
343 $ret['suffix'] = $suffix;
344
345 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
346
347 $ret['params'] += $params;
348
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'];
354
355 return $ret;
356 }
357
358 /**
359 * Work out the site and language name from a database name
360 * @param $db
361 *
362 * @return array
363 */
364 public function siteFromDB( $db ) {
365 // Allow override
366 $def = $this->getWikiParams( $db );
367 if( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) )
368 return array( $def['suffix'], $def['lang'] );
369
370 $site = null;
371 $lang = null;
372 foreach ( $this->suffixes as $suffix ) {
373 if ( $suffix === '' ) {
374 $site = '';
375 $lang = $db;
376 break;
377 } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
378 $site = $suffix == 'wiki' ? 'wikipedia' : $suffix;
379 $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
380 break;
381 }
382 }
383 $lang = str_replace( '_', '-', $lang );
384 return array( $site, $lang );
385 }
386
387 /**
388 * Returns true if the given vhost is handled locally.
389 * @param $vhost String
390 * @return bool
391 */
392 public function isLocalVHost( $vhost ) {
393 return in_array( $vhost, $this->localVHosts );
394 }
395
396 /**
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,
400 * which is not fun
401 *
402 * @param $array1 array
403 *
404 * @return array
405 */
406 static function arrayMerge( $array1/* ... */ ) {
407 $out = $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.
414 $out[$key] = $value;
415 } elseif ( is_numeric( $key ) ) {
416 $out[] = $value;
417 }
418 }
419 }
420
421 return $out;
422 }
423
424 public function loadFullData() {
425 if ($this->fullLoadCallback && !$this->fullLoadDone) {
426 call_user_func( $this->fullLoadCallback, $this );
427 $this->fullLoadDone = true;
428 }
429 }
430 }