00001 <?php
00002
00009
00011 if( !defined( 'SITE_CONFIGURATION' ) ){
00012 define( 'SITE_CONFIGURATION', 1 );
00014
00018 class SiteConfiguration {
00019
00023 public $suffixes = array();
00024
00028 public $wikis = array();
00029
00033 public $settings = array();
00034
00038 public $localVHosts = array();
00039
00043 public $fullLoadCallback = null;
00044
00046 public $fullLoadDone = false;
00047
00060 public $siteParamsCallback = null;
00061
00071 public function get( $settingName, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
00072 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
00073 return $this->getSetting( $settingName, $wiki, $params );
00074 }
00075
00084 protected function getSetting( $settingName, $wiki, $params ){
00085 $retval = null;
00086 if( array_key_exists( $settingName, $this->settings ) ) {
00087 $thisSetting =& $this->settings[$settingName];
00088 do {
00089
00090 if( array_key_exists( $wiki, $thisSetting ) ) {
00091 $retval = $thisSetting[$wiki];
00092 break;
00093 } elseif( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
00094 $retval = $thisSetting["+$wiki"];
00095 }
00096
00097
00098 foreach( $params['tags'] as $tag ) {
00099 if( array_key_exists( $tag, $thisSetting ) ) {
00100 if ( isset( $retval ) && is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
00101 $retval = self::arrayMerge( $retval, $thisSetting[$tag] );
00102 } else {
00103 $retval = $thisSetting[$tag];
00104 }
00105 break 2;
00106 } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) {
00107 if( !isset( $retval ) )
00108 $retval = array();
00109 $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] );
00110 }
00111 }
00112
00113 $suffix = $params['suffix'];
00114 if( !is_null( $suffix ) ) {
00115 if( array_key_exists( $suffix, $thisSetting ) ) {
00116 if ( isset($retval) && is_array($retval) && is_array($thisSetting[$suffix]) ) {
00117 $retval = self::arrayMerge( $retval, $thisSetting[$suffix] );
00118 } else {
00119 $retval = $thisSetting[$suffix];
00120 }
00121 break;
00122 } elseif( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) {
00123 if (!isset($retval))
00124 $retval = array();
00125 $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] );
00126 }
00127 }
00128
00129
00130 if( array_key_exists( 'default', $thisSetting ) ) {
00131 if( is_array( $retval ) && is_array( $thisSetting['default'] ) ) {
00132 $retval = self::arrayMerge( $retval, $thisSetting['default'] );
00133 } else {
00134 $retval = $thisSetting['default'];
00135 }
00136 break;
00137 }
00138 } while ( false );
00139 }
00140
00141 if( !is_null( $retval ) && count( $params['params'] ) ) {
00142 foreach ( $params['params'] as $key => $value ) {
00143 $retval = $this->doReplace( '$' . $key, $value, $retval );
00144 }
00145 }
00146 return $retval;
00147 }
00148
00153 function doReplace( $from, $to, $in ) {
00154 if( is_string( $in ) ) {
00155 return str_replace( $from, $to, $in );
00156 } elseif( is_array( $in ) ) {
00157 foreach( $in as $key => $val ) {
00158 $in[$key] = $this->doReplace( $from, $to, $val );
00159 }
00160 return $in;
00161 } else {
00162 return $in;
00163 }
00164 }
00165
00174 public function getAll( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
00175 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
00176 $localSettings = array();
00177 foreach( $this->settings as $varname => $stuff ) {
00178 $append = false;
00179 $var = $varname;
00180 if ( substr( $varname, 0, 1 ) == '+' ) {
00181 $append = true;
00182 $var = substr( $varname, 1 );
00183 }
00184
00185 $value = $this->getSetting( $varname, $wiki, $params );
00186 if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) )
00187 $value = self::arrayMerge( $value, $GLOBALS[$var] );
00188 if ( !is_null( $value ) ) {
00189 $localSettings[$var] = $value;
00190 }
00191 }
00192 return $localSettings;
00193 }
00194
00204 public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) {
00205 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
00206 }
00207
00209 function &getLocalDatabases() {
00210 return $this->wikis;
00211 }
00212
00214 function initialise() {
00215 }
00216
00226 public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) {
00227 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
00228 if ( !is_null( $value ) ) {
00229 $var = $value;
00230 }
00231 }
00232
00241 public function extractGlobal( $setting, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
00242 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
00243 $this->extractGlobalSetting( $setting, $wiki, $params );
00244 }
00245
00246 public function extractGlobalSetting( $setting, $wiki, $params ) {
00247 $value = $this->getSetting( $setting, $wiki, $params );
00248 if ( !is_null( $value ) ) {
00249 if (substr($setting,0,1) == '+' && is_array($value)) {
00250 $setting = substr($setting,1);
00251 if ( is_array($GLOBALS[$setting]) ) {
00252 $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value );
00253 } else {
00254 $GLOBALS[$setting] = $value;
00255 }
00256 } else {
00257 $GLOBALS[$setting] = $value;
00258 }
00259 }
00260 }
00261
00269 public function extractAllGlobals( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
00270 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
00271 foreach ( $this->settings as $varName => $setting ) {
00272 $this->extractGlobalSetting( $varName, $wiki, $params );
00273 }
00274 }
00275
00284 protected function getWikiParams( $wiki ){
00285 static $default = array(
00286 'suffix' => null,
00287 'lang' => null,
00288 'tags' => array(),
00289 'params' => array(),
00290 );
00291
00292 if( !is_callable( $this->siteParamsCallback ) )
00293 return $default;
00294
00295 $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) );
00296 # Validate the returned value
00297 if( !is_array( $ret ) )
00298 return $default;
00299
00300 foreach( $default as $name => $def ){
00301 if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) )
00302 $ret[$name] = $default[$name];
00303 }
00304
00305 return $ret;
00306 }
00307
00320 protected function mergeParams( $wiki, $suffix, $params, $wikiTags ){
00321 $ret = $this->getWikiParams( $wiki );
00322
00323 if( is_null( $ret['suffix'] ) )
00324 $ret['suffix'] = $suffix;
00325
00326 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
00327
00328 $ret['params'] += $params;
00329
00330
00331 if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) )
00332 $ret['params']['lang'] = $ret['lang'];
00333 if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) )
00334 $ret['params']['site'] = $ret['suffix'];
00335
00336 return $ret;
00337 }
00338
00343 public function siteFromDB( $db ) {
00344
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
00381 static function arrayMerge( $array1 ) {
00382 $out = $array1;
00383 for( $i=1; $i < func_num_args(); $i++ ) {
00384 foreach( func_get_arg( $i ) as $key => $value ) {
00385 if ( isset($out[$key]) && is_array($out[$key]) && is_array($value) ) {
00386 $out[$key] = self::arrayMerge( $out[$key], $value );
00387 } elseif ( !isset($out[$key]) || !$out[$key] && !is_numeric($key) ) {
00388
00389 $out[$key] = $value;
00390 } elseif ( is_numeric( $key ) ) {
00391 $out[] = $value;
00392 }
00393 }
00394 }
00395
00396 return $out;
00397 }
00398
00399 public function loadFullData() {
00400 if ($this->fullLoadCallback && !$this->fullLoadDone) {
00401 call_user_func( $this->fullLoadCallback, $this );
00402 $this->fullLoadDone = true;
00403 }
00404 }
00405 }
00406 }