From 0a5e1bf76a2c71239984b8c1fa6ec732ee86d0e6 Mon Sep 17 00:00:00 2001 From: umherirrender Date: Sun, 13 May 2012 11:20:04 +0200 Subject: [PATCH] Add DebugInfos to api result This allow to get the queries of the api due the same api request, when setting $wgDebugToolbar = true, that acts like the inline script added to index.php Change-Id: I7c121822827137ba098d95a54ec90f824e0ddf01 --- includes/api/ApiMain.php | 3 ++ includes/debug/Debug.php | 65 ++++++++++++++++++++++++++++++++-------- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 10540a3984..7414a97f37 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -715,6 +715,9 @@ class ApiMain extends ApiBase { $module->profileOut(); if ( !$this->mInternalMode ) { + //append Debug information + MWDebug::appendDebugInfoToApiResult( $this->getContext(), $this->getResult() ); + // Print result data $this->printResult( false ); } diff --git a/includes/debug/Debug.php b/includes/debug/Debug.php index ed73522f80..75ff406688 100644 --- a/includes/debug/Debug.php +++ b/includes/debug/Debug.php @@ -272,10 +272,61 @@ class MWDebug { return ''; } - global $wgVersion, $wgRequestTime; MWDebug::log( 'MWDebug output complete' ); + $debugInfo = self::getDebugInfo( $context ); + + // Cannot use OutputPage::addJsConfigVars because those are already outputted + // by the time this method is called. + $html = Html::inlineScript( + ResourceLoader::makeLoaderConditionalScript( + ResourceLoader::makeConfigSetScript( array( 'debugInfo' => $debugInfo ) ) + ) + ); + + return $html; + } + + /** + * Append the debug info to given ApiResult + * + * @param $context IContextSource + * @param $result ApiResult + */ + public static function appendDebugInfoToApiResult( IContextSource $context, ApiResult $result ) { + if ( !self::$enabled ) { + return; + } + + MWDebug::log( 'MWDebug output complete' ); + $debugInfo = self::getDebugInfo( $context ); + + $result->setIndexedTagName( $debugInfo, 'debuginfo' ); + $result->setIndexedTagName( $debugInfo['log'], 'line' ); + foreach( $debugInfo['debugLog'] as $index => $debugLogText ) { + $vals = array(); + ApiResult::setContent( $vals, $debugLogText ); + $debugInfo['debugLog'][$index] = $vals; //replace + } + $result->setIndexedTagName( $debugInfo['debugLog'], 'msg' ); + $result->setIndexedTagName( $debugInfo['queries'], 'query' ); + $result->setIndexedTagName( $debugInfo['includes'], 'queries' ); + $result->addValue( array(), 'debuginfo', $debugInfo ); + } + + /** + * Returns the HTML to add to the page for the toolbar + * + * @param $context IContextSource + * @return array + */ + public static function getDebugInfo( IContextSource $context ) { + if ( !self::$enabled ) { + return array(); + } + + global $wgVersion, $wgRequestTime; $request = $context->getRequest(); - $debugInfo = array( + return array( 'mwVersion' => $wgVersion, 'phpVersion' => PHP_VERSION, 'gitRevision' => GitInfo::headSHA1(), @@ -295,15 +346,5 @@ class MWDebug { 'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage() ), 'includes' => self::getFilesIncluded( $context ), ); - - // Cannot use OutputPage::addJsConfigVars because those are already outputted - // by the time this method is called. - $html = Html::inlineScript( - ResourceLoader::makeLoaderConditionalScript( - ResourceLoader::makeConfigSetScript( array( 'debugInfo' => $debugInfo ) ) - ) - ); - - return $html; } } -- 2.20.1