3 * Debug toolbar related code
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * New debugger system that outputs a toolbar on page view
26 * By default, most methods do nothing ( self::$enabled = false ). You have
27 * to explicitly call MWDebug::init() to enabled them.
29 * @todo Profiler support
40 protected static $log = array();
43 * Debug messages from wfDebug()
47 protected static $debug = array();
54 protected static $query = array();
57 * Is the debugger enabled?
61 protected static $enabled = false;
64 * Array of functions that have already been warned, formatted
65 * function-caller to prevent a buttload of warnings
69 protected static $deprecationWarnings = array();
72 * Enabled the debugger and load resource module.
73 * This is called by Setup.php when $wgDebugToolbar is true.
77 public static function init() {
78 self
::$enabled = true;
82 * Add ResourceLoader modules to the OutputPage object if debugging is
86 * @param $out OutputPage
88 public static function addModules( OutputPage
$out ) {
89 if ( self
::$enabled ) {
90 $out->addModules( 'mediawiki.debug.init' );
95 * Adds a line to the log
97 * @todo Add support for passing objects
102 public static function log( $str ) {
103 if ( !self
::$enabled ) {
107 self
::$log[] = array(
108 'msg' => htmlspecialchars( $str ),
110 'caller' => wfGetCaller(),
115 * Returns internal log array
119 public static function getLog() {
124 * Clears internal log array and deprecation tracking
127 public static function clearLog() {
128 self
::$log = array();
129 self
::$deprecationWarnings = array();
133 * Adds a warning entry to the log
137 * @param int $callerOffset
140 public static function warning( $msg, $callerOffset = 1 ) {
141 if ( !self
::$enabled ) {
145 // Check to see if there was already a deprecation notice, so not to
146 // get a duplicate warning
147 $logCount = count( self
::$log );
148 $caller = wfGetCaller( $callerOffset +
1 );
150 $lastLog = self
::$log[ $logCount - 1 ];
151 if ( $lastLog['type'] == 'deprecated' && $lastLog['caller'] == $caller ) {
156 self
::$log[] = array(
157 'msg' => htmlspecialchars( $msg ),
164 * Adds a depreciation entry to the log, along with a backtrace
172 public static function deprecated( $function, $version, $component ) {
173 if ( !self
::$enabled ) {
177 // Chain: This function -> wfDeprecated -> deprecatedFunction -> caller
178 $caller = wfGetCaller( 4 );
180 // Check to see if there already was a warning about this function
181 $functionString = "$function-$caller";
182 if ( in_array( $functionString, self
::$deprecationWarnings ) ) {
186 $version = $version === false ?
'(unknown version)' : $version;
187 $component = $component === false ?
'MediaWiki' : $component;
188 $msg = htmlspecialchars( "Use of function $function was deprecated in $component $version" );
189 $msg .= Html
::rawElement( 'div', array( 'class' => 'mw-debug-backtrace' ),
190 Html
::element( 'span', array(), 'Backtrace:' )
194 self
::$deprecationWarnings[] = $functionString;
195 self
::$log[] = array(
197 'type' => 'deprecated',
203 * This is a method to pass messages from wfDebug to the pretty debugger.
204 * Do NOT use this method, use MWDebug::log or wfDebug()
209 public static function debugMsg( $str ) {
210 if ( !self
::$enabled ) {
214 self
::$debug[] = trim( $str );
218 * Begins profiling on a database query
222 * @param $function string
223 * @param $isMaster bool
224 * @return int ID number of the query to pass to queryTime or -1 if the
225 * debugger is disabled
227 public static function query( $sql, $function, $isMaster ) {
228 if ( !self
::$enabled ) {
232 self
::$query[] = array(
234 'function' => $function,
235 'master' => (bool) $isMaster,
237 '_start' => microtime( true ),
240 return count( self
::$query ) - 1;
244 * Calculates how long a query took.
249 public static function queryTime( $id ) {
250 if ( $id === -1 ||
!self
::$enabled ) {
254 self
::$query[$id]['time'] = microtime( true ) - self
::$query[$id]['_start'];
255 unset( self
::$query[$id]['_start'] );
259 * Returns a list of files included, along with their size
261 * @param $context IContextSource
264 protected static function getFilesIncluded( IContextSource
$context ) {
265 $files = get_included_files();
267 foreach ( $files as $file ) {
268 $size = filesize( $file );
271 'size' => $context->getLanguage()->formatSize( $size ),
279 * Returns the HTML to add to the page for the toolbar
282 * @param $context IContextSource
285 public static function getDebugHTML( IContextSource
$context ) {
286 if ( !self
::$enabled ) {
290 MWDebug
::log( 'MWDebug output complete' );
291 $debugInfo = self
::getDebugInfo( $context );
293 // Cannot use OutputPage::addJsConfigVars because those are already outputted
294 // by the time this method is called.
295 $html = Html
::inlineScript(
296 ResourceLoader
::makeLoaderConditionalScript(
297 ResourceLoader
::makeConfigSetScript( array( 'debugInfo' => $debugInfo ) )
305 * Append the debug info to given ApiResult
307 * @param $context IContextSource
308 * @param $result ApiResult
310 public static function appendDebugInfoToApiResult( IContextSource
$context, ApiResult
$result ) {
311 if ( !self
::$enabled ) {
315 // output errors as debug info, when display_errors is on
316 // this is necessary for all non html output of the api, because that clears all errors first
317 $obContents = ob_get_contents();
319 $obContentArray = explode( '<br />', $obContents );
320 foreach( $obContentArray as $obContent ) {
321 if( trim( $obContent ) ) {
322 self
::debugMsg( Sanitizer
::stripAllTags( $obContent ) );
327 MWDebug
::log( 'MWDebug output complete' );
328 $debugInfo = self
::getDebugInfo( $context );
330 $result->setIndexedTagName( $debugInfo, 'debuginfo' );
331 $result->setIndexedTagName( $debugInfo['log'], 'line' );
332 foreach( $debugInfo['debugLog'] as $index => $debugLogText ) {
334 ApiResult
::setContent( $vals, $debugLogText );
335 $debugInfo['debugLog'][$index] = $vals; //replace
337 $result->setIndexedTagName( $debugInfo['debugLog'], 'msg' );
338 $result->setIndexedTagName( $debugInfo['queries'], 'query' );
339 $result->setIndexedTagName( $debugInfo['includes'], 'queries' );
340 $result->addValue( array(), 'debuginfo', $debugInfo );
344 * Returns the HTML to add to the page for the toolbar
346 * @param $context IContextSource
349 public static function getDebugInfo( IContextSource
$context ) {
350 if ( !self
::$enabled ) {
354 global $wgVersion, $wgRequestTime;
355 $request = $context->getRequest();
357 'mwVersion' => $wgVersion,
358 'phpVersion' => PHP_VERSION
,
359 'gitRevision' => GitInfo
::headSHA1(),
360 'gitBranch' => GitInfo
::currentBranch(),
361 'gitViewUrl' => GitInfo
::headViewUrl(),
362 'time' => microtime( true ) - $wgRequestTime,
364 'debugLog' => self
::$debug,
365 'queries' => self
::$query,
367 'method' => $request->getMethod(),
368 'url' => $request->getRequestURL(),
369 'headers' => $request->getAllHeaders(),
370 'params' => $request->getValues(),
372 'memory' => $context->getLanguage()->formatSize( memory_get_usage() ),
373 'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage() ),
374 'includes' => self
::getFilesIncluded( $context ),