MediaWiki
REL1_19
|
00001 <?php 00005 class SiteConfiguration { 00006 00010 public $suffixes = array(); 00011 00015 public $wikis = array(); 00016 00020 public $settings = array(); 00021 00025 public $localVHosts = array(); 00026 00030 public $fullLoadCallback = null; 00031 00033 public $fullLoadDone = false; 00034 00047 public $siteParamsCallback = null; 00048 00058 public function get( $settingName, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) { 00059 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags ); 00060 return $this->getSetting( $settingName, $wiki, $params ); 00061 } 00062 00071 protected function getSetting( $settingName, $wiki, /*array*/ $params ){ 00072 $retval = null; 00073 if( array_key_exists( $settingName, $this->settings ) ) { 00074 $thisSetting =& $this->settings[$settingName]; 00075 do { 00076 // Do individual wiki settings 00077 if( array_key_exists( $wiki, $thisSetting ) ) { 00078 $retval = $thisSetting[$wiki]; 00079 break; 00080 } elseif( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) { 00081 $retval = $thisSetting["+$wiki"]; 00082 } 00083 00084 // Do tag settings 00085 foreach( $params['tags'] as $tag ) { 00086 if( array_key_exists( $tag, $thisSetting ) ) { 00087 if ( isset( $retval ) && is_array( $retval ) && is_array( $thisSetting[$tag] ) ) { 00088 $retval = self::arrayMerge( $retval, $thisSetting[$tag] ); 00089 } else { 00090 $retval = $thisSetting[$tag]; 00091 } 00092 break 2; 00093 } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) { 00094 if( !isset( $retval ) ) 00095 $retval = array(); 00096 $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] ); 00097 } 00098 } 00099 // Do suffix settings 00100 $suffix = $params['suffix']; 00101 if( !is_null( $suffix ) ) { 00102 if( array_key_exists( $suffix, $thisSetting ) ) { 00103 if ( isset($retval) && is_array($retval) && is_array($thisSetting[$suffix]) ) { 00104 $retval = self::arrayMerge( $retval, $thisSetting[$suffix] ); 00105 } else { 00106 $retval = $thisSetting[$suffix]; 00107 } 00108 break; 00109 } elseif( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) { 00110 if (!isset($retval)) 00111 $retval = array(); 00112 $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] ); 00113 } 00114 } 00115 00116 // Fall back to default. 00117 if( array_key_exists( 'default', $thisSetting ) ) { 00118 if( is_array( $retval ) && is_array( $thisSetting['default'] ) ) { 00119 $retval = self::arrayMerge( $retval, $thisSetting['default'] ); 00120 } else { 00121 $retval = $thisSetting['default']; 00122 } 00123 break; 00124 } 00125 } while ( false ); 00126 } 00127 00128 if( !is_null( $retval ) && count( $params['params'] ) ) { 00129 foreach ( $params['params'] as $key => $value ) { 00130 $retval = $this->doReplace( '$' . $key, $value, $retval ); 00131 } 00132 } 00133 return $retval; 00134 } 00135 00145 function doReplace( $from, $to, $in ) { 00146 if( is_string( $in ) ) { 00147 return str_replace( $from, $to, $in ); 00148 } elseif( is_array( $in ) ) { 00149 foreach( $in as $key => $val ) { 00150 $in[$key] = $this->doReplace( $from, $to, $val ); 00151 } 00152 return $in; 00153 } else { 00154 return $in; 00155 } 00156 } 00157 00166 public function getAll( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) { 00167 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags ); 00168 $localSettings = array(); 00169 foreach( $this->settings as $varname => $stuff ) { 00170 $append = false; 00171 $var = $varname; 00172 if ( substr( $varname, 0, 1 ) == '+' ) { 00173 $append = true; 00174 $var = substr( $varname, 1 ); 00175 } 00176 00177 $value = $this->getSetting( $varname, $wiki, $params ); 00178 if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) ) 00179 $value = self::arrayMerge( $value, $GLOBALS[$var] ); 00180 if ( !is_null( $value ) ) { 00181 $localSettings[$var] = $value; 00182 } 00183 } 00184 return $localSettings; 00185 } 00186 00195 public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) { 00196 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) ); 00197 } 00198 00204 function &getLocalDatabases() { 00205 return $this->wikis; 00206 } 00207 00217 public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) { 00218 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags ); 00219 if ( !is_null( $value ) ) { 00220 $var = $value; 00221 } 00222 } 00223 00232 public function extractGlobal( $setting, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) { 00233 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags ); 00234 $this->extractGlobalSetting( $setting, $wiki, $params ); 00235 } 00236 00242 public function extractGlobalSetting( $setting, $wiki, $params ) { 00243 $value = $this->getSetting( $setting, $wiki, $params ); 00244 if ( !is_null( $value ) ) { 00245 if (substr($setting,0,1) == '+' && is_array($value)) { 00246 $setting = substr($setting,1); 00247 if ( is_array($GLOBALS[$setting]) ) { 00248 $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value ); 00249 } else { 00250 $GLOBALS[$setting] = $value; 00251 } 00252 } else { 00253 $GLOBALS[$setting] = $value; 00254 } 00255 } 00256 } 00257 00265 public function extractAllGlobals( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) { 00266 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags ); 00267 foreach ( $this->settings as $varName => $setting ) { 00268 $this->extractGlobalSetting( $varName, $wiki, $params ); 00269 } 00270 } 00271 00280 protected function getWikiParams( $wiki ){ 00281 static $default = array( 00282 'suffix' => null, 00283 'lang' => null, 00284 'tags' => array(), 00285 'params' => array(), 00286 ); 00287 00288 if( !is_callable( $this->siteParamsCallback ) ) { 00289 return $default; 00290 } 00291 00292 $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) ); 00293 # Validate the returned value 00294 if( !is_array( $ret ) ) { 00295 return $default; 00296 } 00297 00298 foreach( $default as $name => $def ){ 00299 if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) ) 00300 $ret[$name] = $default[$name]; 00301 } 00302 00303 return $ret; 00304 } 00305 00318 protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ){ 00319 $ret = $this->getWikiParams( $wiki ); 00320 00321 if( is_null( $ret['suffix'] ) ) 00322 $ret['suffix'] = $suffix; 00323 00324 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) ); 00325 00326 $ret['params'] += $params; 00327 00328 // Automatically fill that ones if needed 00329 if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) ) 00330 $ret['params']['lang'] = $ret['lang']; 00331 if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) ) 00332 $ret['params']['site'] = $ret['suffix']; 00333 00334 return $ret; 00335 } 00336 00343 public function siteFromDB( $db ) { 00344 // Allow override 00345 $def = $this->getWikiParams( $db ); 00346 if( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) ) 00347 return array( $def['suffix'], $def['lang'] ); 00348 00349 $site = null; 00350 $lang = null; 00351 foreach ( $this->suffixes as $suffix ) { 00352 if ( $suffix === '' ) { 00353 $site = ''; 00354 $lang = $db; 00355 break; 00356 } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) { 00357 $site = $suffix == 'wiki' ? 'wikipedia' : $suffix; 00358 $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) ); 00359 break; 00360 } 00361 } 00362 $lang = str_replace( '_', '-', $lang ); 00363 return array( $site, $lang ); 00364 } 00365 00371 public function isLocalVHost( $vhost ) { 00372 return in_array( $vhost, $this->localVHosts ); 00373 } 00374 00385 static function arrayMerge( $array1/* ... */ ) { 00386 $out = $array1; 00387 for( $i = 1; $i < func_num_args(); $i++ ) { 00388 foreach( func_get_arg( $i ) as $key => $value ) { 00389 if ( isset($out[$key]) && is_array($out[$key]) && is_array($value) ) { 00390 $out[$key] = self::arrayMerge( $out[$key], $value ); 00391 } elseif ( !isset($out[$key]) || !$out[$key] && !is_numeric($key) ) { 00392 // Values that evaluate to true given precedence, for the primary purpose of merging permissions arrays. 00393 $out[$key] = $value; 00394 } elseif ( is_numeric( $key ) ) { 00395 $out[] = $value; 00396 } 00397 } 00398 } 00399 00400 return $out; 00401 } 00402 00403 public function loadFullData() { 00404 if ($this->fullLoadCallback && !$this->fullLoadDone) { 00405 call_user_func( $this->fullLoadCallback, $this ); 00406 $this->fullLoadDone = true; 00407 } 00408 } 00409 }