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
00052 public $siteParamsCallback = null;
00053
00063 public function get( $settingName, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
00064 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
00065 return $this->getSetting( $settingName, $wiki, $params );
00066 }
00067
00076 protected function getSetting( $settingName, $wiki, $params ){
00077 $retval = null;
00078 if( array_key_exists( $settingName, $this->settings ) ) {
00079 $thisSetting =& $this->settings[$settingName];
00080 do {
00081
00082 if( array_key_exists( $wiki, $thisSetting ) ) {
00083 $retval = $thisSetting[$wiki];
00084 break;
00085 } elseif( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
00086 $retval = $thisSetting["+$wiki"];
00087 }
00088
00089
00090 foreach( $params['tags'] as $tag ) {
00091 if( array_key_exists( $tag, $thisSetting ) ) {
00092 if ( isset( $retval ) && is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
00093 $retval = self::arrayMerge( $retval, $thisSetting[$tag] );
00094 } else {
00095 $retval = $thisSetting[$tag];
00096 }
00097 break 2;
00098 } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) {
00099 if( !isset( $retval ) )
00100 $retval = array();
00101 $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] );
00102 }
00103 }
00104
00105 $suffix = $params['suffix'];
00106 if( !is_null( $suffix ) ) {
00107 if( array_key_exists( $suffix, $thisSetting ) ) {
00108 if ( isset($retval) && is_array($retval) && is_array($thisSetting[$suffix]) ) {
00109 $retval = self::arrayMerge( $retval, $thisSetting[$suffix] );
00110 } else {
00111 $retval = $thisSetting[$suffix];
00112 }
00113 break;
00114 } elseif( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) {
00115 if (!isset($retval))
00116 $retval = array();
00117 $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] );
00118 }
00119 }
00120
00121
00122 if( array_key_exists( 'default', $thisSetting ) ) {
00123 if( is_array( $retval ) && is_array( $thisSetting['default'] ) ) {
00124 $retval = self::arrayMerge( $retval, $thisSetting['default'] );
00125 } else {
00126 $retval = $thisSetting['default'];
00127 }
00128 break;
00129 }
00130 } while ( false );
00131 }
00132
00133 if( !is_null( $retval ) && count( $params['params'] ) ) {
00134 foreach ( $params['params'] as $key => $value ) {
00135 $retval = $this->doReplace( '$' . $key, $value, $retval );
00136 }
00137 }
00138 return $retval;
00139 }
00140
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
00196 public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) {
00197 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
00198 }
00199
00201 function &getLocalDatabases() {
00202 return $this->wikis;
00203 }
00204
00206 function initialise() {
00207 }
00208
00218 public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) {
00219 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
00220 if ( !is_null( $value ) ) {
00221 $var = $value;
00222 }
00223 }
00224
00233 public function extractGlobal( $setting, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
00234 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
00235 $this->extractGlobalSetting( $setting, $wiki, $params );
00236 }
00237
00238 public function extractGlobalSetting( $setting, $wiki, $params ) {
00239 $value = $this->getSetting( $setting, $wiki, $params );
00240 if ( !is_null( $value ) ) {
00241 if (substr($setting,0,1) == '+' && is_array($value)) {
00242 $setting = substr($setting,1);
00243 if ( is_array($GLOBALS[$setting]) ) {
00244 $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value );
00245 } else {
00246 $GLOBALS[$setting] = $value;
00247 }
00248 } else {
00249 $GLOBALS[$setting] = $value;
00250 }
00251 }
00252 }
00253
00261 public function extractAllGlobals( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
00262 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
00263 foreach ( $this->settings as $varName => $setting ) {
00264 $this->extractGlobalSetting( $varName, $wiki, $params );
00265 }
00266 }
00267
00276 protected function getWikiParams( $wiki ){
00277 static $default = array(
00278 'suffix' => null,
00279 'lang' => null,
00280 'tags' => array(),
00281 'params' => array(),
00282 );
00283
00284 if( !is_callable( $this->siteParamsCallback ) )
00285 return $default;
00286
00287 $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) );
00288 # Validate the returned value
00289 if( !is_array( $ret ) )
00290 return $default;
00291
00292 foreach( $default as $name => $def ){
00293 if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) )
00294 $ret[$name] = $default[$name];
00295 }
00296
00297 return $ret;
00298 }
00299
00312 protected function mergeParams( $wiki, $suffix, $params, $wikiTags ){
00313 $ret = $this->getWikiParams( $wiki );
00314
00315 if( is_null( $ret['suffix'] ) )
00316 $ret['suffix'] = $suffix;
00317
00318 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
00319
00320 $ret['params'] += $params;
00321
00322
00323 if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) )
00324 $ret['params']['lang'] = $ret['lang'];
00325 if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) )
00326 $ret['params']['site'] = $ret['suffix'];
00327
00328 return $ret;
00329 }
00330
00335 public function siteFromDB( $db ) {
00336
00337 $def = $this->getWikiParams( $db );
00338 if( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) )
00339 return array( $def['suffix'], $def['lang'] );
00340
00341 $site = null;
00342 $lang = null;
00343 foreach ( $this->suffixes as $suffix ) {
00344 if ( $suffix === '' ) {
00345 $site = '';
00346 $lang = $db;
00347 break;
00348 } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
00349 $site = $suffix == 'wiki' ? 'wikipedia' : $suffix;
00350 $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
00351 break;
00352 }
00353 }
00354 $lang = str_replace( '_', '-', $lang );
00355 return array( $site, $lang );
00356 }
00357
00363 public function isLocalVHost( $vhost ) {
00364 return in_array( $vhost, $this->localVHosts );
00365 }
00366
00373 static function arrayMerge( $array1 ) {
00374 $out = $array1;
00375 for( $i=1; $i < func_num_args(); $i++ ) {
00376 foreach( func_get_arg( $i ) as $key => $value ) {
00377 if ( isset($out[$key]) && is_array($out[$key]) && is_array($value) ) {
00378 $out[$key] = self::arrayMerge( $out[$key], $value );
00379 } elseif ( !isset($out[$key]) || !$out[$key] && !is_numeric($key) ) {
00380
00381 $out[$key] = $value;
00382 } elseif ( is_numeric( $key ) ) {
00383 $out[] = $value;
00384 }
00385 }
00386 }
00387
00388 return $out;
00389 }
00390 }
00391 }