Revert to arbitrarily old point before initial remote branch creation to help clean up
[lhc/web/wiklou.git] / includes / debug / Debug.php
1 <?php
2 /**
3 * Debug toolbar related code
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * New debugger system that outputs a toolbar on page view
25 *
26 * By default, most methods do nothing ( self::$enabled = false ). You have
27 * to explicitly call MWDebug::init() to enabled them.
28 *
29 * @todo Profiler support
30 *
31 * @since 1.19
32 */
33 class MWDebug {
34
35 /**
36 * Log lines
37 *
38 * @var array
39 */
40 protected static $log = array();
41
42 /**
43 * Debug messages from wfDebug()
44 *
45 * @var array
46 */
47 protected static $debug = array();
48
49 /**
50 * Queries
51 *
52 * @var array
53 */
54 protected static $query = array();
55
56 /**
57 * Is the debugger enabled?
58 *
59 * @var bool
60 */
61 protected static $enabled = false;
62
63 /**
64 * Array of functions that have already been warned, formatted
65 * function-caller to prevent a buttload of warnings
66 *
67 * @var array
68 */
69 protected static $deprecationWarnings = array();
70
71 /**
72 * Enabled the debugger and load resource module.
73 * This is called by Setup.php when $wgDebugToolbar is true.
74 *
75 * @since 1.19
76 */
77 public static function init() {
78 self::$enabled = true;
79 }
80
81 /**
82 * Add ResourceLoader modules to the OutputPage object if debugging is
83 * enabled.
84 *
85 * @since 1.19
86 * @param $out OutputPage
87 */
88 public static function addModules( OutputPage $out ) {
89 if ( self::$enabled ) {
90 $out->addModules( 'mediawiki.debug.init' );
91 }
92 }
93
94 /**
95 * Adds a line to the log
96 *
97 * @todo Add support for passing objects
98 *
99 * @since 1.19
100 * @param $str string
101 */
102 public static function log( $str ) {
103 if ( !self::$enabled ) {
104 return;
105 }
106
107 self::$log[] = array(
108 'msg' => htmlspecialchars( $str ),
109 'type' => 'log',
110 'caller' => wfGetCaller(),
111 );
112 }
113
114 /**
115 * Returns internal log array
116 * @since 1.19
117 * @return array
118 */
119 public static function getLog() {
120 return self::$log;
121 }
122
123 /**
124 * Clears internal log array and deprecation tracking
125 * @since 1.19
126 */
127 public static function clearLog() {
128 self::$log = array();
129 self::$deprecationWarnings = array();
130 }
131
132 /**
133 * Adds a warning entry to the log
134 *
135 * @since 1.19
136 * @param $msg
137 * @param int $callerOffset
138 * @return mixed
139 */
140 public static function warning( $msg, $callerOffset = 1 ) {
141 if ( !self::$enabled ) {
142 return;
143 }
144
145 // Check to see if there was already a deprecation notice, so not to
146 // get a duplicate warning
147 $logCount = count( self::$log );
148 if ( $logCount ) {
149 $lastLog = self::$log[ $logCount - 1 ];
150 if ( $lastLog['type'] == 'deprecated' && $lastLog['caller'] == wfGetCaller( $callerOffset + 1 ) ) {
151 return;
152 }
153 }
154
155 self::$log[] = array(
156 'msg' => htmlspecialchars( $msg ),
157 'type' => 'warn',
158 'caller' => wfGetCaller( $callerOffset ),
159 );
160 }
161
162 /**
163 * Adds a depreciation entry to the log, along with a backtrace
164 *
165 * @since 1.19
166 * @param $function
167 * @param $version
168 * @param $component
169 * @return mixed
170 */
171 public static function deprecated( $function, $version, $component ) {
172 if ( !self::$enabled ) {
173 return;
174 }
175
176 // Chain: This function -> wfDeprecated -> deprecatedFunction -> caller
177 $caller = wfGetCaller( 4 );
178
179 // Check to see if there already was a warning about this function
180 $functionString = "$function-$caller";
181 if ( in_array( $functionString, self::$deprecationWarnings ) ) {
182 return;
183 }
184
185 $version = $version === false ? '(unknown version)' : $version;
186 $component = $component === false ? 'MediaWiki' : $component;
187 $msg = htmlspecialchars( "Use of function $function was deprecated in $component $version" );
188 $msg .= Html::rawElement( 'div', array( 'class' => 'mw-debug-backtrace' ),
189 Html::element( 'span', array(), 'Backtrace:' )
190 . wfBacktrace()
191 );
192
193 self::$deprecationWarnings[] = $functionString;
194 self::$log[] = array(
195 'msg' => $msg,
196 'type' => 'deprecated',
197 'caller' => $caller,
198 );
199 }
200
201 /**
202 * This is a method to pass messages from wfDebug to the pretty debugger.
203 * Do NOT use this method, use MWDebug::log or wfDebug()
204 *
205 * @since 1.19
206 * @param $str string
207 */
208 public static function debugMsg( $str ) {
209 if ( !self::$enabled ) {
210 return;
211 }
212
213 self::$debug[] = trim( $str );
214 }
215
216 /**
217 * Begins profiling on a database query
218 *
219 * @since 1.19
220 * @param $sql string
221 * @param $function string
222 * @param $isMaster bool
223 * @return int ID number of the query to pass to queryTime or -1 if the
224 * debugger is disabled
225 */
226 public static function query( $sql, $function, $isMaster ) {
227 if ( !self::$enabled ) {
228 return -1;
229 }
230
231 self::$query[] = array(
232 'sql' => $sql,
233 'function' => $function,
234 'master' => (bool) $isMaster,
235 'time' => 0.0,
236 '_start' => microtime( true ),
237 );
238
239 return count( self::$query ) - 1;
240 }
241
242 /**
243 * Calculates how long a query took.
244 *
245 * @since 1.19
246 * @param $id int
247 */
248 public static function queryTime( $id ) {
249 if ( $id === -1 || !self::$enabled ) {
250 return;
251 }
252
253 self::$query[$id]['time'] = microtime( true ) - self::$query[$id]['_start'];
254 unset( self::$query[$id]['_start'] );
255 }
256
257 /**
258 * Returns a list of files included, along with their size
259 *
260 * @param $context IContextSource
261 * @return array
262 */
263 protected static function getFilesIncluded( IContextSource $context ) {
264 $files = get_included_files();
265 $fileList = array();
266 foreach ( $files as $file ) {
267 $size = filesize( $file );
268 $fileList[] = array(
269 'name' => $file,
270 'size' => $context->getLanguage()->formatSize( $size ),
271 );
272 }
273
274 return $fileList;
275 }
276
277 /**
278 * Returns the HTML to add to the page for the toolbar
279 *
280 * @since 1.19
281 * @param $context IContextSource
282 * @return string
283 */
284 public static function getDebugHTML( IContextSource $context ) {
285 if ( !self::$enabled ) {
286 return '';
287 }
288
289 MWDebug::log( 'MWDebug output complete' );
290 $debugInfo = self::getDebugInfo( $context );
291
292 // Cannot use OutputPage::addJsConfigVars because those are already outputted
293 // by the time this method is called.
294 $html = Html::inlineScript(
295 ResourceLoader::makeLoaderConditionalScript(
296 ResourceLoader::makeConfigSetScript( array( 'debugInfo' => $debugInfo ) )
297 )
298 );
299
300 return $html;
301 }
302
303 /**
304 * Append the debug info to given ApiResult
305 *
306 * @param $context IContextSource
307 * @param $result ApiResult
308 */
309 public static function appendDebugInfoToApiResult( IContextSource $context, ApiResult $result ) {
310 if ( !self::$enabled ) {
311 return;
312 }
313
314 MWDebug::log( 'MWDebug output complete' );
315 $debugInfo = self::getDebugInfo( $context );
316
317 $result->setIndexedTagName( $debugInfo, 'debuginfo' );
318 $result->setIndexedTagName( $debugInfo['log'], 'line' );
319 foreach( $debugInfo['debugLog'] as $index => $debugLogText ) {
320 $vals = array();
321 ApiResult::setContent( $vals, $debugLogText );
322 $debugInfo['debugLog'][$index] = $vals; //replace
323 }
324 $result->setIndexedTagName( $debugInfo['debugLog'], 'msg' );
325 $result->setIndexedTagName( $debugInfo['queries'], 'query' );
326 $result->setIndexedTagName( $debugInfo['includes'], 'queries' );
327 $result->addValue( array(), 'debuginfo', $debugInfo );
328 }
329
330 /**
331 * Returns the HTML to add to the page for the toolbar
332 *
333 * @param $context IContextSource
334 * @return array
335 */
336 public static function getDebugInfo( IContextSource $context ) {
337 if ( !self::$enabled ) {
338 return array();
339 }
340
341 global $wgVersion, $wgRequestTime;
342 $request = $context->getRequest();
343 return array(
344 'mwVersion' => $wgVersion,
345 'phpVersion' => PHP_VERSION,
346 'gitRevision' => GitInfo::headSHA1(),
347 'gitBranch' => GitInfo::currentBranch(),
348 'gitViewUrl' => GitInfo::headViewUrl(),
349 'time' => microtime( true ) - $wgRequestTime,
350 'log' => self::$log,
351 'debugLog' => self::$debug,
352 'queries' => self::$query,
353 'request' => array(
354 'method' => $_SERVER['REQUEST_METHOD'],
355 'url' => $request->getRequestURL(),
356 'headers' => $request->getAllHeaders(),
357 'params' => $request->getValues(),
358 ),
359 'memory' => $context->getLanguage()->formatSize( memory_get_usage() ),
360 'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage() ),
361 'includes' => self::getFilesIncluded( $context ),
362 );
363 }
364 }