4 * New debugger system that outputs a toolbar on page view
6 * By default, most methods do nothing ( self::$enabled = false ). You have
7 * to explicitly call MWDebug::init() to enabled them.
9 * @todo Profiler support
18 protected static $log = array();
21 * Debug messages from wfDebug()
25 protected static $debug = array();
32 protected static $query = array();
35 * Is the debugger enabled?
39 protected static $enabled = false;
42 * Array of functions that have already been warned, formatted
43 * function-caller to prevent a buttload of warnings
47 protected static $deprecationWarnings = array();
50 * Enabled the debugger and load resource module.
51 * This is called by Setup.php when $wgDebugToolbar is true.
53 public static function init() {
54 self
::$enabled = true;
58 * Add ResourceLoader modules to the OutputPage object if debugging is
61 * @param $out OutputPage
63 public static function addModules( OutputPage
$out ) {
64 if ( self
::$enabled ) {
65 $out->addModules( 'mediawiki.debug' );
70 * Adds a line to the log
72 * @todo Add support for passing objects
76 public static function log( $str ) {
77 if ( !self
::$enabled ) {
82 'msg' => htmlspecialchars( $str ),
84 'caller' => wfGetCaller(),
89 * Returns internal log array
91 public static function getLog() {
96 * Clears internal log array and deprecation tracking
98 public static function clearLog() {
100 self
::$deprecationWarnings = array();
104 * Adds a warning entry to the log
107 * @param int $callerOffset
110 public static function warning( $msg, $callerOffset = 1 ) {
111 if ( !self
::$enabled ) {
115 // Check to see if there was already a deprecation notice, so not to
116 // get a duplicate warning
117 $logCount = count( self
::$log );
119 $lastLog = self
::$log[ $logCount - 1 ];
120 if ( $lastLog['type'] == 'deprecated' && $lastLog['caller'] == wfGetCaller( $callerOffset +
1 ) ) {
125 self
::$log[] = array(
126 'msg' => htmlspecialchars( $msg ),
128 'caller' => wfGetCaller( $callerOffset ),
133 * Adds a depreciation entry to the log, along with a backtrace
140 public static function deprecated( $function, $version, $component ) {
141 if ( !self
::$enabled ) {
145 // Chain: This function -> wfDeprecated -> deprecatedFunction -> caller
146 $caller = wfGetCaller( 4 );
148 // Check to see if there already was a warning about this function
149 $functionString = "$function-$caller";
150 if ( in_array( $functionString, self
::$deprecationWarnings ) ) {
154 $version = $version === false ?
'(unknown version)' : $version;
155 $component = $component === false ?
'MediaWiki' : $component;
156 $msg = htmlspecialchars( "Use of function $function was deprecated in $component $version" );
157 $msg .= Html
::rawElement( 'div', array( 'class' => 'mw-debug-backtrace' ),
158 Html
::element( 'span', array(), 'Backtrace:' )
162 self
::$deprecationWarnings[] = $functionString;
163 self
::$log[] = array(
165 'type' => 'deprecated',
171 * This is a method to pass messages from wfDebug to the pretty debugger.
172 * Do NOT use this method, use MWDebug::log or wfDebug()
176 public static function debugMsg( $str ) {
177 if ( !self
::$enabled ) {
181 self
::$debug[] = trim( $str );
185 * Begins profiling on a database query
188 * @param $function string
189 * @param $isMaster bool
190 * @return int ID number of the query to pass to queryTime or -1 if the
191 * debugger is disabled
193 public static function query( $sql, $function, $isMaster ) {
194 if ( !self
::$enabled ) {
198 self
::$query[] = array(
200 'function' => $function,
201 'master' => (bool) $isMaster,
203 '_start' => microtime( true ),
206 return count( self
::$query ) - 1;
210 * Calculates how long a query took.
214 public static function queryTime( $id ) {
215 if ( $id === -1 ||
!self
::$enabled ) {
219 self
::$query[$id]['time'] = microtime( true ) - self
::$query[$id]['_start'];
220 unset( self
::$query[$id]['_start'] );
224 * Returns a list of files included, along with their size
226 * @param $context IContextSource
229 protected static function getFilesIncluded( IContextSource
$context ) {
230 $files = get_included_files();
232 foreach ( $files as $file ) {
233 $size = filesize( $file );
236 'size' => $context->getLanguage()->formatSize( $size ),
244 * Returns the HTML to add to the page for the toolbar
246 * @param $context IContextSource
249 public static function getDebugHTML( IContextSource
$context ) {
250 if ( !self
::$enabled ) {
254 global $wgVersion, $wgRequestTime;
255 MWDebug
::log( 'MWDebug output complete' );
256 $request = $context->getRequest();
258 'mwVersion' => $wgVersion,
259 'phpVersion' => PHP_VERSION
,
260 'time' => microtime( true ) - $wgRequestTime,
262 'debugLog' => self
::$debug,
263 'queries' => self
::$query,
265 'method' => $_SERVER['REQUEST_METHOD'],
266 'url' => $request->getRequestURL(),
267 'headers' => $request->getAllHeaders(),
268 'params' => $request->getValues(),
270 'memory' => $context->getLanguage()->formatSize( memory_get_usage() ),
271 'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage() ),
272 'includes' => self
::getFilesIncluded( $context ),
274 // TODO: Clean this up
275 $html = Html
::openElement( 'script' );
276 $html .= 'var debugInfo = ' . Xml
::encodeJsVar( $debugInfo ) . ';';
277 $html .= " $(function() { mw.loader.using( 'mediawiki.debug', function() { mw.Debug.init( debugInfo ) } ); }); ";
278 $html .= Html
::closeElement( 'script' );