2 * JavaScript for the new debug toolbar, enabled with $wgDebugToolbar
8 ( function ( $, mw
, undefined ) {
11 var debug
= mw
.Debug
= {
13 * Toolbar container element
20 * Object containing data for the debug toolbar
27 * Initializes the debugging pane
29 * @param {Object} data
31 init: function ( data
) {
35 // Insert the container into the DOM
36 $( 'body' ).append( this.$container
);
38 $( '.mw-debug-panelink' ).click( this.switchPane
);
42 * Switches between panes
44 * @todo Store cookie for last pane open
46 * @param {jQuery.Event} e
48 switchPane: function ( e
) {
49 var currentPaneId
= debug
.$container
.data( 'currentPane' ),
50 requestedPaneId
= $(this).parent().attr( 'id' ).substr( 9 ),
51 $currentPane
= $( '#mw-debug-pane-' + currentPaneId
),
52 $requestedPane
= $( '#mw-debug-pane-' + requestedPaneId
);
55 // Hide the current pane
56 if ( requestedPaneId
=== currentPaneId
) {
57 $currentPane
.slideUp();
58 debug
.$container
.data( 'currentPane', null );
62 debug
.$container
.data( 'currentPane', requestedPaneId
);
64 if ( currentPaneId
=== undefined || currentPaneId
=== null ) {
65 $requestedPane
.slideDown();
68 $requestedPane
.show();
73 * Constructs the HTML for the debugging toolbar
75 buildHtml: function () {
76 var $container
, panes
, id
;
78 $container
= $( '<div>' )
80 id
: 'mw-debug-container',
85 * Returns a jQuery element for a debug-bit div
90 function bitDiv( id
) {
91 return $( '<div>' ).attr({
93 'class': 'mw-debug-bit'
98 * Returns a jQuery element for a pane link
104 function paneLink( id
, text
) {
108 'class': 'mw-debug-panelink',
109 id
: 'mw-debug-' + id
+ '-link'
114 bitDiv( 'mwversion' )
115 .append( $( '<a href="//www.mediawiki.org/"></a>' ).text( 'MediaWiki' ) )
116 .append( ': ' + this.data
.mwVersion
)
117 .appendTo( $container
);
119 bitDiv( 'phpversion' )
120 .append( $( '<a href="//www.php.net/"></a>' ).text( 'PHP' ) )
121 .append( ': ' + this.data
.phpVersion
)
122 .appendTo( $container
);
125 .text( 'Time: ' + this.data
.time
.toFixed( 5 ) )
126 .appendTo( $container
);
128 .text( 'Memory: ' + this.data
.memory
)
129 .append( $( '<span title="Peak usage"></span>' ).text( ' (' + this.data
.memoryPeak
+ ')' ) )
130 .appendTo( $container
);
132 bitDiv( 'querylist' )
133 .append( paneLink( 'query', 'Queries: ' + this.data
.queries
.length
) )
134 .appendTo( $container
);
137 .append( paneLink( 'debuglog', 'Debug Log (' + this.data
.debugLog
.length
+ ' lines)' ) )
138 .appendTo( $container
);
141 .append( paneLink( 'request', 'Request' ) )
142 .appendTo( $container
);
145 .append( paneLink( 'files-includes', this.data
.includes
.length
+ ' Files Included' ) )
146 .appendTo( $container
);
149 querylist
: this.buildQueryTable(),
150 debuglog
: this.buildDebugLogTable(),
151 request
: this.buildRequestPane(),
152 includes
: this.buildIncludesPane()
155 for ( id
in panes
) {
156 if ( !panes
.hasOwnProperty( id
) ) {
162 'class': 'mw-debug-pane',
163 id
: 'mw-debug-pane-' + id
166 .appendTo( $container
);
169 this.$container
= $container
;
175 buildQueryTable: function () {
176 var $table
, i
, length
, query
;
178 $table
= $( '<table id="mw-debug-querylist"></table>' );
180 for ( i
= 0, length
= this.data
.queries
.length
; i
< length
; i
+= 1 ) {
181 query
= this.data
.queries
[i
];
184 .append( $( '<td>' ).text( i
+ 1 ) )
185 .append( $( '<td>' ).text( query
.sql
) )
187 .append( $( '<span class="mw-debug-query-time"></span>' ).text( '(' + query
.time
.toFixed( 4 ) + 'ms) ' ) )
188 .append( query
['function'] )
197 * Legacy debug log pane
199 buildDebugLogTable: function () {
200 var $list
, i
, length
, line
;
203 for ( i
= 0, length
= this.data
.debugLog
.length
; i
< length
; i
+= 1 ) {
204 line
= this.data
.debugLog
[i
];
206 .html( mw
.html
.escape( line
).replace( /\n/g, "<br />\n" ) )
214 * Request information pane
216 buildRequestPane: function () {
218 function buildTable( title
, data
) {
219 var $unit
, $table
, key
;
221 $unit
= $( '<div>' ).append( $( '<h2>' ).text( title
) );
223 $table
= $( '<table>' ).appendTo( $unit
);
226 .html( '<th>Key</th><th>Value</th>' )
229 for ( key
in data
) {
230 if ( !data
.hasOwnProperty( key
) ) {
235 .append( $( '<th>' ).text( key
) )
236 .append( $( '<td>' ).text( data
[key
] ) )
244 .text( this.data
.request
.method
+ ' ' + this.data
.request
.url
)
245 .append( buildTable( 'Headers', this.data
.request
.headers
) )
246 .append( buildTable( 'Parameters', this.data
.request
.params
) );
250 * Included files pane
252 buildIncludesPane: function () {
253 var $list
, i
, len
, file
;
257 for ( i
= 0, len
= this.data
.includes
.length
; i
< len
; i
+= 1 ) {
258 file
= this.data
.includes
[i
];
261 .prepend( $( '<span class="mw-debug-right"></span>' ).text( file
.size
) )
269 } )( jQuery
, mediaWiki
);