From b03b387e5a78239cd6b3db3d136a7d5856d482b2 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Thu, 10 Nov 2016 12:29:27 -0800 Subject: [PATCH] Include JS variable for NewPP report Adapted from reverted commit b7c4c8717f9. Bug: T110763 Change-Id: If249b679c534879bfac622592a1d2fa913a0cf9d --- RELEASE-NOTES-1.29 | 2 ++ includes/OutputPage.php | 18 +++++++++++++++++- includes/parser/Parser.php | 22 ++++++++++++++++++---- includes/parser/ParserOutput.php | 27 +++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 5 deletions(-) diff --git a/RELEASE-NOTES-1.29 b/RELEASE-NOTES-1.29 index 72c82de100..b8867389be 100644 --- a/RELEASE-NOTES-1.29 +++ b/RELEASE-NOTES-1.29 @@ -37,6 +37,8 @@ production. dnsbls, are now indicated as such and use a new i18n message when displayed. * Added new $wgHTTPImportTimeout setting. Sets timeout for downloading the XML dump during a transwiki import in seconds. +* Parser limit report is now available in machine-readable format to JavaScript + via mw.config.get('wgPageParseReport'). === External library changes in 1.29 === diff --git a/includes/OutputPage.php b/includes/OutputPage.php index f140f548b3..211f44bf5c 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -299,6 +299,9 @@ class OutputPage extends ContextSource { */ private $copyrightUrl; + /** @var array Profiling data */ + private $limitReportJSData = []; + /** * Constructor for OutputPage. This should not be called directly. * Instead a new RequestContext should be created and it will implicitly create @@ -1804,11 +1807,16 @@ class OutputPage extends ContextSource { } } - // enable OOUI if requested via ParserOutput + // Enable OOUI if requested via ParserOutput if ( $parserOutput->getEnableOOUI() ) { $this->enableOOUI(); } + // Include parser limit report + if ( !$this->limitReportJSData ) { + $this->limitReportJSData = $parserOutput->getLimitReportJSData(); + } + // Link flags are ignored for now, but may in the future be // used to mark individual language links. $linkFlags = []; @@ -3005,6 +3013,14 @@ class OutputPage extends ContextSource { } } + if ( $this->limitReportJSData ) { + $chunks[] = ResourceLoader::makeInlineScript( + ResourceLoader::makeConfigSetScript( + [ 'wgPageParseReport' => $this->limitReportJSData ] + ) + ); + } + return self::combineWrappedStrings( $chunks ); } diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index 0e9aa5971b..157946c385 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -547,18 +547,32 @@ class Parser { $limitReport = str_replace( [ '-', '&' ], [ '‐', '&' ], $limitReport ); $text .= "\n\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", + $profileReport[] = sprintf( "%6.2f%% %8.3f %6d %s", $item['%real'], $item['real'], $item['calls'], htmlspecialchars( $item['name'] ) ); } - $text .= "\n\n"; + $text .= "\n"; + + $this->mOutput->setLimitReportData( 'limitreport-timingprofile', $profileReport ); + + // 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 . ' ' . diff --git a/includes/parser/ParserOutput.php b/includes/parser/ParserOutput.php index bfaa4f7dae..7bf848fb90 100644 --- a/includes/parser/ParserOutput.php +++ b/includes/parser/ParserOutput.php @@ -193,6 +193,9 @@ class ParserOutput extends CacheTime { */ private $mLimitReportData = []; + /** @var array Parser limit report data for JSON */ + private $mLimitReportJSData = []; + /** * @var array $mParseStartTime Timestamps for getTimeSinceStart(). */ @@ -411,6 +414,10 @@ class ParserOutput extends CacheTime { return $this->mLimitReportData; } + public function getLimitReportJSData() { + return $this->mLimitReportJSData; + } + public function getTOCEnabled() { return $this->mTOCEnabled; } @@ -1010,6 +1017,26 @@ class ParserOutput extends CacheTime { */ public function setLimitReportData( $key, $value ) { $this->mLimitReportData[$key] = $value; + + if ( is_array( $value ) ) { + if ( array_keys( $value ) === [ 0, 1 ] + && is_numeric( $value[0] ) + && is_numeric( $value[1] ) + ) { + $data = [ 'value' => $value[0], 'limit' => $value[1] ]; + } else { + $data = $value; + } + } else { + $data = $value; + } + + if ( strpos( $key, '-' ) ) { + list( $ns, $name ) = explode( '-', $key, 2 ); + $this->mLimitReportJSData[$ns][$name] = $data; + } else { + $this->mLimitReportJSData[$key] = $data; + } } /** -- 2.20.1