X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;ds=sidebyside;f=includes%2FSiteConfiguration.php;h=6a861d8e885e028bc3353f96eca26e76d34b3d18;hb=4dd5bf200838679b6a1bf4b1b5e7e4a521cb07bd;hp=ff5548ea777a2f3a8baacdaeef69a1e22d5c7404;hpb=c73a549f6533ebd822e660ac881e1594f181c01f;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/SiteConfiguration.php b/includes/SiteConfiguration.php index ff5548ea77..6a861d8e88 100644 --- a/includes/SiteConfiguration.php +++ b/includes/SiteConfiguration.php @@ -21,7 +21,98 @@ */ /** - * This is a class used to hold configuration settings, particularly for multi-wiki sites. + * This is a class for holding configuration settings, particularly for + * multi-wiki sites. + * + * A basic synopsis: + * + * Consider a wikifarm having three sites: two production sites, one in English + * and one in German, and one testing site. You can assign them easy-to-remember + * identifiers - ISO 639 codes 'en' and 'de' for language wikis, and 'beta' for + * the testing wiki. + * + * You would thus initialize the site configuration by specifying the wiki + * identifiers: + * + * @code + * $conf = new SiteConfiguration; + * $conf->wikis = array( 'de', 'en', 'beta' ); + * @endcode + * + * When configuring the MediaWiki global settings (the $wg variables), + * the identifiers will be available to specify settings on a per wiki basis. + * + * @code + * $conf->settings = array( + * 'wgSomeSetting' => array( + * + * # production: + * 'de' => false, + * 'en' => false, + * + * # test: + * 'beta => true, + * ), + * ); + * @endcode + * + * With three wikis, that is easy to manage. But what about a farm with + * hundreds of wikis? Site configuration provides a special keyword named + * 'default' which is the value used when a wiki is not found. Hence + * the above code could be written: + * + * @code + * $conf->settings = array( + * 'wgSomeSetting' => array( + * + * 'default' => false, + * + * # Enable feature on test + * 'beta' => true, + * ), + * ); + * @endcode + * + * + * Since settings can contain arrays, site configuration provides a way + * to merge an array with the default. This is very useful to avoid + * repeating settings again and again while still maintaining specific changes + * on a per wiki basis. + * + * @code + * $conf->settings = array( + * 'wgMergeSetting' = array( + * # Value that will be shared among all wikis: + * 'default' => array( NS_USER => true ), + * + * # Leading '+' means merging the array of value with the defaults + * '+beta' => array( NS_HELP => true ), + * ), + * ); + * + * # Get configuration for the German site: + * $conf->get( 'wgMergeSetting', 'de' ); + * // --> array( NS_USER => true ); + * + * # Get configuration for the testing site: + * $conf->get( 'wgMergeSetting', 'beta' ); + * // --> array( NS_USER => true, NS_HELP => true ); + * @endcode + * + * Finally, to load all configuration settings, extract them in global context: + * + * @code + * # Name / identifier of the wiki as set in $conf->wikis + * $wikiID = 'beta'; + * $globals = $conf->getAll( $wikiID ); + * extract( $globals ); + * @endcode + * + * TODO: give examples for, + * suffixes: + * $conf->suffixes = array( 'wiki' ); + * localVHosts + * callbacks! */ class SiteConfiguration { @@ -47,6 +138,7 @@ class SiteConfiguration { /** * Optional callback to load full configuration data. + * @var string|array */ public $fullLoadCallback = null; @@ -64,6 +156,8 @@ class SiteConfiguration { * argument and the wiki in the second one. * if suffix and lang are passed they will be used for the return value of * self::siteFromDB() and self::$suffixes will be ignored + * + * @var string|array */ public $siteParamsCallback = null; @@ -98,7 +192,7 @@ class SiteConfiguration { if( array_key_exists( $wiki, $thisSetting ) ) { $retval = $thisSetting[$wiki]; break; - } elseif( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) { + } elseif ( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) { $retval = $thisSetting["+$wiki"]; } @@ -112,8 +206,9 @@ class SiteConfiguration { } break 2; } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) { - if( !isset( $retval ) ) + if( !isset( $retval ) ) { $retval = array(); + } $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] ); } } @@ -127,9 +222,10 @@ class SiteConfiguration { $retval = $thisSetting[$suffix]; } break; - } elseif( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) { - if (!isset($retval)) + } elseif ( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) { + if ( !isset( $retval ) ) { $retval = array(); + } $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] ); } } @@ -196,8 +292,9 @@ class SiteConfiguration { } $value = $this->getSetting( $varname, $wiki, $params ); - if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) ) + if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) ) { $value = self::arrayMerge( $value, $GLOBALS[$var] ); + } if ( !is_null( $value ) ) { $localSettings[$var] = $value; } @@ -317,8 +414,9 @@ class SiteConfiguration { } foreach( $default as $name => $def ){ - if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) ) + if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) ) { $ret[$name] = $default[$name]; + } } return $ret; @@ -339,18 +437,21 @@ class SiteConfiguration { protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ){ $ret = $this->getWikiParams( $wiki ); - if( is_null( $ret['suffix'] ) ) + if( is_null( $ret['suffix'] ) ) { $ret['suffix'] = $suffix; + } $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) ); $ret['params'] += $params; // Automatically fill that ones if needed - if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) ) + if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) ){ $ret['params']['lang'] = $ret['lang']; - if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) ) + } + if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) ) { $ret['params']['site'] = $ret['suffix']; + } return $ret; } @@ -364,8 +465,9 @@ class SiteConfiguration { public function siteFromDB( $db ) { // Allow override $def = $this->getWikiParams( $db ); - if( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) ) + if( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) ) { return array( $def['suffix'], $def['lang'] ); + } $site = null; $lang = null; @@ -422,7 +524,7 @@ class SiteConfiguration { } public function loadFullData() { - if ($this->fullLoadCallback && !$this->fullLoadDone) { + if ( $this->fullLoadCallback && !$this->fullLoadDone ) { call_user_func( $this->fullLoadCallback, $this ); $this->fullLoadDone = true; }