resourceloader: Ensure 'user' loads after 'site' (asynchronously)
[lhc/web/wiklou.git] / includes / OutputPage.php
index e832b82..e2caf80 100644 (file)
@@ -2286,9 +2286,10 @@ class OutputPage extends ContextSource {
                        // add skin specific modules
                        $modules = $sk->getDefaultModules();
 
-                       // enforce various default modules for all skins
+                       // Enforce various default modules for all skins
                        $coreModules = array(
-                               // keep this list as small as possible
+                               // Keep this list as small as possible
+                               'site',
                                'mediawiki.page.startup',
                                'mediawiki.user',
                        );
@@ -3004,7 +3005,8 @@ class OutputPage extends ContextSource {
                // Separate user.tokens as otherwise caching will be allowed (T84960)
                $links[] = $this->makeResourceLoaderLink( 'user.tokens', ResourceLoaderModule::TYPE_COMBINED );
 
-               // Scripts and messages "only" requests marked for top inclusion
+               // "Scripts only" modules marked for top inclusion
+               $styleModules = $this->getModuleScripts( true, 'top' );
                $links[] = $this->makeResourceLoaderLink(
                        $this->getModuleScripts( true, 'top' ),
                        ResourceLoaderModule::TYPE_SCRIPTS
@@ -3064,34 +3066,43 @@ class OutputPage extends ContextSource {
                // Legacy Scripts
                $links[] = "\n" . $this->mScripts;
 
-               // Add site JS if enabled
-               $links[] = $this->makeResourceLoaderLink( 'site', ResourceLoaderModule::TYPE_SCRIPTS,
-                       /* $useESI = */ false, /* $extraQuery = */ array(), /* $loadCall = */ $inHead
-               );
-
                // Add user JS if enabled
+               // This must use TYPE_COMBINED instead of only=scripts so that its request is handled by
+               // mw.loader.implement() which ensures that execution is scheduled after the "site" module.
                if ( $this->getConfig()->get( 'AllowUserJs' )
                        && $this->getUser()->isLoggedIn()
                        && $this->getTitle()
                        && $this->getTitle()->isJsSubpage()
                        && $this->userCanPreview()
                ) {
-                       # XXX: additional security check/prompt?
-                       // We're on a preview of a JS subpage
-                       // Exclude this page from the user module in case it's in there (bug 26283)
-                       $links[] = $this->makeResourceLoaderLink( 'user', ResourceLoaderModule::TYPE_SCRIPTS, false,
+                       // We're on a preview of a JS subpage. Exclude this page from the user module (T28283)
+                       // and include the draft contents as a raw script instead.
+                       $links[] = $this->makeResourceLoaderLink( 'user', ResourceLoaderModule::TYPE_COMBINED, false,
                                array( 'excludepage' => $this->getTitle()->getPrefixedDBkey() ), $inHead
                        );
                        // Load the previewed JS
-                       $links[] = Html::inlineScript( "\n"
-                                       . $this->getRequest()->getText( 'wpTextbox1' ) . "\n" ) . "\n";
+                       $links[] = ResourceLoader::makeInlineScript(
+                               Xml::encodeJsCall( 'mw.loader.using', array(
+                                       array( 'user', 'site' ),
+                                       new XmlJsCode(
+                                               'function () {'
+                                                       . Xml::encodeJsCall( '$.globalEval', array(
+                                                               $this->getRequest()->getText( 'wpTextbox1' )
+                                                       ) )
+                                                       . '}'
+                                       )
+                               ) )
+                       );
 
                        // FIXME: If the user is previewing, say, ./vector.js, his ./common.js will be loaded
                        // asynchronously and may arrive *after* the inline script here. So the previewed code
-                       // may execute before ./common.js runs. Normally, ./common.js runs before ./vector.js...
+                       // may execute before ./common.js runs. Normally, ./common.js runs before ./vector.js.
+                       // Similarly, when previewing ./common.js and the user module does arrive first, it will
+                       // arrive without common.js and the inline script runs after. Thus running common after
+                       // the excluded subpage.
                } else {
                        // Include the user module normally, i.e., raw to avoid it being wrapped in a closure.
-                       $links[] = $this->makeResourceLoaderLink( 'user', ResourceLoaderModule::TYPE_SCRIPTS,
+                       $links[] = $this->makeResourceLoaderLink( 'user', ResourceLoaderModule::TYPE_COMBINED,
                                /* $useESI = */ false, /* $extraQuery = */ array(), /* $loadCall = */ $inHead
                        );
                }
@@ -3433,11 +3444,11 @@ class OutputPage extends ContextSource {
                        $lang = $this->getTitle()->getPageLanguage();
                        if ( $lang->hasVariants() ) {
                                $variants = $lang->getVariants();
-                               foreach ( $variants as $_v ) {
-                                       $tags["variant-$_v"] = Html::element( 'link', array(
+                               foreach ( $variants as $variant ) {
+                                       $tags["variant-$variant"] = Html::element( 'link', array(
                                                'rel' => 'alternate',
-                                               'hreflang' => wfBCP47( $_v ),
-                                               'href' => $this->getTitle()->getLocalURL( array( 'variant' => $_v ) ) )
+                                               'hreflang' => wfBCP47( $variant ),
+                                               'href' => $this->getTitle()->getLocalURL( array( 'variant' => $variant ) ) )
                                        );
                                }
                                # x-default link per https://support.google.com/webmasters/answer/189077?hl=en
@@ -3683,9 +3694,17 @@ class OutputPage extends ContextSource {
                        if ( !$module ) {
                                continue;
                        }
+                       if ( $name === 'site' ) {
+                               // HACK: The site module shouldn't be fragmented with a cache group and
+                               // http request. But in order to ensure its styles are separated and after the
+                               // ResourceLoaderDynamicStyles marker, pretend it is in a group called 'site'.
+                               // The scripts remain ungrouped and rides the bottom queue.
+                               $styles['site'][] = $name;
+                               continue;
+                       }
                        $group = $module->getGroup();
-                       // Modules in groups different than the ones listed on top (see $styles assignment)
-                       // will be placed in the "other" group
+                       // Modules in groups other than the ones needing special treatment (see $styles assignment)
+                       // will be placed in the "other" style category.
                        $styles[isset( $styles[$group] ) ? $group : 'other'][] = $name;
                }
 
@@ -3704,7 +3723,7 @@ class OutputPage extends ContextSource {
                        array( 'name' => 'ResourceLoaderDynamicStyles', 'content' => '' )
                ) . "\n";
 
-               // Add site, private and user styles
+               // Add site-specific and user-specific styles
                // 'private' at present only contains user.options, so put that before 'user'
                // Any future private modules will likely have a similar user-specific character
                foreach ( array( 'site', 'noscript', 'private', 'user' ) as $group ) {
@@ -3952,7 +3971,14 @@ class OutputPage extends ContextSource {
         * @since 1.25
         */
        public function enableOOUI() {
-               OOUI\Theme::setSingleton( new OOUI\MediaWikiTheme() );
+               $themes = ExtensionRegistry::getInstance()->getAttribute( 'SkinOOUIThemes' );
+               // Make keys (skin names) lowercase for case-insensitive matching.
+               $themes = array_change_key_case( $themes, CASE_LOWER );
+               $skinName = strtolower( $this->getSkin()->getSkinName() );
+               $theme = isset( $themes[ $skinName ] ) ? $themes[ $skinName ] : 'MediaWiki';
+               // For example, 'OOUI\MediaWikiTheme'.
+               $themeClass = "OOUI\\{$theme}Theme";
+               OOUI\Theme::setSingleton( new $themeClass() );
                OOUI\Element::setDefaultDir( $this->getLanguage()->getDir() );
                $this->addModuleStyles( array(
                        'oojs-ui.styles',