Debian Codegraph @

resources/src/mediawiki.debug/debug.js

raw symbols SCIP
1 /* eslint-disable es-x/no-array-prototype-includes */
2
3 ( function () {
4 'use strict';
5
6 const hovzer = $.getFootHovzer();
7
8 OO.ui.getViewportSpacing = function () {
9 return {
10 top: 0,
11 right: 0,
12 bottom: hovzer.$.outerHeight(),
13 left: 0
14 };
15 };
16
17 /**
18 * Access the debug toolbar. Enabled server-side through `$wgDebugToolbar`.
19 *
20 * @namespace mw.Debug
21 * @author John Du Hart
22 * @since 1.19
23 */
24 const debug = mw.Debug = {
25 /**
26 * Toolbar container element.
27 *
28 * @type {jQuery}
29 */
30 $container: null,
31
32 /**
33 * Object containing data for the debug toolbar.
34 *
35 * @type {Object}
36 */
37 data: {},
38
39 /**
40 * Initialize the debugging pane.
41 *
42 * Shouldn't be called before the document is ready
43 * (since it binds to elements on the page).
44 *
45 * @param {Object} [data] Defaults to 'debugInfo' from mw.config
46 */
47 init: function ( data ) {
48
49 this.data = data || mw.config.get( 'debugInfo' );
50 this.buildHtml();
51
52 // Insert the container into the DOM
53 hovzer.$.append( this.$container );
54 hovzer.update();
55
56 $( '.mw-debug-panelink' ).on( 'click', this.switchPane );
57 },
58
59 /**
60 * Switch between panes.
61 *
62 * Should be called with an HTMLElement as its thisArg,
63 * because it's meant to be an event handler.
64 *
65 * TODO: Store cookie for last pane open.
66 *
67 * @param {jQuery.Event} e
68 */
69 switchPane: function ( e ) {
70 const currentPaneId = debug.$container.data( 'currentPane' ),
71 requestedPaneId = $( this ).prop( 'id' ).slice( 9 ),
72 $currentPane = $( '#mw-debug-pane-' + currentPaneId ),
73 $requestedPane = $( '#mw-debug-pane-' + requestedPaneId );
74 let hovDone = false;
75
76 function updateHov() {
77 if ( !hovDone ) {
78 hovzer.update();
79 hovDone = true;
80 }
81 }
82
83 // Skip hash fragment handling. Prevents screen from jumping.
84 e.preventDefault();
85
86 $( this ).addClass( 'current' );
87 $( '.mw-debug-panelink' ).not( this ).removeClass( 'current' );
88
89 // Hide the current pane
90 if ( requestedPaneId === currentPaneId ) {
91 // FIXME: Use CSS transition
92 // eslint-disable-next-line no-jquery/no-slide
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 // FIXME: Use CSS transition
102 // eslint-disable-next-line no-jquery/no-slide
103 $requestedPane.slideDown( updateHov );
104 } else {
105 $currentPane.hide();
106 $requestedPane.show();
107 updateHov();
108 }
109 },
110
111 /**
112 * Construct the HTML for the debugging toolbar.
113 */
114 buildHtml: function () {
115 const $container = $( '<div>' )
116 .attr( {
117 id: 'mw-debug-toolbar',
118 lang: 'en',
119 dir: 'ltr'
120 } )
121 .addClass( 'mw-debug' );
122
123 const $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 let $gitInfo;
188 if ( this.data.gitRevision !== false ) {
189 const gitInfoText = '(' + this.data.gitRevision.slice( 0, 7 ) + ')';
190 if ( this.data.gitViewUrl !== false ) {
191 $gitInfo = $( '<a>' )
192 .attr( 'href', this.data.gitViewUrl )
193 .text( gitInfoText );
194 } else {
195 $gitInfo = $( document.createTextNode( gitInfoText ) );
196 }
197 }
198
199 bitDiv( 'mwversion' )
200 .append( $( '<a>' ).attr( 'href', 'https://www.mediawiki.org/' ).text( 'MediaWiki' ) )
201 .append( document.createTextNode( ': ' + this.data.mwVersion + ' ' ) )
202 .append( $gitInfo );
203
204 if ( this.data.gitBranch !== false ) {
205 bitDiv( 'gitbranch' ).text( 'Git branch: ' + this.data.gitBranch );
206 }
207
208 bitDiv( 'phpversion' )
209 .append( $( '<a>' ).attr( 'href', 'https://php.net/' ).text( 'PHP' ) )
210 .append( ': ' + this.data.phpVersion );
211
212 bitDiv( 'time' )
213 .text( 'Time: ' + this.data.time.toFixed( 5 ) );
214
215 bitDiv( 'memory' )
216 .text( 'Memory: ' + this.data.memory + ' (Peak: ' + this.data.memoryPeak + ')' );
217
218 $bits.appendTo( $container );
219
220 const panes = {
221 console: this.buildConsoleTable(),
222 querylist: this.buildQueryTable(),
223 debuglog: this.buildDebugLogTable(),
224 request: this.buildRequestPane(),
225 includes: this.buildIncludesPane()
226 };
227
228 for ( const paneId in panes ) {
229 $( '<div>' )
230 .prop( {
231 className: 'mw-debug-pane',
232 id: 'mw-debug-pane-' + paneId
233 } )
234 .append( panes[ paneId ] )
235 .appendTo( $container );
236 }
237
238 this.$container = $container;
239 },
240
241 /**
242 * Build the console panel.
243 *
244 * @return {jQuery} Console panel
245 */
246 buildConsoleTable: function () {
247 const $table = $( '<table>' ).attr( 'id', 'mw-debug-console' );
248 const length = this.data.log.length;
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 const 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 ( let i = 0; i < length; i++ ) {
268 const entry = this.data.log[ i ];
269 entry.typeText = entryTypeText( entry.type );
270
271 // The following classes are used here:
272 // * mw-debug-console-log
273 // * mw-debug-console-warn
274 // * mw-debug-console-deprecated
275 $( '<tr>' )
276 .append( $( '<td>' )
277 .text( entry.typeText )
278 .addClass( 'mw-debug-console-' + entry.type )
279 )
280 .append( $( '<td>' ).html( entry.msg ) )
281 .append( $( '<td>' ).text( entry.caller ) )
282 .appendTo( $table );
283 }
284
285 return $table;
286 },
287
288 /**
289 * Build query list pane.
290 *
291 * @return {jQuery}
292 */
293 buildQueryTable: function () {
294 const $table = $( '<table>' ).attr( 'id', 'mw-debug-querylist' );
295 const length = this.data.queries.length;
296
297 $( '<tr>' )
298 .append( $( '<th>' ).attr( 'scope', 'col' ).text( '#' ).css( 'width', '4em' ) )
299 .append( $( '<th>' ).attr( 'scope', 'col' ).text( 'SQL' ) )
300 .append( $( '<th>' ).attr( 'scope', 'col' ).text( 'Time' ).css( 'width', '8em' ) )
301 .append( $( '<th>' ).attr( 'scope', 'col' ).text( 'Call' ).css( 'width', '18em' ) )
302 .appendTo( $table );
303
304 for ( let i = 0; i < length; i++ ) {
305 const query = this.data.queries[ i ];
306
307 $( '<tr>' )
308 .append( $( '<td>' ).text( i + 1 ) )
309 .append( $( '<td>' ).text( query.sql ) )
310 .append( $( '<td>' ).text( ( query.time * 1000 ).toFixed( 3 ) + 'ms' ).addClass( 'stats' ) )
311 .append( $( '<td>' ).text( query.function ) )
312 .appendTo( $table );
313 }
314
315 return $table;
316 },
317
318 /**
319 * Build legacy debug log pane.
320 *
321 * @return {jQuery}
322 */
323 buildDebugLogTable: function () {
324 const $list = $( '<ul>' );
325 const length = this.data.debugLog.length;
326
327 for ( let i = 0; i < length; i++ ) {
328 const line = this.data.debugLog[ i ];
329 $( '<li>' )
330 .html( mw.html.escape( line ).replace( /\n/g, '<br />\n' ) )
331 .appendTo( $list );
332 }
333
334 return $list;
335 },
336
337 /**
338 * Build request information pane.
339 *
340 * @return {jQuery}
341 */
342 buildRequestPane: function () {
343
344 function buildTable( title, data ) {
345 const $unit = $( '<div>' ).append( $( '<h2>' ).text( title ) );
346
347 const $table = $( '<table>' ).appendTo( $unit );
348
349 $( '<tr>' )
350 .append(
351 $( '<th>' ).attr( 'scope', 'col' ).text( 'Key' ),
352 $( '<th>' ).attr( 'scope', 'col' ).text( 'Value' )
353 )
354 .appendTo( $table );
355
356 for ( const key in data ) {
357 $( '<tr>' )
358 .append( $( '<th>' ).attr( 'scope', 'row' ).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 const $table = $( '<table>' );
379 const length = this.data.includes.length;
380
381 for ( let i = 0; i < length; i++ ) {
382 const file = this.data.includes[ i ];
383 $( '<tr>' )
384 .append( $( '<td>' ).text( file.name ) )
385 .append( $( '<td>' ).text( file.size ).addClass( 'nr' ) )
386 .appendTo( $table );
387 }
388
389 return $table;
390 }
391 };
392
393 $( () => {
394 debug.init();
395 } );
396
397 }() );