mediawiki.jqueryMsg: Phase out redundant data module and minor clean up
authorTimo Tijhof <krinklemail@gmail.com>
Mon, 8 Jun 2015 14:47:21 +0000 (15:47 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Tue, 9 Jun 2015 18:14:20 +0000 (19:14 +0100)
Follows-up 4a3e50a54.

* Merge mediawiki.jqueryMsg.data and mediawiki.jqueryMsg modules.

  There's no need for this to be a separate module. The data is not for public consumption,
  it's provided to jqueryMsg only.

* Remove unused default-default values for 'allowedHtmlElements'.

* Remove conditionals around data providing at initial run-time. Instead, expose
  private method can call that. This way, we don't have two code paths claim
  ownership over the namespace. And it makes the module easier to test and re-use
  by not requiring the data to exist at first run time.

* Fix getDefinitionSummary() implementation to append data instead of setting
  arbitary keys in parent data. ResourceLoader documentation of getDefinitionSummary()
  has been updated to reflect this practice.

Change-Id: I40006d39514a997dce4930756a3dac84a0c9bb83

autoload.php
includes/resourceloader/ResourceLoaderJqueryMsgDataModule.php [deleted file]
includes/resourceloader/ResourceLoaderJqueryMsgModule.php [new file with mode: 0644]
resources/Resources.php
resources/src/mediawiki/mediawiki.jqueryMsg.js

index 74c7efd..d543fe0 100644 (file)
@@ -993,7 +993,7 @@ $wgAutoloadLocalClasses = array(
        'ResourceLoaderFilePath' => __DIR__ . '/includes/resourceloader/ResourceLoaderFilePath.php',
        'ResourceLoaderImage' => __DIR__ . '/includes/resourceloader/ResourceLoaderImage.php',
        'ResourceLoaderImageModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderImageModule.php',
-       'ResourceLoaderJqueryMsgDataModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderJqueryMsgDataModule.php',
+       'ResourceLoaderJqueryMsgModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderJqueryMsgModule.php',
        'ResourceLoaderLanguageDataModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderLanguageDataModule.php',
        'ResourceLoaderLanguageNamesModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderLanguageNamesModule.php',
        'ResourceLoaderModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderModule.php',
diff --git a/includes/resourceloader/ResourceLoaderJqueryMsgDataModule.php b/includes/resourceloader/ResourceLoaderJqueryMsgDataModule.php
deleted file mode 100644 (file)
index fda3faa..0000000
+++ /dev/null
@@ -1,62 +0,0 @@
-<?php
-/**
- * Resource loader module for populating mediawiki.jqueryMsg data.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @file
- * @author Brad Jorsch
- */
-
-/**
- * ResourceLoader module for populating mediawiki.jqueryMsg data.
- */
-class ResourceLoaderJqueryMsgDataModule extends ResourceLoaderModule {
-
-       protected $targets = array( 'desktop', 'mobile' );
-
-       /**
-        * @param ResourceLoaderContext $context
-        * @return string JavaScript code
-        */
-       public function getScript( ResourceLoaderContext $context ) {
-               $jsData = array();
-
-               $tagData = Sanitizer::getRecognizedTagData();
-               $jsData['allowedHtmlElements'] = array_merge(
-                       array_keys( $tagData['htmlpairs'] ),
-                       array_diff(
-                               array_keys( $tagData['htmlsingle'] ),
-                               array_keys( $tagData['htmlsingleonly'] )
-                       )
-               );
-
-               return "if ( !mw.jqueryMsg ) {\n" .
-                       "\tmw.jqueryMsg = {};\n" .
-                       "}\n" .
-                       "mw.jqueryMsg.data = " . Xml::encodeJsVar( $jsData ) . ";\n";
-       }
-
-       /**
-        * @param ResourceLoaderContext $context
-        * @return array|null
-        */
-       public function getDefinitionSummary( ResourceLoaderContext $context ) {
-               $ret = parent::getDefinitionSummary( $context );
-               $ret['hash'] = md5( $this->getScript( $context ) );
-               return $ret;
-       }
-}
diff --git a/includes/resourceloader/ResourceLoaderJqueryMsgModule.php b/includes/resourceloader/ResourceLoaderJqueryMsgModule.php
new file mode 100644 (file)
index 0000000..b905781
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+/**
+ * ResourceLoader module for mediawiki.jqueryMsg that provides generated data.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @author Brad Jorsch
+ */
+
+/**
+ * ResourceLoader module for mediawiki.jqueryMsg and its generated data
+ */
+class ResourceLoaderJqueryMsgModule extends ResourceLoaderFileModule {
+
+       /**
+        * @param ResourceLoaderContext $context
+        * @return string JavaScript code
+        */
+       public function getScript( ResourceLoaderContext $context ) {
+               $fileScript = parent::getScript( $context );
+
+               $tagData = Sanitizer::getRecognizedTagData();
+               $parserDefaults = array();
+               $parserDefaults['allowedHtmlElements'] = array_merge(
+                       array_keys( $tagData['htmlpairs'] ),
+                       array_diff(
+                               array_keys( $tagData['htmlsingle'] ),
+                               array_keys( $tagData['htmlsingleonly'] )
+                       )
+               );
+
+               $dataScript = Xml::encodeJsCall( 'mw.jqueryMsg.setParserDefaults', array( $parserDefaults ) );
+
+               return $fileScript . $dataScript;
+       }
+
+       /**
+        * @param ResourceLoaderContext $context
+        * @return array|null
+        */
+       public function getDefinitionSummary( ResourceLoaderContext $context ) {
+               $summary = parent::getDefinitionSummary( $context );
+               $summary[] = array(
+                       'sanitizerData' => Sanitizer::getRecognizedTagData()
+               );
+               return $summary;
+       }
+}
index 644ff9c..7ba1b2d 100644 (file)
@@ -59,9 +59,6 @@ return array(
        // Scripts for the dynamic language specific data, like grammar forms.
        'mediawiki.language.data' => array( 'class' => 'ResourceLoaderLanguageDataModule' ),
 
-       // Dynamic data for mediawiki.jqueryMsg, such as allowed tags
-       'mediawiki.jqueryMsg.data' => array( 'class' => 'ResourceLoaderJqueryMsgDataModule' ),
-
        /* MediaWiki base skinning modules */
 
        /**
@@ -1297,9 +1294,10 @@ return array(
        ),
 
        'mediawiki.jqueryMsg' => array(
+               // Add data for mediawiki.jqueryMsg, such as allowed tags
+               'class' => 'ResourceLoaderJqueryMsgModule',
                'scripts' => 'resources/src/mediawiki/mediawiki.jqueryMsg.js',
                'dependencies' => array(
-                       'mediawiki.jqueryMsg.data',
                        'mediawiki.util',
                        'mediawiki.language',
                        'dom-level2-shim',
index 98148d5..66d560e 100644 (file)
                        magic: {
                                'SITENAME': mw.config.get( 'wgSiteName' )
                        },
-                       // This is a whitelist like Sanitizer.php.
+                       // Whitelist for allowed HTML elements in wikitext.
                        // Self-closing tags are not currently supported.
-                       // The simplified default here is overridden below by data supplied
-                       // by the mediawiki.jqueryMsg.data module.
-                       allowedHtmlElements: [
-                               'b',
-                               'i'
-                       ],
+                       // Can be populated via setPrivateData().
+                       allowedHtmlElements: [],
                        // Key tag name, value allowed attributes for that tag.
                        // See Sanitizer::setupAttributeWhitelist
                        allowedHtmlCommonAttributes: [
                };
        }
 
-       // Use data from mediawiki.jqueryMsg.data to override defaults, if
-       // available
-       if ( mw.jqueryMsg && mw.jqueryMsg.data ) {
-               if ( mw.jqueryMsg.data.allowedHtmlElements ) {
-                       parserDefaults.allowedHtmlElements = mw.jqueryMsg.data.allowedHtmlElements;
-               }
-       }
-
        mw.jqueryMsg = {};
 
+       /**
+        * Initialize parser defaults.
+        *
+        * ResourceLoaderJqueryMsgModule calls this to provide default values from
+        * Sanitizer.php for allowed HTML elements. To override this data for individual
+        * parsers, pass the relevant options to mw.jqueryMsg.parser.
+        *
+        * @private
+        * @param {Object} data
+        */
+       mw.jqueryMsg.setParserDefaults = function ( data ) {
+               if ( data.allowedHtmlElements ) {
+                       parserDefaults.allowedHtmlElements = data.allowedHtmlElements;
+               }
+       };
+
        /**
         * Returns a function suitable for use as a global, to construct strings from the message key (and optional replacements).
         * e.g.