32dbdc199eacd974cce54c96f8d1d8e1ff003cb0
[lhc/web/wiklou.git] / resources / src / mediawiki.debug / debug.js
1 ( function () {
2 'use strict';
3
4 var debug,
5 hovzer = $.getFootHovzer();
6
7 OO.ui.getViewportSpacing = function () {
8 return {
9 top: 0,
10 right: 0,
11 bottom: hovzer.$.outerHeight(),
12 left: 0
13 };
14 };
15
16 /**
17 * Debug toolbar.
18 *
19 * Enabled server-side through `$wgDebugToolbar`.
20 *
21 * @class mw.Debug
22 * @singleton
23 * @author John Du Hart
24 * @since 1.19
25 */
26 debug = mw.Debug = {
27 /**
28 * Toolbar container element
29 *
30 * @property {jQuery}
31 */
32 $container: null,
33
34 /**
35 * Object containing data for the debug toolbar
36 *
37 * @property {Object}
38 */
39 data: {},
40
41 /**
42 * Initialize the debugging pane
43 *
44 * Shouldn't be called before the document is ready
45 * (since it binds to elements on the page).
46 *
47 * @param {Object} [data] Defaults to 'debugInfo' from mw.config
48 */
49 init: function ( data ) {
50
51 this.data = data || mw.config.get( 'debugInfo' );
52 this.buildHtml();
53
54 // Insert the container into the DOM
55 hovzer.$.append( this.$container );
56 hovzer.update();
57
58 $( '.mw-debug-panelink' ).click( this.switchPane );
59 },
60
61 /**
62 * Switch between panes
63 *
64 * Should be called with an HTMLElement as its thisArg,
65 * because it's meant to be an event handler.
66 *
67 * TODO: Store cookie for last pane open.
68 *
69 * @param {jQuery.Event} e
70 */
71 switchPane: function ( e ) {
72 var currentPaneId = debug.$container.data( 'currentPane' ),
73 requestedPaneId = $( this ).prop( 'id' ).slice( 9 ),
74 $currentPane = $( '#mw-debug-pane-' + currentPaneId ),
75 $requestedPane = $( '#mw-debug-pane-' + requestedPaneId ),
76 hovDone = false;
77
78 function updateHov() {
79 if ( !hovDone ) {
80 hovzer.update();
81 hovDone = true;
82 }
83 }
84
85 // Skip hash fragment handling. Prevents screen from jumping.
86 e.preventDefault();
87
88 $( this ).addClass( 'current ' );
89 $( '.mw-debug-panelink' ).not( this ).removeClass( 'current ' );
90
91 // Hide the current pane
92 if ( requestedPaneId === currentPaneId ) {
93 $currentPane.slideUp( updateHov );
94 debug.$container.data( 'currentPane', null );
95 return;
96 }
97
98 debug.$container.data( 'currentPane', requestedPaneId );
99
100 if ( currentPaneId === undefined || currentPaneId === null ) {
101 $requestedPane.slideDown( updateHov );
102 } else {
103 $currentPane.hide();
104 $requestedPane.show();
105 updateHov();
106 }
107 },
108
109 /**
110 * Construct the HTML for the debugging toolbar
111 */
112 buildHtml: function () {
113 var $container, $bits, panes, id, gitInfo;
114
115 $container = $( '<div>' )
116 .attr( {
117 id: 'mw-debug-toolbar',
118 lang: 'en',
119 dir: 'ltr'
120 } )
121 .addClass( 'mw-debug' );
122
123 $bits = $( '<div>' ).addClass( 'mw-debug-bits' );
124
125 /**
126 * Returns a jQuery element for a debug-bit div
127 *
128 * @ignore
129 * @param {string} id
130 * @return {jQuery}
131 */
132 function bitDiv( id ) {
133 return $( '<div>' ).prop( {
134 id: 'mw-debug-' + id,
135 className: 'mw-debug-bit'
136 } ).appendTo( $bits );
137 }
138
139 /**
140 * Returns a jQuery element for a pane link
141 *
142 * @ignore
143 * @param {string} id
144 * @param {string} text
145 * @return {jQuery}
146 */
147 function paneLabel( id, text ) {
148 return $( '<a>' )
149 .prop( {
150 className: 'mw-debug-panelabel',
151 href: '#mw-debug-pane-' + id
152 } )
153 .text( text );
154 }
155
156 /**
157 * Returns a jQuery element for a debug-bit div with a for a pane link
158 *
159 * @ignore
160 * @param {string} id CSS id snippet. Will be prefixed with 'mw-debug-'
161 * @param {string} text Text to show
162 * @param {string} count Optional count to show
163 * @return {jQuery}
164 */
165 function paneTriggerBitDiv( id, text, count ) {
166 if ( count ) {
167 text = text + ' (' + count + ')';
168 }
169 return $( '<div>' ).prop( {
170 id: 'mw-debug-' + id,
171 className: 'mw-debug-bit mw-debug-panelink'
172 } )
173 .append( paneLabel( id, text ) )
174 .appendTo( $bits );
175 }
176
177 paneTriggerBitDiv( 'console', 'Console', this.data.log.length );
178
179 paneTriggerBitDiv( 'querylist', 'Queries', this.data.queries.length );
180
181 paneTriggerBitDiv( 'debuglog', 'Debug log', this.data.debugLog.length );
182
183 paneTriggerBitDiv( 'request', 'Request' );
184
185 paneTriggerBitDiv( 'includes', 'PHP includes', this.data.includes.length );
186
187 gitInfo = '';
188 if ( this.data.gitRevision !== false ) {
189 gitInfo = '(' + this.data.gitRevision.slice( 0, 7 ) + ')';
190 if ( this.data.gitViewUrl !== false ) {
191 gitInfo = $( '<a>' )
192 .attr( 'href', this.data.gitViewUrl )
193 .text( gitInfo );
194 }
195 }
196
197 bitDiv( 'mwversion' )
198 .append( $( '<a>' ).attr( 'href', '//www.mediawiki.org/' ).text( 'MediaWiki' ) )
199 .append( document.createTextNode( ': ' + this.data.mwVersion + ' ' ) )
200 .append( gitInfo );
201
202 if ( this.data.gitBranch !== false ) {
203 bitDiv( 'gitbranch' ).text( 'Git branch: ' + this.data.gitBranch );
204 }
205
206 bitDiv( 'phpversion' )
207 .append( this.data.phpEngine === 'HHVM' ?
208 $( '<a>' ).attr( 'href', 'https://hhvm.com/' ).text( 'HHVM' ) :
209 $( '<a>' ).attr( 'href', 'https://php.net/' ).text( 'PHP' )
210 )
211 .append( ': ' + this.data.phpVersion );
212
213 bitDiv( 'time' )
214 .text( 'Time: ' + this.data.time.toFixed( 5 ) );
215
216 bitDiv( 'memory' )
217 .text( 'Memory: ' + this.data.memory + ' (Peak: ' + this.data.memoryPeak + ')' );
218
219 $bits.appendTo( $container );
220
221 panes = {
222 console: this.buildConsoleTable(),
223 querylist: this.buildQueryTable(),
224 debuglog: this.buildDebugLogTable(),
225 request: this.buildRequestPane(),
226 includes: this.buildIncludesPane()
227 };
228
229 for ( id in panes ) {
230 $( '<div>' )
231 .prop( {
232 className: 'mw-debug-pane',
233 id: 'mw-debug-pane-' + id
234 } )
235 .append( panes[ id ] )
236 .appendTo( $container );
237 }
238
239 this.$container = $container;
240 },
241
242 /**
243 * Build the console panel
244 *
245 * @return {jQuery} Console panel
246 */
247 buildConsoleTable: function () {
248 var $table, entryTypeText, i, length, entry;
249
250 $table = $( '<table>' ).attr( 'id', 'mw-debug-console' );
251
252 $( '<colgroup>' ).css( 'width', /* padding = */ 20 + ( 10 * /* fontSize = */ 11 ) ).appendTo( $table );
253 $( '<colgroup>' ).appendTo( $table );
254 $( '<colgroup>' ).css( 'width', 350 ).appendTo( $table );
255
256 entryTypeText = function ( entryType ) {
257 switch ( entryType ) {
258 case 'log':
259 return 'Log';
260 case 'warn':
261 return 'Warning';
262 case 'deprecated':
263 return 'Deprecated';
264 default:
265 return 'Unknown';
266 }
267 };
268
269 for ( i = 0, length = this.data.log.length; i < length; i += 1 ) {
270 entry = this.data.log[ i ];
271 entry.typeText = entryTypeText( entry.type );
272
273 $( '<tr>' )
274 .append( $( '<td>' )
275 .text( entry.typeText )
276 .addClass( 'mw-debug-console-' + entry.type )
277 )
278 .append( $( '<td>' ).html( entry.msg ) )
279 .append( $( '<td>' ).text( entry.caller ) )
280 .appendTo( $table );
281 }
282
283 return $table;
284 },
285
286 /**
287 * Build query list pane
288 *
289 * @return {jQuery}
290 */
291 buildQueryTable: function () {
292 var $table, i, length, query;
293
294 $table = $( '<table>' ).attr( 'id', 'mw-debug-querylist' );
295
296 $( '<tr>' )
297 .append( $( '<th>' ).text( '#' ).css( 'width', '4em' ) )
298 .append( $( '<th>' ).text( 'SQL' ) )
299 .append( $( '<th>' ).text( 'Time' ).css( 'width', '8em' ) )
300 .append( $( '<th>' ).text( 'Call' ).css( 'width', '18em' ) )
301 .appendTo( $table );
302
303 for ( i = 0, length = this.data.queries.length; i < length; i += 1 ) {
304 query = this.data.queries[ i ];
305
306 $( '<tr>' )
307 .append( $( '<td>' ).text( i + 1 ) )
308 .append( $( '<td>' ).text( query.sql ) )
309 .append( $( '<td>' ).text( ( query.time * 1000 ).toFixed( 4 ) + 'ms' ).addClass( 'stats' ) )
310 .append( $( '<td>' ).text( query.function ) )
311 .appendTo( $table );
312 }
313
314 return $table;
315 },
316
317 /**
318 * Build legacy debug log pane
319 *
320 * @return {jQuery}
321 */
322 buildDebugLogTable: function () {
323 var $list, i, length, line;
324 $list = $( '<ul>' );
325
326 for ( i = 0, length = this.data.debugLog.length; i < length; i += 1 ) {
327 line = this.data.debugLog[ i ];
328 $( '<li>' )
329 .html( mw.html.escape( line ).replace( /\n/g, '<br />\n' ) )
330 .appendTo( $list );
331 }
332
333 return $list;
334 },
335
336 /**
337 * Build request information pane
338 *
339 * @return {jQuery}
340 */
341 buildRequestPane: function () {
342
343 function buildTable( title, data ) {
344 var $unit, $table, key;
345
346 $unit = $( '<div>' ).append( $( '<h2>' ).text( title ) );
347
348 $table = $( '<table>' ).appendTo( $unit );
349
350 $( '<tr>' )
351 .html( '<th>Key</th><th>Value</th>' )
352 .appendTo( $table );
353
354 for ( key in data ) {
355 $( '<tr>' )
356 .append( $( '<th>' ).text( key ) )
357 .append( $( '<td>' ).text( data[ key ] ) )
358 .appendTo( $table );
359 }
360
361 return $unit;
362 }
363
364 return $( '<div>' )
365 .text( this.data.request.method + ' ' + this.data.request.url )
366 .append( buildTable( 'Headers', this.data.request.headers ) )
367 .append( buildTable( 'Parameters', this.data.request.params ) );
368 },
369
370 /**
371 * Build included files pane
372 *
373 * @return {jQuery}
374 */
375 buildIncludesPane: function () {
376 var $table, i, length, file;
377
378 $table = $( '<table>' );
379
380 for ( i = 0, length = this.data.includes.length; i < length; i += 1 ) {
381 file = this.data.includes[ i ];
382 $( '<tr>' )
383 .append( $( '<td>' ).text( file.name ) )
384 .append( $( '<td>' ).text( file.size ).addClass( 'nr' ) )
385 .appendTo( $table );
386 }
387
388 return $table;
389 }
390 };
391
392 $( function () {
393 debug.init();
394 } );
395
396 }() );