2 * JavaScript for the new debug toolbar, enabled with $wgDebugToolbar
8 ( function ( $, mw
, undefined ) {
11 var hovzer
= $.getFootHovzer();
13 var debug
= mw
.Debug
= {
15 * Toolbar container element
22 * Object containing data for the debug toolbar
29 * Initializes the debugging pane
31 * @param {Object} data
33 init: function ( data
) {
38 // Insert the container into the DOM
39 hovzer
.$.append( this.$container
);
42 $( '.mw-debug-panelink' ).click( this.switchPane
);
46 * Switches between panes
48 * @todo Store cookie for last pane open
50 * @param {jQuery.Event} e
52 switchPane: function ( e
) {
53 var currentPaneId
= debug
.$container
.data( 'currentPane' ),
54 requestedPaneId
= $(this).prop( 'id' ).substr( 9 ),
55 $currentPane
= $( '#mw-debug-pane-' + currentPaneId
),
56 $requestedPane
= $( '#mw-debug-pane-' + requestedPaneId
),
59 function updateHov() {
66 // Skip hash fragment handling. Prevents screen from jumping.
69 $( this ).addClass( 'current ');
70 $( '.mw-debug-panelink' ).not( this ).removeClass( 'current ');
72 // Hide the current pane
73 if ( requestedPaneId
=== currentPaneId
) {
74 $currentPane
.slideUp( updateHov
);
75 debug
.$container
.data( 'currentPane', null );
79 debug
.$container
.data( 'currentPane', requestedPaneId
);
81 if ( currentPaneId
=== undefined || currentPaneId
=== null ) {
82 $requestedPane
.slideDown( updateHov
);
85 $requestedPane
.show();
91 * Constructs the HTML for the debugging toolbar
93 buildHtml: function () {
94 var $container
, $bits
, panes
, id
;
96 $container
= $( '<div id="mw-debug-toolbar" class="mw-debug"></div>' );
98 $bits
= $( '<div class="mw-debug-bits"></div>' );
101 * Returns a jQuery element for a debug-bit div
106 function bitDiv( id
) {
107 return $( '<div>' ).attr({
108 id
: 'mw-debug-' + id
,
109 'class': 'mw-debug-bit'
115 * Returns a jQuery element for a pane link
121 function paneLabel( id
, text
) {
124 'class': 'mw-debug-panelabel',
125 href
: '#mw-debug-pane-' + id
131 * Returns a jQuery element for a debug-bit div with a for a pane link
133 * @param id CSS id snippet. Will be prefixed with 'mw-debug-'
134 * @param text Text to show
135 * @param count Optional count to show
138 function paneTriggerBitDiv( id
, text
, count
) {
140 text
= text
+ ' (' + count
+ ')';
142 return $( '<div>' ).attr({
143 id
: 'mw-debug-' + id
,
144 'class': 'mw-debug-bit mw-debug-panelink'
146 .append( paneLabel( id
, text
) )
150 paneTriggerBitDiv( 'console', 'Console', this.data
.log
.length
);
152 paneTriggerBitDiv( 'querylist', 'Queries', this.data
.queries
.length
);
154 paneTriggerBitDiv( 'debuglog', 'Debug Log', this.data
.debugLog
.length
);
156 paneTriggerBitDiv( 'request', 'Request' );
158 paneTriggerBitDiv( 'includes', 'PHP includes', this.data
.includes
.length
);
160 bitDiv( 'mwversion' )
161 .append( $( '<a href="//www.mediawiki.org/"></a>' ).text( 'MediaWiki' ) )
162 .append( ': ' + this.data
.mwVersion
);
164 bitDiv( 'phpversion' )
165 .append( $( '<a href="//www.php.net/"></a>' ).text( 'PHP' ) )
166 .append( ': ' + this.data
.phpVersion
);
169 .text( 'Time: ' + this.data
.time
.toFixed( 5 ) );
172 .text( 'Memory: ' + this.data
.memory
)
173 .append( $( '<span title="Peak usage"></span>' ).text( ' (' + this.data
.memoryPeak
+ ')' ) );
176 $bits
.appendTo( $container
);
179 console
: this.buildConsoleTable(),
180 querylist
: this.buildQueryTable(),
181 debuglog
: this.buildDebugLogTable(),
182 request
: this.buildRequestPane(),
183 includes
: this.buildIncludesPane()
186 for ( id
in panes
) {
187 if ( !panes
.hasOwnProperty( id
) ) {
193 'class': 'mw-debug-pane',
194 id
: 'mw-debug-pane-' + id
197 .appendTo( $container
);
200 this.$container
= $container
;
204 * Builds the console panel
206 buildConsoleTable: function () {
207 var $table
, entryTypeText
, i
, length
, entry
;
209 $table
= $( '<table id="mw-debug-console">' );
211 $('<colgroup>').css( 'width', /*padding=*/20 + ( 10*/
*fontSize*/
11 ) ).appendTo( $table
);
212 $('<colgroup>').appendTo( $table
);
213 $('<colgroup>').css( 'width', 350 ).appendTo( $table
);
216 entryTypeText = function( entryType
) {
217 switch ( entryType
) {
229 for ( i
= 0, length
= this.data
.log
.length
; i
< length
; i
+= 1 ) {
230 entry
= this.data
.log
[i
];
231 entry
.typeText
= entryTypeText( entry
.type
);
235 .text( entry
.typeText
)
236 .attr( 'class', 'mw-debug-console-' + entry
.type
)
238 .append( $( '<td>' ).html( entry
.msg
) )
239 .append( $( '<td>' ).text( entry
.caller
) )
249 buildQueryTable: function () {
250 var $table
, i
, length
, query
;
252 $table
= $( '<table id="mw-debug-querylist"></table>' );
255 .append( $('<th>#</th>').css( 'width', '4em' ) )
256 .append( $('<th>SQL</th>') )
257 .append( $('<th>Time</th>').css( 'width', '8em' ) )
258 .append( $('<th>Call</th>').css( 'width', '18em' ) )
261 for ( i
= 0, length
= this.data
.queries
.length
; i
< length
; i
+= 1 ) {
262 query
= this.data
.queries
[i
];
265 .append( $( '<td>' ).text( i
+ 1 ) )
266 .append( $( '<td>' ).text( query
.sql
) )
267 .append( $( '<td class="stats">' ).text( ( query
.time
* 1000 ).toFixed( 4 ) + 'ms' ) )
268 .append( $( '<td>' ).text( query
['function'] ) )
277 * Legacy debug log pane
279 buildDebugLogTable: function () {
280 var $list
, i
, length
, line
;
283 for ( i
= 0, length
= this.data
.debugLog
.length
; i
< length
; i
+= 1 ) {
284 line
= this.data
.debugLog
[i
];
286 .html( mw
.html
.escape( line
).replace( /\n/g, "<br />\n" ) )
294 * Request information pane
296 buildRequestPane: function () {
298 function buildTable( title
, data
) {
299 var $unit
, $table
, key
;
301 $unit
= $( '<div>' ).append( $( '<h2>' ).text( title
) );
303 $table
= $( '<table>' ).appendTo( $unit
);
306 .html( '<th>Key</th><th>Value</th>' )
309 for ( key
in data
) {
310 if ( !data
.hasOwnProperty( key
) ) {
315 .append( $( '<th>' ).text( key
) )
316 .append( $( '<td>' ).text( data
[key
] ) )
324 .text( this.data
.request
.method
+ ' ' + this.data
.request
.url
)
325 .append( buildTable( 'Headers', this.data
.request
.headers
) )
326 .append( buildTable( 'Parameters', this.data
.request
.params
) );
330 * Included files pane
332 buildIncludesPane: function () {
333 var $table
, i
, length
, file
;
335 $table
= $( '<table>' );
337 for ( i
= 0, length
= this.data
.includes
.length
; i
< length
; i
+= 1 ) {
338 file
= this.data
.includes
[i
];
340 .append( $( '<td>' ).text( file
.name
) )
341 .append( $( '<td class="nr">' ).text( file
.size
) )
349 } )( jQuery
, mediaWiki
);