4 * New debugger system that outputs a toolbar on page view
6 * @todo Profiler support
15 protected static $log = array();
18 * Debug messages from wfDebug()
22 protected static $debug = array();
29 protected static $query = array();
36 protected static $request = array();
39 * Is the debugger enabled?
43 protected static $enabled = true;
46 * Array of functions that have already been warned, formatted
47 * function-caller to prevent a buttload of warnings
51 protected static $deprecationWarnings = array();
54 * Called in Setup.php, initializes the debugger if it is enabled with
57 public static function init() {
58 global $wgDebugToolbar;
60 if ( !$wgDebugToolbar ) {
61 self
::$enabled = false;
65 RequestContext
::getMain()->getOutput()->addModules( 'mediawiki.debug' );
69 * Adds a line to the log
71 * @todo Add support for passing objects
75 public static function log( $str ) {
76 if ( !self
::$enabled ) {
81 'msg' => htmlspecialchars( $str ),
83 'caller' => wfGetCaller(),
88 * Adds a warning entry to the log
91 * @param int $callerOffset
94 public static function warning( $msg, $callerOffset = 1 ) {
95 if ( !self
::$enabled ) {
99 // Check to see if there was already a deprecation notice, so not to
100 // get a duplicate warning
101 if ( count( self
::$log ) ) {
102 $lastLog = self
::$log[ count( self
::$log ) - 1 ];
103 if ( $lastLog['type'] == 'deprecated' && $lastLog['caller'] == wfGetCaller( $callerOffset +
1 ) ) {
108 self
::$log[] = array(
109 'msg' => htmlspecialchars( $msg ),
111 'caller' => wfGetCaller( $callerOffset ),
116 * Adds a depreciation entry to the log, along with a backtrace
123 public static function deprecated( $function, $version, $component ) {
124 if ( !self
::$enabled ) {
128 // Chain: This function -> wfDeprecated -> deprecatedFunction -> caller
129 $caller = wfGetCaller( 4 );
131 // Check to see if there already was a warning about this function
132 $functionString = "$function-$caller";
133 if ( in_array( $functionString, self
::$deprecationWarnings ) ) {
137 $version = $version === false ?
'(unknown version)' : $version;
138 $component = $component === false ?
'MediaWiki' : $component;
139 $msg = htmlspecialchars( "Use of function $function was deprecated in $component $version" );
140 $msg .= Html
::rawElement( 'div', array( 'class' => 'mw-debug-backtrace' ),
141 Html
::element( 'span', array(), 'Backtrace:' )
145 self
::$deprecationWarnings[] = $functionString;
146 self
::$log[] = array(
148 'type' => 'deprecated',
154 * This is a method to pass messages from wfDebug to the pretty debugger.
155 * Do NOT use this method, use MWDebug::log or wfDebug()
159 public static function debugMsg( $str ) {
160 if ( !self
::$enabled ) {
164 self
::$debug[] = trim( $str );
168 * Begins profiling on a database query
171 * @param $function string
172 * @param $isMaster bool
173 * @return int ID number of the query to pass to queryTime or -1 if the
174 * debugger is disabled
176 public static function query( $sql, $function, $isMaster ) {
177 if ( !self
::$enabled ) {
181 self
::$query[] = array(
183 'function' => $function,
184 'master' => (bool) $isMaster,
186 '_start' => wfTime(),
189 return count( self
::$query ) - 1;
193 * Calculates how long a query took.
197 public static function queryTime( $id ) {
198 if ( $id === -1 ||
!self
::$enabled ) {
202 self
::$query[$id]['time'] = wfTime() - self
::$query[$id]['_start'];
203 unset( self
::$query[$id]['_start'] );
207 * Processes a WebRequest object
209 * @param $request WebRequest
211 public static function processRequest( WebRequest
$request ) {
212 if ( !self
::$enabled ) {
216 self
::$request = array(
217 'method' => $_SERVER['REQUEST_METHOD'],
218 'url' => $request->getRequestURL(),
219 'headers' => $request->getAllHeaders(),
220 'params' => $request->getValues(),
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 wfWarn( 'johnduhart is pretty cool' );
257 wfDeprecated( __METHOD__
, '1.19' );
258 MWDebug
::log( 'MWDebug output complete' );
260 'mwVersion' => $wgVersion,
261 'phpVersion' => PHP_VERSION
,
262 'time' => wfTime() - $wgRequestTime,
264 'debugLog' => self
::$debug,
265 'queries' => self
::$query,
266 'request' => self
::$request,
267 'memory' => $context->getLanguage()->formatSize( memory_get_usage() ),
268 'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage() ),
269 'includes' => self
::getFilesIncluded( $context ),
271 // TODO: Clean this up
272 $html = Html
::openElement( 'script' );
273 $html .= 'var debugInfo = ' . Xml
::encodeJsVar( $debugInfo ) . ';';
274 $html .= " $(function() { mw.loader.using( 'mediawiki.debug', function() { mw.Debug.init( debugInfo ) } ); }); ";
275 $html .= Html
::closeElement( 'script' );