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