4 * New debugger system that outputs a toolbar on page view
6 * @todo Clean up HTML generated by the javascript
7 * @todo Profiler support
16 protected static $log = array();
19 * Debug messages from wfDebug()
23 protected static $debug = array();
30 protected static $query = array();
37 protected static $request = array();
40 * Is the debugger enabled?
44 protected static $enabled = true;
47 * Called in Setup.php, initializes the debugger if it is enabled with
50 public static function init() {
51 global $wgDebugToolbar;
53 if ( !$wgDebugToolbar ) {
54 self
::$enabled = false;
58 RequestContext
::getMain()->getOutput()->addModules( 'mediawiki.debug' );
62 * Adds a line to the log
64 * This does nothing atm, there's not frontend for it
66 * @todo Add error and warning log type
67 * @todo Add support for passing objects
71 public static function log( $str ) {
72 if ( !self
::$enabled ) {
80 * This is a method to pass messages from wfDebug to the pretty debugger.
81 * Do NOT use this method, use MWDebug::log or wfDebug()
85 public static function debugMsg( $str ) {
86 if ( !self
::$enabled ) {
90 self
::$debug[] = trim( $str );
94 * Begins profiling on a database query
97 * @param $function string
98 * @param $isMaster bool
99 * @return int ID number of the query to pass to queryTime or -1 if the
100 * debugger is disabled
102 public static function query( $sql, $function, $isMaster ) {
103 if ( !self
::$enabled ) {
107 self
::$query[] = array(
109 'function' => $function,
110 'master' => (bool) $isMaster,
112 '_start' => microtime( true ),
115 return count( self
::$query ) - 1;
119 * Calculates how long a query took.
123 public static function queryTime( $id ) {
124 if ( $id === -1 ||
!self
::$enabled ) {
128 self
::$query[$id]['time'] = microtime( true ) - self
::$query[$id]['_start'];
129 unset( self
::$query[$id]['_start'] );
133 * Processes a WebRequest object
135 * @param $request WebRequest
137 public static function processRequest( WebRequest
$request ) {
138 if ( !self
::$enabled ) {
142 self
::$request = array(
143 'method' => $_SERVER['REQUEST_METHOD'],
144 'url' => $request->getRequestURL(),
145 'headers' => $request->getAllHeaders(),
146 'params' => $request->getValues(),
151 * Returns a list of files included, along with their size
155 protected static function getFilesIncluded() {
156 $files = get_included_files();
158 foreach ( $files as $file ) {
159 $size = filesize( $file );
162 'size' => self
::formatBytes( $size ),
170 * Returns the HTML to add to the page for the toolbar
174 public static function getDebugHTML() {
175 if ( !self
::$enabled ) {
179 global $wgVersion, $wgRequestTime;
181 'mwVersion' => $wgVersion,
182 'phpVersion' => PHP_VERSION
,
183 'time' => microtime( true ) - $wgRequestTime,
185 'debugLog' => self
::$debug,
186 'queries' => self
::$query,
187 'request' => self
::$request,
188 'memory' => self
::formatBytes( memory_get_usage() ),
189 'memoryPeak' => self
::formatBytes( memory_get_peak_usage() ),
190 'includes' => self
::getFilesIncluded(),
192 // TODO: Clean this up
193 $html = Html
::openElement( 'script' );
194 $html .= 'var debugInfo = ' . Xml
::encodeJsVar( $debugInfo ) . ';';
195 $html .= " $(function() { mw.loader.using( 'mediawiki.debug', function() { mw.Debug.init( debugInfo ) } ); }); ";
196 $html .= Html
::closeElement( 'script' );
202 * Formats raw bytes integer into a human readable format
204 * @author John Himmelman - http://stackoverflow.com/a/2510540/343911
206 * @param $precision int
209 protected static function formatBytes( $size, $precision = 2 ) {
210 $base = log( $size ) / log( 1024 );
211 // If we ever use 1TB of RAM we're fucked
212 $suffixes = array( '', 'kb', 'MB', 'GB', 'TB' );
214 return round( pow( 1024, $base - floor( $base ) ), $precision ) . $suffixes[floor( $base )];