Move NewPP limit report HTML comments to JS variables
[lhc/web/wiklou.git] / includes / parser / Parser.php
index aca32ed..e57ec8e 100644 (file)
@@ -501,60 +501,46 @@ class Parser {
                                [ $this->mHighestExpansionDepth, $this->mOptions->getMaxPPExpandDepth() ]
                        );
                        $this->mOutput->setLimitReportData( 'limitreport-expensivefunctioncount',
-                               [ $this->mExpensiveFunctionCount, $this->mOptions->getExpensiveParserFunctionLimit() ]
+                               [ $this->mExpensiveFunctionCount,
+                                       $this->mOptions->getExpensiveParserFunctionLimit() ]
                        );
                        Hooks::run( 'ParserLimitReportPrepare', [ $this, $this->mOutput ] );
 
-                       $limitReport = "NewPP limit report\n";
-                       if ( $wgShowHostnames ) {
-                               $limitReport .= 'Parsed by ' . wfHostname() . "\n";
-                       }
-                       $limitReport .= 'Cached time: ' . $this->mOutput->getCacheTime() . "\n";
-                       $limitReport .= 'Cache expiry: ' . $this->mOutput->getCacheExpiry() . "\n";
-                       $limitReport .= 'Dynamic content: ' .
-                               ( $this->mOutput->hasDynamicContent() ? 'true' : 'false' ) .
-                               "\n";
-
-                       foreach ( $this->mOutput->getLimitReportData() as $key => $value ) {
-                               if ( Hooks::run( 'ParserLimitReportFormat',
-                                       [ $key, &$value, &$limitReport, false, false ]
-                               ) ) {
-                                       $keyMsg = wfMessage( $key )->inLanguage( 'en' )->useDatabase( false );
-                                       $valueMsg = wfMessage( [ "$key-value-text", "$key-value" ] )
-                                               ->inLanguage( 'en' )->useDatabase( false );
-                                       if ( !$valueMsg->exists() ) {
-                                               $valueMsg = new RawMessage( '$1' );
-                                       }
-                                       if ( !$keyMsg->isDisabled() && !$valueMsg->isDisabled() ) {
-                                               $valueMsg->params( $value );
-                                               $limitReport .= "{$keyMsg->text()}: {$valueMsg->text()}\n";
-                                       }
-                               }
-                       }
-                       // Since we're not really outputting HTML, decode the entities and
-                       // then re-encode the things that need hiding inside HTML comments.
-                       $limitReport = htmlspecialchars_decode( $limitReport );
+                       $limitReport = '';
                        Hooks::run( 'ParserLimitReport', [ $this, &$limitReport ] );
+                       if ( $limitReport != '' ) {
+                               // Sanitize for comment. Note '‐' in the replacement is U+2010,
+                               // which looks much like the problematic '-'.
+                               $limitReport = str_replace( [ '-', '&' ], [ '‐', '&' ], $limitReport );
+                               $text .= "\n<!-- \nNewPP limit report\n$limitReport-->\n";
+                       }
 
-                       // Sanitize for comment. Note '‐' in the replacement is U+2010,
-                       // which looks much like the problematic '-'.
-                       $limitReport = str_replace( [ '-', '&' ], [ '‐', '&amp;' ], $limitReport );
-                       $text .= "\n<!-- \n$limitReport-->\n";
-
-                       // Add on template profiling data
+                       // Add on template profiling data in human/machine readable way
                        $dataByFunc = $this->mProfiler->getFunctionStats();
                        uasort( $dataByFunc, function ( $a, $b ) {
                                return $a['real'] < $b['real']; // descending order
                        } );
-                       $profileReport = "Transclusion expansion time report (%,ms,calls,template)\n";
+                       $profileReport = [];
                        foreach ( array_slice( $dataByFunc, 0, 10 ) as $item ) {
-                               $profileReport .= sprintf( "%6.2f%% %8.3f %6d - %s\n",
-                                       $item['%real'], $item['real'], $item['calls'],
-                                       htmlspecialchars( $item['name'] ) );
+                               $profileReport[] = sprintf( "%6.2f%% %8.3f %6d %s",
+                                       $item['%real'], $item['real'], $item['calls'], $item['name'] );
                        }
-                       $text .= "\n<!-- \n$profileReport-->\n";
+                       $this->mOutput->setLimitReportData( 'limitreport-timingprofile', $profileReport );
 
