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