Index: trunk/phase3/includes/SiteConfiguration.php |
— | — | @@ -8,50 +8,21 @@ |
9 | 9 | |
10 | 10 | // Hide this pattern from Doxygen, which spazzes out at it |
11 | 11 | /// @cond |
12 | | -if( !defined( 'SITE_CONFIGURATION' ) ){ |
13 | | -define( 'SITE_CONFIGURATION', 1 ); |
| 12 | +if (!defined('SITE_CONFIGURATION')) { |
| 13 | +define('SITE_CONFIGURATION', 1); |
14 | 14 | /// @endcond |
15 | 15 | |
16 | 16 | /** |
17 | 17 | * This is a class used to hold configuration settings, particularly for multi-wiki sites. |
| 18 | + * |
18 | 19 | */ |
19 | 20 | class SiteConfiguration { |
| 21 | + var $suffixes = array(); |
| 22 | + var $wikis = array(); |
| 23 | + var $settings = array(); |
| 24 | + var $localVHosts = array(); |
20 | 25 | |
21 | 26 | /** |
22 | | - * Array of suffixes, for self::siteFromDB() |
23 | | - */ |
24 | | - public $suffixes = array(); |
25 | | - |
26 | | - /** |
27 | | - * Array of wikis, should be the same as $wgLocalDatabases |
28 | | - */ |
29 | | - public $wikis = array(); |
30 | | - |
31 | | - /** |
32 | | - * The whole array of settings |
33 | | - */ |
34 | | - public $settings = array(); |
35 | | - |
36 | | - /** |
37 | | - * Array of domains that are local and can be handled by the same server |
38 | | - */ |
39 | | - public $localVHosts = array(); |
40 | | - |
41 | | - /** |
42 | | - * A callback function that returns an array with the following keys (all |
43 | | - * optional): |
44 | | - * - suffix: site's suffix |
45 | | - * - lang: site's lang |
46 | | - * - tags: array of wiki tags |
47 | | - * - params: array of parameters to be replaced |
48 | | - * The function will receive the SiteConfiguration instance in the first |
49 | | - * argument and the wiki in the second one. |
50 | | - * if suffix and lang are passed they will be used for the return value of |
51 | | - * self::siteFromDB() and self::$suffixes will be ignored |
52 | | - */ |
53 | | - public $siteParamsCallback = null; |
54 | | - |
55 | | - /** |
56 | 27 | * Retrieves a configuration setting for a given wiki. |
57 | 28 | * @param $settingName String ID of the setting name to retrieve |
58 | 29 | * @param $wiki String Wiki ID of the wiki in question. |
— | — | @@ -60,88 +31,72 @@ |
61 | 32 | * @param $wikiTags Array The tags assigned to the wiki. |
62 | 33 | * @return Mixed the value of the setting requested. |
63 | 34 | */ |
64 | | - public function get( $settingName, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) { |
65 | | - $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags ); |
66 | | - return $this->getSetting( $settingName, $wiki, $params ); |
67 | | - } |
68 | | - |
69 | | - /** |
70 | | - * Really retrieves a configuration setting for a given wiki. |
71 | | - * |
72 | | - * @param $settingName String ID of the setting name to retrieve. |
73 | | - * @param $wiki String Wiki ID of the wiki in question. |
74 | | - * @param $params Array: array of parameters. |
75 | | - * @return Mixed the value of the setting requested. |
76 | | - */ |
77 | | - protected function getSetting( $settingName, $wiki, /*array*/ $params ){ |
78 | | - $retval = null; |
79 | | - if( array_key_exists( $settingName, $this->settings ) ) { |
| 35 | + function get( $settingName, $wiki, $suffix, $params = array(), $wikiTags = array() ) { |
| 36 | + if ( array_key_exists( $settingName, $this->settings ) ) { |
80 | 37 | $thisSetting =& $this->settings[$settingName]; |
81 | 38 | do { |
82 | 39 | // Do individual wiki settings |
83 | | - if( array_key_exists( $wiki, $thisSetting ) ) { |
| 40 | + if ( array_key_exists( $wiki, $thisSetting ) ) { |
84 | 41 | $retval = $thisSetting[$wiki]; |
85 | 42 | break; |
86 | | - } elseif( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) { |
| 43 | + } elseif ( array_key_exists( "+$wiki", $thisSetting ) && is_array($thisSetting["+$wiki"]) ) { |
87 | 44 | $retval = $thisSetting["+$wiki"]; |
88 | 45 | } |
89 | 46 | |
90 | 47 | // Do tag settings |
91 | | - foreach( $params['tags'] as $tag ) { |
92 | | - if( array_key_exists( $tag, $thisSetting ) ) { |
93 | | - if ( isset( $retval ) && is_array( $retval ) && is_array( $thisSetting[$tag] ) ) { |
94 | | - $retval = self::arrayMerge( $retval, $thisSetting[$tag] ); |
| 48 | + foreach ( $wikiTags as $tag ) { |
| 49 | + if ( array_key_exists( $tag, $thisSetting ) ) { |
| 50 | + if ( isset($retval) && is_array($retval) && is_array($thisSetting[$tag]) ) { |
| 51 | + $retval = array_merge( $retval, $thisSetting[$tag] ); |
95 | 52 | } else { |
96 | 53 | $retval = $thisSetting[$tag]; |
97 | 54 | } |
98 | 55 | break 2; |
99 | | - } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) { |
100 | | - if( !isset( $retval ) ) |
| 56 | + } elseif ( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) { |
| 57 | + if (!isset($retval)) |
101 | 58 | $retval = array(); |
102 | | - $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] ); |
| 59 | + $retval = array_merge( $retval, $thisSetting["+$tag"] ); |
103 | 60 | } |
104 | 61 | } |
| 62 | + |
105 | 63 | // Do suffix settings |
106 | | - $suffix = $params['suffix']; |
107 | | - if( !is_null( $suffix ) ) { |
108 | | - if( array_key_exists( $suffix, $thisSetting ) ) { |
109 | | - if ( isset($retval) && is_array($retval) && is_array($thisSetting[$suffix]) ) { |
110 | | - $retval = self::arrayMerge( $retval, $thisSetting[$suffix] ); |
111 | | - } else { |
112 | | - $retval = $thisSetting[$suffix]; |
113 | | - } |
114 | | - break; |
115 | | - } elseif( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) { |
116 | | - if (!isset($retval)) |
117 | | - $retval = array(); |
118 | | - $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] ); |
| 64 | + if ( array_key_exists( $suffix, $thisSetting ) ) { |
| 65 | + if ( isset($retval) && is_array($retval) && is_array($thisSetting[$suffix]) ) { |
| 66 | + $retval = array_merge( $retval, $thisSetting[$suffix] ); |
| 67 | + } else { |
| 68 | + $retval = $thisSetting[$suffix]; |
119 | 69 | } |
| 70 | + break; |
| 71 | + } elseif ( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) { |
| 72 | + if (!isset($retval)) |
| 73 | + $retval = array(); |
| 74 | + $retval = array_merge( $retval, $thisSetting["+$suffix"] ); |
120 | 75 | } |
121 | 76 | |
122 | 77 | // Fall back to default. |
123 | | - if( array_key_exists( 'default', $thisSetting ) ) { |
124 | | - if( is_array( $retval ) && is_array( $thisSetting['default'] ) ) { |
125 | | - $retval = self::arrayMerge( $retval, $thisSetting['default'] ); |
| 78 | + if ( array_key_exists( 'default', $thisSetting ) ) { |
| 79 | + if ( isset($retval) && is_array($retval) && is_array($thisSetting['default']) ) { |
| 80 | + $retval = array_merge( $retval, $thisSetting['default'] ); |
126 | 81 | } else { |
127 | 82 | $retval = $thisSetting['default']; |
128 | 83 | } |
129 | 84 | break; |
130 | 85 | } |
| 86 | + $retval = null; |
131 | 87 | } while ( false ); |
| 88 | + } else { |
| 89 | + $retval = NULL; |
132 | 90 | } |
133 | 91 | |
134 | | - if( !is_null( $retval ) && count( $params['params'] ) ) { |
135 | | - foreach ( $params['params'] as $key => $value ) { |
| 92 | + if ( !is_null( $retval ) && count( $params ) ) { |
| 93 | + foreach ( $params as $key => $value ) { |
136 | 94 | $retval = $this->doReplace( '$' . $key, $value, $retval ); |
137 | 95 | } |
138 | 96 | } |
139 | 97 | return $retval; |
140 | 98 | } |
141 | 99 | |
142 | | - /** |
143 | | - * Type-safe string replace; won't do replacements on non-strings |
144 | | - * private? |
145 | | - */ |
| 100 | + /** Type-safe string replace; won't do replacements on non-strings */ |
146 | 101 | function doReplace( $from, $to, $in ) { |
147 | 102 | if( is_string( $in ) ) { |
148 | 103 | return str_replace( $from, $to, $in ); |
— | — | @@ -163,20 +118,19 @@ |
164 | 119 | * @param $wikiTags Array The tags assigned to the wiki. |
165 | 120 | * @return Array Array of settings requested. |
166 | 121 | */ |
167 | | - public function getAll( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) { |
168 | | - $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags ); |
| 122 | + function getAll( $wiki, $suffix, $params, $wikiTags = array() ) { |
169 | 123 | $localSettings = array(); |
170 | | - foreach( $this->settings as $varname => $stuff ) { |
| 124 | + foreach ( $this->settings as $varname => $stuff ) { |
171 | 125 | $append = false; |
172 | 126 | $var = $varname; |
173 | 127 | if ( substr( $varname, 0, 1 ) == '+' ) { |
174 | 128 | $append = true; |
175 | 129 | $var = substr( $varname, 1 ); |
176 | 130 | } |
177 | | - |
178 | | - $value = $this->getSetting( $varname, $wiki, $params ); |
179 | | - if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) ) |
180 | | - $value = self::arrayMerge( $value, $GLOBALS[$var] ); |
| 131 | + |
| 132 | + $value = $this->get( $varname, $wiki, $suffix, $params, $wikiTags ); |
| 133 | + if ( $append && is_array($value) && is_array( $GLOBALS[$var] ) ) |
| 134 | + $value = array_merge( $value, $GLOBALS[$var] ); |
181 | 135 | if ( !is_null( $value ) ) { |
182 | 136 | $localSettings[$var] = $value; |
183 | 137 | } |
— | — | @@ -193,7 +147,7 @@ |
194 | 148 | * @param $wikiTags Array The tags assigned to the wiki. |
195 | 149 | * @return bool The value of the setting requested. |
196 | 150 | */ |
197 | | - public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) { |
| 151 | + function getBool( $setting, $wiki, $suffix, $wikiTags = array() ) { |
198 | 152 | return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) ); |
199 | 153 | } |
200 | 154 | |
— | — | @@ -215,7 +169,7 @@ |
216 | 170 | * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data. |
217 | 171 | * @param $wikiTags Array The tags assigned to the wiki. |
218 | 172 | */ |
219 | | - public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) { |
| 173 | + function extractVar( $setting, $wiki, $suffix, &$var, $params, $wikiTags = array() ) { |
220 | 174 | $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags ); |
221 | 175 | if ( !is_null( $value ) ) { |
222 | 176 | $var = $value; |
— | — | @@ -230,18 +184,13 @@ |
231 | 185 | * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data. |
232 | 186 | * @param $wikiTags Array The tags assigned to the wiki. |
233 | 187 | */ |
234 | | - public function extractGlobal( $setting, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) { |
235 | | - $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags ); |
236 | | - $this->extractGlobalSetting( $setting, $wiki, $params ); |
237 | | - } |
238 | | - |
239 | | - public function extractGlobalSetting( $setting, $wiki, $params ) { |
240 | | - $value = $this->getSetting( $setting, $wiki, $params ); |
| 188 | + function extractGlobal( $setting, $wiki, $suffix, $params, $wikiTags = array() ) { |
| 189 | + $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags ); |
241 | 190 | if ( !is_null( $value ) ) { |
242 | 191 | if (substr($setting,0,1) == '+' && is_array($value)) { |
243 | 192 | $setting = substr($setting,1); |
244 | 193 | if ( is_array($GLOBALS[$setting]) ) { |
245 | | - $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value ); |
| 194 | + $GLOBALS[$setting] = array_merge( $GLOBALS[$setting], $value ); |
246 | 195 | } else { |
247 | 196 | $GLOBALS[$setting] = $value; |
248 | 197 | } |
— | — | @@ -258,87 +207,23 @@ |
259 | 208 | * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data. |
260 | 209 | * @param $wikiTags Array The tags assigned to the wiki. |
261 | 210 | */ |
262 | | - public function extractAllGlobals( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) { |
263 | | - $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags ); |
| 211 | + function extractAllGlobals( $wiki, $suffix, $params, $wikiTags = array() ) { |
264 | 212 | foreach ( $this->settings as $varName => $setting ) { |
265 | | - $this->extractGlobalSetting( $varName, $wiki, $params ); |
| 213 | + $this->extractGlobal( $varName, $wiki, $suffix, $params, $wikiTags ); |
266 | 214 | } |
267 | 215 | } |
268 | 216 | |
269 | 217 | /** |
270 | | - * Return specific settings for $wiki |
271 | | - * See the documentation of self::$siteParamsCallback for more in-depth |
272 | | - * documentation about this function |
273 | | - * |
274 | | - * @param $wiki String |
275 | | - * @return array |
276 | | - */ |
277 | | - protected function getWikiParams( $wiki ){ |
278 | | - static $default = array( |
279 | | - 'suffix' => null, |
280 | | - 'lang' => null, |
281 | | - 'tags' => array(), |
282 | | - 'params' => array(), |
283 | | - ); |
284 | | - |
285 | | - if( !is_callable( $this->siteParamsCallback ) ) |
286 | | - return $default; |
287 | | - |
288 | | - $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) ); |
289 | | - # Validate the returned value |
290 | | - if( !is_array( $ret ) ) |
291 | | - return $default; |
292 | | - |
293 | | - foreach( $default as $name => $def ){ |
294 | | - if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) ) |
295 | | - $ret[$name] = $default[$name]; |
296 | | - } |
297 | | - |
298 | | - return $ret; |
299 | | - } |
300 | | - |
301 | | - /** |
302 | | - * Merge params beetween the ones passed to the function and the ones given |
303 | | - * by self::$siteParamsCallback for backward compatibility |
304 | | - * Values returned by self::getWikiParams() have the priority. |
305 | | - * |
306 | | - * @param $wiki String Wiki ID of the wiki in question. |
307 | | - * @param $suffix String The suffix of the wiki in question. |
308 | | - * @param $params Array List of parameters. $.'key' is replaced by $value in |
309 | | - * all returned data. |
310 | | - * @param $wikiTags Array The tags assigned to the wiki. |
311 | | - * @return array |
312 | | - */ |
313 | | - protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ){ |
314 | | - $ret = $this->getWikiParams( $wiki ); |
315 | | - |
316 | | - if( is_null( $ret['suffix'] ) ) |
317 | | - $ret['suffix'] = $suffix; |
318 | | - |
319 | | - $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) ); |
320 | | - |
321 | | - // Automatically fill that ones if needed |
322 | | - if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) ) |
323 | | - $ret['params']['lang'] = $ret['lang']; |
324 | | - if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) ) |
325 | | - $ret['params']['site'] = $ret['suffix']; |
326 | | - |
327 | | - $ret['params'] += $params; |
328 | | - return $ret; |
329 | | - } |
330 | | - |
331 | | - /** |
332 | 218 | * Work out the site and language name from a database name |
333 | 219 | * @param $db |
334 | 220 | */ |
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; |
| 221 | + function siteFromDB( $db ) { |
| 222 | + $site = NULL; |
| 223 | + $lang = NULL; |
| 224 | + |
| 225 | + // Only run hooks if they *can* be run. |
| 226 | + if (function_exists( 'wfRunHooks' ) && !wfRunHooks( 'SiteFromDB', array( $db, &$site, &$lang ) ) ) |
| 227 | + return array( $site, $lang ); |
343 | 228 | foreach ( $this->suffixes as $suffix ) { |
344 | 229 | if ( $suffix === '' ) { |
345 | 230 | $site = ''; |
— | — | @@ -354,37 +239,9 @@ |
355 | 240 | return array( $site, $lang ); |
356 | 241 | } |
357 | 242 | |
358 | | - /** |
359 | | - * Returns true if the given vhost is handled locally. |
360 | | - * @param $vhost String |
361 | | - * @return bool |
362 | | - */ |
363 | | - public function isLocalVHost( $vhost ) { |
| 243 | + /** Returns true if the given vhost is handled locally. */ |
| 244 | + function isLocalVHost( $vhost ) { |
364 | 245 | return in_array( $vhost, $this->localVHosts ); |
365 | 246 | } |
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 | 247 | } |
391 | 248 | } |