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 * Called in Setup.php, initializes the debugger if it is enabled with
49 public static function init() {
50 global $wgDebugToolbar;
52 if ( !$wgDebugToolbar ) {
53 self
::$enabled = false;
57 RequestContext
::getMain()->getOutput()->addModules( 'mediawiki.debug' );
61 * Adds a line to the log
63 * This does nothing atm, there's not frontend for it
65 * @todo Add error and warning log type
66 * @todo Add support for passing objects
70 public static function log( $str ) {
71 if ( !self
::$enabled ) {
79 * This is a method to pass messages from wfDebug to the pretty debugger.
80 * Do NOT use this method, use MWDebug::log or wfDebug()
84 public static function debugMsg( $str ) {
85 if ( !self
::$enabled ) {
89 self
::$debug[] = trim( $str );
93 * Begins profiling on a database query
96 * @param $function string
97 * @param $isMaster bool
98 * @return int ID number of the query to pass to queryTime or -1 if the
99 * debugger is disabled
101 public static function query( $sql, $function, $isMaster ) {
102 if ( !self
::$enabled ) {
106 self
::$query[] = array(
108 'function' => $function,
109 'master' => (bool) $isMaster,
111 '_start' => wfTime(),
114 return count( self
::$query ) - 1;
118 * Calculates how long a query took.
122 public static function queryTime( $id ) {
123 if ( $id === -1 ||
!self
::$enabled ) {
127 self
::$query[$id]['time'] = wfTime() - self
::$query[$id]['_start'];
128 unset( self
::$query[$id]['_start'] );
132 * Processes a WebRequest object
134 * @param $request WebRequest
136 public static function processRequest( WebRequest
$request ) {
137 if ( !self
::$enabled ) {
141 self
::$request = array(
142 'method' => $_SERVER['REQUEST_METHOD'],
143 'url' => $request->getRequestURL(),
144 'headers' => $request->getAllHeaders(),
145 'params' => $request->getValues(),
150 * Returns a list of files included, along with their size
152 * @param $context IContextSource
155 protected static function getFilesIncluded( IContextSource
$context ) {
156 $files = get_included_files();
158 foreach ( $files as $file ) {
159 $size = filesize( $file );
162 'size' => $context->getLanguage()->formatSize( $size ),
170 * Returns the HTML to add to the page for the toolbar
172 * @param $context IContextSource
175 public static function getDebugHTML( IContextSource
$context ) {
176 if ( !self
::$enabled ) {
180 global $wgVersion, $wgRequestTime;
182 'mwVersion' => $wgVersion,
183 'phpVersion' => PHP_VERSION
,
184 'time' => wfTime() - $wgRequestTime,
186 'debugLog' => self
::$debug,
187 'queries' => self
::$query,
188 'request' => self
::$request,
189 'memory' => $context->getLanguage()->formatSize( memory_get_usage() ),
190 'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage() ),
191 'includes' => self
::getFilesIncluded( $context ),
193 // TODO: Clean this up
194 $html = Html
::openElement( 'script' );
195 $html .= 'var debugInfo = ' . Xml
::encodeJsVar( $debugInfo ) . ';';
196 $html .= " $(function() { mw.loader.using( 'mediawiki.debug', function() { mw.Debug.init( debugInfo ) } ); }); ";
197 $html .= Html
::closeElement( 'script' );