summaryrefslogtreecommitdiff
path: root/resources/src/mediawiki/mediawiki.inspect.js
blob: 8e9fc89f4b07e2391edd66510cb98a19b407dd27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*!
 * Tools for inspecting page composition and performance.
 *
 * @author Ori Livneh
 * @since 1.22
 */
/*jshint devel:true */
( function ( mw, $ ) {

	function sortByProperty( array, prop, descending ) {
		var order = descending ? -1 : 1;
		return array.sort( function ( a, b ) {
			return a[prop] > b[prop] ? order : a[prop] < b[prop] ? -order : 0;
		} );
	}

	function humanSize( bytes ) {
		if ( !$.isNumeric( bytes ) || bytes === 0 ) { return bytes; }
		var i = 0, units = [ '', ' kB', ' MB', ' GB', ' TB', ' PB' ];
		for ( ; bytes >= 1024; bytes /= 1024 ) { i++; }
		return bytes.toFixed( 1 ) + units[i];
	}

	/**
	 * @class mw.inspect
	 * @singleton
	 */
	var inspect = {

		/**
		 * Return a map of all dependency relationships between loaded modules.
		 *
		 * @return {Object} Maps module names to objects. Each sub-object has
		 *  two properties, 'requires' and 'requiredBy'.
		 */
		getDependencyGraph: function () {
			var modules = inspect.getLoadedModules(), graph = {};

			$.each( modules, function ( moduleIndex, moduleName ) {
				var dependencies = mw.loader.moduleRegistry[moduleName].dependencies || [];

				graph[moduleName] = graph[moduleName] || { requiredBy: [] };
				graph[moduleName].requires = dependencies;

				$.each( dependencies, function ( depIndex, depName ) {
					graph[depName] = graph[depName] || { requiredBy: [] };
					graph[depName].requiredBy.push( moduleName );
				} );
			} );
			return graph;
		},

		/**
		 * Calculate the byte size of a ResourceLoader module.
		 *
		 * @param {string} moduleName The name of the module
		 * @return {number|null} Module size in bytes or null
		 */
		getModuleSize: function ( moduleName ) {
			var module = mw.loader.moduleRegistry[ moduleName ],
				payload = 0;

			if ( mw.loader.getState( moduleName ) !== 'ready' ) {
				return null;
			}

			if ( !module.style && !module.script ) {
				return null;
			}

			// Tally CSS
			if ( module.style && $.isArray( module.style.css ) ) {
				$.each( module.style.css, function ( i, stylesheet ) {
					payload += $.byteLength( stylesheet );
				} );
			}

			// Tally JavaScript
			if ( $.isFunction( module.script ) ) {
				payload += $.byteLength( module.script.toString() );
			}

			return payload;
		},

		/**
		 * Given CSS source, count both the total number of selectors it
		 * contains and the number which match some element in the current
		 * document.
		 *
		 * @param {string} css CSS source
		 * @return Selector counts
		 * @return {number} return.selectors Total number of selectors
		 * @return {number} return.matched Number of matched selectors
		 */
		auditSelectors: function ( css ) {
			var selectors = { total: 0, matched: 0 },
				style = document.createElement( 'style' ),
				sheet, rules;

			style.textContent = css;
			document.body.appendChild( style );
			// Standards-compliant browsers use .sheet.cssRules, IE8 uses .styleSheet.rules…
			sheet = style.sheet || style.styleSheet;
			rules = sheet.cssRules || sheet.rules;
			$.each( rules, function ( index, rule ) {
				selectors.total++;
				if ( document.querySelector( rule.selectorText ) !== null ) {
					selectors.matched++;
				}
			} );
			document.body.removeChild( style );
			return selectors;
		},

		/**
		 * Get a list of all loaded ResourceLoader modules.
		 *
		 * @return {Array} List of module names
		 */
		getLoadedModules: function () {
			return $.grep( mw.loader.getModuleNames(), function ( module ) {
				return mw.loader.getState( module ) === 'ready';
			} );
		},

		/**
		 * Print tabular data to the console, using console.table, console.log,
		 * or mw.log (in declining order of preference).
		 *
		 * @param {Array} data Tabular data represented as an array of objects
		 *  with common properties.
		 */
		dumpTable: function ( data ) {
			try {
				// Bartosz made me put this here.
				if ( window.opera ) { throw window.opera; }
				// Use Function.prototype#call to force an exception on Firefox,
				// which doesn't define console#table but doesn't complain if you
				// try to invoke it.
				console.table.call( console, data );
				return;
			} catch ( e ) {}
			try {
				console.log( JSON.stringify( data, null, 2 ) );
				return;
			} catch ( e ) {}
			mw.log( data );
		},

		/**
		 * Generate and print one more reports. When invoked with no arguments,
		 * print all reports.
		 *
		 * @param {string...} [reports] Report names to run, or unset to print
		 *  all available reports.
		 */
		runReports: function () {
			var reports = arguments.length > 0 ?
				Array.prototype.slice.call( arguments ) :
				$.map( inspect.reports, function ( v, k ) { return k; } );

			$.each( reports, function ( index, name ) {
				inspect.dumpTable( inspect.reports[name]() );
			} );
		},

		/**
		 * @class mw.inspect.reports
		 * @singleton
		 */
		reports: {
			/**
			 * Generate a breakdown of all loaded modules and their size in
			 * kilobytes. Modules are ordered from largest to smallest.
			 */
			size: function () {
				// Map each module to a descriptor object.
				var modules = $.map( inspect.getLoadedModules(), function ( module ) {
					return {
						name: module,
						size: inspect.getModuleSize( module )
					};
				} );

				// Sort module descriptors by size, largest first.
				sortByProperty( modules, 'size', true );

				// Convert size to human-readable string.
				$.each( modules, function ( i, module ) {
					module.size = humanSize( module.size );
				} );

				return modules;
			},

			/**
			 * For each module with styles, count the number of selectors, and
			 * count how many match against some element currently in the DOM.
			 */
			css: function () {
				var modules = [];

				$.each( inspect.getLoadedModules(), function ( index, name ) {
					var css, stats, module = mw.loader.moduleRegistry[name];

					try {
						css = module.style.css.join();
					} catch ( e ) { return; } // skip

					stats = inspect.auditSelectors( css );
					modules.push( {
						module: name,
						allSelectors: stats.total,
						matchedSelectors: stats.matched,
						percentMatched: stats.total !== 0 ?
							( stats.matched / stats.total * 100 ).toFixed( 2 )  + '%' : null
					} );
				} );
				sortByProperty( modules, 'allSelectors', true );
				return modules;
			},

			/**
			 * Report stats on mw.loader.store: the number of localStorage
			 * cache hits and misses, the number of items purged from the
			 * cache, and the total size of the module blob in localStorage.
			 */
			store: function () {
				var raw, stats = { enabled: mw.loader.store.enabled };
				if ( stats.enabled ) {
					$.extend( stats, mw.loader.store.stats );
					try {
						raw = localStorage.getItem( mw.loader.store.getStoreKey() );
						stats.totalSize = humanSize( $.byteLength( raw ) );
					} catch ( e ) {}
				}
				return [stats];
			}
		},

		/**
		 * Perform a string search across the JavaScript and CSS source code
		 * of all loaded modules and return an array of the names of the
		 * modules that matched.
		 *
		 * @param {string|RegExp} pattern String or regexp to match.
		 * @return {Array} Array of the names of modules that matched.
		 */
		grep: function ( pattern ) {
			if ( typeof pattern.test !== 'function' ) {
				// Based on Y.Escape.regex from YUI v3.15.0
				pattern = new RegExp( pattern.replace( /[\-$\^*()+\[\]{}|\\,.?\s]/g, '\\$&' ), 'g' );
			}

			return $.grep( inspect.getLoadedModules(), function ( moduleName ) {
				var module = mw.loader.moduleRegistry[moduleName];

				// Grep module's JavaScript
				if ( $.isFunction( module.script ) && pattern.test( module.script.toString() ) ) {
					return true;
				}

				// Grep module's CSS
				if (
					$.isPlainObject( module.style ) && $.isArray( module.style.css )
					&& pattern.test( module.style.css.join( '' ) )
				) {
					// Module's CSS source matches
					return true;
				}

				return false;
			} );
		}
	};

	if ( mw.config.get( 'debug' ) ) {
		mw.log( 'mw.inspect: reports are not available in debug mode.' );
	}

	mw.inspect = inspect;

}( mediaWiki, jQuery ) );