3 * Debug toolbar related code
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.
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.
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
24 * New debugger system that outputs a toolbar on page view
26 * By default, most methods do nothing ( self::$enabled = false ). You have
27 * to explicitly call MWDebug::init() to enabled them.
29 * @todo Profiler support
38 protected static $log = array();
41 * Debug messages from wfDebug()
45 protected static $debug = array();
52 protected static $query = array();
55 * Is the debugger enabled?
59 protected static $enabled = false;
62 * Array of functions that have already been warned, formatted
63 * function-caller to prevent a buttload of warnings
67 protected static $deprecationWarnings = array();
70 * Enabled the debugger and load resource module.
71 * This is called by Setup.php when $wgDebugToolbar is true.
73 public static function init() {
74 self
::$enabled = true;
78 * Add ResourceLoader modules to the OutputPage object if debugging is
81 * @param $out OutputPage
83 public static function addModules( OutputPage
$out ) {
84 if ( self
::$enabled ) {
85 $out->addModules( 'mediawiki.debug.init' );
90 * Adds a line to the log
92 * @todo Add support for passing objects
96 public static function log( $str ) {
97 if ( !self
::$enabled ) {
101 self
::$log[] = array(
102 'msg' => htmlspecialchars( $str ),
104 'caller' => wfGetCaller(),
109 * Returns internal log array
112 public static function getLog() {
117 * Clears internal log array and deprecation tracking
119 public static function clearLog() {
120 self
::$log = array();
121 self
::$deprecationWarnings = array();
125 * Adds a warning entry to the log
128 * @param int $callerOffset
131 public static function warning( $msg, $callerOffset = 1 ) {
132 if ( !self
::$enabled ) {
136 // Check to see if there was already a deprecation notice, so not to
137 // get a duplicate warning
138 $logCount = count( self
::$log );
140 $lastLog = self
::$log[ $logCount - 1 ];
141 if ( $lastLog['type'] == 'deprecated' && $lastLog['caller'] == wfGetCaller( $callerOffset +
1 ) ) {
146 self
::$log[] = array(
147 'msg' => htmlspecialchars( $msg ),
149 'caller' => wfGetCaller( $callerOffset ),
154 * Adds a depreciation entry to the log, along with a backtrace
161 public static function deprecated( $function, $version, $component ) {
162 if ( !self
::$enabled ) {
166 // Chain: This function -> wfDeprecated -> deprecatedFunction -> caller
167 $caller = wfGetCaller( 4 );
169 // Check to see if there already was a warning about this function
170 $functionString = "$function-$caller";
171 if ( in_array( $functionString, self
::$deprecationWarnings ) ) {
175 $version = $version === false ?
'(unknown version)' : $version;
176 $component = $component === false ?
'MediaWiki' : $component;
177 $msg = htmlspecialchars( "Use of function $function was deprecated in $component $version" );
178 $msg .= Html
::rawElement( 'div', array( 'class' => 'mw-debug-backtrace' ),
179 Html
::element( 'span', array(), 'Backtrace:' )
183 self
::$deprecationWarnings[] = $functionString;
184 self
::$log[] = array(
186 'type' => 'deprecated',
192 * This is a method to pass messages from wfDebug to the pretty debugger.
193 * Do NOT use this method, use MWDebug::log or wfDebug()
197 public static function debugMsg( $str ) {
198 if ( !self
::$enabled ) {
202 self
::$debug[] = trim( $str );
206 * Begins profiling on a database query
209 * @param $function string
210 * @param $isMaster bool
211 * @return int ID number of the query to pass to queryTime or -1 if the
212 * debugger is disabled
214 public static function query( $sql, $function, $isMaster ) {
215 if ( !self
::$enabled ) {
219 self
::$query[] = array(
221 'function' => $function,
222 'master' => (bool) $isMaster,
224 '_start' => microtime( true ),
227 return count( self
::$query ) - 1;
231 * Calculates how long a query took.
235 public static function queryTime( $id ) {
236 if ( $id === -1 ||
!self
::$enabled ) {
240 self
::$query[$id]['time'] = microtime( true ) - self
::$query[$id]['_start'];
241 unset( self
::$query[$id]['_start'] );
245 * Returns a list of files included, along with their size
247 * @param $context IContextSource
250 protected static function getFilesIncluded( IContextSource
$context ) {
251 $files = get_included_files();
253 foreach ( $files as $file ) {
254 $size = filesize( $file );
257 'size' => $context->getLanguage()->formatSize( $size ),
265 * Returns the HTML to add to the page for the toolbar
267 * @param $context IContextSource
270 public static function getDebugHTML( IContextSource
$context ) {
271 if ( !self
::$enabled ) {
275 global $wgVersion, $wgRequestTime;
276 MWDebug
::log( 'MWDebug output complete' );
277 $request = $context->getRequest();
279 'mwVersion' => $wgVersion,
280 'phpVersion' => PHP_VERSION
,
281 'gitRevision' => GitInfo
::headSHA1(),
282 'gitBranch' => GitInfo
::currentBranch(),
283 'gitViewUrl' => GitInfo
::headViewUrl(),
284 'time' => microtime( true ) - $wgRequestTime,
286 'debugLog' => self
::$debug,
287 'queries' => self
::$query,
289 'method' => $_SERVER['REQUEST_METHOD'],
290 'url' => $request->getRequestURL(),
291 'headers' => $request->getAllHeaders(),
292 'params' => $request->getValues(),
294 'memory' => $context->getLanguage()->formatSize( memory_get_usage() ),
295 'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage() ),
296 'includes' => self
::getFilesIncluded( $context ),
299 // Cannot use OutputPage::addJsConfigVars because those are already outputted
300 // by the time this method is called.
301 $html = Html
::inlineScript(
302 ResourceLoader
::makeLoaderConditionalScript(
303 ResourceLoader
::makeConfigSetScript( array( 'debugInfo' => $debugInfo ) )