Merge "objectcache: use INTERIM_KEY_TTL constant in WANObjectCache for readability"
[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 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="http://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 if ( !panes.hasOwnProperty( id ) ) {
225 continue;
226 }
227
228 $( '<div>' )
229 .prop( {
230 className: 'mw-debug-pane',
231 id: 'mw-debug-pane-' + id
232 } )
233 .append( panes[ id ] )
234 .appendTo( $container );
235 }
236
237 this.$container = $container;
238 },
239
240 /**
241 * Build the console panel
242 *
243 * @return {jQuery} Console panel
244 */
245 buildConsoleTable: function () {
246 var $table, entryTypeText, i, length, entry;
247
248 $table = $( '<table id="mw-debug-console">' );
249
250 $( '<colgroup>' ).css( 'width', /* padding = */ 20 + ( 10 * /* fontSize = */ 11 ) ).appendTo( $table );
251 $( '<colgroup>' ).appendTo( $table );
252 $( '<colgroup>' ).css( 'width', 350 ).appendTo( $table );
253
254 entryTypeText = function ( entryType ) {
255 switch ( entryType ) {
256 case 'log':
257 return 'Log';
258 case 'warn':
259 return 'Warning';
260 case 'deprecated':
261 return 'Deprecated';
262 default:
263 return 'Unknown';
264 }
265 };
266
267 for ( i = 0, length = this.data.log.length; i < length; i += 1 ) {
268 entry = this.data.log[ i ];
269 entry.typeText = entryTypeText( entry.type );
270
271 $( '<tr>' )
272 .append( $( '<td>' )
273 .text( entry.typeText )
274 .addClass( 'mw-debug-console-' + entry.type )
275 )
276 .append( $( '<td>' ).html( entry.msg ) )
277 .append( $( '<td>' ).text( entry.caller ) )
278 .appendTo( $table );
279 }
280
281 return $table;
282 },
283
284 /**
285 * Build query list pane
286 *
287 * @return {jQuery}
288 */
289 buildQueryTable: function () {
290 var $table, i, length, query;
291
292 $table = $( '<table id="mw-debug-querylist"></table>' );
293
294 $( '<tr>' )
295 .append( $( '<th>#</th>' ).css( 'width', '4em' ) )
296 .append( $( '<th>SQL</th>' ) )
297 .append( $( '<th>Time</th>' ).css( 'width', '8em' ) )
298 .append( $( '<th>Call</th>' ).css( 'width', '18em' ) )
299 .appendTo( $table );
300
301 for ( i = 0, length = this.data.queries.length; i < length; i += 1 ) {
302 query = this.data.queries[ i ];
303
304 $( '<tr>' )
305 .append( $( '<td>' ).text( i + 1 ) )
306 .append( $( '<td>' ).text( query.sql ) )
307 .append( $( '<td class="stats">' ).text( ( query.time * 1000 ).toFixed( 4 ) + 'ms' ) )
308 .append( $( '<td>' ).text( query[ 'function' ] ) )
309 .appendTo( $table );
310 }
311
312 return $table;
313 },
314
315 /**
316 * Build legacy debug log pane
317 *
318 * @return {jQuery}
319 */
320 buildDebugLogTable: function () {
321 var $list, i, length, line;
322 $list = $( '<ul>' );
323
324 for ( i = 0, length = this.data.debugLog.length; i < length; i += 1 ) {
325 line = this.data.debugLog[ i ];
326 $( '<li>' )
327 .html( mw.html.escape( line ).replace( /\n/g, '<br />\n' ) )
328 .appendTo( $list );
329 }
330
331 return $list;
332 },
333
334 /**
335 * Build request information pane
336 *
337 * @return {jQuery}
338 */
339 buildRequestPane: function () {
340
341 function buildTable( title, data ) {
342 var $unit, $table, key;
343
344 $unit = $( '<div>' ).append( $( '<h2>' ).text( title ) );
345
346 $table = $( '<table>' ).appendTo( $unit );
347
348 $( '<tr>' )
349 .html( '<th>Key</th><th>Value</th>' )
350 .appendTo( $table );
351
352 for ( key in data ) {
353 if ( !data.hasOwnProperty( key ) ) {
354 continue;
355 }
356
357 $( '<tr>' )
358 .append( $( '<th>' ).text( key ) )
359 .append( $( '<td>' ).text( data[ key ] ) )
360 .appendTo( $table );
361 }
362
363 return $unit;
364 }
365
366 return $( '<div>' )
367 .text( this.data.request.method + ' ' + this.data.request.url )
368 .append( buildTable( 'Headers', this.data.request.headers ) )
369 .append( buildTable( 'Parameters', this.data.request.params ) );
370 },
371
372 /**
373 * Build included files pane
374 *
375 * @return {jQuery}
376 */
377 buildIncludesPane: function () {
378 var $table, i, length, file;
379
380 $table = $( '<table>' );
381
382 for ( i = 0, length = this.data.includes.length; i < length; i += 1 ) {
383 file = this.data.includes[ i ];
384 $( '<tr>' )
385 .append( $( '<td>' ).text( file.name ) )
386 .append( $( '<td class="nr">' ).text( file.size ) )
387 .appendTo( $table );
388 }
389
390 return $table;
391 }
392 };
393
394 $( function () {
395 debug.init();
396 } );
397
398 }( mediaWiki, jQuery ) );