Allow extensions to add jQueryMsg magic words

Change-Id: Ie82a147ff32ccda3f757108474f5cbab71d45ace
This commit is contained in:
Matthew Flaschen 2017-03-22 02:09:48 -04:00
parent 1f6811eac2
commit 7e65b6b3a7
4 changed files with 71 additions and 7 deletions

View file

@ -2702,6 +2702,13 @@ variables from $wgResourceLoaderLESSVars are added. Can be used to add
context-based variables.
&$lessVars: array of variables already added
'ResourceLoaderJqueryMsgModuleMagicWords': Called in
ResourceLoaderJqueryMsgModule to allow adding magic words for jQueryMsg.
The value should be a string, and they can depend only on the
ResourceLoaderContext.
$context: ResourceLoaderContext
&$magicWords: Associative array mapping all-caps magic word to a string value
'ResourceLoaderRegisterModules': Right before modules information is required,
such as when responding to a resource
loader request or generating HTML output.

View file

@ -43,9 +43,26 @@ class ResourceLoaderJqueryMsgModule extends ResourceLoaderFileModule {
)
);
$dataScript = Xml::encodeJsCall( 'mw.jqueryMsg.setParserDefaults', [ $parserDefaults ] );
$mainDataScript = Xml::encodeJsCall( 'mw.jqueryMsg.setParserDefaults', [ $parserDefaults ] );
return $fileScript . $dataScript;
// Associative array mapping magic words (e.g. SITENAME)
// to their values.
$magicWords = [
'SITENAME' => $this->getConfig()->get( 'Sitename' ),
];
Hooks::run( 'ResourceLoaderJqueryMsgModuleMagicWords', [ $context, &$magicWords ] );
$magicWordExtendData = [
'magic' => $magicWords,
];
$magicWordDataScript = Xml::encodeJsCall( 'mw.jqueryMsg.setParserDefaults', [
$magicWordExtendData,
/* deep= */ true
] );
return $fileScript . $mainDataScript . $magicWordDataScript;
}
/**

View file

@ -16,8 +16,7 @@
parserDefaults = {
magic: {
PAGENAME: mw.config.get( 'wgPageName' ),
PAGENAMEE: mw.util.wikiUrlencode( mw.config.get( 'wgPageName' ) ),
SITENAME: mw.config.get( 'wgSiteName' )
PAGENAMEE: mw.util.wikiUrlencode( mw.config.get( 'wgPageName' ) )
},
// Whitelist for allowed HTML elements in wikitext.
// Self-closing tags are not currently supported.
@ -164,10 +163,15 @@
* parsers, pass the relevant options to mw.jqueryMsg.parser.
*
* @private
* @param {Object} data
* @param {Object} data New data to extend parser defaults with
* @param {boolean} [deep=false] Whether the extend is done recursively (deep)
*/
mw.jqueryMsg.setParserDefaults = function ( data ) {
$.extend( parserDefaults, data );
mw.jqueryMsg.setParserDefaults = function ( data, deep ) {
if ( deep ) {
$.extend( true, parserDefaults, data );
} else {
$.extend( parserDefaults, data );
}
};
/**

View file

@ -1196,4 +1196,40 @@
);
} );
QUnit.test( 'setParserDefaults', function ( assert ) {
mw.jqueryMsg.setParserDefaults( {
magic: {
FOO: 'foo',
BAR: 'bar'
}
} );
assert.deepEqual(
mw.jqueryMsg.getParserDefaults().magic,
{
FOO: 'foo',
BAR: 'bar'
},
'setParserDefaults is shallow by default'
);
mw.jqueryMsg.setParserDefaults(
{
magic: {
BAZ: 'baz'
}
},
true
);
assert.deepEqual(
mw.jqueryMsg.getParserDefaults().magic,
{
FOO: 'foo',
BAR: 'bar',
BAZ: 'baz'
},
'setParserDefaults is deep if requested'
);
} );
}( mediaWiki, jQuery ) );