Merge changes Ic13414f0,I26085bfc
[lhc/web/wiklou.git] / includes / SiteConfiguration.php
index afe0cc7..fa871fe 100644 (file)
@@ -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 ),
@@ -161,13 +161,19 @@ class SiteConfiguration {
         */
        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() ) {
@@ -178,18 +184,18 @@ 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 ) {
                $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"] ) ) {
@@ -197,16 +203,16 @@ class SiteConfiguration {
                                }
 
                                // 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"] );
@@ -214,15 +220,15 @@ class SiteConfiguration {
                                }
                                // 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"]) ) {
+                                       } elseif ( array_key_exists( "+$suffix", $thisSetting ) && is_array( $thisSetting["+$suffix"] ) ) {
                                                if ( !isset( $retval ) ) {
                                                        $retval = array();
                                                }
@@ -231,8 +237,8 @@ class SiteConfiguration {
                                }
 
                                // 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'];
@@ -242,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 );
                        }
@@ -260,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;
@@ -274,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 ) == '+' ) {
@@ -304,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 );
        }
 
        /**
@@ -325,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 );
@@ -341,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 );
@@ -360,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;
@@ -375,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 );
@@ -403,18 +409,18 @@ 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];
                        }
                }
@@ -427,17 +433,17 @@ 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 ) {
                $ret = $this->getWikiParams( $wiki );
 
-               if( is_null( $ret['suffix'] ) ) {
+               if ( is_null( $ret['suffix'] ) ) {
                        $ret['suffix'] = $suffix;
                }
 
@@ -446,10 +452,10 @@ class SiteConfiguration {
                $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'];
                }
 
@@ -465,19 +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;
                        }
@@ -486,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
@@ -507,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 ) ) {