r39978 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r39977‎ | r39978 | r39979 >
Date:22:06, 25 August 2008
Author:brion
Status:old
Tags:
Comment:
Revert r39954, 39958 "Tweaks for SiteConfiguration:"
This seems to have broken upload dirs on live site (eg http://upload.wikimedia.org/wiki/commons/6/66/Diemetrie.png in place of http://upload.wikimedia.org/wikipedia/commons/6/66/Diemetrie.png)
Please make no further changes without some regression testing...
Modified paths:
  • /trunk/phase3/includes/SiteConfiguration.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/SiteConfiguration.php
@@ -8,50 +8,21 @@
99
1010 // Hide this pattern from Doxygen, which spazzes out at it
1111 /// @cond
12 -if( !defined( 'SITE_CONFIGURATION' ) ){
13 -define( 'SITE_CONFIGURATION', 1 );
 12+if (!defined('SITE_CONFIGURATION')) {
 13+define('SITE_CONFIGURATION', 1);
1414 /// @endcond
1515
1616 /**
1717 * This is a class used to hold configuration settings, particularly for multi-wiki sites.
 18+ *
1819 */
1920 class SiteConfiguration {
 21+ var $suffixes = array();
 22+ var $wikis = array();
 23+ var $settings = array();
 24+ var $localVHosts = array();
2025
2126 /**
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 - /**
5627 * Retrieves a configuration setting for a given wiki.
5728 * @param $settingName String ID of the setting name to retrieve
5829 * @param $wiki String Wiki ID of the wiki in question.
@@ -60,88 +31,72 @@
6132 * @param $wikiTags Array The tags assigned to the wiki.
6233 * @return Mixed the value of the setting requested.
6334 */
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 ) ) {
8037 $thisSetting =& $this->settings[$settingName];
8138 do {
8239 // Do individual wiki settings
83 - if( array_key_exists( $wiki, $thisSetting ) ) {
 40+ if ( array_key_exists( $wiki, $thisSetting ) ) {
8441 $retval = $thisSetting[$wiki];
8542 break;
86 - } elseif( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
 43+ } elseif ( array_key_exists( "+$wiki", $thisSetting ) && is_array($thisSetting["+$wiki"]) ) {
8744 $retval = $thisSetting["+$wiki"];
8845 }
8946
9047 // 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] );
9552 } else {
9653 $retval = $thisSetting[$tag];
9754 }
9855 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))
10158 $retval = array();
102 - $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] );
 59+ $retval = array_merge( $retval, $thisSetting["+$tag"] );
10360 }
10461 }
 62+
10563 // 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];
11969 }
 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"] );
12075 }
12176
12277 // 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'] );
12681 } else {
12782 $retval = $thisSetting['default'];
12883 }
12984 break;
13085 }
 86+ $retval = null;
13187 } while ( false );
 88+ } else {
 89+ $retval = NULL;
13290 }
13391
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 ) {
13694 $retval = $this->doReplace( '$' . $key, $value, $retval );
13795 }
13896 }
13997 return $retval;
14098 }
14199
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 */
146101 function doReplace( $from, $to, $in ) {
147102 if( is_string( $in ) ) {
148103 return str_replace( $from, $to, $in );
@@ -163,20 +118,19 @@
164119 * @param $wikiTags Array The tags assigned to the wiki.
165120 * @return Array Array of settings requested.
166121 */
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() ) {
169123 $localSettings = array();
170 - foreach( $this->settings as $varname => $stuff ) {
 124+ foreach ( $this->settings as $varname => $stuff ) {
171125 $append = false;
172126 $var = $varname;
173127 if ( substr( $varname, 0, 1 ) == '+' ) {
174128 $append = true;
175129 $var = substr( $varname, 1 );
176130 }
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] );
181135 if ( !is_null( $value ) ) {
182136 $localSettings[$var] = $value;
183137 }
@@ -193,7 +147,7 @@
194148 * @param $wikiTags Array The tags assigned to the wiki.
195149 * @return bool The value of the setting requested.
196150 */
197 - public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) {
 151+ function getBool( $setting, $wiki, $suffix, $wikiTags = array() ) {
198152 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
199153 }
200154
@@ -215,7 +169,7 @@
216170 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
217171 * @param $wikiTags Array The tags assigned to the wiki.
218172 */
219 - public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) {
 173+ function extractVar( $setting, $wiki, $suffix, &$var, $params, $wikiTags = array() ) {
220174 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
221175 if ( !is_null( $value ) ) {
222176 $var = $value;
@@ -230,18 +184,13 @@
231185 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
232186 * @param $wikiTags Array The tags assigned to the wiki.
233187 */
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 );
241190 if ( !is_null( $value ) ) {
242191 if (substr($setting,0,1) == '+' && is_array($value)) {
243192 $setting = substr($setting,1);
244193 if ( is_array($GLOBALS[$setting]) ) {
245 - $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value );
 194+ $GLOBALS[$setting] = array_merge( $GLOBALS[$setting], $value );
246195 } else {
247196 $GLOBALS[$setting] = $value;
248197 }
@@ -258,87 +207,23 @@
259208 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
260209 * @param $wikiTags Array The tags assigned to the wiki.
261210 */
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() ) {
264212 foreach ( $this->settings as $varName => $setting ) {
265 - $this->extractGlobalSetting( $varName, $wiki, $params );
 213+ $this->extractGlobal( $varName, $wiki, $suffix, $params, $wikiTags );
266214 }
267215 }
268216
269217 /**
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 - /**
332218 * Work out the site and language name from a database name
333219 * @param $db
334220 */
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 );
343228 foreach ( $this->suffixes as $suffix ) {
344229 if ( $suffix === '' ) {
345230 $site = '';
@@ -354,37 +239,9 @@
355240 return array( $site, $lang );
356241 }
357242
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 ) {
364245 return in_array( $vhost, $this->localVHosts );
365246 }
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 - }
390247 }
391248 }

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r39954Tweaks for SiteConfiguration:...ialex16:46, 25 August 2008

Status & tagging log