Replace get{Local,Full,Link,Canonical}URL's $variant argument with a secondary $query...
[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 Clean up HTML generated by the javascript
7 * @todo Profiler support
8 */
9 class MWDebug {
10
11 /**
12 * Log lines
13 *
14 * @var array
15 */
16 protected static $log = array();
17
18 /**
19 * Debug messages from wfDebug()
20 *
21 * @var array
22 */
23 protected static $debug = array();
24
25 /**
26 * Queries
27 *
28 * @var array
29 */
30 protected static $query = array();
31
32 /**
33 * Request information
34 *
35 * @var array
36 */
37 protected static $request = array();
38
39 /**
40 * Is the debugger enabled?
41 *
42 * @var bool
43 */
44 protected static $enabled = true;
45
46 /**
47 * Called in Setup.php, initializes the debugger if it is enabled with
48 * $wgDebugToolbar
49 */
50 public static function init() {
51 global $wgDebugToolbar;
52
53 if ( !$wgDebugToolbar ) {
54 self::$enabled = false;
55 return;
56 }
57
58 RequestContext::getMain()->getOutput()->addModules( 'mediawiki.debug' );
59 }
60
61 /**
62 * Adds a line to the log
63 *
64 * This does nothing atm, there's not frontend for it
65 *
66 * @todo Add error and warning log type
67 * @todo Add support for passing objects
68 *
69 * @param $str string
70 */
71 public static function log( $str ) {
72 if ( !self::$enabled ) {
73 return;
74 }
75
76 self::$log[] = $str;
77 }
78
79 /**
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()
82 *
83 * @param $str string
84 */
85 public static function debugMsg( $str ) {
86 if ( !self::$enabled ) {
87 return;
88 }
89
90 self::$debug[] = trim( $str );
91 }
92
93 /**
94 * Begins profiling on a database query
95 *
96 * @param $sql string
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
101 */
102 public static function query( $sql, $function, $isMaster ) {
103 if ( !self::$enabled ) {
104 return -1;
105 }
106
107 self::$query[] = array(
108 'sql' => $sql,
109 'function' => $function,
110 'master' => (bool) $isMaster,
111 'time' > 0.0,
112 '_start' => microtime( true ),
113 );
114
115 return count( self::$query ) - 1;
116 }
117
118 /**
119 * Calculates how long a query took.
120 *
121 * @param $id int
122 */
123 public static function queryTime( $id ) {
124 if ( $id === -1 || !self::$enabled ) {
125 return;
126 }
127
128 self::$query[$id]['time'] = microtime( true ) - self::$query[$id]['_start'];
129 unset( self::$query[$id]['_start'] );
130 }
131
132 /**
133 * Processes a WebRequest object
134 *
135 * @param $request WebRequest
136 */
137 public static function processRequest( WebRequest $request ) {
138 if ( !self::$enabled ) {
139 return;
140 }
141
142 self::$request = array(
143 'method' => $_SERVER['REQUEST_METHOD'],
144 'url' => $request->getRequestURL(),
145 'headers' => $request->getAllHeaders(),
146 'params' => $request->getValues(),
147 );
148 }
149
150 /**
151 * Returns a list of files included, along with their size
152 *
153 * @return array
154 */
155 protected static function getFilesIncluded() {
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' => self::formatBytes( $size ),
163 );
164 }
165
166 return $fileList;
167 }
168
169 /**
170 * Returns the HTML to add to the page for the toolbar
171 *
172 * @return string
173 */
174 public static function getDebugHTML() {
175 if ( !self::$enabled ) {
176 return '';
177 }
178
179 global $wgVersion, $wgRequestTime;
180 $debugInfo = array(
181 'mwVersion' => $wgVersion,
182 'phpVersion' => PHP_VERSION,
183 'time' => microtime( true ) - $wgRequestTime,
184 'log' => self::$log,
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(),
191 );
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' );
197
198 return $html;
199 }
200
201 /**
202 * Formats raw bytes integer into a human readable format
203 *
204 * @author John Himmelman - http://stackoverflow.com/a/2510540/343911
205 * @param $size int
206 * @param $precision int
207 * @return string
208 */
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' );
213
214 return round( pow( 1024, $base - floor( $base ) ), $precision ) . $suffixes[floor( $base )];
215 }
216 }