]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/SiteConfiguration.php
MediaWiki 1.17.1
[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          * 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         function doReplace( $from, $to, $in ) {
154                 if( is_string( $in ) ) {
155                         return str_replace( $from, $to, $in );
156                 } elseif( is_array( $in ) ) {
157                         foreach( $in as $key => $val ) {
158                                 $in[$key] = $this->doReplace( $from, $to, $val );
159                         }
160                         return $in;
161                 } else {
162                         return $in;
163                 }
164         }
165
166         /**
167          * Gets all settings for a wiki
168          * @param $wiki String Wiki ID of the wiki in question.
169          * @param $suffix String The suffix of the wiki in question.
170          * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
171          * @param $wikiTags Array The tags assigned to the wiki.
172          * @return Array Array of settings requested.
173          */
174         public function getAll( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
175                 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
176                 $localSettings = array();
177                 foreach( $this->settings as $varname => $stuff ) {
178                         $append = false;
179                         $var = $varname;
180                         if ( substr( $varname, 0, 1 ) == '+' ) {
181                                 $append = true;
182                                 $var = substr( $varname, 1 );
183                         }
184
185                         $value = $this->getSetting( $varname, $wiki, $params );
186                         if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) )
187                                 $value = self::arrayMerge( $value, $GLOBALS[$var] );
188                         if ( !is_null( $value ) ) {
189                                 $localSettings[$var] = $value;
190                         }
191                 }
192                 return $localSettings;
193         }
194
195         /**
196          * Retrieves a configuration setting for a given wiki, forced to a boolean.
197          * @param $setting String ID of the setting name to retrieve
198          * @param $wiki String Wiki ID of the wiki in question.
199          * @param $suffix String The suffix of the wiki in question.
200          * @param $wikiTags Array The tags assigned to the wiki.
201          * @return bool The value of the setting requested.
202          */
203         public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) {
204                 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
205         }
206
207         /** Retrieves an array of local databases */
208         function &getLocalDatabases() {
209                 return $this->wikis;
210         }
211
212         /** A no-op */
213         function initialise() {
214         }
215
216         /**
217          * Retrieves the value of a given setting, and places it in a variable passed by reference.
218          * @param $setting String ID of the setting name to retrieve
219          * @param $wiki String Wiki ID of the wiki in question.
220          * @param $suffix String The suffix of the wiki in question.
221          * @param $var Reference The variable to insert the value into.
222          * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
223          * @param $wikiTags Array The tags assigned to the wiki.
224          */
225         public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) {
226                 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
227                 if ( !is_null( $value ) ) {
228                         $var = $value;
229                 }
230         }
231
232         /**
233          * Retrieves the value of a given setting, and places it in its corresponding global variable.
234          * @param $setting String ID of the setting name to retrieve
235          * @param $wiki String Wiki ID of the wiki in question.
236          * @param $suffix String The suffix of the wiki in question.
237          * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
238          * @param $wikiTags Array The tags assigned to the wiki.
239          */
240         public function extractGlobal( $setting, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
241                 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
242                 $this->extractGlobalSetting( $setting, $wiki, $params );
243         }
244
245         public function extractGlobalSetting( $setting, $wiki, $params ) {
246                 $value = $this->getSetting( $setting, $wiki, $params );
247                 if ( !is_null( $value ) ) {
248                         if (substr($setting,0,1) == '+' && is_array($value)) {
249                                 $setting = substr($setting,1);
250                                 if ( is_array($GLOBALS[$setting]) ) {
251                                         $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value );
252                                 } else {
253                                         $GLOBALS[$setting] = $value;
254                                 }
255                         } else {
256                                 $GLOBALS[$setting] = $value;
257                         }
258                 }
259         }
260
261         /**
262          * Retrieves the values of all settings, and places them in their corresponding global variables.
263          * @param $wiki String Wiki ID of the wiki in question.
264          * @param $suffix String The suffix of the wiki in question.
265          * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
266          * @param $wikiTags Array The tags assigned to the wiki.
267          */
268         public function extractAllGlobals( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
269                 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
270                 foreach ( $this->settings as $varName => $setting ) {
271                         $this->extractGlobalSetting( $varName, $wiki, $params );
272                 }
273         }
274
275         /**
276          * Return specific settings for $wiki
277          * See the documentation of self::$siteParamsCallback for more in-depth
278          * documentation about this function
279          *
280          * @param $wiki String
281          * @return array
282          */
283         protected function getWikiParams( $wiki ){
284                 static $default = array(
285                         'suffix' => null,
286                         'lang' => null,
287                         'tags' => array(),
288                         'params' => array(),
289                 );
290
291                 if( !is_callable( $this->siteParamsCallback ) )
292                         return $default;
293
294                 $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) );
295                 # Validate the returned value
296                 if( !is_array( $ret ) ) {
297                         return $default;
298                 }
299
300                 foreach( $default as $name => $def ){
301                         if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) )
302                                 $ret[$name] = $default[$name];
303                 }
304
305                 return $ret;
306         }
307
308         /**
309          * Merge params beetween the ones passed to the function and the ones given
310          * by self::$siteParamsCallback for backward compatibility
311          * Values returned by self::getWikiParams() have the priority.
312          *
313          * @param $wiki String Wiki ID of the wiki in question.
314          * @param $suffix String The suffix of the wiki in question.
315          * @param $params Array List of parameters. $.'key' is replaced by $value in
316          *                all returned data.
317          * @param $wikiTags Array The tags assigned to the wiki.
318          * @return array
319          */
320         protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ){
321                 $ret = $this->getWikiParams( $wiki );
322
323                 if( is_null( $ret['suffix'] ) )
324                         $ret['suffix'] = $suffix;
325
326                 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
327
328                 $ret['params'] += $params;
329
330                 // Automatically fill that ones if needed
331                 if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) )
332                         $ret['params']['lang'] = $ret['lang'];
333                 if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) )
334                         $ret['params']['site'] = $ret['suffix'];
335
336                 return $ret;
337         }
338
339         /**
340          * Work out the site and language name from a database name
341          * @param $db
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         static function arrayMerge( $array1/* ... */ ) {
382                 $out = $array1;
383                 for( $i=1; $i < func_num_args(); $i++ ) {
384                         foreach( func_get_arg( $i ) as $key => $value ) {
385                                 if ( isset($out[$key]) && is_array($out[$key]) && is_array($value) ) {
386                                         $out[$key] = self::arrayMerge( $out[$key], $value );
387                                 } elseif ( !isset($out[$key]) || !$out[$key] && !is_numeric($key) ) {
388                                         // Values that evaluate to true given precedence, for the primary purpose of merging permissions arrays.
389                                         $out[$key] = $value;
390                                 } elseif ( is_numeric( $key ) ) {
391                                         $out[] = $value;
392                                 }
393                         }
394                 }
395
396                 return $out;
397         }
398         
399         public function loadFullData() {
400                 if ($this->fullLoadCallback && !$this->fullLoadDone) {
401                         call_user_func( $this->fullLoadCallback, $this );
402                         $this->fullLoadDone = true;
403                 }
404         }
405 }
406 } // End of multiple inclusion guard