2 * JavaScript for the new debug toolbar, enabled through $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.
30 * Shouldn't be called before the document is ready
31 * (since it binds to elements on the page).
33 * @param {Object} data, defaults to 'debugInfo' from mw.config
35 init: function ( data ) {
37 this.data = data || mw.config.get( 'debugInfo' );
40 // Insert the container into the DOM
41 hovzer.$.append( this.$container );
44 $( '.mw-debug-panelink' ).click( this.switchPane );
48 * Switches between panes
50 * @todo Store cookie for last pane open
52 * @param {jQuery.Event} e
54 switchPane: function ( e ) {
55 var currentPaneId = debug.$container.data( 'currentPane' ),
56 requestedPaneId = $(this).prop( 'id' ).substr( 9 ),
57 $currentPane = $( '#mw-debug-pane-' + currentPaneId ),
58 $requestedPane = $( '#mw-debug-pane-' + requestedPaneId ),
61 function updateHov() {
68 // Skip hash fragment handling. Prevents screen from jumping.
71 $( this ).addClass( 'current ');
72 $( '.mw-debug-panelink' ).not( this ).removeClass( 'current ');
74 // Hide the current pane
75 if ( requestedPaneId === currentPaneId ) {
76 $currentPane.slideUp( updateHov );
77 debug.$container.data( 'currentPane', null );
81 debug.$container.data( 'currentPane', requestedPaneId );
83 if ( currentPaneId === undefined || currentPaneId === null ) {
84 $requestedPane.slideDown( updateHov );
87 $requestedPane.show();
93 * Constructs the HTML for the debugging toolbar
95 buildHtml: function () {
96 var $container, $bits, panes, id;
98 $container = $( '<div id="mw-debug-toolbar" class="mw-debug"></div>' );
100 $bits = $( '<div class="mw-debug-bits"></div>' );
103 * Returns a jQuery element for a debug-bit div
108 function bitDiv( id ) {
109 return $( '<div>' ).attr({
110 id: 'mw-debug-' + id,
111 'class': 'mw-debug-bit'
117 * Returns a jQuery element for a pane link
123 function paneLabel( id, text ) {
126 'class': 'mw-debug-panelabel',
127 href: '#mw-debug-pane-' + id
133 * Returns a jQuery element for a debug-bit div with a for a pane link
135 * @param id CSS id snippet. Will be prefixed with 'mw-debug-'
136 * @param text Text to show
137 * @param count Optional count to show
140 function paneTriggerBitDiv( id, text, count ) {
142 text = text + ' (' + count + ')';
144 return $( '<div>' ).attr({
145 id: 'mw-debug-' + id,
146 'class': 'mw-debug-bit mw-debug-panelink'
148 .append( paneLabel( id, text ) )
152 paneTriggerBitDiv( 'console', 'Console', this.data.log.length );
154 paneTriggerBitDiv( 'querylist', 'Queries', this.data.queries.length );
156 paneTriggerBitDiv( 'debuglog', 'Debug log', this.data.debugLog.length );
158 paneTriggerBitDiv( 'request', 'Request' );
160 paneTriggerBitDiv( 'includes', 'PHP includes', this.data.includes.length );
163 if ( this.data.gitRevision != false ) {
164 gitInfo = '(' + this.data.gitRevision.substring( 0, 7 ) + ')';
165 if ( this.data.gitViewUrl != false ) {
166 gitInfo = $( '<a></a>' ).attr( 'href', this.data.gitViewUrl ).text( gitInfo );
170 bitDiv( 'mwversion' )
171 .append( $( '<a href="//www.mediawiki.org/"></a>' ).text( 'MediaWiki' ) )
172 .append( ': ' + this.data.mwVersion + ' ' )
175 if ( this.data.gitBranch != false ) {
176 bitDiv( 'gitbranch' ).text( 'Git branch: ' + this.data.gitBranch );
179 bitDiv( 'phpversion' )
180 .append( $( '<a href="//www.php.net/"></a>' ).text( 'PHP' ) )
181 .append( ': ' + this.data.phpVersion );
184 .text( 'Time: ' + this.data.time.toFixed( 5 ) );
187 .text( 'Memory: ' + this.data.memory )
188 .append( $( '<span title="Peak usage"></span>' ).text( ' (' + this.data.memoryPeak + ')' ) );
191 $bits.appendTo( $container );
194 console: this.buildConsoleTable(),
195 querylist: this.buildQueryTable(),
196 debuglog: this.buildDebugLogTable(),
197 request: this.buildRequestPane(),
198 includes: this.buildIncludesPane()
201 for ( id in panes ) {
202 if ( !panes.hasOwnProperty( id ) ) {
208 'class': 'mw-debug-pane',
209 id: 'mw-debug-pane-' + id
212 .appendTo( $container );
215 this.$container = $container;
219 * Builds the console panel
221 buildConsoleTable: function () {
222 var $table, entryTypeText, i, length, entry;
224 $table = $( '<table id="mw-debug-console">' );
226 $('<colgroup>').css( 'width', /*padding=*/20 + ( 10*/*fontSize*/11 ) ).appendTo( $table );
227 $('<colgroup>').appendTo( $table );
228 $('<colgroup>').css( 'width', 350 ).appendTo( $table );
231 entryTypeText = function( entryType ) {
232 switch ( entryType ) {
244 for ( i = 0, length = this.data.log.length; i < length; i += 1 ) {
245 entry = this.data.log[i];
246 entry.typeText = entryTypeText( entry.type );
250 .text( entry.typeText )
251 .attr( 'class', 'mw-debug-console-' + entry.type )
253 .append( $( '<td>' ).html( entry.msg ) )
254 .append( $( '<td>' ).text( entry.caller ) )
264 buildQueryTable: function () {
265 var $table, i, length, query;
267 $table = $( '<table id="mw-debug-querylist"></table>' );
270 .append( $('<th>#</th>').css( 'width', '4em' ) )
271 .append( $('<th>SQL</th>') )
272 .append( $('<th>Time</th>').css( 'width', '8em' ) )
273 .append( $('<th>Call</th>').css( 'width', '18em' ) )
276 for ( i = 0, length = this.data.queries.length; i < length; i += 1 ) {
277 query = this.data.queries[i];
280 .append( $( '<td>' ).text( i + 1 ) )
281 .append( $( '<td>' ).text( query.sql ) )
282 .append( $( '<td class="stats">' ).text( ( query.time * 1000 ).toFixed( 4 ) + 'ms' ) )
283 .append( $( '<td>' ).text( query['function'] ) )
292 * Legacy debug log pane
294 buildDebugLogTable: function () {
295 var $list, i, length, line;
298 for ( i = 0, length = this.data.debugLog.length; i < length; i += 1 ) {
299 line = this.data.debugLog[i];
301 .html( mw.html.escape( line ).replace( /\n/g, "<br />\n" ) )
309 * Request information pane
311 buildRequestPane: function () {
313 function buildTable( title, data ) {
314 var $unit, $table, key;
316 $unit = $( '<div>' ).append( $( '<h2>' ).text( title ) );
318 $table = $( '<table>' ).appendTo( $unit );
321 .html( '<th>Key</th><th>Value</th>' )
324 for ( key in data ) {
325 if ( !data.hasOwnProperty( key ) ) {
330 .append( $( '<th>' ).text( key ) )
331 .append( $( '<td>' ).text( data[key] ) )
339 .text( this.data.request.method + ' ' + this.data.request.url )
340 .append( buildTable( 'Headers', this.data.request.headers ) )
341 .append( buildTable( 'Parameters', this.data.request.params ) );
345 * Included files pane
347 buildIncludesPane: function () {
348 var $table, i, length, file;
350 $table = $( '<table>' );
352 for ( i = 0, length = this.data.includes.length; i < length; i += 1 ) {
353 file = this.data.includes[i];
355 .append( $( '<td>' ).text( file.name ) )
356 .append( $( '<td class="nr">' ).text( file.size ) )
364 } )( jQuery, mediaWiki );