Allow extensions to add jQueryMsg magic words
authorMatthew Flaschen <mflaschen@wikimedia.org>
Wed, 22 Mar 2017 06:09:48 +0000 (02:09 -0400)
committerMatthew Flaschen <mflaschen@wikimedia.org>
Wed, 22 Mar 2017 19:09:13 +0000 (15:09 -0400)
Change-Id: Ie82a147ff32ccda3f757108474f5cbab71d45ace

docs/hooks.txt
includes/resourceloader/ResourceLoaderJqueryMsgModule.php
resources/src/mediawiki/mediawiki.jqueryMsg.js
tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js

index f307f45..cf4e3ab 100644 (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.
index a3b059b..1704481 100644 (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;
        }
 
        /**
index c82b9cb..282a2ee 100644 (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.
         * 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 );
+               }
        };
 
        /**
index 3b549bd..3c77a00 100644 (file)
                );
        } );
 
+       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 ) );