From 542d2e8a90b4708b32bd8a82f09e7ff88b86c30a Mon Sep 17 00:00:00 2001 From: umherirrender Date: Sun, 19 Jan 2014 16:39:46 +0100 Subject: [PATCH] Add ParserOutput::addJsConfigVars 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 | 13 ++++++++++++- includes/parser/ParserOutput.php | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 2b1d4a0596..efcd838df3 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -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() ); } /** diff --git a/includes/parser/ParserOutput.php b/includes/parser/ParserOutput.php index c021bea85b..b2c9757d4c 100644 --- a/includes/parser/ParserOutput.php +++ b/includes/parser/ParserOutput.php @@ -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() ); } -- 2.20.1