Avoid deprecated LinkCache::singleton()
[lhc/web/wiklou.git] / includes / OutputPage.php
index dd1a4db..1dd6a41 100644 (file)
@@ -463,24 +463,22 @@ class OutputPage extends ContextSource {
        }
 
        /**
-        * Add a JavaScript file out of skins/common, or a given relative path.
+        * Add a JavaScript file to be loaded as `<script>` on this page.
+        *
         * Internal use only. Use OutputPage::addModules() if possible.
         *
-        * @param string $file Filename in skins/common or complete on-server path
-        *              (/foo/bar.js)
-        * @param string $version Style version of the file. Defaults to $wgStyleVersion
+        * @param string $file URL to file (absolute path, protocol-relative, or full url)
+        * @param string $unused Previously used to change the cache-busting query parameter
         */
-       public function addScriptFile( $file, $version = null ) {
-               // See if $file parameter is an absolute URL or begins with a slash
-               if ( substr( $file, 0, 1 ) == '/' || preg_match( '#^[a-z]*://#i', $file ) ) {
-                       $path = $file;
-               } else {
-                       $path = $this->getConfig()->get( 'StylePath' ) . "/common/{$file}";
-               }
-               if ( is_null( $version ) ) {
-                       $version = $this->getConfig()->get( 'StyleVersion' );
+       public function addScriptFile( $file, $unused = null ) {
+               if ( substr( $file, 0, 1 ) !== '/' && !preg_match( '#^[a-z]*://#i', $file ) ) {
+                       // This is not an absolute path, protocol-relative url, or full scheme url,
+                       // presumed to be an old call intended to include a file from /w/skins/common,
+                       // which doesn't exist anymore as of MediaWiki 1.24 per T71277. Ignore.
+                       wfDeprecated( __METHOD__, '1.24' );
+                       return;
                }
-               $this->addScript( Html::linkedScript( wfAppendQuery( $path, $version ), $this->getCSPNonce() ) );
+               $this->addScript( Html::linkedScript( $file, $this->getCSPNonce() ) );
        }
 
        /**
@@ -644,9 +642,7 @@ class OutputPage extends ContextSource {
                        // Register a callback for $this->contentOverrides on the first call
                        $this->addContentOverrideCallback( function ( LinkTarget $target ) {
                                $key = $target->getNamespace() . ':' . $target->getDBkey();
-                               return isset( $this->contentOverrides[$key] )
-                                       ? $this->contentOverrides[$key]
-                                       : null;
+                               return $this->contentOverrides[$key] ?? null;
                        } );
                }
 
@@ -1372,7 +1368,8 @@ class OutputPage extends ContextSource {
                );
 
                # Add the results to the link cache
-               $lb->addResultToCache( LinkCache::singleton(), $res );
+               $linkCache = MediaWikiServices::getInstance()->getLinkCache();
+               $lb->addResultToCache( $linkCache, $res );
 
                return $res;
        }
@@ -1518,9 +1515,7 @@ class OutputPage extends ContextSource {
                if ( $type == ResourceLoaderModule::TYPE_COMBINED ) {
                        return min( array_values( $this->mAllowedModules ) );
                } else {
-                       return isset( $this->mAllowedModules[$type] )
-                               ? $this->mAllowedModules[$type]
-                               : ResourceLoaderModule::ORIGIN_ALL;
+                       return $this->mAllowedModules[$type] ?? ResourceLoaderModule::ORIGIN_ALL;
                }
        }
 
@@ -2415,10 +2410,6 @@ class OutputPage extends ContextSource {
                $response->header( 'Content-type: ' . $config->get( 'MimeType' ) . '; charset=UTF-8' );
                $response->header( 'Content-language: ' . $wgContLang->getHtmlCode() );
 
-               // Avoid Internet Explorer "compatibility view" in IE 8-10, so that
-               // jQuery etc. can work correctly.
-               $response->header( 'X-UA-Compatible: IE=Edge' );
-
                if ( !$this->mArticleBodyOnly ) {
                        $sk = $this->getSkin();
 
@@ -2443,7 +2434,7 @@ class OutputPage extends ContextSource {
                if ( $this->mArticleBodyOnly ) {
                        echo $this->mBodytext;
                } else {
-                       // Enable safe mode if requested
+                       // Enable safe mode if requested (T152169)
                        if ( $this->getRequest()->getBool( 'safemode' ) ) {
                                $this->disallowUserJs();
                        }
@@ -2655,13 +2646,13 @@ class OutputPage extends ContextSource {
 
                        foreach ( $errors as $error ) {
                                $text .= '<li>';
-                               $text .= call_user_func_array( [ $this, 'msg' ], $error )->plain();
+                               $text .= $this->msg( ...$error )->plain();
                                $text .= "</li>\n";
                        }
                        $text .= '</ul>';
                } else {
                        $text .= "<div class=\"permissions-errors\">\n" .
-                                       call_user_func_array( [ $this, 'msg' ], reset( $errors ) )->plain() .
+                                       $this->msg( ...reset( $errors ) )->plain() .
                                        "\n</div>";
                }
 
@@ -2862,6 +2853,16 @@ class OutputPage extends ContextSource {
                        $rlClient = new ResourceLoaderClientHtml( $context, [
                                'target' => $this->getTarget(),
                                'nonce' => $this->getCSPNonce(),
+                               // When 'safemode', disallowUserJs(), or reduceAllowedModules() is used
+                               // to only restrict modules to ORIGIN_CORE (ie. disallow ORIGIN_USER), the list of
+                               // modules enqueud for loading on this page is filtered to just those.
+                               // However, to make sure we also apply the restriction to dynamic dependencies and
+                               // lazy-loaded modules at run-time on the client-side, pass 'safemode' down to the
+                               // StartupModule so that the client-side registry will not contain any restricted
+                               // modules either. (T152169, T185303)
+                               'safemode' => ( $this->getAllowedModules( ResourceLoaderModule::TYPE_COMBINED )
+                                       <= ResourceLoaderModule::ORIGIN_CORE_INDIVIDUAL
+                               ) ? '1' : null,
                        ] );
                        $rlClient->setConfig( $this->getJSVars() );
                        $rlClient->setModules( $this->getModules( /*filter*/ true ) );
