MediaWiki
REL1_24
|
00001 <?php 00117 class SiteConfiguration { 00118 00122 public $suffixes = array(); 00123 00127 public $wikis = array(); 00128 00132 public $settings = array(); 00133 00137 public $localVHosts = array(); 00138 00143 public $fullLoadCallback = null; 00144 00146 public $fullLoadDone = false; 00147 00162 public $siteParamsCallback = null; 00163 00168 protected $cfgCache = array(); 00169 00179 public function get( $settingName, $wiki, $suffix = null, $params = array(), 00180 $wikiTags = array() 00181 ) { 00182 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags ); 00183 return $this->getSetting( $settingName, $wiki, $params ); 00184 } 00185 00194 protected function getSetting( $settingName, $wiki, array $params ) { 00195 $retval = null; 00196 if ( array_key_exists( $settingName, $this->settings ) ) { 00197 $thisSetting =& $this->settings[$settingName]; 00198 do { 00199 // Do individual wiki settings 00200 if ( array_key_exists( $wiki, $thisSetting ) ) { 00201 $retval = $thisSetting[$wiki]; 00202 break; 00203 } elseif ( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) { 00204 $retval = $thisSetting["+$wiki"]; 00205 } 00206 00207 // Do tag settings 00208 foreach ( $params['tags'] as $tag ) { 00209 if ( array_key_exists( $tag, $thisSetting ) ) { 00210 if ( is_array( $retval ) && is_array( $thisSetting[$tag] ) ) { 00211 $retval = self::arrayMerge( $retval, $thisSetting[$tag] ); 00212 } else { 00213 $retval = $thisSetting[$tag]; 00214 } 00215 break 2; 00216 } elseif ( array_key_exists( "+$tag", $thisSetting ) && is_array( $thisSetting["+$tag"] ) ) { 00217 if ( $retval === null ) { 00218 $retval = array(); 00219 } 00220 $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] ); 00221 } 00222 } 00223 // Do suffix settings 00224 $suffix = $params['suffix']; 00225 if ( !is_null( $suffix ) ) { 00226 if ( array_key_exists( $suffix, $thisSetting ) ) { 00227 if ( is_array( $retval ) && is_array( $thisSetting[$suffix] ) ) { 00228 $retval = self::arrayMerge( $retval, $thisSetting[$suffix] ); 00229 } else { 00230 $retval = $thisSetting[$suffix]; 00231 } 00232 break; 00233 } elseif ( array_key_exists( "+$suffix", $thisSetting ) 00234 && is_array( $thisSetting["+$suffix"] ) 00235 ) { 00236 if ( $retval === null ) { 00237 $retval = array(); 00238 } 00239 $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] ); 00240 } 00241 } 00242 00243 // Fall back to default. 00244 if ( array_key_exists( 'default', $thisSetting ) ) { 00245 if ( is_array( $retval ) && is_array( $thisSetting['default'] ) ) { 00246 $retval = self::arrayMerge( $retval, $thisSetting['default'] ); 00247 } else { 00248 $retval = $thisSetting['default']; 00249 } 00250 break; 00251 } 00252 } while ( false ); 00253 } 00254 00255 if ( !is_null( $retval ) && count( $params['params'] ) ) { 00256 foreach ( $params['params'] as $key => $value ) { 00257 $retval = $this->doReplace( '$' . $key, $value, $retval ); 00258 } 00259 } 00260 return $retval; 00261 } 00262 00272 function doReplace( $from, $to, $in ) { 00273 if ( is_string( $in ) ) { 00274 return str_replace( $from, $to, $in ); 00275 } elseif ( is_array( $in ) ) { 00276 foreach ( $in as $key => $val ) { 00277 $in[$key] = $this->doReplace( $from, $to, $val ); 00278 } 00279 return $in; 00280 } else { 00281 return $in; 00282 } 00283 } 00284 00293 public function getAll( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) { 00294 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags ); 00295 $localSettings = array(); 00296 foreach ( $this->settings as $varname => $stuff ) { 00297 $append = false; 00298 $var = $varname; 00299 if ( substr( $varname, 0, 1 ) == '+' ) { 00300 $append = true; 00301 $var = substr( $varname, 1 ); 00302 } 00303 00304 $value = $this->getSetting( $varname, $wiki, $params ); 00305 if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) ) { 00306 $value = self::arrayMerge( $value, $GLOBALS[$var] ); 00307 } 00308 if ( !is_null( $value ) ) { 00309 $localSettings[$var] = $value; 00310 } 00311 } 00312 return $localSettings; 00313 } 00314 00323 public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) { 00324 return (bool)$this->get( $setting, $wiki, $suffix, array(), $wikiTags ); 00325 } 00326 00332 function &getLocalDatabases() { 00333 return $this->wikis; 00334 } 00335 00345 public function extractVar( $setting, $wiki, $suffix, &$var, 00346 $params = array(), $wikiTags = array() 00347 ) { 00348 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags ); 00349 if ( !is_null( $value ) ) { 00350 $var = $value; 00351 } 00352 } 00353 00362 public function extractGlobal( $setting, $wiki, $suffix = null, 00363 $params = array(), $wikiTags = array() 00364 ) { 00365 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags ); 00366 $this->extractGlobalSetting( $setting, $wiki, $params ); 00367 } 00368 00374 public function extractGlobalSetting( $setting, $wiki, $params ) { 00375 $value = $this->getSetting( $setting, $wiki, $params ); 00376 if ( !is_null( $value ) ) { 00377 if ( substr( $setting, 0, 1 ) == '+' && is_array( $value ) ) { 00378 $setting = substr( $setting, 1 ); 00379 if ( is_array( $GLOBALS[$setting] ) ) { 00380 $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value ); 00381 } else { 00382 $GLOBALS[$setting] = $value; 00383 } 00384 } else { 00385 $GLOBALS[$setting] = $value; 00386 } 00387 } 00388 } 00389 00397 public function extractAllGlobals( $wiki, $suffix = null, $params = array(), 00398 $wikiTags = array() 00399 ) { 00400 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags ); 00401 foreach ( $this->settings as $varName => $setting ) { 00402 $this->extractGlobalSetting( $varName, $wiki, $params ); 00403 } 00404 } 00405 00414 protected function getWikiParams( $wiki ) { 00415 static $default = array( 00416 'suffix' => null, 00417 'lang' => null, 00418 'tags' => array(), 00419 'params' => array(), 00420 ); 00421 00422 if ( !is_callable( $this->siteParamsCallback ) ) { 00423 return $default; 00424 } 00425 00426 $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) ); 00427 # Validate the returned value 00428 if ( !is_array( $ret ) ) { 00429 return $default; 00430 } 00431 00432 foreach ( $default as $name => $def ) { 00433 if ( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) ) { 00434 $ret[$name] = $default[$name]; 00435 } 00436 } 00437 00438 return $ret; 00439 } 00440 00453 protected function mergeParams( $wiki, $suffix, array $params, array $wikiTags ) { 00454 $ret = $this->getWikiParams( $wiki ); 00455 00456 if ( is_null( $ret['suffix'] ) ) { 00457 $ret['suffix'] = $suffix; 00458 } 00459 00460 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) ); 00461 00462 $ret['params'] += $params; 00463 00464 // Automatically fill that ones if needed 00465 if ( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) ) { 00466 $ret['params']['lang'] = $ret['lang']; 00467 } 00468 if ( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) ) { 00469 $ret['params']['site'] = $ret['suffix']; 00470 } 00471 00472 return $ret; 00473 } 00474 00481 public function siteFromDB( $db ) { 00482 // Allow override 00483 $def = $this->getWikiParams( $db ); 00484 if ( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) ) { 00485 return array( $def['suffix'], $def['lang'] ); 00486 } 00487 00488 $site = null; 00489 $lang = null; 00490 foreach ( $this->suffixes as $altSite => $suffix ) { 00491 if ( $suffix === '' ) { 00492 $site = ''; 00493 $lang = $db; 00494 break; 00495 } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) { 00496 $site = is_numeric( $altSite ) ? $suffix : $altSite; 00497 $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) ); 00498 break; 00499 } 00500 } 00501 $lang = str_replace( '_', '-', $lang ); 00502 return array( $site, $lang ); 00503 } 00504 00516 public function getConfig( $wiki, $settings ) { 00517 global $IP; 00518 00519 $multi = is_array( $settings ); 00520 $settings = (array)$settings; 00521 if ( $wiki === wfWikiID() ) { // $wiki is this wiki 00522 $res = array(); 00523 foreach ( $settings as $name ) { 00524 if ( !preg_match( '/^wg[A-Z]/', $name ) ) { 00525 throw new MWException( "Variable '$name' does start with 'wg'." ); 00526 } elseif ( !isset( $GLOBALS[$name] ) ) { 00527 throw new MWException( "Variable '$name' is not set." ); 00528 } 00529 $res[$name] = $GLOBALS[$name]; 00530 } 00531 } else { // $wiki is a foreign wiki 00532 if ( isset( $this->cfgCache[$wiki] ) ) { 00533 $res = array_intersect_key( $this->cfgCache[$wiki], array_flip( $settings ) ); 00534 if ( count( $res ) == count( $settings ) ) { 00535 return $multi ? $res : current( $res ); // cache hit 00536 } 00537 } elseif ( !in_array( $wiki, $this->wikis ) ) { 00538 throw new MWException( "No such wiki '$wiki'." ); 00539 } else { 00540 $this->cfgCache[$wiki] = array(); 00541 } 00542 $retVal = 1; 00543 $cmd = wfShellWikiCmd( 00544 "$IP/maintenance/getConfiguration.php", 00545 array( 00546 '--wiki', $wiki, 00547 '--settings', implode( ' ', $settings ), 00548 '--format', 'PHP' 00549 ) 00550 ); 00551 // ulimit5.sh breaks this call 00552 $data = trim( wfShellExec( $cmd, $retVal, array(), array( 'memory' => 0 ) ) ); 00553 if ( $retVal != 0 || !strlen( $data ) ) { 00554 throw new MWException( "Failed to run getConfiguration.php." ); 00555 } 00556 $res = unserialize( $data ); 00557 if ( !is_array( $res ) ) { 00558 throw new MWException( "Failed to unserialize configuration array." ); 00559 } 00560 $this->cfgCache[$wiki] = $this->cfgCache[$wiki] + $res; 00561 } 00562 00563 return $multi ? $res : current( $res ); 00564 } 00565 00571 public function isLocalVHost( $vhost ) { 00572 return in_array( $vhost, $this->localVHosts ); 00573 } 00574 00585 static function arrayMerge( $array1/* ... */ ) { 00586 $out = $array1; 00587 $argsCount = func_num_args(); 00588 for ( $i = 1; $i < $argsCount; $i++ ) { 00589 foreach ( func_get_arg( $i ) as $key => $value ) { 00590 if ( isset( $out[$key] ) && is_array( $out[$key] ) && is_array( $value ) ) { 00591 $out[$key] = self::arrayMerge( $out[$key], $value ); 00592 } elseif ( !isset( $out[$key] ) || !$out[$key] && !is_numeric( $key ) ) { 00593 // Values that evaluate to true given precedence, for the 00594 // primary purpose of merging permissions arrays. 00595 $out[$key] = $value; 00596 } elseif ( is_numeric( $key ) ) { 00597 $out[] = $value; 00598 } 00599 } 00600 } 00601 00602 return $out; 00603 } 00604 00605 public function loadFullData() { 00606 if ( $this->fullLoadCallback && !$this->fullLoadDone ) { 00607 call_user_func( $this->fullLoadCallback, $this ); 00608 $this->fullLoadDone = true; 00609 } 00610 } 00611 }