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 * Returns internal log array
90 public static function getLog() {
95 * Clears internal log array and deprecation tracking
97 public static function clearLog() {
99 self
::$deprecationWarnings = array();
103 * Adds a warning entry to the log
106 * @param int $callerOffset
109 public static function warning( $msg, $callerOffset = 1 ) {
110 if ( !self
::$enabled ) {
114 // Check to see if there was already a deprecation notice, so not to
115 // get a duplicate warning
116 $logCount = count( self
::$log );
118 $lastLog = self
::$log[ $logCount - 1 ];
119 if ( $lastLog['type'] == 'deprecated' && $lastLog['caller'] == wfGetCaller( $callerOffset +
1 ) ) {
124 self
::$log[] = array(
125 'msg' => htmlspecialchars( $msg ),
127 'caller' => wfGetCaller( $callerOffset ),
132 * Adds a depreciation entry to the log, along with a backtrace
139 public static function deprecated( $function, $version, $component ) {
140 if ( !self
::$enabled ) {
144 // Chain: This function -> wfDeprecated -> deprecatedFunction -> caller
145 $caller = wfGetCaller( 4 );
147 // Check to see if there already was a warning about this function
148 $functionString = "$function-$caller";
149 if ( in_array( $functionString, self
::$deprecationWarnings ) ) {
153 $version = $version === false ?
'(unknown version)' : $version;
154 $component = $component === false ?
'MediaWiki' : $component;
155 $msg = htmlspecialchars( "Use of function $function was deprecated in $component $version" );
156 $msg .= Html
::rawElement( 'div', array( 'class' => 'mw-debug-backtrace' ),
157 Html
::element( 'span', array(), 'Backtrace:' )
161 self
::$deprecationWarnings[] = $functionString;
162 self
::$log[] = array(
164 'type' => 'deprecated',
170 * This is a method to pass messages from wfDebug to the pretty debugger.
171 * Do NOT use this method, use MWDebug::log or wfDebug()
175 public static function debugMsg( $str ) {
176 if ( !self
::$enabled ) {
180 self
::$debug[] = trim( $str );
184 * Begins profiling on a database query
187 * @param $function string
188 * @param $isMaster bool
189 * @return int ID number of the query to pass to queryTime or -1 if the
190 * debugger is disabled
192 public static function query( $sql, $function, $isMaster ) {
193 if ( !self
::$enabled ) {
197 self
::$query[] = array(
199 'function' => $function,
200 'master' => (bool) $isMaster,
202 '_start' => wfTime(),
205 return count( self
::$query ) - 1;
209 * Calculates how long a query took.
213 public static function queryTime( $id ) {
214 if ( $id === -1 ||
!self
::$enabled ) {
218 self
::$query[$id]['time'] = wfTime() - self
::$query[$id]['_start'];
219 unset( self
::$query[$id]['_start'] );
223 * Processes a WebRequest object
225 * @param $request WebRequest
227 public static function processRequest( WebRequest
$request ) {
228 if ( !self
::$enabled ) {
232 self
::$request = array(
233 'method' => $_SERVER['REQUEST_METHOD'],
234 'url' => $request->getRequestURL(),
235 'headers' => $request->getAllHeaders(),
236 'params' => $request->getValues(),
241 * Returns a list of files included, along with their size
243 * @param $context IContextSource
246 protected static function getFilesIncluded( IContextSource
$context ) {
247 $files = get_included_files();
249 foreach ( $files as $file ) {
250 $size = filesize( $file );
253 'size' => $context->getLanguage()->formatSize( $size ),
261 * Returns the HTML to add to the page for the toolbar
263 * @param $context IContextSource
266 public static function getDebugHTML( IContextSource
$context ) {
267 if ( !self
::$enabled ) {
271 global $wgVersion, $wgRequestTime;
272 MWDebug
::log( 'MWDebug output complete' );
274 'mwVersion' => $wgVersion,
275 'phpVersion' => PHP_VERSION
,
276 'time' => wfTime() - $wgRequestTime,
278 'debugLog' => self
::$debug,
279 'queries' => self
::$query,
280 'request' => self
::$request,
281 'memory' => $context->getLanguage()->formatSize( memory_get_usage() ),
282 'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage() ),
283 'includes' => self
::getFilesIncluded( $context ),
285 // TODO: Clean this up
286 $html = Html
::openElement( 'script' );
287 $html .= 'var debugInfo = ' . Xml
::encodeJsVar( $debugInfo ) . ';';
288 $html .= " $(function() { mw.loader.using( 'mediawiki.debug', function() { mw.Debug.init( debugInfo ) } ); }); ";
289 $html .= Html
::closeElement( 'script' );