MediaWiki  REL1_20
SiteConfiguration.php
Go to the documentation of this file.
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 
00173         public function get( $settingName, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
00174                 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
00175                 return $this->getSetting( $settingName, $wiki, $params );
00176         }
00177 
00186         protected function getSetting( $settingName, $wiki, /*array*/ $params ){
00187                 $retval = null;
00188                 if( array_key_exists( $settingName, $this->settings ) ) {
00189                         $thisSetting =& $this->settings[$settingName];
00190                         do {
00191                                 // Do individual wiki settings
00192                                 if( array_key_exists( $wiki, $thisSetting ) ) {
00193                                         $retval = $thisSetting[$wiki];
00194                                         break;
00195                                 } elseif ( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
00196                                         $retval = $thisSetting["+$wiki"];
00197                                 }
00198 
00199                                 // Do tag settings
00200                                 foreach( $params['tags'] as $tag ) {
00201                                         if( array_key_exists( $tag, $thisSetting ) ) {
00202                                                 if ( isset( $retval ) && is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
00203                                                         $retval = self::arrayMerge( $retval, $thisSetting[$tag] );
00204                                                 } else {
00205                                                         $retval = $thisSetting[$tag];
00206                                                 }
00207                                                 break 2;
00208                                         } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) {
00209                                                 if( !isset( $retval ) ) {
00210                                                         $retval = array();
00211                                                 }
00212                                                 $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] );
00213                                         }
00214                                 }
00215                                 // Do suffix settings
00216                                 $suffix = $params['suffix'];
00217                                 if( !is_null( $suffix ) ) {
00218                                         if( array_key_exists( $suffix, $thisSetting ) ) {
00219                                                 if ( isset($retval) && is_array($retval) && is_array($thisSetting[$suffix]) ) {
00220                                                         $retval = self::arrayMerge( $retval, $thisSetting[$suffix] );
00221                                                 } else {
00222                                                         $retval = $thisSetting[$suffix];
00223                                                 }
00224                                                 break;
00225                                         } elseif ( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) {
00226                                                 if ( !isset( $retval ) ) {
00227                                                         $retval = array();
00228                                                 }
00229                                                 $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] );
00230                                         }
00231                                 }
00232 
00233                                 // Fall back to default.
00234                                 if( array_key_exists( 'default', $thisSetting ) ) {
00235                                         if( is_array( $retval ) && is_array( $thisSetting['default'] ) ) {
00236                                                 $retval = self::arrayMerge( $retval, $thisSetting['default'] );
00237                                         } else {
00238                                                 $retval = $thisSetting['default'];
00239                                         }
00240                                         break;
00241                                 }
00242                         } while ( false );
00243                 }
00244 
00245                 if( !is_null( $retval ) && count( $params['params'] ) ) {
00246                         foreach ( $params['params'] as $key => $value ) {
00247                                 $retval = $this->doReplace( '$' . $key, $value, $retval );
00248                         }
00249                 }
00250                 return $retval;
00251         }
00252 
00262         function doReplace( $from, $to, $in ) {
00263                 if( is_string( $in ) ) {
00264                         return str_replace( $from, $to, $in );
00265                 } elseif( is_array( $in ) ) {
00266                         foreach( $in as $key => $val ) {
00267                                 $in[$key] = $this->doReplace( $from, $to, $val );
00268                         }
00269                         return $in;
00270                 } else {
00271                         return $in;
00272                 }
00273         }
00274 
00283         public function getAll( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
00284                 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
00285                 $localSettings = array();
00286                 foreach( $this->settings as $varname => $stuff ) {
00287                         $append = false;
00288                         $var = $varname;
00289                         if ( substr( $varname, 0, 1 ) == '+' ) {
00290                                 $append = true;
00291                                 $var = substr( $varname, 1 );
00292                         }
00293 
00294                         $value = $this->getSetting( $varname, $wiki, $params );
00295                         if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) ) {
00296                                 $value = self::arrayMerge( $value, $GLOBALS[$var] );
00297                         }
00298                         if ( !is_null( $value ) ) {
00299                                 $localSettings[$var] = $value;
00300                         }
00301                 }
00302                 return $localSettings;
00303         }
00304 
00313         public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) {
00314                 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
00315         }
00316 
00322         function &getLocalDatabases() {
00323                 return $this->wikis;
00324         }
00325 
00335         public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) {
00336                 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
00337                 if ( !is_null( $value ) ) {
00338                         $var = $value;
00339                 }
00340         }
00341 
00350         public function extractGlobal( $setting, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
00351                 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
00352                 $this->extractGlobalSetting( $setting, $wiki, $params );
00353         }
00354 
00360         public function extractGlobalSetting( $setting, $wiki, $params ) {
00361                 $value = $this->getSetting( $setting, $wiki, $params );
00362                 if ( !is_null( $value ) ) {
00363                         if (substr($setting,0,1) == '+' && is_array($value)) {
00364                                 $setting = substr($setting,1);
00365                                 if ( is_array($GLOBALS[$setting]) ) {
00366                                         $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value );
00367                                 } else {
00368                                         $GLOBALS[$setting] = $value;
00369                                 }
00370                         } else {
00371                                 $GLOBALS[$setting] = $value;
00372                         }
00373                 }
00374         }
00375 
00383         public function extractAllGlobals( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
00384                 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
00385                 foreach ( $this->settings as $varName => $setting ) {
00386                         $this->extractGlobalSetting( $varName, $wiki, $params );
00387                 }
00388         }
00389 
00398         protected function getWikiParams( $wiki ){
00399                 static $default = array(
00400                         'suffix' => null,
00401                         'lang' => null,
00402                         'tags' => array(),
00403                         'params' => array(),
00404                 );
00405 
00406                 if( !is_callable( $this->siteParamsCallback ) ) {
00407                         return $default;
00408                 }
00409 
00410                 $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) );
00411                 # Validate the returned value
00412                 if( !is_array( $ret ) ) {
00413                         return $default;
00414                 }
00415 
00416                 foreach( $default as $name => $def ){
00417                         if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) ) {
00418                                 $ret[$name] = $default[$name];
00419                         }
00420                 }
00421 
00422                 return $ret;
00423         }
00424 
00437         protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ){
00438                 $ret = $this->getWikiParams( $wiki );
00439 
00440                 if( is_null( $ret['suffix'] ) ) {
00441                         $ret['suffix'] = $suffix;
00442                 }
00443 
00444                 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
00445 
00446                 $ret['params'] += $params;
00447 
00448                 // Automatically fill that ones if needed
00449                 if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) ){
00450                         $ret['params']['lang'] = $ret['lang'];
00451                 }
00452                 if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) ) {
00453                         $ret['params']['site'] = $ret['suffix'];
00454                 }
00455 
00456                 return $ret;
00457         }
00458 
00465         public function siteFromDB( $db ) {
00466                 // Allow override
00467                 $def = $this->getWikiParams( $db );
00468                 if( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) ) {
00469                         return array( $def['suffix'], $def['lang'] );
00470                 }
00471 
00472                 $site = null;
00473                 $lang = null;
00474                 foreach ( $this->suffixes as $suffix ) {
00475                         if ( $suffix === '' ) {
00476                                 $site = '';
00477                                 $lang = $db;
00478                                 break;
00479                         } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
00480                                 $site = $suffix == 'wiki' ? 'wikipedia' : $suffix;
00481                                 $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
00482                                 break;
00483                         }
00484                 }
00485                 $lang = str_replace( '_', '-', $lang );
00486                 return array( $site, $lang );
00487         }
00488 
00494         public function isLocalVHost( $vhost ) {
00495                 return in_array( $vhost, $this->localVHosts );
00496         }
00497 
00508         static function arrayMerge( $array1/* ... */ ) {
00509                 $out = $array1;
00510                 for( $i = 1; $i < func_num_args(); $i++ ) {
00511                         foreach( func_get_arg( $i ) as $key => $value ) {
00512                                 if ( isset($out[$key]) && is_array($out[$key]) && is_array($value) ) {
00513                                         $out[$key] = self::arrayMerge( $out[$key], $value );
00514                                 } elseif ( !isset($out[$key]) || !$out[$key] && !is_numeric($key) ) {
00515                                         // Values that evaluate to true given precedence, for the primary purpose of merging permissions arrays.
00516                                         $out[$key] = $value;
00517                                 } elseif ( is_numeric( $key ) ) {
00518                                         $out[] = $value;
00519                                 }
00520                         }
00521                 }
00522 
00523                 return $out;
00524         }
00525 
00526         public function loadFullData() {
00527                 if ( $this->fullLoadCallback && !$this->fullLoadDone ) {
00528                         call_user_func( $this->fullLoadCallback, $this );
00529                         $this->fullLoadDone = true;
00530                 }
00531         }
00532 }