Only pretty-print the parser report JS vars
[lhc/web/wiklou.git] / includes / OutputPage.php
index ad7c976..d34ace8 100644 (file)
@@ -23,6 +23,7 @@
 use MediaWiki\Logger\LoggerFactory;
 use MediaWiki\Session\SessionManager;
 use WrappedString\WrappedString;
+use WrappedString\WrappedStringList;
 
 /**
  * This class should be covered by a general architecture document which does
@@ -104,22 +105,11 @@ class OutputPage extends ContextSource {
        protected $mStatusCode;
 
        /**
-        * @var string Variable mLastModified and mEtag are used for sending cache control.
+        * @var string Used for sending cache control.
         *   The whole caching system should probably be moved into its own class.
         */
        protected $mLastModified = '';
 
-       /**
-        * Contains an HTTP Entity Tags (see RFC 2616 section 3.13) which is used
-        * as a unique identifier for the content. It is later used by the client
-        * to compare its cached version with the server version. Client sends
-        * headers If-Match and If-None-Match containing its locally cached ETAG value.
-        *
-        * To get more information, you will have to look at HTTP/1.1 protocol which
-        * is properly described in RFC 2616 : http://tools.ietf.org/html/rfc2616
-        */
-       private $mETag = false;
-
        /** @var array */
        protected $mCategoryLinks = [];
 
