Adding output parameter to PageHistoryBeforeList hook
[lhc/web/wiklou.git] / includes / SiteConfiguration.php
index 7e96d45..bbc14a1 100644 (file)
@@ -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,9 +184,9 @@ 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 ) {
@@ -274,10 +280,10 @@ 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() ) {
@@ -304,10 +310,10 @@ 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() ) {
@@ -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 );
@@ -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 );
@@ -427,11 +433,11 @@ 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 ) {
@@ -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