Merge "Special:Preferences: Remove unnecessary OOUI styles override"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 19 Sep 2018 18:53:14 +0000 (18:53 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 19 Sep 2018 18:53:14 +0000 (18:53 +0000)
includes/resourceloader/ResourceLoader.php
includes/resourceloader/ResourceLoaderClientHtml.php
includes/resourceloader/ResourceLoaderStartUpModule.php
tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php
tests/phpunit/includes/resourceloader/ResourceLoaderTest.php

index dde72b2..604a140 100644 (file)
@@ -1162,8 +1162,8 @@ MESSAGE;
                        }
                } else {
                        if ( $states ) {
-                               // Keep default escaping of slashes (e.g. "</script>") for ResourceLoaderClientHtml.
-                               $this->errors[] = 'Problematic modules: ' . json_encode( $states, JSON_PRETTY_PRINT );
+                               $this->errors[] = 'Problematic modules: '
+                                       . self::encodeJsonForScript( $states );
                        }
                }
 
@@ -1292,6 +1292,35 @@ MESSAGE;
                return $out;
        }
 
+       /**
+        * Wrapper around json_encode that avoids needless escapes,
+        * and pretty-prints in debug mode.
+        *
+        * @internal
+        * @since 1.32
+        * @param bool|string|array $data
+        * @return string JSON
+        */
+       public static function encodeJsonForScript( $data ) {
+               // Keep output as small as possible by disabling needless escape modes
+               // that PHP uses by default.
+               // However, while most module scripts are only served on HTTP responses
+               // for JavaScript, some modules can also be embedded in the HTML as inline
+               // scripts. This, and the fact that we sometimes need to export strings
+               // containing user-generated content and labels that may genuinely contain
+               // a sequences like "</script>", we need to encode either '/' or '<'.
+               // By default PHP escapes '/'. Let's escape '<' instead which is less common
+               // and allows URLs to mostly remain readable.
+               $jsonFlags = JSON_UNESCAPED_SLASHES |
+                       JSON_UNESCAPED_UNICODE |
+                       JSON_HEX_TAG |
+                       JSON_HEX_AMP;
+               if ( self::inDebugMode() ) {
+                       $jsonFlags |= JSON_PRETTY_PRINT;
+               }
+               return json_encode( $data, $jsonFlags );
+       }
+
        /**
         * Returns a JS call to mw.loader.state, which sets the state of one
         * ore more modules to a given value. Has two calling conventions:
@@ -1353,69 +1382,56 @@ MESSAGE;
 
        /**
         * Returns JS code which calls mw.loader.register with the given
-        * parameters. Has three calling conventions:
+        * parameter.
         *
-        *   - ResourceLoader::makeLoaderRegisterScript( $name, $version,
-        *        $dependencies, $group, $source, $skip
-        *     ):
-        *        Register a single module.
+        * @par Example
+        * @code
         *
-        *   - ResourceLoader::makeLoaderRegisterScript( [ $name1, $name2 ] ):
-        *        Register modules with the given names.
-        *
-        *   - ResourceLoader::makeLoaderRegisterScript( [
+        *     ResourceLoader::makeLoaderRegisterScript( [
         *        [ $name1, $version1, $dependencies1, $group1, $source1, $skip1 ],
         *        [ $name2, $version2, $dependencies1, $group2, $source2, $skip2 ],
         *        ...
         *     ] ):
-        *        Registers modules with the given names and parameters.
+        * @endcode
         *
-        * @param string $name Module name
-        * @param string|null $version Module version hash
-        * @param array|null $dependencies List of module names on which this module depends
-        * @param string|null $group Group which the module is in
-        * @param string|null $source Source of the module, or 'local' if not foreign
-        * @param string|null $skip Script body of the skip function
+        * @internal
+        * @since 1.32
+        * @param array $modules Array of module registration arrays, each containing
+        *  - string: module name
+        *  - string: module version
+        *  - array|null: List of dependencies (optional)
+        *  - string|null: Module group (optional)
+        *  - string|null: Name of foreign module source, or 'local' (optional)
+        *  - string|null: Script body of a skip function (optional)
         * @return string JavaScript code
         */