@@ -261,11 +251,6 @@ class OutputPage extends ContextSource {
         */
        protected $styles = [];
 
-       /**
-        * Whether jQuery is already handled.
-        */
-       protected $mJQueryDone = false;
-
        private $mIndexPolicy = 'index';
        private $mFollowPolicy = 'follow';
        private $mVaryHeader = [
@@ -305,6 +290,9 @@ class OutputPage extends ContextSource {
         */
        private $copyrightUrl;
 
+       /** @var array Profiling data */
+       private $limitReportData = [];
+
        /**
         * Constructor for OutputPage. This should not be called directly.
         * Instead a new RequestContext should be created and it will implicitly create
@@ -619,29 +607,6 @@ class OutputPage extends ContextSource {
                $this->mModuleStyles = array_merge( $this->mModuleStyles, (array)$modules );
        }
 
-       /**
-        * Get the list of module messages to include on this page
-        *
-        * @deprecated since 1.26 Obsolete
-        * @param bool $filter
-        * @param string|null $position
-        * @return array Array of module names
-        */
-       public function getModuleMessages( $filter = false, $position = null ) {
-               wfDeprecated( __METHOD__, '1.26' );
-               return [];
-       }
-
-       /**
-        * Load messages of one or more ResourceLoader modules.
-        *
-        * @deprecated since 1.26 Use addModules() instead
-        * @param string|array $modules Module name (string) or array of module names
-        */
-       public function addModuleMessages( $modules ) {
-               wfDeprecated( __METHOD__, '1.26' );
-       }
-
        /**
         * @return null|string ResourceLoader target
         */
@@ -694,12 +659,10 @@ class OutputPage extends ContextSource {
        }
 
        /**
-        * Set the value of the ETag HTTP header, only used if $wgUseETag is true
-        *
-        * @param string $tag Value of "ETag" header
+        * @deprecated since 1.28 Obsolete - wgUseETag experiment was removed.
+        * @param string $tag
         */
-       function setETag( $tag ) {
-               $this->mETag = $tag;
+       public function setETag( $tag ) {
        }
 
        /**
@@ -1794,11 +1757,14 @@ class OutputPage extends ContextSource {
                        }
                }
 
-               // enable OOUI if requested via ParserOutput
+               // Enable OOUI if requested via ParserOutput
                if ( $parserOutput->getEnableOOUI() ) {
                        $this->enableOOUI();
                }
 
+               // Include profiling data
+               $this->limitReportData = $parserOutput->getLimitReportData();
+
                // Link flags are ignored for now, but may in the future be
                // used to mark individual language links.
                $linkFlags = [];
@@ -2156,9 +2122,6 @@ class OutputPage extends ContextSource {
        public function sendCacheControl() {
                $response = $this->getRequest()->response();
                $config = $this->getConfig();
-               if ( $config->get( 'UseETag' ) && $this->mETag ) {
-                       $response->header( "ETag: $this->mETag" );
-               }
 
                $this->addVaryHeader( 'Cookie' );
                $this->addAcceptLanguage();
@@ -2649,7 +2612,8 @@ class OutputPage extends ContextSource {
                $userdir = $this->getLanguage()->getDir();
                $sitedir = $wgContLang->getDir();
 
-               $ret = Html::htmlHeader( $sk->getHtmlElementAttributes() );
+               $pieces = [];
+               $pieces[] = Html::htmlHeader( $sk->getHtmlElementAttributes() );
 
                if ( $this->getHTMLTitle() == '' ) {
                        $this->setHTMLTitle( $this->msg( 'pagetitle', $this->getPageTitle() )->inContentLanguage() );
@@ -2657,8 +2621,7 @@ class OutputPage extends ContextSource {
 
                $openHead = Html::openElement( 'head' );
                if ( $openHead ) {
-                       # Don't bother with the newline if $head == ''
-                       $ret .= "$openHead\n";
+                       $pieces[] = $openHead;
                }
 
                if ( !Html::isXmlMimeType( $this->getConfig()->get( 'MimeType' ) ) ) {
@@ -2670,25 +2633,25 @@ class OutputPage extends ContextSource {
                        // Our XML declaration is output by Html::htmlHeader.
                        // http://www.whatwg.org/html/semantics.html#attr-meta-http-equiv-content-type
                        // http://www.whatwg.org/html/semantics.html#charset
-                       $ret .= Html::element( 'meta', [ 'charset' => 'UTF-8' ] ) . "\n";
+                       $pieces[] = Html::element( 'meta', [ 'charset' => 'UTF-8' ] );
                }
 
-               $ret .= Html::element( 'title', null, $this->getHTMLTitle() ) . "\n";
-               $ret .= $this->getInlineHeadScripts() . "\n";
-               $ret .= $this->buildCssLinks() . "\n";
-               $ret .= $this->getExternalHeadScripts() . "\n";
+               $pieces[] = Html::element( 'title', null, $this->getHTMLTitle() );
+               $pieces[] = $this->getInlineHeadScripts();
+               $pieces[] = $this->buildCssLinks();
+               $pieces[] = $this->getExternalHeadScripts();
 
                foreach ( $this->getHeadLinksArray() as $item ) {
-                       $ret .= $item . "\n";
+                       $pieces[] = $item;
                }
 
                foreach ( $this->mHeadItems as $item ) {
-                       $ret .= $item . "\n";
+                       $pieces[] = $item;
                }
 
                $closeHead = Html::closeElement( 'head' );
                if ( $closeHead ) {
-                       $ret .= "$closeHead\n";
+                       $pieces[] = $closeHead;
                }
 
                $bodyClasses = [];
@@ -2703,6 +2666,11 @@ class OutputPage extends ContextSource {
                        $bodyClasses[] = 'capitalize-all-nouns';
                }
 
+               // Parser feature migration class
+               // The idea is that this will eventually be removed, after the wikitext
+               // which requires it is cleaned up.
+               $bodyClasses[] = 'mw-hide-empty-elt';
+
                $bodyClasses[] = $sk->getPageClasses( $this->getTitle() );
                $bodyClasses[] = 'skin-' . Sanitizer::escapeClass( $sk->getSkinName() );
                $bodyClasses[] =
@@ -2717,9 +2685,9 @@ class OutputPage extends ContextSource {
                $sk->addToBodyAttributes( $this, $bodyAttrs );
                Hooks::run( 'OutputPageBodyAttributes', [ $this, $sk, &$bodyAttrs ] );
 
-               $ret .= Html::openElement( 'body', $bodyAttrs ) . "\n";
+               $pieces[] = Html::openElement( 'body', $bodyAttrs );
 
-               return $ret;
+               return WrappedStringList::join( "\n", $pieces );
        }
 
        /**
@@ -2800,6 +2768,17 @@ class OutputPage extends ContextSource {
                                continue;
                        }
 
+                       if ( $only === ResourceLoaderModule::TYPE_STYLES ) {
+                               if ( $module->getType() !== ResourceLoaderModule::LOAD_STYLES ) {
+                                       $logger = $resourceLoader->getLogger();
+                                       $logger->debug( 'Unexpected general module "{module}" in styles queue.', [
+                                               'module' => $name,
+                                       ] );
+                               } else {
+                                       $links['states'][$name] = 'ready';
+                               }
+                       }
+
                        $sortedModules[$module->getSource()][$module->getGroup()][$name] = $module;
                }
 
@@ -2920,7 +2899,7 @@ class OutputPage extends ContextSource {
        /**
         * Build html output from an array of links from makeResourceLoaderLink.
         * @param array $links
-        * @return string HTML
+        * @return string|WrappedStringList HTML
         */
        protected static function getHtmlFromLoaderLinks( array $links ) {
                $html = [];
@@ -2936,7 +2915,7 @@ class OutputPage extends ContextSource {
                // Filter out empty values
                $html = array_filter( $html, 'strlen' );
 
-               if ( count( $states ) ) {
+               if ( $states ) {
                        array_unshift( $html, ResourceLoader::makeInlineScript(
                                ResourceLoader::makeLoaderStateScript( $states )
                        ) );
@@ -2956,25 +2935,23 @@ class OutputPage extends ContextSource {
        }
 
        /**
-        * <script src="..."> tags for "<head>". This is the startup module
+        * <script src="..."> tags for "<head>".This is the startup module
         * and other modules marked with position 'top'.
         *
-        * @return string HTML fragment
+        * @return string|WrappedStringList HTML
         */
        function getExternalHeadScripts() {
-               $links = [];
-
                // Startup - this provides the client with the module
                // manifest and loads jquery and mediawiki base modules
+               $links = [];
                $links[] = $this->makeResourceLoaderLink( 'startup', ResourceLoaderModule::TYPE_SCRIPTS );
-
                return self::getHtmlFromLoaderLinks( $links );
        }
 
        /**
-        * <script>...</script> tags to put in "<head>".
+        * Inline "<script>" tags to put in "<head>".
         *
-        * @return string HTML fragment
+        * @return string|WrappedStringList HTML
         */
        function getInlineHeadScripts() {
                $links = [];
@@ -3034,7 +3011,7 @@ class OutputPage extends ContextSource {
         *
         * @param bool $unused Previously used to let this method change its output based
         *  on whether it was called by getExternalHeadScripts() or getBottomScripts().
-        * @return string
+        * @return string|WrappedStringList HTML
         */
        function getScriptsForBottomQueue( $unused = null ) {
                // Scripts "only" requests marked for bottom inclusion
@@ -3104,7 +3081,13 @@ class OutputPage extends ContextSource {
         * @return string
         */
        function getBottomScripts() {
-               return $this->getScriptsForBottomQueue();
+               return $this->getScriptsForBottomQueue() .
+                       ResourceLoader::makeInlineScript(
+                               ResourceLoader::makeConfigSetScript(
+                                       [ 'wgPageParseReport' => $this->limitReportData ],
+                                       true
+                               )
+                       );
        }
 
        /**
@@ -3633,10 +3616,9 @@ class OutputPage extends ContextSource {
        }
 
        /**
-        * Build a set of "<link>" elements for the stylesheets specified in the $this->styles array.
-        * These will be applied to various media & IE conditionals.
+        * Build a set of "<link>" elements for stylesheets specified in the $this->styles array.
         *
-        * @return string
+        * @return string|WrappedStringList HTML
         */
        public function buildCssLinks() {
                global $wgContLang;
@@ -3659,7 +3641,7 @@ class OutputPage extends ContextSource {
                $moduleStyles = $this->getModuleStyles();
 
                // Per-site custom styles
-               $moduleStyles[] = 'site';
+               $moduleStyles[] = 'site.styles';
                $moduleStyles[] = 'noscript';
 
                // Per-user custom styles
@@ -3694,7 +3676,7 @@ class OutputPage extends ContextSource {
                        if ( !$module ) {
                                continue;
                        }
-                       if ( $name === 'site' ) {
+                       if ( $name === 'site.styles' ) {
                                // 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'.
@@ -3738,7 +3720,9 @@ class OutputPage extends ContextSource {
                }
 
                // Add stuff in $otherTags (previewed user CSS if applicable)
-               return self::getHtmlFromLoaderLinks( $links ) . implode( '', $otherTags );
+               $links[] = implode( '', $otherTags );
+
+               return self::getHtmlFromLoaderLinks( $links );
        }
 
        /**