Add ParserOutput::addJsConfigVars
authorumherirrender <umherirrender_de.wp@web.de>
Sun, 19 Jan 2014 15:39:46 +0000 (16:39 +0100)
committerUmherirrender <umherirrender_de.wp@web.de>
Sun, 16 Feb 2014 20:35:49 +0000 (20:35 +0000)
The OutputPage has variables for modules, moduleScripts, moduleStyles,
moduleMessages and the config vars, but the ParserOutput is missing the
last one.

With ParserOutput::addJsConfigVars it is possible to add scripts and it
config vars at one place and have not use the MakeGlobalVariablesScript
hook or other ways to get the needed javascript variable in the output.

Change-Id: I6ad61cca76805f6b9d76e053c98c7509c323d5da

includes/OutputPage.php
includes/parser/ParserOutput.php

index 2b1d4a0..efcd838 100644 (file)
@@ -1574,6 +1574,7 @@ class OutputPage extends ContextSource {
                $this->addModuleScripts( $parserOutput->getModuleScripts() );
                $this->addModuleStyles( $parserOutput->getModuleStyles() );
                $this->addModuleMessages( $parserOutput->getModuleMessages() );
+               $this->addJsConfigVars( $parserOutput->getJsConfigVars() );
 
                // Template versioning...
                foreach ( (array)$parserOutput->getTemplateIds() as $ns => $dbks ) {
@@ -2945,6 +2946,16 @@ $templates
                return $html;
        }
 
+       /**
+        * Get the javascript config vars to include on this page
+        *
+        * @return Array of javascript config vars
+        * @since 1.23
+        */
+       public function getJsConfigVars() {
+               return $this->mJsConfigVars;
+       }
+
        /**
         * Add one or more variables to be set in mw.config in JavaScript.
         *
@@ -3077,7 +3088,7 @@ $templates
                wfRunHooks( 'MakeGlobalVariablesScript', array( &$vars, $this ) );
 
                // Merge in variables from addJsConfigVars last
-               return array_merge( $vars, $this->mJsConfigVars );
+               return array_merge( $vars, $this->getJsConfigVars() );
        }
 
        /**
index c021bea..b2c9757 100644 (file)
@@ -41,6 +41,7 @@ class ParserOutput extends CacheTime {
                $mModuleScripts = array(),    # Modules of which only the JS will be loaded by the resource loader
                $mModuleStyles = array(),     # Modules of which only the CSSS will be loaded by the resource loader
                $mModuleMessages = array(),   # Modules of which only the messages will be loaded by the resource loader
+               $mJsConfigVars = array(),     # JavaScript config variable for mw.config combined with this page
                $mOutputHooks = array(),      # Hook tags as per $wgParserOutputHooks
                $mWarnings = array(),         # Warning text to be returned to the user. Wikitext formatted, in the key only
                $mSections = array(),         # Table of contents
@@ -134,6 +135,8 @@ class ParserOutput extends CacheTime {
        function getModuleScripts()          { return $this->mModuleScripts; }
        function getModuleStyles()           { return $this->mModuleStyles; }
        function getModuleMessages()         { return $this->mModuleMessages; }
+       /** @since 1.23 */
+       function getJsConfigVars()           { return $this->mJsConfigVars; }
        function getOutputHooks()            { return (array)$this->mOutputHooks; }
        function getWarnings()               { return array_keys( $this->mWarnings ); }
        function getIndexPolicy()            { return $this->mIndexPolicy; }
@@ -318,6 +321,24 @@ class ParserOutput extends CacheTime {
                $this->mModuleMessages = array_merge( $this->mModuleMessages, (array)$modules );
        }
 
+       /**
+        * Add one or more variables to be set in mw.config in JavaScript.
+        *
+        * @param $keys {String|Array} Key or array of key/value pairs.
+        * @param $value {Mixed} [optional] Value of the configuration variable.
+        * @since 1.23
+        */
+       public function addJsConfigVars( $keys, $value = null ) {
+               if ( is_array( $keys ) ) {
+                       foreach ( $keys as $key => $value ) {
+                               $this->mJsConfigVars[$key] = $value;
+                       }
+                       return;
+               }
+
+               $this->mJsConfigVars[$keys] = $value;
+       }
+
        /**
         * Copy items from the OutputPage object into this one
         *
@@ -328,6 +349,7 @@ class ParserOutput extends CacheTime {
                $this->addModuleScripts( $out->getModuleScripts() );
                $this->addModuleStyles( $out->getModuleStyles() );
                $this->addModuleMessages( $out->getModuleMessages() );
+               $this->addJsConfigVars( $out->getJsConfigVars() );
 
                $this->mHeadItems = array_merge( $this->mHeadItems, $out->getHeadItemsArray() );
        }