-       public static function makeLoaderRegisterScript( $name, $version = null,
-               $dependencies = null, $group = null, $source = null, $skip = null
-       ) {
-               if ( is_array( $name ) ) {
+       public static function makeLoaderRegisterScript( array $modules ) {
+               // Optimisation: Transform dependency names into indexes when possible
+               // to produce smaller output. They are expanded by mw.loader.register on
+               // the other end using resolveIndexedDependencies().
+               $index = [];
+               foreach ( $modules as $i => &$module ) {
                        // Build module name index
-                       $index = [];
-                       foreach ( $name as $i => &$module ) {
-                               $index[$module[0]] = $i;
-                       }
-
-                       // Transform dependency names into indexes when possible, they will be resolved by
-                       // mw.loader.register on the other end
-                       foreach ( $name as &$module ) {
-                               if ( isset( $module[2] ) ) {
-                                       foreach ( $module[2] as &$dependency ) {
-                                               if ( isset( $index[$dependency] ) ) {
-                                                       $dependency = $index[$dependency];
-                                               }
+                       $index[$module[0]] = $i;
+               }
+               foreach ( $modules as &$module ) {
+                       if ( isset( $module[2] ) ) {
+                               foreach ( $module[2] as &$dependency ) {
+                                       if ( isset( $index[$dependency] ) ) {
+                                               // Replace module name in dependency list with index
+                                               $dependency = $index[$dependency];
                                        }
                                }
                        }
+               }
 
-                       array_walk( $name, [ 'self', 'trimArray' ] );
+               array_walk( $modules, [ 'self', 'trimArray' ] );
 
-                       return Xml::encodeJsCall(
-                               'mw.loader.register',
-                               [ $name ],
-                               self::inDebugMode()
-                       );
-               } else {
-                       $registration = [ $name, $version, $dependencies, $group, $source, $skip ];
-                       self::trimArray( $registration );
-                       return Xml::encodeJsCall(
-                               'mw.loader.register',
-                               $registration,
-                               self::inDebugMode()
-                       );
-               }
+               return Xml::encodeJsCall(
+                       'mw.loader.register',
+                       [ $modules ],
+                       self::inDebugMode()
+               );
        }
 
        /**
@@ -1466,7 +1482,7 @@ MESSAGE;
        public static function makeInlineCodeWithModule( $modules, $script ) {
                // Adds an array to lazy-created RLQ
                return '(window.RLQ=window.RLQ||[]).push(['
-                       . json_encode( $modules ) . ','
+                       . self::encodeJsonForScript( $modules ) . ','
                        . 'function(){' . trim( $script ) . '}'
                        . ']);';
        }
index 8d08366..5c072bf 100644 (file)
@@ -304,7 +304,10 @@ class ResourceLoaderClientHtml {
                // Inline RLQ: Load general modules
                if ( $data['general'] ) {
                        $chunks[] = ResourceLoader::makeInlineScript(
-                               Xml::encodeJsCall( 'mw.loader.load', [ $data['general'] ] ),
+                               'RLPAGEMODULES='
+                                       . ResourceLoader::encodeJsonForScript( $data['general'] )
+                                       . ';'
+                                       . 'mw.loader.load(RLPAGEMODULES);',
                                $nonce
                        );
                }
index 8140c2c..e4a753f 100644 (file)
@@ -404,16 +404,9 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
                        $mwLoaderCode .= file_get_contents( "$IP/resources/src/startup/profiler.js" );
                }
 
-               // Keep output as small as possible by disabling needless escapes that PHP uses by default.
-               // This is not HTML output, only used in a JS response.
-               $jsonFlags = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
-               if ( ResourceLoader::inDebugMode() ) {
-                       $jsonFlags |= JSON_PRETTY_PRINT;
-               }
-
                // Perform replacements for mediawiki.js
                $mwLoaderPairs = [
-                       '$VARS.baseModules' => json_encode( $this->getBaseModules(), $jsonFlags ),
+                       '$VARS.baseModules' => ResourceLoader::encodeJsonForScript( $this->getBaseModules() ),
                ];
                $profilerStubs = [
                        '$CODE.profileExecuteStart();' => 'mw.loader.profiler.onExecuteStart( module );',
@@ -432,13 +425,11 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
 
                // Perform string replacements for startup.js
                $pairs = [
-                       '$VARS.wgLegacyJavaScriptGlobals' => json_encode(
-                               $this->getConfig()->get( 'LegacyJavaScriptGlobals' ),
-                               $jsonFlags
+                       '$VARS.wgLegacyJavaScriptGlobals' => ResourceLoader::encodeJsonForScript(
+                               $this->getConfig()->get( 'LegacyJavaScriptGlobals' )
                        ),
-                       '$VARS.configuration' => json_encode(
-                               $this->getConfigSettings( $context ),
-                               $jsonFlags
+                       '$VARS.configuration' => ResourceLoader::encodeJsonForScript(
+                               $this->getConfigSettings( $context )
                        ),
                        // Raw JavaScript code (not JSON)
                        '$CODE.registrations();' => trim( $this->getModuleRegistrations( $context ) ),
index 7cd6983..dbc757f 100644 (file)
@@ -226,7 +226,7 @@ Deprecation message.' ]
                        . 'mw.config.set({"key":"value"});'
                        . 'mw.loader.state({"test.exempt":"ready","test.private":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.styles.deprecated":"ready","test.scripts":"loading"});'
                        . 'mw.loader.implement("test.private@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
-                       . 'mw.loader.load(["test"]);'
+                       . 'RLPAGEMODULES=["test"];mw.loader.load(RLPAGEMODULES);'
                        . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts\u0026only=scripts\u0026skin=fallback");'
                        . '});</script>' . "\n"
                        . '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.deprecated%2Cpure&amp;only=styles&amp;skin=fallback"/>' . "\n"
index 9040f39..171f2a6 100644 (file)
@@ -499,12 +499,42 @@ mw.example();
                );
 
                $this->assertEquals(
-                       'mw.loader.register( "test.name", "1234567" );',
-                       ResourceLoader::makeLoaderRegisterScript(
-                               'test.name',
-                               '1234567'
-                       ),
-                       'Variadic parameters'
+                       'mw.loader.register( [
+    [
+        "test.foo",
+        "100"
+    ],
+    [
+        "test.bar",
+        "200",
+        [
+            "test.unknown"
+        ]
+    ],
+    [
+        "test.baz",
+        "300",
+        [
+            3,
+            0
+        ]
+    ],
+    [
+        "test.quux",
+        "400",
+        [],
+        null,
+        null,
+        "return true;"
+    ]
+] );',
+                       ResourceLoader::makeLoaderRegisterScript( [
+                               [ 'test.foo', '100' , [], null, null ],
+                               [ 'test.bar', '200', [ 'test.unknown' ], null ],
+                               [ 'test.baz', '300', [ 'test.quux', 'test.foo' ], null ],
+                               [ 'test.quux', '400', [], null, null, 'return true;' ],
+                       ] ),
+                       'Compact dependency indexes'
                );
        }