New OutputPage::addJsConfigVars() method (bug 31233)
authorKrinkle <krinkle@users.mediawiki.org>
Wed, 28 Sep 2011 22:08:08 +0000 (22:08 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Wed, 28 Sep 2011 22:08:08 +0000 (22:08 +0000)
* to make the output page specific mw.config map extendable.
* fixes (bug 31233) Make OutputPage's mw.config array extendable internally instead of hard coded
* fixes weird unfinished comment from r61690

RELEASE-NOTES-1.19
includes/OutputPage.php

index 329274b..08dc004 100644 (file)
@@ -69,6 +69,8 @@ production.
 * New common*.css files usable by skins instead of having to copy piles
   of generic styles from MonoBook or Vector's css.
 * Some deprecated presentational html attributes will now be automatically converted to css.
+* (bug 31233) New OutputPage::addJsConfigVars() method to make the output page specific
+  mw.config map extendable.
 
 === Bug fixes in 1.19 ===
 * $wgUploadNavigationUrl should be used for file redlinks if
index 7fd423f..6ea70b9 100644 (file)
@@ -124,6 +124,7 @@ class OutputPage extends ContextSource {
        // @todo FIXME: Next variables probably comes from the resource loader
        var $mModules = array(), $mModuleScripts = array(), $mModuleStyles = array(), $mModuleMessages = array();
        var $mResourceLoader;
+       var $mJsConfigVars = array();
 
        /** @todo FIXME: Is this still used ?*/
        var $mInlineMsg = array();
@@ -1330,7 +1331,7 @@ class OutputPage extends ContextSource {
        }
 
        /**
-        * Add wikitext with a custom Title object and
+        * Add wikitext with a custom Title object and tidy enabled.
         *
         * @param $text String: wikitext
         * @param $title Title object
@@ -2606,11 +2607,29 @@ $distantTemplates
        }
 
        /**
-        * Get an array containing global JS variables
+        * Add one or more variables to be set in mw.config in JavaScript.
         *
-        * Do not add things here which can be evaluated in
-        * ResourceLoaderStartupScript - in other words, without state.
-        * You will only be adding bloat to the page and causing page caches to
+        * @param $key {String|Array} Key or array of key/value pars.
+        * @param $value {Mixed} Value of the configuration variable.
+        */
+       public function addJsConfigVars( $keys, $value ) {
+               if ( is_array( $keys ) ) {
+                       foreach ( $keys as $key => $value ) {
+                               $this->mJsConfigVars[$key] = $value;
+                       }
+                       return;
+               }
+
+               $this->mJsConfigVars[$keys] = $value;
+       }
+
+
+       /**
+        * Get an array containing the variables to be set in mw.config in JavaScript.
+        *
+        * Do not add things here which can be evaluated in ResourceLoaderStartupScript
+        * - in other words, page-indendent/site-wide variables (without state).
+        * You will only be adding bloat to the html page and causing page caches to
         * have to be purged on configuration changes.
         */
        protected function getJSVars() {
@@ -2654,10 +2673,14 @@ $distantTemplates
                        $vars['wgIsMainPage'] = true;
                }
 
-               // Allow extensions to add their custom variables to the global JS variables
+               // Allow extensions to add their custom variables to the mw.config map.
+               // Use the 'ResourceLoaderGetConfigVars' hook if the variable is not
+               // page-dependant but site-wide (without state).
+               // Alternatively, you may want to use OutputPage->addJsConfigVars() instead.
                wfRunHooks( 'MakeGlobalVariablesScript', array( &$vars, &$this ) );
 
-               return $vars;
+               // Merge in variables from addJsConfigVars last
+               return array_merge( $vars, $this->mJsConfigVars );
        }
 
        /**