resourceloader: Audit use of JSON encoding and use json_encode directly
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderStartUpModule.php
index 99ffcd2..18cc4d5 100644 (file)
@@ -392,16 +392,16 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
                        $mwLoaderCode .= file_get_contents( "$IP/resources/src/startup/profiler.js" );
                }
 
-               $mapToJson = function ( $value ) {
-                       $value = FormatJson::encode( $value, ResourceLoader::inDebugMode(), FormatJson::ALL_OK );
-                       // Fix indentation
-                       $value = str_replace( "\n", "\n\t", $value );
-                       return $value;
-               };
+               // 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' => $mapToJson( $this->getBaseModules() ),
+                       '$VARS.baseModules' => json_encode( $this->getBaseModules(), $jsonFlags ),
                ];
                $profilerStubs = [
                        '$CODE.profileExecuteStart();' => 'mw.loader.profiler.onExecuteStart( module );',
@@ -418,18 +418,20 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
                }
                $mwLoaderCode = strtr( $mwLoaderCode, $mwLoaderPairs );
 
-               // Perform replacements for startup.js
-               $pairs = array_map( $mapToJson, [
-                       '$VARS.wgLegacyJavaScriptGlobals' => $this->getConfig()->get( 'LegacyJavaScriptGlobals' ),
-                       '$VARS.configuration' => $this->getConfigSettings( $context ),
-               ] );
-               // Raw JavaScript code (not for JSON)
-               $pairs['$CODE.registrations();'] = str_replace(
-                       "\n",
-                       "\n\t",
-                       trim( $this->getModuleRegistrations( $context ) )
-               );
-               $pairs['$CODE.defineLoader();'] = $mwLoaderCode;
+               // Perform string replacements for startup.js
+               $pairs = [
+                       '$VARS.wgLegacyJavaScriptGlobals' => json_encode(
+                               $this->getConfig()->get( 'LegacyJavaScriptGlobals' ),
+                               $jsonFlags
+                       ),
+                       '$VARS.configuration' => json_encode(
+                               $this->getConfigSettings( $context ),
+                               $jsonFlags
+                       ),
+                       // Raw JavaScript code (not JSON)
+                       '$CODE.registrations();' => trim( $this->getModuleRegistrations( $context ) ),
+                       '$CODE.defineLoader();' => $mwLoaderCode,
+               ];
                $startupCode = strtr( $startupCode, $pairs );
 
                return $startupCode;