dbg toolbar query list
[lhc/web/wiklou.git] / resources / mediawiki / mediawiki.debug.js
1 /**
2 * JavaScript for the new debug toolbar, enabled with $wgDebugToolbar
3 *
4 * @author John Du Hart
5 * @since 1.19
6 */
7
8 ( function ( $, mw, undefined ) {
9 "use strict";
10
11 var hovzer = $.getFootHovzer();
12
13 var debug = mw.Debug = {
14 /**
15 * Toolbar container element
16 *
17 * @var {jQuery}
18 */
19 $container: null,
20
21 /**
22 * Object containing data for the debug toolbar
23 *
24 * @var {Object}
25 */
26 data: {},
27
28 /**
29 * Initializes the debugging pane
30 *
31 * @param {Object} data
32 */
33 init: function ( data ) {
34
35 this.data = data;
36 this.buildHtml();
37
38 // Insert the container into the DOM
39 hovzer.$.append( this.$container );
40 hovzer.update();
41
42 $( '.mw-debug-panelink' ).click( this.switchPane );
43 },
44
45 /**
46 * Switches between panes
47 *
48 * @todo Store cookie for last pane open
49 * @context {Element}
50 * @param {jQuery.Event} e
51 */
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 ),
57 hovDone = false;
58
59 function updateHov() {
60 if ( !hovDone ) {
61 hovzer.update();
62 hovDone = true;
63 }
64 }
65
66 $( this ).addClass( 'current ');
67 $( '.mw-debug-panelink' ).not( this ).removeClass( 'current ');
68
69 // Hide the current pane
70 if ( requestedPaneId === currentPaneId ) {
71 $currentPane.slideUp( updateHov );
72 debug.$container.data( 'currentPane', null );
73 return;
74 }
75
76 debug.$container.data( 'currentPane', requestedPaneId );
77
78 if ( currentPaneId === undefined || currentPaneId === null ) {
79 $requestedPane.slideDown( updateHov );
80 } else {
81 $currentPane.hide();
82 $requestedPane.show();
83 updateHov();
84 }
85 },
86
87 /**
88 * Constructs the HTML for the debugging toolbar
89 */
90 buildHtml: function () {
91 var $container, $bits, panes, id;
92
93 $container = $( '<div id="mw-debug-toolbar" class="mw-debug"></div>' );
94
95 $bits = $( '<div class="mw-debug-bits"></div>' );
96
97 /**
98 * Returns a jQuery element for a debug-bit div
99 *
100 * @param id
101 * @return {jQuery}
102 */
103 function bitDiv( id ) {
104 return $( '<div>' ).attr({
105 id: 'mw-debug-' + id,
106 'class': 'mw-debug-bit'
107 })
108 .appendTo( $bits );
109 }
110
111 /**
112 * Returns a jQuery element for a pane link
113 *
114 * @param id
115 * @param text
116 * @return {jQuery}
117 */
118 function paneLabel( id, text ) {
119 return $( '<a>' )
120 .attr({
121 'class': 'mw-debug-panelabel',
122 href: '#mw-debug-pane-' + id
123 })
124 .text( text );
125 }
126
127 /**
128 * Returns a jQuery element for a debug-bit div with a for a pane link
129 *
130 * @param id
131 * @return {jQuery}
132 */
133 function paneTriggerBitDiv( id, text ) {
134 return $( '<div>' ).attr({
135 id: 'mw-debug-' + id,
136 'class': 'mw-debug-bit mw-debug-panelink'
137 })
138 .append( paneLabel( id, text ) )
139 .appendTo( $bits );
140 }
141
142 paneTriggerBitDiv( 'console', 'Console (' + this.data.log.length + ')' );
143
144 paneTriggerBitDiv( 'querylist', 'Queries: ' + this.data.queries.length );
145
146 paneTriggerBitDiv( 'debuglog', 'Debug Log (' + this.data.debugLog.length + ' lines)' );
147
148 paneTriggerBitDiv( 'request', 'Request' );
149
150 paneTriggerBitDiv( 'includes', this.data.includes.length + ' Files Included' );
151
152 bitDiv( 'mwversion' )
153 .append( $( '<a href="//www.mediawiki.org/"></a>' ).text( 'MediaWiki' ) )
154 .append( ': ' + this.data.mwVersion );
155
156 bitDiv( 'phpversion' )
157 .append( $( '<a href="//www.php.net/"></a>' ).text( 'PHP' ) )
158 .append( ': ' + this.data.phpVersion );
159
160 bitDiv( 'time' )
161 .text( 'Time: ' + this.data.time.toFixed( 5 ) );
162
163 bitDiv( 'memory' )
164 .text( 'Memory: ' + this.data.memory )
165 .append( $( '<span title="Peak usage"></span>' ).text( ' (' + this.data.memoryPeak + ')' ) );
166
167
168 $bits.appendTo( $container );
169
170 panes = {
171 console: this.buildConsoleTable(),
172 querylist: this.buildQueryTable(),
173 debuglog: this.buildDebugLogTable(),
174 request: this.buildRequestPane(),
175 includes: this.buildIncludesPane()
176 };
177
178 for ( id in panes ) {
179 if ( !panes.hasOwnProperty( id ) ) {
180 continue;
181 }
182
183 $( '<div>' )
184 .attr({
185 'class': 'mw-debug-pane',
186 id: 'mw-debug-pane-' + id
187 })
188 .append( panes[id] )
189 .appendTo( $container );
190 }
191
192 this.$container = $container;
193 },
194
195 /**
196 * Builds the console panel
197 */
198 buildConsoleTable: function () {
199 var $table, entryTypeText, i, length, entry;
200
201 $table = $( '<table id="mw-debug-console">' );
202
203 $('<colgroup>').css( 'width', /*padding=*/20 + ( 10*/*fontSize*/11 ) ).appendTo( $table );
204 $('<colgroup>').appendTo( $table );
205 $('<colgroup>').css( 'width', 350 ).appendTo( $table );
206
207
208 entryTypeText = function( entryType ) {
209 switch ( entryType ) {
210 case 'log':
211 return 'Log';
212 case 'warn':
213 return 'Warning';
214 case 'deprecated':
215 return 'Deprecated';
216 default:
217 return 'Unknown';
218 }
219 };
220
221 for ( i = 0, length = this.data.log.length; i < length; i += 1 ) {
222 entry = this.data.log[i];
223 entry.typeText = entryTypeText( entry.type );
224
225 $( '<tr>' )
226 .append( $( '<td>' )
227 .text( entry.typeText )
228 .attr( 'class', 'mw-debug-console-' + entry.type )
229 )
230 .append( $( '<td>' ).html( entry.msg ) )
231 .append( $( '<td>' ).text( entry.caller ) )
232 .appendTo( $table );
233 }
234
235 return $table;
236 },
237
238 /**
239 * Query list pane
240 */
241 buildQueryTable: function () {
242 var $table, i, length, query;
243
244 $table = $( '<table id="mw-debug-querylist"></table>' );
245
246 $( '<tr>' )
247 .append( $('<th>#</th>').css( 'width', '4em' ) )
248 .append( $('<th>SQL</th>') )
249 .append( $('<th>Time</th>').css( 'width', '8em' ) )
250 .append( $('<th>Call</th>').css( 'width', '12em' ) )
251 .appendTo( $table );
252
253 for ( i = 0, length = this.data.queries.length; i < length; i += 1 ) {
254 query = this.data.queries[i];
255
256 $( '<tr>' )
257 .append( $( '<td>' ).text( i + 1 ) )
258 .append( $( '<td>' ).text( query.sql ) )
259 .append( $( '<td class="stats">' ).text( query.time.toFixed( 4 )+ 'ms' ) )
260 .append( $( '<td>' ).text( query['function'] ) )
261 .appendTo( $table );
262 }
263
264
265 return $table;
266 },
267
268 /**
269 * Legacy debug log pane
270 */
271 buildDebugLogTable: function () {
272 var $list, i, length, line;
273 $list = $( '<ul>' );
274
275 for ( i = 0, length = this.data.debugLog.length; i < length; i += 1 ) {
276 line = this.data.debugLog[i];
277 $( '<li>' )
278 .html( mw.html.escape( line ).replace( /\n/g, "<br />\n" ) )
279 .appendTo( $list );
280 }
281
282 return $list;
283 },
284
285 /**
286 * Request information pane
287 */
288 buildRequestPane: function () {
289
290 function buildTable( title, data ) {
291 var $unit, $table, key;
292
293 $unit = $( '<div>' ).append( $( '<h2>' ).text( title ) );
294
295 $table = $( '<table>' ).appendTo( $unit );
296
297 $( '<tr>' )
298 .html( '<th>Key</th><th>Value</th>' )
299 .appendTo( $table );
300
301 for ( key in data ) {
302 if ( !data.hasOwnProperty( key ) ) {
303 continue;
304 }
305
306 $( '<tr>' )
307 .append( $( '<th>' ).text( key ) )
308 .append( $( '<td>' ).text( data[key] ) )
309 .appendTo( $table );
310 }
311
312 return $unit;
313 }
314
315 return $( '<div>' )
316 .text( this.data.request.method + ' ' + this.data.request.url )
317 .append( buildTable( 'Headers', this.data.request.headers ) )
318 .append( buildTable( 'Parameters', this.data.request.params ) );
319 },
320
321 /**
322 * Included files pane
323 */
324 buildIncludesPane: function () {
325 var $table, i, length, file;
326
327 $table = $( '<table>' );
328
329 for ( i = 0, length = this.data.includes.length; i < length; i += 1 ) {
330 file = this.data.includes[i];
331 $( '<tr>' )
332 .append( $( '<td>' ).text( file.name ) )
333 .append( $( '<td class="nr">' ).text( file.size ) )
334 .appendTo( $table );
335 }
336
337 return $table;
338 }
339 };
340
341 } )( jQuery, mediaWiki );