-                       if ( $this->mGeneratedPPNodeCount > $this->mOptions->getMaxGeneratedPPNodeCount() / 10 ) {
+                       // Add other cache related metadata
+                       if ( $wgShowHostnames ) {
+                               $this->mOutput->setLimitReportData( 'cachereport-origin', wfHostname() );
+                       }
+                       $this->mOutput->setLimitReportData( 'cachereport-timestamp',
+                               $this->mOutput->getCacheTime() );
+                       $this->mOutput->setLimitReportData( 'cachereport-ttl',
+                               $this->mOutput->getCacheExpiry() );
+                       $this->mOutput->setLimitReportData( 'cachereport-transientcontent',
+                               $this->mOutput->hasDynamicContent() );
+
+                       if ( $this->mGeneratedPPNodeCount
+                               > $this->mOptions->getMaxGeneratedPPNodeCount() / 10
+                       ) {
                                wfDebugLog( 'generated-pp-node-count', $this->mGeneratedPPNodeCount . ' ' .
                                        $this->mTitle->getPrefixedDBkey() );
                        }
@@ -1264,7 +1250,9 @@ class Parser {
                        $text,
                        [ &$this, 'attributeStripCallback' ],
                        false,
-                       array_keys( $this->mTransparentTagHooks )
+                       array_keys( $this->mTransparentTagHooks ),
+                       [],
+                       [ &$this, 'addTrackingCategory' ]
                );
                Hooks::run( 'InternalParseBeforeLinks', [ &$this, &$text, &$this->mStripState ] );
 
@@ -1355,7 +1343,6 @@ class Parser {
 
                if ( MWTidy::isEnabled() && $this->mOptions->getTidy() ) {
                        $text = MWTidy::tidy( $text );
-                       $this->mOutput->addModuleStyles( MWTidy::getModuleStyles() );
                } else {
                        # attempt to sanitize at least some nesting problems
                        # (bug #2702 and quite a few others)
@@ -1470,10 +1457,14 @@ class Parser {
                                ' ' => '',
                                'x' => 'X',
                        ] );
-                       $titleObj = SpecialPage::getTitleFor( 'Booksources', $num );
-                       return '<a href="' .
-                               htmlspecialchars( $titleObj->getLocalURL() ) .
-                               "\" class=\"internal mw-magiclink-isbn\">ISBN $isbn</a>";
+                       return $this->getLinkRenderer()->makeKnownLink(
+                               SpecialPage::getTitleFor( 'Booksources', $num ),
+                               "ISBN $isbn",
+                               [
+                                       'class' => 'internal mw-magiclink-isbn',
+                                       'title' => false // suppress title attribute
+                               ]
+                       );
                } else {
                        return $m[0];
                }
@@ -2596,9 +2587,13 @@ class Parser {
                        case 'revisionid':
                                # Let the edit saving system know we should parse the page
                                # *after* a revision ID has been assigned.
-                               $this->mOutput->setFlag( 'vary-revision' );
-                               wfDebug( __METHOD__ . ": {{REVISIONID}} used, setting vary-revision...\n" );
+                               $this->mOutput->setFlag( 'vary-revision-id' );
+                               wfDebug( __METHOD__ . ": {{REVISIONID}} used, setting vary-revision-id...\n" );
                                $value = $this->mRevisionId;
+                               if ( !$value && $this->mOptions->getSpeculativeRevIdCallback() ) {
+                                       $value = call_user_func( $this->mOptions->getSpeculativeRevIdCallback() );
+                                       $this->mOutput->setSpeculativeRevIdUsed( $value );
+                               }
                                break;
                        case 'revisionday':
                                # Let the edit saving system know we should parse the page
@@ -3147,7 +3142,7 @@ class Parser {
                                                $context->setUser( User::newFromName( '127.0.0.1', false ) );
                                        }
                                        $context->setLanguage( $this->mOptions->getUserLangObj() );
-                                       $ret = SpecialPageFactory::capturePath( $title, $context );
+                                       $ret = SpecialPageFactory::capturePath( $title, $context, $this->getLinkRenderer() );
                                        if ( $ret ) {
                                                $text = $context->getOutput()->getHTML();
                                                $this->mOutput->addOutputPageMetadata( $context->getOutput() );