2011-12-04 18:29:57 +00:00
|
|
|
/**
|
2011-12-29 23:49:38 +00:00
|
|
|
* JavaScript for the new debug toolbar, enabled with $wgDebugToolbar
|
2011-12-04 18:29:57 +00:00
|
|
|
*
|
|
|
|
|
* @author John Du Hart
|
|
|
|
|
* @since 1.19
|
|
|
|
|
*/
|
|
|
|
|
|
2011-12-29 23:49:38 +00:00
|
|
|
( function ( $, mw, undefined ) {
|
|
|
|
|
"use strict";
|
2011-12-04 18:29:57 +00:00
|
|
|
|
|
|
|
|
var debug = mw.Debug = {
|
|
|
|
|
/**
|
|
|
|
|
* Toolbar container element
|
|
|
|
|
*
|
|
|
|
|
* @var {jQuery}
|
|
|
|
|
*/
|
|
|
|
|
$container: null,
|
|
|
|
|
|
|
|
|
|
/**
|
2011-12-29 23:49:38 +00:00
|
|
|
* Object containing data for the debug toolbar
|
2011-12-04 18:29:57 +00:00
|
|
|
*
|
2011-12-29 23:49:38 +00:00
|
|
|
* @var {Object}
|
2011-12-04 18:29:57 +00:00
|
|
|
*/
|
|
|
|
|
data: {},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initializes the debugging pane
|
|
|
|
|
*
|
2011-12-29 23:49:38 +00:00
|
|
|
* @param {Object} data
|
2011-12-04 18:29:57 +00:00
|
|
|
*/
|
2011-12-29 23:49:38 +00:00
|
|
|
init: function ( data ) {
|
2011-12-04 18:29:57 +00:00
|
|
|
this.data = data;
|
|
|
|
|
this.buildHtml();
|
|
|
|
|
|
|
|
|
|
// Insert the container into the DOM
|
|
|
|
|
$( 'body' ).append( this.$container );
|
|
|
|
|
|
|
|
|
|
$( '.mw-debug-panelink' ).click( this.switchPane );
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Switches between panes
|
|
|
|
|
*
|
|
|
|
|
* @todo Store cookie for last pane open
|
|
|
|
|
* @context {Element}
|
|
|
|
|
* @param {jQuery.Event} e
|
|
|
|
|
*/
|
2011-12-29 23:49:38 +00:00
|
|
|
switchPane: function ( e ) {
|
2011-12-04 18:29:57 +00:00
|
|
|
var currentPaneId = debug.$container.data( 'currentPane' ),
|
|
|
|
|
requestedPaneId = $(this).parent().attr( 'id' ).substr( 9 ),
|
|
|
|
|
$currentPane = $( '#mw-debug-pane-' + currentPaneId ),
|
|
|
|
|
$requestedPane = $( '#mw-debug-pane-' + requestedPaneId );
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
// Hide the current pane
|
|
|
|
|
if ( requestedPaneId === currentPaneId ) {
|
|
|
|
|
$currentPane.slideUp();
|
|
|
|
|
debug.$container.data( 'currentPane', null );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debug.$container.data( 'currentPane', requestedPaneId );
|
|
|
|
|
|
|
|
|
|
if ( currentPaneId === undefined || currentPaneId === null ) {
|
|
|
|
|
$requestedPane.slideDown();
|
|
|
|
|
} else {
|
|
|
|
|
$currentPane.hide();
|
|
|
|
|
$requestedPane.show();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs the HTML for the debugging toolbar
|
|
|
|
|
*/
|
2011-12-29 23:49:38 +00:00
|
|
|
buildHtml: function () {
|
|
|
|
|
var $container, panes, id;
|
|
|
|
|
|
|
|
|
|
$container = $( '<div>' )
|
2011-12-04 18:29:57 +00:00
|
|
|
.attr({
|
|
|
|
|
id: 'mw-debug-container',
|
2011-12-29 23:49:38 +00:00
|
|
|
'class': 'mw-debug'
|
2011-12-04 18:29:57 +00:00
|
|
|
});
|
|
|
|
|
|
2011-12-15 02:26:14 +00:00
|
|
|
/**
|
|
|
|
|
* Returns a jQuery element for a debug-bit div
|
|
|
|
|
*
|
|
|
|
|
* @param id
|
|
|
|
|
* @return {jQuery}
|
|
|
|
|
*/
|
2011-12-29 23:49:38 +00:00
|
|
|
function bitDiv( id ) {
|
|
|
|
|
return $( '<div>' ).attr({
|
2011-12-15 02:26:14 +00:00
|
|
|
id: 'mw-debug-' + id,
|
2011-12-29 23:49:38 +00:00
|
|
|
'class': 'mw-debug-bit'
|
2011-12-15 02:26:14 +00:00
|
|
|
});
|
2011-12-29 23:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-15 02:26:14 +00:00
|
|
|
/**
|
|
|
|
|
* Returns a jQuery element for a pane link
|
|
|
|
|
*
|
|
|
|
|
* @param id
|
|
|
|
|
* @param text
|
|
|
|
|
* @return {jQuery}
|
|
|
|
|
*/
|
2011-12-29 23:49:38 +00:00
|
|
|
function paneLink( id, text ) {
|
|
|
|
|
return $( '<a>' )
|
2011-12-15 02:26:14 +00:00
|
|
|
.attr({
|
|
|
|
|
href: '#',
|
2011-12-29 23:49:38 +00:00
|
|
|
'class': 'mw-debug-panelink',
|
2011-12-15 02:26:14 +00:00
|
|
|
id: 'mw-debug-' + id + '-link'
|
|
|
|
|
})
|
|
|
|
|
.text( text );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bitDiv( 'mwversion' )
|
2011-12-29 23:49:38 +00:00
|
|
|
.append( $( '<a href="//www.mediawiki.org/"></a>' ).text( 'MediaWiki' ) )
|
2011-12-15 02:26:14 +00:00
|
|
|
.append( ': ' + this.data.mwVersion )
|
|
|
|
|
.appendTo( $container );
|
|
|
|
|
|
|
|
|
|
bitDiv( 'phpversion' )
|
2011-12-29 23:49:38 +00:00
|
|
|
.append( $( '<a href="//www.php.net/"></a>' ).text( 'PHP' ) )
|
2011-12-15 02:26:14 +00:00
|
|
|
.append( ': ' + this.data.phpVersion )
|
|
|
|
|
.appendTo( $container );
|
|
|
|
|
|
|
|
|
|
bitDiv( 'time' )
|
|
|
|
|
.text( 'Time: ' + this.data.time.toFixed( 5 ) )
|
|
|
|
|
.appendTo( $container );
|
|
|
|
|
bitDiv( 'memory' )
|
|
|
|
|
.text( 'Memory: ' + this.data.memory )
|
|
|
|
|
.append( $( '<span title="Peak usage"></span>' ).text( ' (' + this.data.memoryPeak + ')' ) )
|
|
|
|
|
.appendTo( $container );
|
|
|
|
|
|
|
|
|
|
bitDiv( 'querylist' )
|
|
|
|
|
.append( paneLink( 'query', 'Queries: ' + this.data.queries.length ) )
|
|
|
|
|
.appendTo( $container );
|
|
|
|
|
|
|
|
|
|
bitDiv( 'debuglog' )
|
|
|
|
|
.append( paneLink( 'debuglog', 'Debug Log (' + this.data.debugLog.length + ' lines)' ) )
|
|
|
|
|
.appendTo( $container );
|
|
|
|
|
|
|
|
|
|
bitDiv( 'request' )
|
|
|
|
|
.append( paneLink( 'request', 'Request' ) )
|
|
|
|
|
.appendTo( $container );
|
|
|
|
|
|
|
|
|
|
bitDiv( 'includes' )
|
|
|
|
|
.append( paneLink( 'files-includes', this.data.includes.length + ' Files Included' ) )
|
|
|
|
|
.appendTo( $container );
|
|
|
|
|
|
2011-12-29 23:49:38 +00:00
|
|
|
panes = {
|
|
|
|
|
querylist: this.buildQueryTable(),
|
|
|
|
|
debuglog: this.buildDebugLogTable(),
|
|
|
|
|
request: this.buildRequestPane(),
|
|
|
|
|
includes: this.buildIncludesPane()
|
2011-12-15 02:26:14 +00:00
|
|
|
};
|
|
|
|
|
|
2011-12-29 23:49:38 +00:00
|
|
|
for ( id in panes ) {
|
2011-12-15 02:26:14 +00:00
|
|
|
if ( !panes.hasOwnProperty( id ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-29 23:49:38 +00:00
|
|
|
$( '<div>' )
|
2011-12-15 02:26:14 +00:00
|
|
|
.attr({
|
2011-12-29 23:49:38 +00:00
|
|
|
'class': 'mw-debug-pane',
|
2011-12-15 02:26:14 +00:00
|
|
|
id: 'mw-debug-pane-' + id
|
|
|
|
|
})
|
|
|
|
|
.append( panes[id] )
|
|
|
|
|
.appendTo( $container );
|
|
|
|
|
}
|
2011-12-29 23:49:38 +00:00
|
|
|
|
|
|
|
|
this.$container = $container;
|
2011-12-04 18:29:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Query list pane
|
|
|
|
|
*/
|
2011-12-29 23:49:38 +00:00
|
|
|
buildQueryTable: function () {
|
|
|
|
|
var $table, i, length, query;
|
|
|
|
|
|
|
|
|
|
$table = $( '<table id="mw-debug-querylist"></table>' );
|
2011-12-04 18:29:57 +00:00
|
|
|
|
2011-12-29 23:49:38 +00:00
|
|
|
for ( i = 0, length = this.data.queries.length; i < length; i += 1 ) {
|
|
|
|
|
query = this.data.queries[i];
|
2011-12-04 18:29:57 +00:00
|
|
|
|
2011-12-15 02:26:14 +00:00
|
|
|
$( '<tr>' )
|
|
|
|
|
.append( $( '<td>' ).text( i + 1 ) )
|
|
|
|
|
.append( $( '<td>' ).text( query.sql ) )
|
|
|
|
|
.append( $( '<td>' )
|
2011-12-29 23:49:38 +00:00
|
|
|
.append( $( '<span class="mw-debug-query-time"></span>' ).text( '(' + query.time.toFixed( 4 ) + 'ms) ' ) )
|
2011-12-15 02:26:14 +00:00
|
|
|
.append( query['function'] )
|
|
|
|
|
)
|
|
|
|
|
.appendTo( $table );
|
2011-12-04 18:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-15 02:26:14 +00:00
|
|
|
return $table;
|
2011-12-04 18:29:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Legacy debug log pane
|
|
|
|
|
*/
|
2011-12-29 23:49:38 +00:00
|
|
|
buildDebugLogTable: function () {
|
|
|
|
|
var $list, i, length, line;
|
|
|
|
|
$list = $( '<ul>' );
|
2011-12-04 18:29:57 +00:00
|
|
|
|
2011-12-29 23:49:38 +00:00
|
|
|
for ( i = 0, length = this.data.debugLog.length; i < length; i += 1 ) {
|
|
|
|
|
line = this.data.debugLog[i];
|
2011-12-15 02:26:14 +00:00
|
|
|
$( '<li>' )
|
|
|
|
|
.html( mw.html.escape( line ).replace( /\n/g, "<br />\n" ) )
|
|
|
|
|
.appendTo( $list );
|
2011-12-04 18:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-15 02:26:14 +00:00
|
|
|
return $list;
|
2011-12-04 18:29:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Request information pane
|
|
|
|
|
*/
|
2011-12-29 23:49:38 +00:00
|
|
|
buildRequestPane: function () {
|
2011-12-15 02:26:14 +00:00
|
|
|
|
2011-12-29 23:49:38 +00:00
|
|
|
function buildTable( title, data ) {
|
|
|
|
|
var $unit, $table, key;
|
|
|
|
|
|
|
|
|
|
$unit = $( '<div>' ).append( $( '<h2>' ).text( title ) );
|
|
|
|
|
|
|
|
|
|
$table = $( '<table>' ).appendTo( $unit );
|
2011-12-15 02:26:14 +00:00
|
|
|
|
|
|
|
|
$( '<tr>' )
|
2011-12-29 23:49:38 +00:00
|
|
|
.html( '<th>Key</th><th>Value</th>' )
|
2011-12-15 02:26:14 +00:00
|
|
|
.appendTo( $table );
|
2011-12-04 18:29:57 +00:00
|
|
|
|
2011-12-29 23:49:38 +00:00
|
|
|
for ( key in data ) {
|
2011-12-04 18:29:57 +00:00
|
|
|
if ( !data.hasOwnProperty( key ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-15 02:26:14 +00:00
|
|
|
$( '<tr>' )
|
|
|
|
|
.append( $( '<th>' ).text( key ) )
|
|
|
|
|
.append( $( '<td>' ).text( data[key] ) )
|
|
|
|
|
.appendTo( $table );
|
2011-12-04 18:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-15 02:26:14 +00:00
|
|
|
return $unit;
|
2011-12-29 23:49:38 +00:00
|
|
|
}
|
2011-12-04 18:29:57 +00:00
|
|
|
|
2011-12-29 23:49:38 +00:00
|
|
|
return $( '<div>' )
|
2011-12-15 02:26:14 +00:00
|
|
|
.text( this.data.request.method + ' ' + this.data.request.url )
|
|
|
|
|
.append( buildTable( 'Headers', this.data.request.headers ) )
|
|
|
|
|
.append( buildTable( 'Parameters', this.data.request.params ) );
|
2011-12-04 18:29:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Included files pane
|
|
|
|
|
*/
|
2011-12-29 23:49:38 +00:00
|
|
|
buildIncludesPane: function () {
|
|
|
|
|
var $list, i, len, file;
|
|
|
|
|
|
|
|
|
|
$list = $( '<ul>' );
|
2011-12-04 18:29:57 +00:00
|
|
|
|
2011-12-29 23:49:38 +00:00
|
|
|
for ( i = 0, len = this.data.includes.length; i < len; i += 1 ) {
|
|
|
|
|
file = this.data.includes[i];
|
2011-12-15 02:26:14 +00:00
|
|
|
$( '<li>' )
|
|
|
|
|
.text( file.name )
|
2011-12-29 23:49:38 +00:00
|
|
|
.prepend( $( '<span class="mw-debug-right"></span>' ).text( file.size ) )
|
2011-12-15 02:26:14 +00:00
|
|
|
.appendTo( $list );
|
2011-12-04 18:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-15 02:26:14 +00:00
|
|
|
return $list;
|
2011-12-04 18:29:57 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2011-12-29 23:49:38 +00:00
|
|
|
} )( jQuery, mediaWiki );
|