@@ -3654,8 +3655,11 @@ class OutputPage extends ContextSource {
                        $url = $style;
                } else {
                        $config = $this->getConfig();
-                       $url = $config->get( 'StylePath' ) . '/' . $style . '?' .
-                               $config->get( 'StyleVersion' );
+                       // Append file hash as query parameter
+                       $url = self::transformResourcePath(
+                               $config,
+                               $config->get( 'StylePath' ) . '/' . $style
+                       );
                }
 
                $link = Html::linkedStyle( $url, $media );
@@ -3897,7 +3901,7 @@ class OutputPage extends ContextSource {
         */
        public static function setupOOUI( $skinName = 'default', $dir = 'ltr' ) {
                $themes = ResourceLoaderOOUIModule::getSkinThemeMap();
-               $theme = isset( $themes[$skinName] ) ? $themes[$skinName] : $themes['default'];
+               $theme = $themes[$skinName] ?? $themes['default'];
                // For example, 'OOUI\WikimediaUITheme'.
                $themeClass = "OOUI\\{$theme}Theme";
                OOUI\Theme::setSingleton( new $themeClass() );
@@ -3961,12 +3965,8 @@ class OutputPage extends ContextSource {
                uksort( $logosPerDppx, function ( $a , $b ) {
                        $a = floatval( $a );
                        $b = floatval( $b );
-
-                       if ( $a == $b ) {
-                               return 0;
-                       }
                        // Sort from smallest to largest (e.g. 1x, 1.5x, 2x)
-                       return ( $a < $b ) ? -1 : 1;
+                       return $a <=> $b;
                } );
 
                foreach ( $logosPerDppx as $dppx => $src ) {
@@ -4019,8 +4019,8 @@ class OutputPage extends ContextSource {
                }
                if ( $this->CSPNonce === null ) {
                        // XXX It might be expensive to generate randomness
-                       // on every request, on windows.
-                       $rand = MWCryptRand::generate( 15 );
+                       // on every request, on Windows.
+                       $rand = random_bytes( 15 );
                        $this->CSPNonce = base64_encode( $rand );
                }
                return $this->CSPNonce;