Merge "resourceloader: Simplify StringSet fallback"
[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 id="mw-debug-toolbar" class="mw-debug" lang="en" dir="ltr"></div>' );
116
117 $bits = $( '<div class="mw-debug-bits"></div>' );
118
119 /**
120 * Returns a jQuery element for a debug-bit div
121 *
122 * @ignore
123 * @param {string} id
124 * @return {jQuery}
125 */
126 function bitDiv( id ) {
127 return $( '<div>' ).prop( {
128 id: 'mw-debug-' + id,
129 className: 'mw-debug-bit'
130 } ).appendTo( $bits );
131 }
132
133 /**
134 * Returns a jQuery element for a pane link
135 *
136 * @ignore
137 * @param {string} id
138 * @param {string} text
139 * @return {jQuery}
140 */
141 function paneLabel( id, text ) {
142 return $( '<a>' )
143 .prop( {
144 className: 'mw-debug-panelabel',
145 href: '#mw-debug-pane-' + id
146 } )
147 .text( text );
148 }
149
150 /**
151 * Returns a jQuery element for a debug-bit div with a for a pane link
152 *
153 * @ignore
154 * @param {string} id CSS id snippet. Will be prefixed with 'mw-debug-'
155 * @param {string} text Text to show
156 * @param {string} count Optional count to show
157 * @return {jQuery}
158 */
159 function paneTriggerBitDiv( id, text, count ) {
160 if ( count ) {
161 text = text + ' (' + count + ')';
162 }
163 return $( '<div>' ).prop( {
164 id: 'mw-debug-' + id,
165 className: 'mw-debug-bit mw-debug-panelink'
166 } )
167 .append( paneLabel( id, text ) )
168 .appendTo( $bits );
169 }
170
171 paneTriggerBitDiv( 'console', 'Console', this.data.log.length );
172
173 paneTriggerBitDiv( 'querylist', 'Queries', this.data.queries.length );
174
175 paneTriggerBitDiv( 'debuglog', 'Debug log', this.data.debugLog.length );
176
177 paneTriggerBitDiv( 'request', 'Request' );
178
179 paneTriggerBitDiv( 'includes', 'PHP includes', this.data.includes.length );
180
181 gitInfo = '';
182 if ( this.data.gitRevision !== false ) {
183 gitInfo = '(' + this.data.gitRevision.slice( 0, 7 ) + ')';
184 if ( this.data.gitViewUrl !== false ) {
185 gitInfo = $( '<a>' )
186 .attr( 'href', this.data.gitViewUrl )
187 .text( gitInfo );
188 }
189 }
190
191 bitDiv( 'mwversion' )
192 .append( $( '<a href="//www.mediawiki.org/">MediaWiki</a>' ) )
193 .append( document.createTextNode( ': ' + this.data.mwVersion + ' ' ) )
194 .append( gitInfo );
195
196 if ( this.data.gitBranch !== false ) {
197 bitDiv( 'gitbranch' ).text( 'Git branch: ' + this.data.gitBranch );
198 }
199
200 bitDiv( 'phpversion' )
201 .append( $( this.data.phpEngine === 'HHVM' ?
202 '<a href="https://hhvm.com/">HHVM</a>' :
203 '<a href="https://php.net/">PHP</a>'
204 ) )
205 .append( ': ' + this.data.phpVersion );
206
207 bitDiv( 'time' )
208 .text( 'Time: ' + this.data.time.toFixed( 5 ) );
209
210 bitDiv( 'memory' )
211 .text( 'Memory: ' + this.data.memory + ' (Peak: ' + this.data.memoryPeak + ')' );
212
213 $bits.appendTo( $container );
214
215 panes = {
216 console: this.buildConsoleTable(),
217 querylist: this.buildQueryTable(),
218 debuglog: this.buildDebugLogTable(),
219 request: this.buildRequestPane(),
220 includes: this.buildIncludesPane()
221 };
222
223 for ( id in panes ) {
224 $( '<div>' )
225 .prop( {
226 className: 'mw-debug-pane',
227 id: 'mw-debug-pane-' + id
228 } )
229 .append( panes[ id ] )
230 .appendTo( $container );
231 }
232
233 this.$container = $container;
234 },
235
236 /**
237 * Build the console panel
238 *
239 * @return {jQuery} Console panel
240 */
241 buildConsoleTable: function () {
242 var $table, entryTypeText, i, length, entry;
243
244 $table = $( '<table id="mw-debug-console">' );
245
246 $( '<colgroup>' ).css( 'width', /* padding = */ 20 + ( 10 * /* fontSize = */ 11 ) ).appendTo( $table );
247 $( '<colgroup>' ).appendTo( $table );
248 $( '<colgroup>' ).css( 'width', 350 ).appendTo( $table );
249
250 entryTypeText = function ( entryType ) {
251 switch ( entryType ) {
252 case 'log':
253 return 'Log';
254 case 'warn':
255 return 'Warning';
256 case 'deprecated':
257 return 'Deprecated';
258 default:
259 return 'Unknown';
260 }
261 };
262
263 for ( i = 0, length = this.data.log.length; i < length; i += 1 ) {
264 entry = this.data.log[ i ];
265 entry.typeText = entryTypeText( entry.type );
266
267 $( '<tr>' )
268 .append( $( '<td>' )
269 .text( entry.typeText )
270 .addClass( 'mw-debug-console-' + entry.type )
271 )
272 .append( $( '<td>' ).html( entry.msg ) )
273 .append( $( '<td>' ).text( entry.caller ) )
274 .appendTo( $table );
275 }
276
277 return $table;
278 },
279
280 /**
281 * Build query list pane
282 *
283 * @return {jQuery}
284 */
285 buildQueryTable: function () {
286 var $table, i, length, query;
287
288 $table = $( '<table id="mw-debug-querylist"></table>' );
289
290 $( '<tr>' )
291 .append( $( '<th>#</th>' ).css( 'width', '4em' ) )
292 .append( $( '<th>SQL</th>' ) )
293 .append( $( '<th>Time</th>' ).css( 'width', '8em' ) )
294 .append( $( '<th>Call</th>' ).css( 'width', '18em' ) )
295 .appendTo( $table );
296
297 for ( i = 0, length = this.data.queries.length; i < length; i += 1 ) {
298 query = this.data.queries[ i ];
299
300 $( '<tr>' )
301 .append( $( '<td>' ).text( i + 1 ) )
302 .append( $( '<td>' ).text( query.sql ) )
303 .append( $( '<td class="stats">' ).text( ( query.time * 1000 ).toFixed( 4 ) + 'ms' ) )
304 .append( $( '<td>' ).text( query[ 'function' ] ) )
305 .appendTo( $table );
306 }
307
308 return $table;
309 },
310
311 /**
312 * Build legacy debug log pane
313 *
314 * @return {jQuery}
315 */
316 buildDebugLogTable: function () {
317 var $list, i, length, line;
318 $list = $( '<ul>' );
319
320 for ( i = 0, length = this.data.debugLog.length; i < length; i += 1 ) {
321 line = this.data.debugLog[ i ];
322 $( '<li>' )
323 .html( mw.html.escape( line ).replace( /\n/g, '<br />\n' ) )
324 .appendTo( $list );
325 }
326
327 return $list;
328 },
329
330 /**
331 * Build request information pane
332 *
333 * @return {jQuery}
334 */
335 buildRequestPane: function () {
336
337 function buildTable( title, data ) {
338 var $unit, $table, key;
339
340 $unit = $( '<div>' ).append( $( '<h2>' ).text( title ) );
341
342 $table = $( '<table>' ).appendTo( $unit );
343
344 $( '<tr>' )
345 .html( '<th>Key</th><th>Value</th>' )
346 .appendTo( $table );
347
348 for ( key in data ) {
349 $( '<tr>' )
350 .append( $( '<th>' ).text( key ) )
351 .append( $( '<td>' ).text( data[ key ] ) )
352 .appendTo( $table );
353 }
354
355 return $unit;
356 }
357
358 return $( '<div>' )
359 .text( this.data.request.method + ' ' + this.data.request.url )
360 .append( buildTable( 'Headers', this.data.request.headers ) )
361 .append( buildTable( 'Parameters', this.data.request.params ) );
362 },
363
364 /**
365 * Build included files pane
366 *
367 * @return {jQuery}
368 */
369 buildIncludesPane: function () {
370 var $table, i, length, file;
371
372 $table = $( '<table>' );
373
374 for ( i = 0, length = this.data.includes.length; i < length; i += 1 ) {
375 file = this.data.includes[ i ];
376 $( '<tr>' )
377 .append( $( '<td>' ).text( file.name ) )
378 .append( $( '<td class="nr">' ).text( file.size ) )
379 .appendTo( $table );
380 }
381
382 return $table;
383 }
384 };
385
386 $( function () {
387 debug.init();
388 } );
389
390 }() );