d56b65bc2381e3da499098241afde0b39f0c3e78
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 if ( count( self
::$log ) ) {
117 $lastLog = self
::$log[ count( self
::$log ) - 1 ];
118 if ( $lastLog['type'] == 'deprecated' && $lastLog['caller'] == wfGetCaller( $callerOffset +
1 ) ) {
123 self
::$log[] = array(
124 'msg' => htmlspecialchars( $msg ),
126 'caller' => wfGetCaller( $callerOffset ),
131 * Adds a depreciation entry to the log, along with a backtrace
138 public static function deprecated( $function, $version, $component ) {
139 if ( !self
::$enabled ) {
143 // Chain: This function -> wfDeprecated -> deprecatedFunction -> caller
144 $caller = wfGetCaller( 4 );
146 // Check to see if there already was a warning about this function
147 $functionString = "$function-$caller";
148 if ( in_array( $functionString, self
::$deprecationWarnings ) ) {
152 $version = $version === false ?
'(unknown version)' : $version;
153 $component = $component === false ?
'MediaWiki' : $component;
154 $msg = htmlspecialchars( "Use of function $function was deprecated in $component $version" );
155 $msg .= Html
::rawElement( 'div', array( 'class' => 'mw-debug-backtrace' ),
156 Html
::element( 'span', array(), 'Backtrace:' )
160 self
::$deprecationWarnings[] = $functionString;
161 self
::$log[] = array(
163 'type' => 'deprecated',
169 * This is a method to pass messages from wfDebug to the pretty debugger.
170 * Do NOT use this method, use MWDebug::log or wfDebug()
174 public static function debugMsg( $str ) {
175 if ( !self
::$enabled ) {
179 self
::$debug[] = trim( $str );
183 * Begins profiling on a database query
186 * @param $function string
187 * @param $isMaster bool
188 * @return int ID number of the query to pass to queryTime or -1 if the
189 * debugger is disabled
191 public static function query( $sql, $function, $isMaster ) {
192 if ( !self
::$enabled ) {
196 self
::$query[] = array(
198 'function' => $function,
199 'master' => (bool) $isMaster,
201 '_start' => wfTime(),
204 return count( self
::$query ) - 1;
208 * Calculates how long a query took.
212 public static function queryTime( $id ) {
213 if ( $id === -1 ||
!self
::$enabled ) {
217 self
::$query[$id]['time'] = wfTime() - self
::$query[$id]['_start'];
218 unset( self
::$query[$id]['_start'] );
222 * Processes a WebRequest object
224 * @param $request WebRequest
226 public static function processRequest( WebRequest
$request ) {
227 if ( !self
::$enabled ) {
231 self
::$request = array(
232 'method' => $_SERVER['REQUEST_METHOD'],
233 'url' => $request->getRequestURL(),
234 'headers' => $request->getAllHeaders(),
235 'params' => $request->getValues(),
240 * Returns a list of files included, along with their size
242 * @param $context IContextSource
245 protected static function getFilesIncluded( IContextSource
$context ) {
246 $files = get_included_files();
248 foreach ( $files as $file ) {
249 $size = filesize( $file );
252 'size' => $context->getLanguage()->formatSize( $size ),
260 * Returns the HTML to add to the page for the toolbar
262 * @param $context IContextSource
265 public static function getDebugHTML( IContextSource
$context ) {
266 if ( !self
::$enabled ) {
270 global $wgVersion, $wgRequestTime;
271 MWDebug
::log( 'MWDebug output complete' );
273 'mwVersion' => $wgVersion,
274 'phpVersion' => PHP_VERSION
,
275 'time' => wfTime() - $wgRequestTime,
277 'debugLog' => self
::$debug,
278 'queries' => self
::$query,
279 'request' => self
::$request,
280 'memory' => $context->getLanguage()->formatSize( memory_get_usage() ),
281 'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage() ),
282 'includes' => self
::getFilesIncluded( $context ),
284 // TODO: Clean this up
285 $html = Html
::openElement( 'script' );
286 $html .= 'var debugInfo = ' . Xml
::encodeJsVar( $debugInfo ) . ';';
287 $html .= " $(function() { mw.loader.using( 'mediawiki.debug', function() { mw.Debug.init( debugInfo ) } ); }); ";
288 $html .= Html
::closeElement( 'script' );