X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FSiteConfiguration.php;h=fa871fe0b3e9f457b2601f605c60ba9eb442835b;hb=f90977bcc58fd88a806a50ca2d3faad0ee2dab2e;hp=6410695bd590ee052febaeb8f77c598641a62e86;hpb=fcd74e5dd3299926be0a41b55e2ed24424568948;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/SiteConfiguration.php b/includes/SiteConfiguration.php index 6410695bd5..fa871fe0b3 100644 --- a/includes/SiteConfiguration.php +++ b/includes/SiteConfiguration.php @@ -83,7 +83,7 @@ * $conf->settings = array( * 'wgMergeSetting' = array( * # Value that will be shared among all wikis: - * 'default' => array( NS_USER => true ), + * 'default' => array( NS_USER => true ), * * # Leading '+' means merging the array of value with the defaults * '+beta' => array( NS_HELP => true ), @@ -138,6 +138,7 @@ class SiteConfiguration { /** * Optional callback to load full configuration data. + * @var string|array */ public $fullLoadCallback = null; @@ -155,16 +156,24 @@ 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; + /** + * Configuration cache for getConfig() + * @var array + */ + protected $cfgCache = array(); + /** * Retrieves a configuration setting for a given wiki. - * @param $settingName String ID of the setting name to retrieve - * @param $wiki String Wiki ID of the wiki in question. - * @param $suffix String The suffix of the wiki in question. - * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data. - * @param $wikiTags Array The tags assigned to the wiki. + * @param string $settingName ID of the setting name to retrieve + * @param string $wiki Wiki ID of the wiki in question. + * @param string $suffix The suffix of the wiki in question. + * @param array $params List of parameters. $.'key' is replaced by $value in all returned data. + * @param array $wikiTags The tags assigned to the wiki. * @return Mixed the value of the setting requested. */ public function get( $settingName, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) { @@ -175,59 +184,61 @@ class SiteConfiguration { /** * Really retrieves a configuration setting for a given wiki. * - * @param $settingName String ID of the setting name to retrieve. - * @param $wiki String Wiki ID of the wiki in question. - * @param $params Array: array of parameters. + * @param string $settingName ID of the setting name to retrieve. + * @param string $wiki Wiki ID of the wiki in question. + * @param array $params array of parameters. * @return Mixed the value of the setting requested. */ - protected function getSetting( $settingName, $wiki, /*array*/ $params ){ + protected function getSetting( $settingName, $wiki, /*array*/ $params ) { $retval = null; - if( array_key_exists( $settingName, $this->settings ) ) { + if ( array_key_exists( $settingName, $this->settings ) ) { $thisSetting =& $this->settings[$settingName]; do { // Do individual wiki settings - if( array_key_exists( $wiki, $thisSetting ) ) { + 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"]; } // Do tag settings - foreach( $params['tags'] as $tag ) { - if( array_key_exists( $tag, $thisSetting ) ) { + foreach ( $params['tags'] as $tag ) { + if ( array_key_exists( $tag, $thisSetting ) ) { if ( isset( $retval ) && is_array( $retval ) && is_array( $thisSetting[$tag] ) ) { $retval = self::arrayMerge( $retval, $thisSetting[$tag] ); } else { $retval = $thisSetting[$tag]; } break 2; - } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) { - if( !isset( $retval ) ) + } elseif ( array_key_exists( "+$tag", $thisSetting ) && is_array( $thisSetting["+$tag"] ) ) { + if ( !isset( $retval ) ) { $retval = array(); + } $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] ); } } // Do suffix settings $suffix = $params['suffix']; - if( !is_null( $suffix ) ) { - if( array_key_exists( $suffix, $thisSetting ) ) { - if ( isset($retval) && is_array($retval) && is_array($thisSetting[$suffix]) ) { + if ( !is_null( $suffix ) ) { + if ( array_key_exists( $suffix, $thisSetting ) ) { + if ( isset( $retval ) && is_array( $retval ) && is_array( $thisSetting[$suffix] ) ) { $retval = self::arrayMerge( $retval, $thisSetting[$suffix] ); } else { $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"] ); } } // Fall back to default. - if( array_key_exists( 'default', $thisSetting ) ) { - if( is_array( $retval ) && is_array( $thisSetting['default'] ) ) { + if ( array_key_exists( 'default', $thisSetting ) ) { + if ( is_array( $retval ) && is_array( $thisSetting['default'] ) ) { $retval = self::arrayMerge( $retval, $thisSetting['default'] ); } else { $retval = $thisSetting['default']; @@ -237,7 +248,7 @@ class SiteConfiguration { } while ( false ); } - if( !is_null( $retval ) && count( $params['params'] ) ) { + if ( !is_null( $retval ) && count( $params['params'] ) ) { foreach ( $params['params'] as $key => $value ) { $retval = $this->doReplace( '$' . $key, $value, $retval ); } @@ -255,10 +266,10 @@ class SiteConfiguration { * @return string */ function doReplace( $from, $to, $in ) { - if( is_string( $in ) ) { + if ( is_string( $in ) ) { return str_replace( $from, $to, $in ); - } elseif( is_array( $in ) ) { - foreach( $in as $key => $val ) { + } elseif ( is_array( $in ) ) { + foreach ( $in as $key => $val ) { $in[$key] = $this->doReplace( $from, $to, $val ); } return $in; @@ -269,16 +280,16 @@ class SiteConfiguration { /** * Gets all settings for a wiki - * @param $wiki String Wiki ID of the wiki in question. - * @param $suffix String The suffix of the wiki in question. - * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data. - * @param $wikiTags Array The tags assigned to the wiki. + * @param string $wiki Wiki ID of the wiki in question. + * @param string $suffix The suffix of the wiki in question. + * @param array $params List of parameters. $.'key' is replaced by $value in all returned data. + * @param array $wikiTags The tags assigned to the wiki. * @return Array Array of settings requested. */ public function getAll( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) { $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags ); $localSettings = array(); - foreach( $this->settings as $varname => $stuff ) { + foreach ( $this->settings as $varname => $stuff ) { $append = false; $var = $varname; if ( substr( $varname, 0, 1 ) == '+' ) { @@ -287,8 +298,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; } @@ -298,14 +310,14 @@ class SiteConfiguration { /** * Retrieves a configuration setting for a given wiki, forced to a boolean. - * @param $setting String ID of the setting name to retrieve - * @param $wiki String Wiki ID of the wiki in question. - * @param $suffix String The suffix of the wiki in question. - * @param $wikiTags Array The tags assigned to the wiki. + * @param string $setting ID of the setting name to retrieve + * @param string $wiki Wiki ID of the wiki in question. + * @param string $suffix The suffix of the wiki in question. + * @param array $wikiTags The tags assigned to the wiki. * @return bool The value of the setting requested. */ public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) { - return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) ); + return (bool)$this->get( $setting, $wiki, $suffix, array(), $wikiTags ); } /** @@ -319,12 +331,12 @@ class SiteConfiguration { /** * Retrieves the value of a given setting, and places it in a variable passed by reference. - * @param $setting String ID of the setting name to retrieve - * @param $wiki String Wiki ID of the wiki in question. - * @param $suffix String The suffix of the wiki in question. - * @param $var array Reference The variable to insert the value into. - * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data. - * @param $wikiTags Array The tags assigned to the wiki. + * @param string $setting ID of the setting name to retrieve + * @param string $wiki Wiki ID of the wiki in question. + * @param string $suffix The suffix of the wiki in question. + * @param array $var Reference The variable to insert the value into. + * @param array $params List of parameters. $.'key' is replaced by $value in all returned data. + * @param array $wikiTags The tags assigned to the wiki. */ public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) { $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags ); @@ -335,11 +347,11 @@ class SiteConfiguration { /** * Retrieves the value of a given setting, and places it in its corresponding global variable. - * @param $setting String ID of the setting name to retrieve - * @param $wiki String Wiki ID of the wiki in question. - * @param $suffix String The suffix of the wiki in question. - * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data. - * @param $wikiTags Array The tags assigned to the wiki. + * @param string $setting ID of the setting name to retrieve + * @param string $wiki Wiki ID of the wiki in question. + * @param string $suffix The suffix of the wiki in question. + * @param array $params List of parameters. $.'key' is replaced by $value in all returned data. + * @param array $wikiTags The tags assigned to the wiki. */ public function extractGlobal( $setting, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) { $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags ); @@ -354,9 +366,9 @@ class SiteConfiguration { public function extractGlobalSetting( $setting, $wiki, $params ) { $value = $this->getSetting( $setting, $wiki, $params ); if ( !is_null( $value ) ) { - if (substr($setting,0,1) == '+' && is_array($value)) { - $setting = substr($setting,1); - if ( is_array($GLOBALS[$setting]) ) { + if ( substr( $setting, 0, 1 ) == '+' && is_array( $value ) ) { + $setting = substr( $setting, 1 ); + if ( is_array( $GLOBALS[$setting] ) ) { $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value ); } else { $GLOBALS[$setting] = $value; @@ -369,10 +381,10 @@ class SiteConfiguration { /** * Retrieves the values of all settings, and places them in their corresponding global variables. - * @param $wiki String Wiki ID of the wiki in question. - * @param $suffix String The suffix of the wiki in question. - * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data. - * @param $wikiTags Array The tags assigned to the wiki. + * @param string $wiki Wiki ID of the wiki in question. + * @param string $suffix The suffix of the wiki in question. + * @param array $params List of parameters. $.'key' is replaced by $value in all returned data. + * @param array $wikiTags The tags assigned to the wiki. */ public function extractAllGlobals( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) { $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags ); @@ -389,7 +401,7 @@ class SiteConfiguration { * @param $wiki String * @return array */ - protected function getWikiParams( $wiki ){ + protected function getWikiParams( $wiki ) { static $default = array( 'suffix' => null, 'lang' => null, @@ -397,19 +409,20 @@ class SiteConfiguration { 'params' => array(), ); - if( !is_callable( $this->siteParamsCallback ) ) { + if ( !is_callable( $this->siteParamsCallback ) ) { return $default; } $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) ); # Validate the returned value - if( !is_array( $ret ) ) { + if ( !is_array( $ret ) ) { return $default; } - foreach( $default as $name => $def ){ - if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) ) + foreach ( $default as $name => $def ) { + if ( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) ) { $ret[$name] = $default[$name]; + } } return $ret; @@ -420,28 +433,31 @@ class SiteConfiguration { * by self::$siteParamsCallback for backward compatibility * Values returned by self::getWikiParams() have the priority. * - * @param $wiki String Wiki ID of the wiki in question. - * @param $suffix String The suffix of the wiki in question. - * @param $params Array List of parameters. $.'key' is replaced by $value in + * @param string $wiki Wiki ID of the wiki in question. + * @param string $suffix The suffix of the wiki in question. + * @param array $params List of parameters. $.'key' is replaced by $value in * all returned data. - * @param $wikiTags Array The tags assigned to the wiki. + * @param array $wikiTags The tags assigned to the wiki. * @return array */ - protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ){ + 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; } @@ -455,18 +471,19 @@ 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; - foreach ( $this->suffixes as $suffix ) { + foreach ( $this->suffixes as $altSite => $suffix ) { if ( $suffix === '' ) { $site = ''; $lang = $db; break; } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) { - $site = $suffix == 'wiki' ? 'wikipedia' : $suffix; + $site = is_numeric( $altSite ) ? $suffix : $altSite; $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) ); break; } @@ -475,6 +492,67 @@ class SiteConfiguration { return array( $site, $lang ); } + /** + * Get the resolved (post-setup) configuration of a potentially foreign wiki. + * For foreign wikis, this is expensive, and only works if maintenance + * scripts are setup to handle the --wiki parameter such as in wiki farms. + * + * @param string $wiki + * @param array|string $settings A setting name or array of setting names + * @return Array|mixed Array if $settings is an array, otherwise the value + * @throws MWException + * @since 1.21 + */ + public function getConfig( $wiki, $settings ) { + global $IP; + + $multi = is_array( $settings ); + $settings = (array)$settings; + if ( $wiki === wfWikiID() ) { // $wiki is this wiki + $res = array(); + foreach ( $settings as $name ) { + if ( !preg_match( '/^wg[A-Z]/', $name ) ) { + throw new MWException( "Variable '$name' does start with 'wg'." ); + } elseif ( !isset( $GLOBALS[$name] ) ) { + throw new MWException( "Variable '$name' is not set." ); + } + $res[$name] = $GLOBALS[$name]; + } + } else { // $wiki is a foreign wiki + if ( isset( $this->cfgCache[$wiki] ) ) { + $res = array_intersect_key( $this->cfgCache[$wiki], array_flip( $settings ) ); + if ( count( $res ) == count( $settings ) ) { + return $res; // cache hit + } + } elseif ( !in_array( $wiki, $this->wikis ) ) { + throw new MWException( "No such wiki '$wiki'." ); + } else { + $this->cfgCache[$wiki] = array(); + } + $retVal = 1; + $cmd = wfShellWikiCmd( + "$IP/maintenance/getConfiguration.php", + array( + '--wiki', $wiki, + '--settings', implode( ' ', $settings ), + '--format', 'PHP' + ) + ); + // ulimit5.sh breaks this call + $data = trim( wfShellExec( $cmd, $retVal, array(), array( 'memory' => 0 ) ) ); + if ( $retVal != 0 || !strlen( $data ) ) { + throw new MWException( "Failed to run getConfiguration.php." ); + } + $res = unserialize( $data ); + if ( !is_array( $res ) ) { + throw new MWException( "Failed to unserialize configuration array." ); + } + $this->cfgCache[$wiki] = $this->cfgCache[$wiki] + $res; + } + + return $multi ? $res : current( $res ); + } + /** * Returns true if the given vhost is handled locally. * @param $vhost String @@ -496,11 +574,11 @@ class SiteConfiguration { */ static function arrayMerge( $array1/* ... */ ) { $out = $array1; - for( $i = 1; $i < func_num_args(); $i++ ) { - foreach( func_get_arg( $i ) as $key => $value ) { - if ( isset($out[$key]) && is_array($out[$key]) && is_array($value) ) { + for ( $i = 1; $i < func_num_args(); $i++ ) { + foreach ( func_get_arg( $i ) as $key => $value ) { + if ( isset( $out[$key] ) && is_array( $out[$key] ) && is_array( $value ) ) { $out[$key] = self::arrayMerge( $out[$key], $value ); - } elseif ( !isset($out[$key]) || !$out[$key] && !is_numeric($key) ) { + } elseif ( !isset( $out[$key] ) || !$out[$key] && !is_numeric( $key ) ) { // Values that evaluate to true given precedence, for the primary purpose of merging permissions arrays. $out[$key] = $value; } elseif ( is_numeric( $key ) ) { @@ -513,7 +591,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; }