OutputPage: Remove deprecated addParserOutputNoText, getHeadLinks methods
[lhc/web/wiklou.git] / includes / OutputPage.php
index 5380264..84a168b 100644 (file)
@@ -302,6 +302,11 @@ class OutputPage extends ContextSource {
        /** @var array Profiling data */
        private $limitReportJSData = [];
 
+       /**
+        * Link: header contents
+        */
+       private $mLinkHeader = [];
+
        /**
         * Constructor for OutputPage. This should not be called directly.
         * Instead a new RequestContext should be created and it will implicitly create
@@ -1768,17 +1773,6 @@ class OutputPage extends ContextSource {
                $this->addParserOutput( $parserOutput );
        }
 
-       /**
-        * Add a ParserOutput object, but without Html.
-        *
-        * @deprecated since 1.24, use addParserOutputMetadata() instead.
-        * @param ParserOutput $parserOutput
-        */
-       public function addParserOutputNoText( $parserOutput ) {
-               wfDeprecated( __METHOD__, '1.24' );
-               $this->addParserOutputMetadata( $parserOutput );
-       }
-
        /**
         * Add all metadata associated with a ParserOutput object, but without the actual HTML. This
         * includes categories, language links, ResourceLoader modules, effects of certain magic words,
@@ -2105,6 +2099,28 @@ class OutputPage extends ContextSource {
                return 'Vary: ' . implode( ', ', array_keys( $this->mVaryHeader ) );
        }
 
+       /**
+        * Add an HTTP Link: header
+        *
+        * @param string $header Header value
+        */
+       public function addLinkHeader( $header ) {
+               $this->mLinkHeader[] = $header;
+       }
+
+       /**
+        * Return a Link: header. Based on the values of $mLinkHeader.
+        *
+        * @return string
+        */
+       public function getLinkHeader() {
+               if ( !$this->mLinkHeader ) {
+                       return false;
+               }
+
+               return 'Link: ' . implode( ',', $this->mLinkHeader );
+       }
+
        /**
         * Get a complete Key header
         *
@@ -2361,6 +2377,19 @@ class OutputPage extends ContextSource {
                // jQuery etc. can work correctly.
                $response->header( 'X-UA-Compatible: IE=Edge' );
 
+               if ( !$this->mArticleBodyOnly ) {
+                       $sk = $this->getSkin();
+
+                       if ( $sk->shouldPreloadLogo() ) {
+                               $this->addLogoPreloadLinkHeaders();
+                       }
+               }
+
+               $linkHeader = $this->getLinkHeader();
+               if ( $linkHeader ) {
+                       $response->header( $linkHeader );
+               }
+
                // Prevent framing, if requested
                $frameOptions = $this->getFrameOptions();
                if ( $frameOptions ) {
@@ -2370,6 +2399,11 @@ class OutputPage extends ContextSource {
                if ( $this->mArticleBodyOnly ) {
                        echo $this->mBodytext;
                } else {
+                       // Enable safe mode if requested
+                       if ( $this->getRequest()->getBool( 'safemode' ) ) {
+                               $this->disallowUserJs();
+                       }
+
                        $sk = $this->getSkin();
                        // add skin specific modules
                        $modules = $sk->getDefaultModules();
@@ -3515,16 +3549,6 @@ class OutputPage extends ContextSource {
                return $tags;
        }
 
-       /**
-        * @return string HTML tag links to be put in the header.
-        * @deprecated since 1.24 Use OutputPage::headElement or if you have to,
-        *   OutputPage::getHeadLinksArray directly.
-        */
-       public function getHeadLinks() {
-               wfDeprecated( __METHOD__, '1.24' );
-               return implode( "\n", $this->getHeadLinksArray() );
-       }
-
        /**
         * Generate a "<link rel/>" for a feed.
         *
@@ -3960,4 +3984,75 @@ class OutputPage extends ContextSource {
                        'mediawiki.widgets.styles',
                ] );
        }
+
+       /**
+        * Add Link headers for preloading the wiki's logo.
+        *
+        * @since 1.26
+        */
+       protected function addLogoPreloadLinkHeaders() {
+               $logo = ResourceLoaderSkinModule::getLogo( $this->getConfig() );
+
+               $tags = [];
+               $logosPerDppx = [];
+               $logos = [];
+
+               if ( !is_array( $logo ) ) {
+                       // No media queries required if we only have one variant
+                       $this->addLinkHeader( '<' . $logo . '>;rel=preload;as=image' );
+                       return;
+               }
+
+               foreach ( $logo as $dppx => $src ) {
+                       // Keys are in this format: "1.5x"
+                       $dppx = substr( $dppx, 0, -1 );
+                       $logosPerDppx[$dppx] = $src;
+               }
+
+               // Because PHP can't have floats as array keys
+               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;
+               } );
+
+               foreach ( $logosPerDppx as $dppx => $src ) {
+                       $logos[] = [ 'dppx' => $dppx, 'src' => $src ];
+               }
+
+               $logosCount = count( $logos );
+               // Logic must match ResourceLoaderSkinModule:
+               // - 1x applies to resolution < 1.5dppx
+               // - 1.5x applies to resolution >= 1.5dppx && < 2dppx
+               // - 2x applies to resolution >= 2dppx
+               // Note that min-resolution and max-resolution are both inclusive.
+               for ( $i = 0; $i < $logosCount; $i++ ) {
+                       if ( $i === 0 ) {
+                               // Smallest dppx
+                               // min-resolution is ">=" (larger than or equal to)
+                               // "not min-resolution" is essentially "<"
+                               $media_query = 'not all and (min-resolution: ' . $logos[ 1 ]['dppx'] . 'dppx)';
+                       } elseif ( $i !== $logosCount - 1 ) {
+                               // In between
+                               // Media query expressions can only apply "not" to the entire expression
+                               // (e.g. can't express ">= 1.5 and not >= 2).
+                               // Workaround: Use <= 1.9999 in place of < 2.
+                               $upper_bound = floatval( $logos[ $i + 1 ]['dppx'] ) - 0.000001;
+                               $media_query = '(min-resolution: ' . $logos[ $i ]['dppx'] .
+                                       'dppx) and (max-resolution: ' . $upper_bound . 'dppx)';
+                       } else {
+                               // Largest dppx
+                               $media_query = '(min-resolution: ' . $logos[ $i ]['dppx'] . 'dppx)';
+                       }
+
+                       $this->addLinkHeader(
+                               '<' . $logos[$i]['src'] . '>;rel=preload;as=image;media=' . $media_query
+                       );
+               }
+       }
 }