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();
39 protected static $request = array();
42 * Is the debugger enabled?
46 protected static $enabled = false;
49 * Array of functions that have already been warned, formatted
50 * function-caller to prevent a buttload of warnings
54 protected static $deprecationWarnings = array();
57 * Enabled the debugger and load resource module.
58 * This is called by Setup.php when $wgDebugToolbar is true.
60 public static function init() {
61 self
::$enabled = true;
62 RequestContext
::getMain()->getOutput()->addModules( 'mediawiki.debug' );
66 * Adds a line to the log
68 * @todo Add support for passing objects
72 public static function log( $str ) {
73 if ( !self
::$enabled ) {
78 'msg' => htmlspecialchars( $str ),
80 'caller' => wfGetCaller(),
85 * Returns internal log array
87 public static function getLog() {
92 * Clears internal log array and deprecation tracking
94 public static function clearLog() {
96 self
::$deprecationWarnings = array();
100 * Adds a warning entry to the log
103 * @param int $callerOffset
106 public static function warning( $msg, $callerOffset = 1 ) {
107 if ( !self
::$enabled ) {
111 // Check to see if there was already a deprecation notice, so not to
112 // get a duplicate warning
113 $logCount = count( self
::$log );
115 $lastLog = self
::$log[ $logCount - 1 ];
116 if ( $lastLog['type'] == 'deprecated' && $lastLog['caller'] == wfGetCaller( $callerOffset +
1 ) ) {
121 self
::$log[] = array(
122 'msg' => htmlspecialchars( $msg ),
124 'caller' => wfGetCaller( $callerOffset ),
129 * Adds a depreciation entry to the log, along with a backtrace
136 public static function deprecated( $function, $version, $component ) {
137 if ( !self
::$enabled ) {
141 // Chain: This function -> wfDeprecated -> deprecatedFunction -> caller
142 $caller = wfGetCaller( 4 );
144 // Check to see if there already was a warning about this function
145 $functionString = "$function-$caller";
146 if ( in_array( $functionString, self
::$deprecationWarnings ) ) {
150 $version = $version === false ?
'(unknown version)' : $version;
151 $component = $component === false ?
'MediaWiki' : $component;
152 $msg = htmlspecialchars( "Use of function $function was deprecated in $component $version" );
153 $msg .= Html
::rawElement( 'div', array( 'class' => 'mw-debug-backtrace' ),
154 Html
::element( 'span', array(), 'Backtrace:' )
158 self
::$deprecationWarnings[] = $functionString;
159 self
::$log[] = array(
161 'type' => 'deprecated',
167 * This is a method to pass messages from wfDebug to the pretty debugger.
168 * Do NOT use this method, use MWDebug::log or wfDebug()
172 public static function debugMsg( $str ) {
173 if ( !self
::$enabled ) {
177 self
::$debug[] = trim( $str );
181 * Begins profiling on a database query
184 * @param $function string
185 * @param $isMaster bool
186 * @return int ID number of the query to pass to queryTime or -1 if the
187 * debugger is disabled
189 public static function query( $sql, $function, $isMaster ) {
190 if ( !self
::$enabled ) {
194 self
::$query[] = array(
196 'function' => $function,
197 'master' => (bool) $isMaster,
199 '_start' => wfTime(),
202 return count( self
::$query ) - 1;
206 * Calculates how long a query took.
210 public static function queryTime( $id ) {
211 if ( $id === -1 ||
!self
::$enabled ) {
215 self
::$query[$id]['time'] = wfTime() - self
::$query[$id]['_start'];
216 unset( self
::$query[$id]['_start'] );
220 * Processes a WebRequest object
222 * @param $request WebRequest
224 public static function processRequest( WebRequest
$request ) {
225 if ( !self
::$enabled ) {
229 self
::$request = array(
230 'method' => $_SERVER['REQUEST_METHOD'],
231 'url' => $request->getRequestURL(),
232 'headers' => $request->getAllHeaders(),
233 'params' => $request->getValues(),
238 * Returns a list of files included, along with their size
240 * @param $context IContextSource
243 protected static function getFilesIncluded( IContextSource
$context ) {
244 $files = get_included_files();
246 foreach ( $files as $file ) {
247 $size = filesize( $file );
250 'size' => $context->getLanguage()->formatSize( $size ),
258 * Returns the HTML to add to the page for the toolbar
260 * @param $context IContextSource
263 public static function getDebugHTML( IContextSource
$context ) {
264 if ( !self
::$enabled ) {
268 global $wgVersion, $wgRequestTime;
269 MWDebug
::log( 'MWDebug output complete' );
271 'mwVersion' => $wgVersion,
272 'phpVersion' => PHP_VERSION
,
273 'time' => wfTime() - $wgRequestTime,
275 'debugLog' => self
::$debug,
276 'queries' => self
::$query,
277 'request' => self
::$request,
278 'memory' => $context->getLanguage()->formatSize( memory_get_usage() ),
279 'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage() ),
280 'includes' => self
::getFilesIncluded( $context ),
282 // TODO: Clean this up
283 $html = Html
::openElement( 'script' );
284 $html .= 'var debugInfo = ' . Xml
::encodeJsVar( $debugInfo ) . ';';
285 $html .= " $(function() { mw.loader.using( 'mediawiki.debug', function() { mw.Debug.init( debugInfo ) } ); }); ";
286 $html .= Html
::closeElement( 'script' );