Using wfTime in place of microtime
[lhc/web/wiklou.git] / includes / debug / Debug.php
1 <?php
2
3 /**
4 * New debugger system that outputs a toolbar on page view
5 *
6 * @todo Profiler support
7 */
8 class MWDebug {
9
10 /**
11 * Log lines
12 *
13 * @var array
14 */
15 protected static $log = array();
16
17 /**
18 * Debug messages from wfDebug()
19 *
20 * @var array
21 */
22 protected static $debug = array();
23
24 /**
25 * Queries
26 *
27 * @var array
28 */
29 protected static $query = array();
30
31 /**
32 * Request information
33 *
34 * @var array
35 */
36 protected static $request = array();
37
38 /**
39 * Is the debugger enabled?
40 *
41 * @var bool
42 */
43 protected static $enabled = true;
44
45 /**
46 * Called in Setup.php, initializes the debugger if it is enabled with
47 * $wgDebugToolbar
48 */
49 public static function init() {
50 global $wgDebugToolbar;
51
52 if ( !$wgDebugToolbar ) {
53 self::$enabled = false;
54 return;
55 }
56
57 RequestContext::getMain()->getOutput()->addModules( 'mediawiki.debug' );
58 }
59
60 /**
61 * Adds a line to the log
62 *
63 * This does nothing atm, there's not frontend for it
64 *
65 * @todo Add error and warning log type
66 * @todo Add support for passing objects
67 *
68 * @param $str string
69 */
70 public static function log( $str ) {
71 if ( !self::$enabled ) {
72 return;
73 }
74
75 self::$log[] = $str;
76 }
77
78 /**
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()
81 *
82 * @param $str string
83 */
84 public static function debugMsg( $str ) {
85 if ( !self::$enabled ) {
86 return;
87 }
88
89 self::$debug[] = trim( $str );
90 }
91
92 /**
93 * Begins profiling on a database query
94 *
95 * @param $sql string
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
100 */
101 public static function query( $sql, $function, $isMaster ) {
102 if ( !self::$enabled ) {
103 return -1;
104 }
105
106 self::$query[] = array(
107 'sql' => $sql,
108 'function' => $function,
109 'master' => (bool) $isMaster,
110 'time' > 0.0,
111 '_start' => wfTime(),
112 );
113
114 return count( self::$query ) - 1;
115 }
116
117 /**
118 * Calculates how long a query took.
119 *
120 * @param $id int
121 */
122 public static function queryTime( $id ) {
123 if ( $id === -1 || !self::$enabled ) {
124 return;
125 }
126
127 self::$query[$id]['time'] = wfTime() - self::$query[$id]['_start'];
128 unset( self::$query[$id]['_start'] );
129 }
130
131 /**
132 * Processes a WebRequest object
133 *
134 * @param $request WebRequest
135 */
136 public static function processRequest( WebRequest $request ) {
137 if ( !self::$enabled ) {
138 return;
139 }
140
141 self::$request = array(
142 'method' => $_SERVER['REQUEST_METHOD'],
143 'url' => $request->getRequestURL(),
144 'headers' => $request->getAllHeaders(),
145 'params' => $request->getValues(),
146 );
147 }
148
149 /**
150 * Returns a list of files included, along with their size
151 *
152 * @param $context IContextSource
153 * @return array
154 */
155 protected static function getFilesIncluded( IContextSource $context ) {
156 $files = get_included_files();
157 $fileList = array();
158 foreach ( $files as $file ) {
159 $size = filesize( $file );
160 $fileList[] = array(
161 'name' => $file,
162 'size' => $context->getLanguage()->formatSize( $size ),
163 );
164 }
165
166 return $fileList;
167 }
168
169 /**
170 * Returns the HTML to add to the page for the toolbar
171 *
172 * @param $context IContextSource
173 * @return string
174 */
175 public static function getDebugHTML( IContextSource $context ) {
176 if ( !self::$enabled ) {
177 return '';
178 }
179
180 global $wgVersion, $wgRequestTime;
181 $debugInfo = array(
182 'mwVersion' => $wgVersion,
183 'phpVersion' => PHP_VERSION,
184 'time' => wfTime() - $wgRequestTime,
185 'log' => self::$log,
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 ),
192 );
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' );
198
199 return $html;
200 }
201 }