Merge "Allow SelectWithInput to be marked as required and handle that dynamically"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderClientHtml.php
index 5c072bf..e324d04 100644 (file)
@@ -22,7 +22,7 @@ use Wikimedia\WrappedString;
 use Wikimedia\WrappedStringList;
 
 /**
- * Bootstrap a ResourceLoader client on an HTML page.
+ * Load and configure a ResourceLoader client on an HTML page.
  *
  * @since 1.28
  */
@@ -46,9 +46,6 @@ class ResourceLoaderClientHtml {
        /** @var array */
        private $moduleStyles = [];
 
-       /** @var array */
-       private $moduleScripts = [];
-
        /** @var array */
        private $exemptStates = [];
 
@@ -101,16 +98,6 @@ class ResourceLoaderClientHtml {
                $this->moduleStyles = $modules;
        }
 
-       /**
-        * Ensure the scripts of one or more modules are loaded.
-        *
-        * @deprecated since 1.28
-        * @param array $modules Array of module names
-        */
-       public function setModuleScripts( array $modules ) {
-               $this->moduleScripts = $modules;
-       }
-
        /**
         * Set state of special modules that are handled by the caller manually.
         *
@@ -139,7 +126,6 @@ class ResourceLoaderClientHtml {
                        ],
                        'general' => [],
                        'styles' => [],
-                       'scripts' => [],
                        // Embedding for private modules
                        'embed' => [
                                'styles' => [],
@@ -217,26 +203,6 @@ class ResourceLoaderClientHtml {
                        }
                }
 
-               foreach ( $this->moduleScripts as $name ) {
-                       $module = $rl->getModule( $name );
-                       if ( !$module ) {
-                               continue;
-                       }
-
-                       $group = $module->getGroup();
-                       $context = $this->getContext( $group, ResourceLoaderModule::TYPE_SCRIPTS );
-                       if ( $module->isKnownEmpty( $context ) ) {
-                               // Avoid needless request for empty module
-                               $data['states'][$name] = 'ready';
-                       } else {
-                               // Load from load.php?only=scripts via <script src></script>
-                               $data['scripts'][] = $name;
-
-                               // Avoid duplicate request from mw.loader
-                               $data['states'][$name] = 'loading';
-                       }
-               }
-
                return $data;
        }
 
@@ -267,56 +233,52 @@ class ResourceLoaderClientHtml {
                $chunks = [];
 
                // Change "client-nojs" class to client-js. This allows easy toggling of UI components.
-               // This happens synchronously on every page view to avoid flashes of wrong content.
+               // This must happen synchronously on every page view to avoid flashes of wrong content.
                // See also #getDocumentAttributes() and /resources/src/startup.js.
-               $chunks[] = Html::inlineScript(
-                       'document.documentElement.className = document.documentElement.className'
-                       . '.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );',
-                       $nonce
-               );
+               $script = <<<JAVASCRIPT
+document.documentElement.className = document.documentElement.className
+       .replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );
+JAVASCRIPT;
 
-               // Inline RLQ: Set page variables
+               // Inline script: Declare mw.config variables for this page.
                if ( $this->config ) {
-                       $chunks[] = ResourceLoader::makeInlineScript(
-                               ResourceLoader::makeConfigSetScript( $this->config ),
-                               $nonce
-                       );
+                       $confJson = ResourceLoader::encodeJsonForScript( $this->config );
+                       $script .= <<<JAVASCRIPT
+RLCONF = {$confJson};
+JAVASCRIPT;
                }
 
-               // Inline RLQ: Initial module states
+               // Inline script: Declare initial module states for this page.
                $states = array_merge( $this->exemptStates, $data['states'] );
                if ( $states ) {
-                       $chunks[] = ResourceLoader::makeInlineScript(
-                               ResourceLoader::makeLoaderStateScript( $states ),
-                               $nonce
-                       );
+                       $stateJson = ResourceLoader::encodeJsonForScript( $states );
+                       $script .= <<<JAVASCRIPT
+RLSTATE = {$stateJson};
+JAVASCRIPT;
                }
 
-               // Inline RLQ: Embedded modules
-               if ( $data['embed']['general'] ) {
-                       $chunks[] = $this->getLoad(
-                               $data['embed']['general'],
-                               ResourceLoaderModule::TYPE_COMBINED,
-                               $nonce
-                       );
+               // Inline script: Declare general modules to load on this page.
+               if ( $data['general'] ) {
+                       $pageModulesJson = ResourceLoader::encodeJsonForScript( $data['general'] );
+                       $script .= <<<JAVASCRIPT
+RLPAGEMODULES = {$pageModulesJson};
+JAVASCRIPT;
                }
 
-               // Inline RLQ: Load general modules
-               if ( $data['general'] ) {
-                       $chunks[] = ResourceLoader::makeInlineScript(
-                               'RLPAGEMODULES='
-                                       . ResourceLoader::encodeJsonForScript( $data['general'] )
-                                       . ';'
-                                       . 'mw.loader.load(RLPAGEMODULES);',
+               if ( $this->context->getDebug() ) {
+                       $chunks[] = Html::inlineScript( $script, $nonce );
+               } else {
+                       $chunks[] = Html::inlineScript(
+                               ResourceLoader::filter( 'minify-js', $script, [ 'cache' => false ] ),
                                $nonce
                        );
                }
 
-               // Inline RLQ: Load only=scripts
-               if ( $data['scripts'] ) {
+               // Inline RLQ: Embedded modules
+               if ( $data['embed']['general'] ) {
                        $chunks[] = $this->getLoad(
-                               $data['scripts'],
-                               ResourceLoaderModule::TYPE_SCRIPTS,
+                               $data['embed']['general'],
+                               ResourceLoaderModule::TYPE_COMBINED,
                                $nonce
                        );
                }
@@ -341,7 +303,7 @@ class ResourceLoaderClientHtml {
 
                // Async scripts. Once the startup is loaded, inline RLQ scripts will run.
                // Pass-through a custom 'target' from OutputPage (T143066).
-               $startupQuery = [];
+               $startupQuery = [ 'raw' => '1' ];
                foreach ( [ 'target', 'safemode' ] as $param ) {
                        if ( $this->options[$param] !== null ) {
                                $startupQuery[$param] = (string)$this->options[$param];
@@ -386,7 +348,7 @@ class ResourceLoaderClientHtml {
        private static function makeContext( ResourceLoaderContext $mainContext, $group, $type,
                array $extraQuery = []
        ) {
-               // Create new ResourceLoaderContext so that $extraQuery may trigger isRaw().
+               // Create new ResourceLoaderContext so that $extraQuery is supported (eg. for 'sync=1').
                $req = new FauxRequest( array_merge( $mainContext->getRequest()->getValues(), $extraQuery ) );
                // Set 'only' if not combined
                $req->setVal( 'only', $type === ResourceLoaderModule::TYPE_COMBINED ? null : $type );
@@ -472,12 +434,6 @@ class ResourceLoaderClientHtml {
                                                        );
                                                }
                                        } else {
-                                               // See if we have one or more raw modules
-                                               $isRaw = false;
-                                               foreach ( $moduleSet as $key => $module ) {
-                                                       $isRaw |= $module->isRaw();
-                                               }
-
                                                // Special handling for the user group; because users might change their stuff
                                                // on-wiki like user pages, or user preferences; we need to find the highest
                                                // timestamp of these user-changeable modules so we can ensure cache misses on change
@@ -493,19 +449,23 @@ class ResourceLoaderClientHtml {
                                                // Decide whether to use 'style' or 'script' element
                                                if ( $only === ResourceLoaderModule::TYPE_STYLES ) {
                                                        $chunk = Html::linkedStyle( $url );
+                                               } elseif ( $context->getRaw() ) {
+                                                       // This request is asking for the module to be delivered standalone,
+                                                       // (aka "raw") without communicating to any mw.loader client.
+                                                       // Use cases:
+                                                       // - startup (naturally because this is what will define mw.loader)
+                                                       // - html5shiv (loads synchronously in old IE before the async startup module arrives)
+                                                       // - QUnit (needed in SpecialJavaScriptTest before async startup)
+                                                       $chunk = Html::element( 'script', [
+                                                               // The 'sync' option is only supported in combination with 'raw'.
+                                                               'async' => !isset( $extraQuery['sync'] ),
+                                                               'src' => $url
+                                                       ] );
                                                } else {
-                                                       if ( $context->getRaw() || $isRaw ) {
-                                                               $chunk = Html::element( 'script', [
-                                                                       // In SpecialJavaScriptTest, QUnit must load synchronous
-                                                                       'async' => !isset( $extraQuery['sync'] ),
-                                                                       'src' => $url
-                                                               ] );
-                                                       } else {
-                                                               $chunk = ResourceLoader::makeInlineScript(
-                                                                       Xml::encodeJsCall( 'mw.loader.load', [ $url ] ),
-                                                                       $nonce
-                                                               );
-                                                       }
+                                                       $chunk = ResourceLoader::makeInlineScript(
+                                                               Xml::encodeJsCall( 'mw.loader.load', [ $url ] ),
+                                                               $nonce
+                                                       );
                                                }
 
                                                if ( $group == 'noscript' ) {