[FileRepo] Purging/transaction fixes.
[lhc/web/wiklou.git] / includes / SiteConfiguration.php
1 <?php
2 /**
3 * This is a class used to hold configuration settings, particularly for multi-wiki sites.
4 */
5 class SiteConfiguration {
6
7 /**
8 * Array of suffixes, for self::siteFromDB()
9 */
10 public $suffixes = array();
11
12 /**
13 * Array of wikis, should be the same as $wgLocalDatabases
14 */
15 public $wikis = array();
16
17 /**
18 * The whole array of settings
19 */
20 public $settings = array();
21
22 /**
23 * Array of domains that are local and can be handled by the same server
24 */
25 public $localVHosts = array();
26
27 /**
28 * Optional callback to load full configuration data.
29 */
30 public $fullLoadCallback = null;
31
32 /** Whether or not all data has been loaded */
33 public $fullLoadDone = false;
34
35 /**
36 * A callback function that returns an array with the following keys (all
37 * optional):
38 * - suffix: site's suffix
39 * - lang: site's lang
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
46 */
47 public $siteParamsCallback = null;
48
49 /**
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.
57 */
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 );
61 }
62
63 /**
64 * Really retrieves a configuration setting for a given wiki.
65 *
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.
70 */
71 protected function getSetting( $settingName, $wiki, /*array*/ $params ){
72 $retval = null;
73 if( array_key_exists( $settingName, $this->settings ) ) {
74 $thisSetting =& $this->settings[$settingName];
75 do {
76 // Do individual wiki settings
77 if( array_key_exists( $wiki, $thisSetting ) ) {
78 $retval = $thisSetting[$wiki];
79 break;
80 } elseif( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
81 $retval = $thisSetting["+$wiki"];
82 }
83
84 // Do tag settings
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] );
89 } else {
90 $retval = $thisSetting[$tag];
91 }
92 break 2;
93 } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) {
94 if( !isset( $retval ) )
95 $retval = array();
96 $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] );
97 }
98 }
99 // Do suffix settings
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] );
105 } else {
106 $retval = $thisSetting[$suffix];
107 }
108 break;
109 } elseif( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) {
110 if (!isset($retval))
111 $retval = array();
112 $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] );
113 }
114 }
115
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'] );
120 } else {
121 $retval = $thisSetting['default'];
122 }
123 break;
124 }
125 } while ( false );
126 }
127
128 if( !is_null( $retval ) && count( $params['params'] ) ) {
129 foreach ( $params['params'] as $key => $value ) {
130 $retval = $this->doReplace( '$' . $key, $value, $retval );
131 }
132 }
133 return $retval;
134 }
135
136 /**
137 * Type-safe string replace; won't do replacements on non-strings
138 * private?
139 *
140 * @param $from
141 * @param $to
142 * @param $in
143 * @return string
144 */
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 );
151 }
152 return $in;
153 } else {
154 return $in;
155 }
156 }
157
158 /**
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.
165 */
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 ) {
170 $append = false;
171 $var = $varname;
172 if ( substr( $varname, 0, 1 ) == '+' ) {
173 $append = true;
174 $var = substr( $varname, 1 );
175 }
176
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;
182 }
183 }
184 return $localSettings;
185 }
186
187 /**
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.
194 */
195 public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) {
196 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
197 }
198
199 /**
200 * Retrieves an array of local databases
201 *
202 * @return array
203 */
204 function &getLocalDatabases() {
205 return $this->wikis;
206 }
207
208 /**
209 * Retrieves the value of a given setting, and places it in a variable passed by reference.
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 $var array Reference The variable to insert the value into.
214 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
215 * @param $wikiTags Array The tags assigned to the wiki.
216 */
217 public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) {
218 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
219 if ( !is_null( $value ) ) {
220 $var = $value;
221 }
222 }
223
224 /**
225 * Retrieves the value of a given setting, and places it in its corresponding global variable.
226 * @param $setting String ID of the setting name to retrieve
227 * @param $wiki String Wiki ID of the wiki in question.
228 * @param $suffix String The suffix of the wiki in question.
229 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
230 * @param $wikiTags Array The tags assigned to the wiki.
231 */
232 public function extractGlobal( $setting, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
233 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
234 $this->extractGlobalSetting( $setting, $wiki, $params );
235 }
236
237 /**
238 * @param $setting string
239 * @param $wiki string
240 * @param $params array
241 */
242 public function extractGlobalSetting( $setting, $wiki, $params ) {
243 $value = $this->getSetting( $setting, $wiki, $params );
244 if ( !is_null( $value ) ) {
245 if (substr($setting,0,1) == '+' && is_array($value)) {
246 $setting = substr($setting,1);
247 if ( is_array($GLOBALS[$setting]) ) {
248 $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value );
249 } else {
250 $GLOBALS[$setting] = $value;
251 }
252 } else {
253 $GLOBALS[$setting] = $value;
254 }
255 }
256 }
257
258 /**
259 * Retrieves the values of all settings, and places them in their corresponding global variables.
260 * @param $wiki String Wiki ID of the wiki in question.
261 * @param $suffix String The suffix of the wiki in question.
262 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
263 * @param $wikiTags Array The tags assigned to the wiki.
264 */
265 public function extractAllGlobals( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
266 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
267 foreach ( $this->settings as $varName => $setting ) {
268 $this->extractGlobalSetting( $varName, $wiki, $params );
269 }
270 }
271
272 /**
273 * Return specific settings for $wiki
274 * See the documentation of self::$siteParamsCallback for more in-depth
275 * documentation about this function
276 *
277 * @param $wiki String
278 * @return array
279 */
280 protected function getWikiParams( $wiki ){
281 static $default = array(
282 'suffix' => null,
283 'lang' => null,
284 'tags' => array(),
285 'params' => array(),
286 );
287
288 if( !is_callable( $this->siteParamsCallback ) ) {
289 return $default;
290 }
291
292 $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) );
293 # Validate the returned value
294 if( !is_array( $ret ) ) {
295 return $default;
296 }
297
298 foreach( $default as $name => $def ){
299 if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) )
300 $ret[$name] = $default[$name];
301 }
302
303 return $ret;
304 }
305
306 /**
307 * Merge params between the ones passed to the function and the ones given
308 * by self::$siteParamsCallback for backward compatibility
309 * Values returned by self::getWikiParams() have the priority.
310 *
311 * @param $wiki String Wiki ID of the wiki in question.
312 * @param $suffix String The suffix of the wiki in question.
313 * @param $params Array List of parameters. $.'key' is replaced by $value in
314 * all returned data.
315 * @param $wikiTags Array The tags assigned to the wiki.
316 * @return array
317 */
318 protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ){
319 $ret = $this->getWikiParams( $wiki );
320
321 if( is_null( $ret['suffix'] ) )
322 $ret['suffix'] = $suffix;
323
324 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
325
326 $ret['params'] += $params;
327
328 // Automatically fill that ones if needed
329 if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) )
330 $ret['params']['lang'] = $ret['lang'];
331 if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) )
332 $ret['params']['site'] = $ret['suffix'];
333
334 return $ret;
335 }
336
337 /**
338 * Work out the site and language name from a database name
339 * @param $db
340 *
341 * @return array
342 */
343 public function siteFromDB( $db ) {
344 // Allow override
345 $def = $this->getWikiParams( $db );
346 if( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) )
347 return array( $def['suffix'], $def['lang'] );
348
349 $site = null;
350 $lang = null;
351 foreach ( $this->suffixes as $suffix ) {
352 if ( $suffix === '' ) {
353 $site = '';
354 $lang = $db;
355 break;
356 } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
357 $site = $suffix == 'wiki' ? 'wikipedia' : $suffix;
358 $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
359 break;
360 }
361 }
362 $lang = str_replace( '_', '-', $lang );
363 return array( $site, $lang );
364 }
365
366 /**
367 * Returns true if the given vhost is handled locally.
368 * @param $vhost String
369 * @return bool
370 */
371 public function isLocalVHost( $vhost ) {
372 return in_array( $vhost, $this->localVHosts );
373 }
374
375 /**
376 * Merge multiple arrays together.
377 * On encountering duplicate keys, merge the two, but ONLY if they're arrays.
378 * PHP's array_merge_recursive() merges ANY duplicate values into arrays,
379 * which is not fun
380 *
381 * @param $array1 array
382 *
383 * @return array
384 */
385 static function arrayMerge( $array1/* ... */ ) {
386 $out = $array1;
387 for( $i = 1; $i < func_num_args(); $i++ ) {
388 foreach( func_get_arg( $i ) as $key => $value ) {
389 if ( isset($out[$key]) && is_array($out[$key]) && is_array($value) ) {
390 $out[$key] = self::arrayMerge( $out[$key], $value );
391 } elseif ( !isset($out[$key]) || !$out[$key] && !is_numeric($key) ) {
392 // Values that evaluate to true given precedence, for the primary purpose of merging permissions arrays.
393 $out[$key] = $value;
394 } elseif ( is_numeric( $key ) ) {
395 $out[] = $value;
396 }
397 }
398 }
399
400 return $out;
401 }
402
403 public function loadFullData() {
404 if ($this->fullLoadCallback && !$this->fullLoadDone) {
405 call_user_func( $this->fullLoadCallback, $this );
406 $this->fullLoadDone = true;
407 }
408 }
409 }