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
92 public static function getLog() {
97 * Clears internal log array and deprecation tracking
99 public static function clearLog() {
100 self
::$log = array();
101 self
::$deprecationWarnings = array();
105 * Adds a warning entry to the log
108 * @param int $callerOffset
111 public static function warning( $msg, $callerOffset = 1 ) {
112 if ( !self
::$enabled ) {
116 // Check to see if there was already a deprecation notice, so not to
117 // get a duplicate warning
118 $logCount = count( self
::$log );
120 $lastLog = self
::$log[ $logCount - 1 ];
121 if ( $lastLog['type'] == 'deprecated' && $lastLog['caller'] == wfGetCaller( $callerOffset +
1 ) ) {
126 self
::$log[] = array(
127 'msg' => htmlspecialchars( $msg ),
129 'caller' => wfGetCaller( $callerOffset ),
134 * Adds a depreciation entry to the log, along with a backtrace
141 public static function deprecated( $function, $version, $component ) {
142 if ( !self
::$enabled ) {
146 // Chain: This function -> wfDeprecated -> deprecatedFunction -> caller
147 $caller = wfGetCaller( 4 );
149 // Check to see if there already was a warning about this function
150 $functionString = "$function-$caller";
151 if ( in_array( $functionString, self
::$deprecationWarnings ) ) {
155 $version = $version === false ?
'(unknown version)' : $version;
156 $component = $component === false ?
'MediaWiki' : $component;
157 $msg = htmlspecialchars( "Use of function $function was deprecated in $component $version" );
158 $msg .= Html
::rawElement( 'div', array( 'class' => 'mw-debug-backtrace' ),
159 Html
::element( 'span', array(), 'Backtrace:' )
163 self
::$deprecationWarnings[] = $functionString;
164 self
::$log[] = array(
166 'type' => 'deprecated',
172 * This is a method to pass messages from wfDebug to the pretty debugger.
173 * Do NOT use this method, use MWDebug::log or wfDebug()
177 public static function debugMsg( $str ) {
178 if ( !self
::$enabled ) {
182 self
::$debug[] = trim( $str );
186 * Begins profiling on a database query
189 * @param $function string
190 * @param $isMaster bool
191 * @return int ID number of the query to pass to queryTime or -1 if the
192 * debugger is disabled
194 public static function query( $sql, $function, $isMaster ) {
195 if ( !self
::$enabled ) {
199 self
::$query[] = array(
201 'function' => $function,
202 'master' => (bool) $isMaster,
204 '_start' => microtime( true ),
207 return count( self
::$query ) - 1;
211 * Calculates how long a query took.
215 public static function queryTime( $id ) {
216 if ( $id === -1 ||
!self
::$enabled ) {
220 self
::$query[$id]['time'] = microtime( true ) - self
::$query[$id]['_start'];
221 unset( self
::$query[$id]['_start'] );
225 * Returns a list of files included, along with their size
227 * @param $context IContextSource
230 protected static function getFilesIncluded( IContextSource
$context ) {
231 $files = get_included_files();
233 foreach ( $files as $file ) {
234 $size = filesize( $file );
237 'size' => $context->getLanguage()->formatSize( $size ),
245 * Returns the HTML to add to the page for the toolbar
247 * @param $context IContextSource
250 public static function getDebugHTML( IContextSource
$context ) {
251 if ( !self
::$enabled ) {
255 global $wgVersion, $wgRequestTime;
256 MWDebug
::log( 'MWDebug output complete' );
257 $request = $context->getRequest();
259 'mwVersion' => $wgVersion,
260 'phpVersion' => PHP_VERSION
,
261 'time' => microtime( true ) - $wgRequestTime,
263 'debugLog' => self
::$debug,
264 'queries' => self
::$query,
266 'method' => $_SERVER['REQUEST_METHOD'],
267 'url' => $request->getRequestURL(),
268 'headers' => $request->getAllHeaders(),
269 'params' => $request->getValues(),
271 'memory' => $context->getLanguage()->formatSize( memory_get_usage() ),
272 'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage() ),
273 'includes' => self
::getFilesIncluded( $context ),
275 // TODO: Clean this up
276 $html = Html
::openElement( 'script' );
277 $html .= 'var debugInfo = ' . Xml
::encodeJsVar( $debugInfo ) . ';';
278 $html .= " $(function() { mw.loader.using( 'mediawiki.debug', function() { mw.Debug.init( debugInfo ) } ); }); ";
279 $html .= Html
::closeElement( 'script' );