resourceloader: Implement support for 'site' into mw.loader
[lhc/web/wiklou.git] / includes / OutputPage.php
index f02f752..1c76f0b 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,11 +3066,6 @@ 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
                if ( $this->getConfig()->get( 'AllowUserJs' )
                        && $this->getUser()->isLoggedIn()
@@ -3433,19 +3430,19 @@ 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
+                               $tags["variant-x-default"] = Html::element( 'link', array(
+                                       'rel' => 'alternate',
+                                       'hreflang' => 'x-default',
+                                       'href' => $this->getTitle()->getLocalURL() ) );
                        }
-                       # x-default link per https://support.google.com/webmasters/answer/189077?hl=en
-                       $tags["variant-x-default"] = Html::element( 'link', array(
-                               'rel' => 'alternate',
-                               'hreflang' => 'x-default',
-                               'href' => $this->getTitle()->getLocalURL() ) );
                }
 
                # Copyright
@@ -3524,8 +3521,25 @@ class OutputPage extends ContextSource {
                        if ( $canonicalUrl !== false ) {
                                $canonicalUrl = wfExpandUrl( $canonicalUrl, PROTO_CANONICAL );
                        } else {
-                               $reqUrl = $this->getRequest()->getRequestURL();
-                               $canonicalUrl = wfExpandUrl( $reqUrl, PROTO_CANONICAL );
+                               if ( $this->isArticleRelated() ) {
+                                       // This affects all requests where "setArticleRelated" is true. This is
+                                       // typically all requests that show content (query title, curid, oldid, diff),
+                                       // and all wikipage actions (edit, delete, purge, info, history etc.).
+                                       // It does not apply to File pages and Special pages.
+                                       // 'history' and 'info' actions address page metadata rather than the page
+                                       // content itself, so they may not be canonicalized to the view page url.
+                                       // TODO: this ought to be better encapsulated in the Action class.
+                                       $action = Action::getActionName( $this->getContext() );
+                                       if ( in_array( $action, array( 'history', 'info' ) ) ) {
+                                               $query = "action={$action}";
+                                       } else {
+                                               $query = '';
+                                       }
+                                       $canonicalUrl = $this->getTitle()->getCanonicalURL( $query );
+                               } else {
+                                       $reqUrl = $this->getRequest()->getRequestURL();
+                                       $canonicalUrl = wfExpandUrl( $reqUrl, PROTO_CANONICAL );
+                               }
                        }
                }
                if ( $canonicalUrl !== false ) {
@@ -3666,9 +3680,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;
                }
 
@@ -3687,7 +3709,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 ) {
@@ -3935,13 +3957,21 @@ 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',
                        'oojs-ui.styles.icons',
                        'oojs-ui.styles.indicators',
                        'oojs-ui.styles.textures',
+                       'mediawiki.widgets.styles',
                ) );
        }
 }