2011-12-04 18:35:40 +00:00
|
|
|
<?php
|
2012-04-28 18:41:55 +00:00
|
|
|
/**
|
|
|
|
|
* Debug toolbar related code
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2011-12-04 18:35:40 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* New debugger system that outputs a toolbar on page view
|
|
|
|
|
*
|
2012-01-16 13:44:46 +00:00
|
|
|
* By default, most methods do nothing ( self::$enabled = false ). You have
|
|
|
|
|
* to explicitly call MWDebug::init() to enabled them.
|
|
|
|
|
*
|
2011-12-04 18:35:40 +00:00
|
|
|
* @todo Profiler support
|
|
|
|
|
*/
|
|
|
|
|
class MWDebug {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Log lines
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected static $log = array();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Debug messages from wfDebug()
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected static $debug = array();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Queries
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected static $query = array();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Is the debugger enabled?
|
|
|
|
|
*
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
2012-01-16 13:44:46 +00:00
|
|
|
protected static $enabled = false;
|
2011-12-04 18:35:40 +00:00
|
|
|
|
2012-01-03 05:56:36 +00:00
|
|
|
/**
|
|
|
|
|
* Array of functions that have already been warned, formatted
|
|
|
|
|
* function-caller to prevent a buttload of warnings
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected static $deprecationWarnings = array();
|
|
|
|
|
|
2011-12-04 18:35:40 +00:00
|
|
|
/**
|
2012-01-16 13:44:46 +00:00
|
|
|
* Enabled the debugger and load resource module.
|
|
|
|
|
* This is called by Setup.php when $wgDebugToolbar is true.
|
2011-12-04 18:35:40 +00:00
|
|
|
*/
|
|
|
|
|
public static function init() {
|
2012-01-16 13:44:46 +00:00
|
|
|
self::$enabled = true;
|
2012-02-06 17:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add ResourceLoader modules to the OutputPage object if debugging is
|
|
|
|
|
* enabled.
|
|
|
|
|
*
|
|
|
|
|
* @param $out OutputPage
|
|
|
|
|
*/
|
|
|
|
|
public static function addModules( OutputPage $out ) {
|
|
|
|
|
if ( self::$enabled ) {
|
2012-02-13 15:25:08 +00:00
|
|
|
$out->addModules( 'mediawiki.debug.init' );
|
2012-02-06 17:34:33 +00:00
|
|
|
}
|
2011-12-04 18:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds a line to the log
|
|
|
|
|
*
|
|
|
|
|
* @todo Add support for passing objects
|
|
|
|
|
*
|
|
|
|
|
* @param $str string
|
|
|
|
|
*/
|
|
|
|
|
public static function log( $str ) {
|
|
|
|
|
if ( !self::$enabled ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-03 05:56:36 +00:00
|
|
|
self::$log[] = array(
|
|
|
|
|
'msg' => htmlspecialchars( $str ),
|
|
|
|
|
'type' => 'log',
|
|
|
|
|
'caller' => wfGetCaller(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-13 23:07:52 +00:00
|
|
|
/**
|
|
|
|
|
* Returns internal log array
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return array
|
2012-01-13 23:07:52 +00:00
|
|
|
*/
|
|
|
|
|
public static function getLog() {
|
|
|
|
|
return self::$log;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clears internal log array and deprecation tracking
|
|
|
|
|
*/
|
|
|
|
|
public static function clearLog() {
|
|
|
|
|
self::$log = array();
|
|
|
|
|
self::$deprecationWarnings = array();
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-03 05:56:36 +00:00
|
|
|
/**
|
|
|
|
|
* Adds a warning entry to the log
|
|
|
|
|
*
|
|
|
|
|
* @param $msg
|
|
|
|
|
* @param int $callerOffset
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public static function warning( $msg, $callerOffset = 1 ) {
|
|
|
|
|
if ( !self::$enabled ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check to see if there was already a deprecation notice, so not to
|
|
|
|
|
// get a duplicate warning
|
2012-01-13 23:10:21 +00:00
|
|
|
$logCount = count( self::$log );
|
|
|
|
|
if ( $logCount ) {
|
|
|
|
|
$lastLog = self::$log[ $logCount - 1 ];
|
2012-01-03 22:36:35 +00:00
|
|
|
if ( $lastLog['type'] == 'deprecated' && $lastLog['caller'] == wfGetCaller( $callerOffset + 1 ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-01-03 05:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self::$log[] = array(
|
|
|
|
|
'msg' => htmlspecialchars( $msg ),
|
|
|
|
|
'type' => 'warn',
|
|
|
|
|
'caller' => wfGetCaller( $callerOffset ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds a depreciation entry to the log, along with a backtrace
|
|
|
|
|
*
|
|
|
|
|
* @param $function
|
|
|
|
|
* @param $version
|
|
|
|
|
* @param $component
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public static function deprecated( $function, $version, $component ) {
|
|
|
|
|
if ( !self::$enabled ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Chain: This function -> wfDeprecated -> deprecatedFunction -> caller
|
|
|
|
|
$caller = wfGetCaller( 4 );
|
|
|
|
|
|
|
|
|
|
// Check to see if there already was a warning about this function
|
|
|
|
|
$functionString = "$function-$caller";
|
|
|
|
|
if ( in_array( $functionString, self::$deprecationWarnings ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$version = $version === false ? '(unknown version)' : $version;
|
|
|
|
|
$component = $component === false ? 'MediaWiki' : $component;
|
|
|
|
|
$msg = htmlspecialchars( "Use of function $function was deprecated in $component $version" );
|
|
|
|
|
$msg .= Html::rawElement( 'div', array( 'class' => 'mw-debug-backtrace' ),
|
|
|
|
|
Html::element( 'span', array(), 'Backtrace:' )
|
|
|
|
|
. wfBacktrace()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
self::$deprecationWarnings[] = $functionString;
|
|
|
|
|
self::$log[] = array(
|
|
|
|
|
'msg' => $msg,
|
|
|
|
|
'type' => 'deprecated',
|
|
|
|
|
'caller' => $caller,
|
|
|
|
|
);
|
2011-12-04 18:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is a method to pass messages from wfDebug to the pretty debugger.
|
|
|
|
|
* Do NOT use this method, use MWDebug::log or wfDebug()
|
|
|
|
|
*
|
|
|
|
|
* @param $str string
|
|
|
|
|
*/
|
|
|
|
|
public static function debugMsg( $str ) {
|
|
|
|
|
if ( !self::$enabled ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self::$debug[] = trim( $str );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Begins profiling on a database query
|
|
|
|
|
*
|
|
|
|
|
* @param $sql string
|
|
|
|
|
* @param $function string
|
|
|
|
|
* @param $isMaster bool
|
|
|
|
|
* @return int ID number of the query to pass to queryTime or -1 if the
|
|
|
|
|
* debugger is disabled
|
|
|
|
|
*/
|
|
|
|
|
public static function query( $sql, $function, $isMaster ) {
|
|
|
|
|
if ( !self::$enabled ) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self::$query[] = array(
|
|
|
|
|
'sql' => $sql,
|
|
|
|
|
'function' => $function,
|
|
|
|
|
'master' => (bool) $isMaster,
|
2012-02-08 22:41:11 +00:00
|
|
|
'time' => 0.0,
|
2012-02-03 08:32:34 +00:00
|
|
|
'_start' => microtime( true ),
|
2011-12-04 18:35:40 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return count( self::$query ) - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculates how long a query took.
|
|
|
|
|
*
|
|
|
|
|
* @param $id int
|
|
|
|
|
*/
|
|
|
|
|
public static function queryTime( $id ) {
|
|
|
|
|
if ( $id === -1 || !self::$enabled ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-03 08:32:34 +00:00
|
|
|
self::$query[$id]['time'] = microtime( true ) - self::$query[$id]['_start'];
|
2011-12-04 18:35:40 +00:00
|
|
|
unset( self::$query[$id]['_start'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a list of files included, along with their size
|
|
|
|
|
*
|
2011-12-15 02:26:14 +00:00
|
|
|
* @param $context IContextSource
|
2011-12-04 18:35:40 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2011-12-15 02:26:14 +00:00
|
|
|
protected static function getFilesIncluded( IContextSource $context ) {
|
2011-12-04 18:35:40 +00:00
|
|
|
$files = get_included_files();
|
|
|
|
|
$fileList = array();
|
|
|
|
|
foreach ( $files as $file ) {
|
|
|
|
|
$size = filesize( $file );
|
|
|
|
|
$fileList[] = array(
|
|
|
|
|
'name' => $file,
|
2011-12-15 02:26:14 +00:00
|
|
|
'size' => $context->getLanguage()->formatSize( $size ),
|
2011-12-04 18:35:40 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $fileList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the HTML to add to the page for the toolbar
|
|
|
|
|
*
|
2011-12-15 02:26:14 +00:00
|
|
|
* @param $context IContextSource
|
2011-12-04 18:35:40 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-12-15 02:26:14 +00:00
|
|
|
public static function getDebugHTML( IContextSource $context ) {
|
2011-12-04 18:35:40 +00:00
|
|
|
if ( !self::$enabled ) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
global $wgVersion, $wgRequestTime;
|
2012-01-03 05:56:36 +00:00
|
|
|
MWDebug::log( 'MWDebug output complete' );
|
2012-02-06 17:34:33 +00:00
|
|
|
$request = $context->getRequest();
|
2011-12-04 18:35:40 +00:00
|
|
|
$debugInfo = array(
|
|
|
|
|
'mwVersion' => $wgVersion,
|
|
|
|
|
'phpVersion' => PHP_VERSION,
|
2012-03-28 10:43:35 +00:00
|
|
|
'gitRevision' => GitInfo::headSHA1(),
|
|
|
|
|
'gitBranch' => GitInfo::currentBranch(),
|
2012-04-10 08:52:11 +00:00
|
|
|
'gitViewUrl' => GitInfo::headViewUrl(),
|
2012-02-03 08:32:34 +00:00
|
|
|
'time' => microtime( true ) - $wgRequestTime,
|
2011-12-04 18:35:40 +00:00
|
|
|
'log' => self::$log,
|
|
|
|
|
'debugLog' => self::$debug,
|
|
|
|
|
'queries' => self::$query,
|
2012-02-06 17:34:33 +00:00
|
|
|
'request' => array(
|
|
|
|
|
'method' => $_SERVER['REQUEST_METHOD'],
|
|
|
|
|
'url' => $request->getRequestURL(),
|
|
|
|
|
'headers' => $request->getAllHeaders(),
|
|
|
|
|
'params' => $request->getValues(),
|
|
|
|
|
),
|
2011-12-15 02:26:14 +00:00
|
|
|
'memory' => $context->getLanguage()->formatSize( memory_get_usage() ),
|
|
|
|
|
'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage() ),
|
|
|
|
|
'includes' => self::getFilesIncluded( $context ),
|
2011-12-04 18:35:40 +00:00
|
|
|
);
|
2012-02-13 15:25:08 +00:00
|
|
|
|
|
|
|
|
// 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 ) )
|
|
|
|
|
)
|
|
|
|
|
);
|
2011-12-04 18:35:40 +00:00
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
|
}
|
2012-01-13 23:07:52 +00:00
|
|
|
}
|