]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/SiteConfiguration.php
MediaWiki 1.14.0
[autoinstallsdev/mediawiki.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          * A callback function that returns an array with the following keys (all
42          * optional):
43          * - suffix: site's suffix
44          * - lang: site's lang
45          * - tags: array of wiki tags
46          * - params: array of parameters to be replaced
47          * The function will receive the SiteConfiguration instance in the first
48          * argument and the wiki in the second one.
49          * if suffix and lang are passed they will be used for the return value of
50          * self::siteFromDB() and self::$suffixes will be ignored
51          */
52         public $siteParamsCallback = null;
53
54         /**
55          * Retrieves a configuration setting for a given wiki.
56          * @param $settingName String ID of the setting name to retrieve
57          * @param $wiki String Wiki ID of the wiki in question.
58          * @param $suffix String The suffix of the wiki in question.
59          * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
60          * @param $wikiTags Array The tags assigned to the wiki.
61          * @return Mixed the value of the setting requested.
62          */
63         public function get( $settingName, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
64                 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
65                 return $this->getSetting( $settingName, $wiki, $params );
66         }
67
68         /**
69          * Really retrieves a configuration setting for a given wiki.
70          *
71          * @param $settingName String ID of the setting name to retrieve.
72          * @param $wiki String Wiki ID of the wiki in question.
73          * @param $params Array: array of parameters.
74          * @return Mixed the value of the setting requested.
75          */
76         protected function getSetting( $settingName, $wiki, /*array*/ $params ){
77                 $retval = null;
78                 if( array_key_exists( $settingName, $this->settings ) ) {
79                         $thisSetting =& $this->settings[$settingName];
80                         do {
81                                 // Do individual wiki settings
82                                 if( array_key_exists( $wiki, $thisSetting ) ) {
83                                         $retval = $thisSetting[$wiki];
84                                         break;
85                                 } elseif( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
86                                         $retval = $thisSetting["+$wiki"];
87                                 }
88
89                                 // Do tag settings
90                                 foreach( $params['tags'] as $tag ) {
91                                         if( array_key_exists( $tag, $thisSetting ) ) {
92                                                 if ( isset( $retval ) && is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
93                                                         $retval = self::arrayMerge( $retval, $thisSetting[$tag] );
94                                                 } else {
95                                                         $retval = $thisSetting[$tag];
96                                                 }
97                                                 break 2;
98                                         } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) {
99                                                 if( !isset( $retval ) )
100                                                         $retval = array();
101                                                 $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] );
102                                         }
103                                 }
104                                 // Do suffix settings
105                                 $suffix = $params['suffix'];
106                                 if( !is_null( $suffix ) ) {
107                                         if( array_key_exists( $suffix, $thisSetting ) ) {
108                                                 if ( isset($retval) && is_array($retval) && is_array($thisSetting[$suffix]) ) {
109                                                         $retval = self::arrayMerge( $retval, $thisSetting[$suffix] );
110                                                 } else {
111                                                         $retval = $thisSetting[$suffix];
112                                                 }
113                                                 break;
114                                         } elseif( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) {
115                                                 if (!isset($retval))
116                                                         $retval = array();
117                                                 $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] );
118                                         }
119                                 }
120
121                                 // Fall back to default.
122                                 if( array_key_exists( 'default', $thisSetting ) ) {
123                                         if( is_array( $retval ) && is_array( $thisSetting['default'] ) ) {
124                                                 $retval = self::arrayMerge( $retval, $thisSetting['default'] );
125                                         } else {
126                                                 $retval = $thisSetting['default'];
127                                         }
128                                         break;
129                                 }
130                         } while ( false );
131                 }
132
133                 if( !is_null( $retval ) && count( $params['params'] ) ) {
134                         foreach ( $params['params'] as $key => $value ) {
135                                 $retval = $this->doReplace( '$' . $key, $value, $retval );
136                         }
137                 }
138                 return $retval;
139         }
140
141         /**
142          * Type-safe string replace; won't do replacements on non-strings
143          * private?
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 $settingName 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 $params Array List of parameters. $.'key' is replaced by $value in all returned data.
193          * @param $wikiTags Array The tags assigned to the wiki.
194          * @return bool The value of the setting requested.
195          */
196         public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) {
197                 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
198         }
199
200         /** Retrieves an array of local databases */
201         function &getLocalDatabases() {
202                 return $this->wikis;
203         }
204
205         /** A no-op */
206         function initialise() {
207         }
208
209         /**
210          * Retrieves the value of a given setting, and places it in a variable passed by reference.
211          * @param $settingName String ID of the setting name to retrieve
212          * @param $wiki String Wiki ID of the wiki in question.
213          * @param $suffix String The suffix of the wiki in question.
214          * @param $var Reference The variable to insert the value into.
215          * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
216          * @param $wikiTags Array The tags assigned to the wiki.
217          */
218         public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) {
219                 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
220                 if ( !is_null( $value ) ) {
221                         $var = $value;
222                 }
223         }
224
225         /**
226          * Retrieves the value of a given setting, and places it in its corresponding global variable.
227          * @param $settingName 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 $params Array List of parameters. $.'key' is replaced by $value in all returned data.
231          * @param $wikiTags Array The tags assigned to the wiki.
232          */
233         public function extractGlobal( $setting, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
234                 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
235                 $this->extractGlobalSetting( $setting, $wiki, $params );
236         }
237
238         public function extractGlobalSetting( $setting, $wiki, $params ) {
239                 $value = $this->getSetting( $setting, $wiki, $params );
240                 if ( !is_null( $value ) ) {
241                         if (substr($setting,0,1) == '+' && is_array($value)) {
242                                 $setting = substr($setting,1);
243                                 if ( is_array($GLOBALS[$setting]) ) {
244                                         $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value );
245                                 } else {
246                                         $GLOBALS[$setting] = $value;
247                                 }
248                         } else {
249                                 $GLOBALS[$setting] = $value;
250                         }
251                 }
252         }
253
254         /**
255          * Retrieves the values of all settings, and places them in their corresponding global variables.
256          * @param $wiki String Wiki ID of the wiki in question.
257          * @param $suffix String The suffix of the wiki in question.
258          * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
259          * @param $wikiTags Array The tags assigned to the wiki.
260          */
261         public function extractAllGlobals( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
262                 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
263                 foreach ( $this->settings as $varName => $setting ) {
264                         $this->extractGlobalSetting( $varName, $wiki, $params );
265                 }
266         }
267
268         /**
269          * Return specific settings for $wiki
270          * See the documentation of self::$siteParamsCallback for more in-depth
271          * documentation about this function
272          *
273          * @param $wiki String
274          * @return array
275          */
276         protected function getWikiParams( $wiki ){
277                 static $default = array(
278                         'suffix' => null,
279                         'lang' => null,
280                         'tags' => array(),
281                         'params' => array(),
282                 );
283
284                 if( !is_callable( $this->siteParamsCallback ) )
285                         return $default;
286
287                 $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) );
288                 # Validate the returned value
289                 if( !is_array( $ret ) )
290                         return $default;
291
292                 foreach( $default as $name => $def ){
293                         if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) )
294                                 $ret[$name] = $default[$name];
295                 }
296
297                 return $ret;
298         }
299
300         /**
301          * Merge params beetween the ones passed to the function and the ones given
302          * by self::$siteParamsCallback for backward compatibility
303          * Values returned by self::getWikiParams() have the priority.
304          *
305          * @param $wiki String Wiki ID of the wiki in question.
306          * @param $suffix String The suffix of the wiki in question.
307          * @param $params Array List of parameters. $.'key' is replaced by $value in
308          *                all returned data.
309          * @param $wikiTags Array The tags assigned to the wiki.
310          * @return array
311          */
312         protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ){
313                 $ret = $this->getWikiParams( $wiki );
314
315                 if( is_null( $ret['suffix'] ) )
316                         $ret['suffix'] = $suffix;
317
318                 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
319
320                 $ret['params'] += $params;
321
322                 // Automatically fill that ones if needed
323                 if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) )
324                         $ret['params']['lang'] = $ret['lang'];
325                 if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) )
326                         $ret['params']['site'] = $ret['suffix'];
327
328                 return $ret;
329         }
330
331         /**
332          * Work out the site and language name from a database name
333          * @param $db
334          */
335         public function siteFromDB( $db ) {
336                 // Allow override
337                 $def = $this->getWikiParams( $db );
338                 if( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) )
339                         return array( $def['suffix'], $def['lang'] );
340
341                 $site = null;
342                 $lang = null;
343                 foreach ( $this->suffixes as $suffix ) {
344                         if ( $suffix === '' ) {
345                                 $site = '';
346                                 $lang = $db;
347                                 break;
348                         } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
349                                 $site = $suffix == 'wiki' ? 'wikipedia' : $suffix;
350                                 $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
351                                 break;
352                         }
353                 }
354                 $lang = str_replace( '_', '-', $lang );
355                 return array( $site, $lang );
356         }
357
358         /**
359          * Returns true if the given vhost is handled locally.
360          * @param $vhost String
361          * @return bool
362          */
363         public function isLocalVHost( $vhost ) {
364                 return in_array( $vhost, $this->localVHosts );
365         }
366
367         /**
368          * Merge multiple arrays together.
369          * On encountering duplicate keys, merge the two, but ONLY if they're arrays.
370          * PHP's array_merge_recursive() merges ANY duplicate values into arrays,
371          * which is not fun
372          */
373         static function arrayMerge( $array1/* ... */ ) {
374                 $out = $array1;
375                 for( $i=1; $i < func_num_args(); $i++ ) {
376                         foreach( func_get_arg( $i ) as $key => $value ) {
377                                 if ( isset($out[$key]) && is_array($out[$key]) && is_array($value) ) {
378                                         $out[$key] = self::arrayMerge( $out[$key], $value );
379                                 } elseif ( !isset($out[$key]) || !$out[$key] && !is_numeric($key) ) {
380                                         // Values that evaluate to true given precedence, for the primary purpose of merging permissions arrays.
381                                         $out[$key] = $value;
382                                 } elseif ( is_numeric( $key ) ) {
383                                         $out[] = $value;
384                                 }
385                         }
386                 }
387
388                 return $out;
389         }
390 }
391 }