2 * Base library for MediaWiki.
4 * Exposed globally as `mediaWiki` with `mw` as shortcut.
7 * @alternateClassName mediaWiki
14 hasOwn
= Object
.prototype.hasOwnProperty
,
15 slice
= Array
.prototype.slice
,
16 trackCallbacks
= $.Callbacks( 'memory' ),
20 * Create an object that can be read from or written to from methods that allow
21 * interaction both with single and multiple properties at once.
25 * var collection, query, results;
27 * // Create your address book
28 * collection = new mw.Map();
30 * // This data could be coming from an external source (eg. API/AJAX)
32 * 'John Doe': 'john@example.org',
33 * 'Jane Doe': 'jane@example.org',
34 * 'George van Halen': 'gvanhalen@example.org'
37 * wanted = ['John Doe', 'Jane Doe', 'Daniel Jackson'];
39 * // You can detect missing keys first
40 * if ( !collection.exists( wanted ) ) {
41 * // One or more are missing (in this case: "Daniel Jackson")
42 * mw.log( 'One or more names were not found in your address book' );
45 * // Or just let it give you what it can. Optionally fill in from a default.
46 * results = collection.get( wanted, 'nobody@example.com' );
47 * mw.log( results['Jane Doe'] ); // "jane@example.org"
48 * mw.log( results['Daniel Jackson'] ); // "nobody@example.com"
53 * @param {Object|boolean} [values] The value-baring object to be mapped. Defaults to an
55 * For backwards-compatibility with mw.config, this can also be `true` in which case values
56 * are copied to the Window object as global variables (T72470). Values are copied in
57 * one direction only. Changes to globals are not reflected in the map.
59 function Map( values
) {
60 if ( values
=== true ) {
63 // Override #set to also set the global variable
64 this.set = function ( selection
, value
) {
67 if ( $.isPlainObject( selection
) ) {
68 for ( s
in selection
) {
69 setGlobalMapValue( this, s
, selection
[s
] );
73 if ( typeof selection
=== 'string' && arguments
.length
) {
74 setGlobalMapValue( this, selection
, value
);
83 this.values
= values
|| {};
87 * Alias property to the global object.
93 * @param {Mixed} value
95 function setGlobalMapValue( map
, key
, value
) {
96 map
.values
[key
] = value
;
101 // Deprecation notice for mw.config globals (T58550, T72470)
102 map
=== mw
.config
&& 'Use mw.config instead.'
108 * Get the value of one or more keys.
110 * If called with no arguments, all values are returned.
112 * @param {string|Array} [selection] Key or array of keys to retrieve values for.
113 * @param {Mixed} [fallback=null] Value for keys that don't exist.
114 * @return {Mixed|Object| null} If selection was a string, returns the value,
115 * If selection was an array, returns an object of key/values.
116 * If no selection is passed, the 'values' container is returned. (Beware that,
117 * as is the default in JavaScript, the object is returned by reference.)
119 get: function ( selection
, fallback
) {
121 // If we only do this in the `return` block, it'll fail for the
122 // call to get() from the mutli-selection block.
123 fallback
= arguments
.length
> 1 ? fallback
: null;
125 if ( $.isArray( selection
) ) {
126 selection
= slice
.call( selection
);
128 for ( i
= 0; i
< selection
.length
; i
++ ) {
129 results
[selection
[i
]] = this.get( selection
[i
], fallback
);
134 if ( typeof selection
=== 'string' ) {
135 if ( !hasOwn
.call( this.values
, selection
) ) {
138 return this.values
[selection
];
141 if ( selection
=== undefined ) {
145 // Invalid selection key
150 * Set one or more key/value pairs.
152 * @param {string|Object} selection Key to set value for, or object mapping keys to values
153 * @param {Mixed} [value] Value to set (optional, only in use when key is a string)
154 * @return {boolean} True on success, false on failure
156 set: function ( selection
, value
) {
159 if ( $.isPlainObject( selection
) ) {
160 for ( s
in selection
) {
161 this.values
[s
] = selection
[s
];
165 if ( typeof selection
=== 'string' && arguments
.length
> 1 ) {
166 this.values
[selection
] = value
;
173 * Check if one or more keys exist.
175 * @param {Mixed} selection Key or array of keys to check
176 * @return {boolean} True if the key(s) exist
178 exists: function ( selection
) {
181 if ( $.isArray( selection
) ) {
182 for ( s
= 0; s
< selection
.length
; s
++ ) {
183 if ( typeof selection
[s
] !== 'string' || !hasOwn
.call( this.values
, selection
[s
] ) ) {
189 return typeof selection
=== 'string' && hasOwn
.call( this.values
, selection
);
194 * Object constructor for messages.
196 * Similar to the Message class in MediaWiki PHP.
198 * Format defaults to 'text'.
204 * 'hello': 'Hello world',
205 * 'hello-user': 'Hello, $1!',
206 * 'welcome-user': 'Welcome back to $2, $1! Last visit by $1: $3'
209 * obj = new mw.Message( mw.messages, 'hello' );
210 * mw.log( obj.text() );
213 * obj = new mw.Message( mw.messages, 'hello-user', [ 'John Doe' ] );
214 * mw.log( obj.text() );
215 * // Hello, John Doe!
217 * obj = new mw.Message( mw.messages, 'welcome-user', [ 'John Doe', 'Wikipedia', '2 hours ago' ] );
218 * mw.log( obj.text() );
219 * // Welcome back to Wikipedia, John Doe! Last visit by John Doe: 2 hours ago
221 * // Using mw.message shortcut
222 * obj = mw.message( 'hello-user', 'John Doe' );
223 * mw.log( obj.text() );
224 * // Hello, John Doe!
226 * // Using mw.msg shortcut
227 * str = mw.msg( 'hello-user', 'John Doe' );
229 * // Hello, John Doe!
231 * // Different formats
232 * obj = new mw.Message( mw.messages, 'hello-user', [ 'John "Wiki" <3 Doe' ] );
234 * obj.format = 'text';
235 * str = obj.toString();
240 * // Hello, John "Wiki" <3 Doe!
242 * mw.log( obj.escaped() );
243 * // Hello, John "Wiki" <3 Doe!
248 * @param {mw.Map} map Message store
249 * @param {string} key
250 * @param {Array} [parameters]
252 function Message( map
, key
, parameters
) {
253 this.format
= 'text';
256 this.parameters
= parameters
=== undefined ? [] : slice
.call( parameters
);
260 Message
.prototype = {
262 * Get parsed contents of the message.
264 * The default parser does simple $N replacements and nothing else.
265 * This may be overridden to provide a more complex message parser.
266 * The primary override is in the mediawiki.jqueryMsg module.
268 * This function will not be called for nonexistent messages.
270 * @return {string} Parsed message
272 parser: function () {
273 return mw
.format
.apply( null, [ this.map
.get( this.key
) ].concat( this.parameters
) );
277 * Add (does not replace) parameters for `N$` placeholder values.
279 * @param {Array} parameters
282 params: function ( parameters
) {
284 for ( i
= 0; i
< parameters
.length
; i
+= 1 ) {
285 this.parameters
.push( parameters
[i
] );
291 * Convert message object to its string form based on current format.
293 * @return {string} Message as a string in the current form, or `<key>` if key
296 toString: function () {
299 if ( !this.exists() ) {
300 // Use <key> as text if key does not exist
301 if ( this.format
=== 'escaped' || this.format
=== 'parse' ) {
302 // format 'escaped' and 'parse' need to have the brackets and key html escaped
303 return mw
.html
.escape( '<' + this.key
+ '>' );
305 return '<' + this.key
+ '>';
308 if ( this.format
=== 'plain' || this.format
=== 'text' || this.format
=== 'parse' ) {
309 text
= this.parser();
312 if ( this.format
=== 'escaped' ) {
313 text
= this.parser();
314 text
= mw
.html
.escape( text
);
321 * Change format to 'parse' and convert message to string
323 * If jqueryMsg is loaded, this parses the message text from wikitext
324 * (where supported) to HTML
326 * Otherwise, it is equivalent to plain.
328 * @return {string} String form of parsed message
331 this.format
= 'parse';
332 return this.toString();
336 * Change format to 'plain' and convert message to string
338 * This substitutes parameters, but otherwise does not change the
341 * @return {string} String form of plain message
344 this.format
= 'plain';
345 return this.toString();
349 * Change format to 'text' and convert message to string
351 * If jqueryMsg is loaded, {{-transformation is done where supported
352 * (such as {{plural:}}, {{gender:}}, {{int:}}).
354 * Otherwise, it is equivalent to plain
356 * @return {string} String form of text message
359 this.format
= 'text';
360 return this.toString();
364 * Change the format to 'escaped' and convert message to string
366 * This is equivalent to using the 'text' format (see #text), then
367 * HTML-escaping the output.
369 * @return {string} String form of html escaped message
371 escaped: function () {
372 this.format
= 'escaped';
373 return this.toString();
377 * Check if a message exists
382 exists: function () {
383 return this.map
.exists( this.key
);
393 * Get the current time, measured in milliseconds since January 1, 1970 (UTC).
395 * On browsers that implement the Navigation Timing API, this function will produce floating-point
396 * values with microsecond precision that are guaranteed to be monotonic. On all other browsers,
397 * it will fall back to using `Date`.
399 * @return {number} Current time
402 var perf
= window
.performance
,
403 navStart
= perf
&& perf
.timing
&& perf
.timing
.navigationStart
;
404 return navStart
&& typeof perf
.now
=== 'function' ?
405 function () { return navStart
+ perf
.now(); } :
406 function () { return +new Date(); };
410 * Format a string. Replace $1, $2 ... $N with positional arguments.
412 * Used by Message#parser().
415 * @param {string} fmt Format string
416 * @param {Mixed...} parameters Values for $N replacements
417 * @return {string} Formatted string
419 format: function ( formatString
) {
420 var parameters
= slice
.call( arguments
, 1 );
421 return formatString
.replace( /\$(\d+)/g, function ( str
, match
) {
422 var index
= parseInt( match
, 10 ) - 1;
423 return parameters
[index
] !== undefined ? parameters
[index
] : '$' + match
;
428 * Track an analytic event.
430 * This method provides a generic means for MediaWiki JavaScript code to capture state
431 * information for analysis. Each logged event specifies a string topic name that describes
432 * the kind of event that it is. Topic names consist of dot-separated path components,
433 * arranged from most general to most specific. Each path component should have a clear and
434 * well-defined purpose.
436 * Data handlers are registered via `mw.trackSubscribe`, and receive the full set of
437 * events that match their subcription, including those that fired before the handler was
440 * @param {string} topic Topic name
441 * @param {Object} [data] Data describing the event, encoded as an object
443 track: function ( topic
, data
) {
444 trackQueue
.push( { topic
: topic
, timeStamp
: mw
.now(), data
: data
} );
445 trackCallbacks
.fire( trackQueue
);
449 * Register a handler for subset of analytic events, specified by topic.
451 * Handlers will be called once for each tracked event, including any events that fired before the
452 * handler was registered; 'this' is set to a plain object with a 'timeStamp' property indicating
453 * the exact time at which the event fired, a string 'topic' property naming the event, and a
454 * 'data' property which is an object of event-specific data. The event topic and event data are
455 * also passed to the callback as the first and second arguments, respectively.
457 * @param {string} topic Handle events whose name starts with this string prefix
458 * @param {Function} callback Handler to call for each matching tracked event
459 * @param {string} callback.topic
460 * @param {Object} [callback.data]
462 trackSubscribe: function ( topic
, callback
) {
465 trackCallbacks
.add( function ( trackQueue
) {
467 for ( ; seen
< trackQueue
.length
; seen
++ ) {
468 event
= trackQueue
[ seen
];
469 if ( event
.topic
.indexOf( topic
) === 0 ) {
470 callback
.call( event
, event
.topic
, event
.data
);
476 // Expose Map constructor
479 // Expose Message constructor
483 * Map of configuration values.
485 * Check out [the complete list of configuration values](https://www.mediawiki.org/wiki/Manual:Interface/JavaScript#mw.config)
488 * If `$wgLegacyJavaScriptGlobals` is true, this Map will add its values to the
489 * global `window` object.
491 * @property {mw.Map} config
493 // Dummy placeholder later assigned in ResourceLoaderStartUpModule
497 * Empty object for third-party libraries, for cases where you don't
498 * want to add a new global, or the global is bad and needs containment
506 * Access container for deprecated functionality that can be moved from
507 * from their legacy location and attached to this object (e.g. a global
508 * function that is deprecated and as stop-gap can be exposed through here).
510 * This was reserved for future use but never ended up being used.
512 * @deprecated since 1.22 Let deprecated identifiers keep their original name
513 * and use mw.log#deprecate to create an access container for tracking.
519 * Store for messages.
526 * Store for templates associated with a module.
530 templates
: new Map(),
533 * Get a message object.
535 * Shorcut for `new mw.Message( mw.messages, key, parameters )`.
538 * @param {string} key Key of message to get
539 * @param {Mixed...} parameters Values for $N replacements
540 * @return {mw.Message}
542 message: function ( key
) {
543 var parameters
= slice
.call( arguments
, 1 );
544 return new Message( mw
.messages
, key
, parameters
);
548 * Get a message string using the (default) 'text' format.
550 * Shortcut for `mw.message( key, parameters... ).text()`.
553 * @param {string} key Key of message to get
554 * @param {Mixed...} parameters Values for $N replacements
558 return mw
.message
.apply( mw
.message
, arguments
).toString();
562 * Dummy placeholder for {@link mw.log}
566 // Also update the restoration of methods in mediawiki.log.js
567 // when adding or removing methods here.
568 var log = function () {};
576 * Write a message the console's warning channel.
577 * Also logs a stacktrace for easier debugging.
578 * Actions not supported by the browser console are silently ignored.
580 * @param {string...} msg Messages to output to console
582 log
.warn = function () {
583 var console
= window
.console
;
584 if ( console
&& console
.warn
&& console
.warn
.apply
) {
585 console
.warn
.apply( console
, arguments
);
586 if ( console
.trace
) {
593 * Create a property in a host object that, when accessed, will produce
594 * a deprecation warning in the console with backtrace.
596 * @param {Object} obj Host object of deprecated property
597 * @param {string} key Name of property to create in `obj`
598 * @param {Mixed} val The value this property should return when accessed
599 * @param {string} [msg] Optional text to include in the deprecation message
601 log
.deprecate
= !Object
.defineProperty
? function ( obj
, key
, val
) {
603 } : function ( obj
, key
, val
, msg
) {
604 msg
= 'Use of "' + key
+ '" is deprecated.' + ( msg
? ( ' ' + msg
) : '' );
606 // Can throw on Object.defineProperty.
608 Object
.defineProperty( obj
, key
, {
612 mw
.track( 'mw.deprecate', key
);
616 set: function ( newVal
) {
617 mw
.track( 'mw.deprecate', key
);
623 // Fallback to creating a copy of the value to the object.
632 * Client for ResourceLoader server end point.
634 * This client is in charge of maintaining the module registry and state
635 * machine, initiating network (batch) requests for loading modules, as
636 * well as dependency resolution and execution of source code.
638 * For more information, refer to
639 * <https://www.mediawiki.org/wiki/ResourceLoader/Features>
644 loader
: ( function () {
647 * Fired via mw.track on various resource loading errors.
649 * @event resourceloader_exception
650 * @param {Error|Mixed} e The error that was thrown. Almost always an Error
651 * object, but in theory module code could manually throw something else, and that
652 * might also end up here.
653 * @param {string} [module] Name of the module which caused the error. Omitted if the
654 * error is not module-related or the module cannot be easily identified due to
656 * @param {string} source Source of the error. Possible values:
658 * - style: stylesheet error (only affects old IE where a special style loading method
660 * - load-callback: exception thrown by user callback
661 * - module-execute: exception thrown by module code
662 * - store-eval: could not evaluate module code cached in localStorage
663 * - store-localstorage-init: localStorage or JSON parse error in mw.loader.store.init
664 * - store-localstorage-json: JSON conversion error in mw.loader.store.set
665 * - store-localstorage-update: localStorage or JSON conversion error in mw.loader.store.update
669 * Fired via mw.track on resource loading error conditions.
671 * @event resourceloader_assert
672 * @param {string} source Source of the error. Possible values:
674 * - bug-T59567: failed to cache script due to an Opera function -> string conversion
675 * bug; see <https://phabricator.wikimedia.org/T59567> for details
679 * Mapping of registered modules.
681 * See #implement for exact details on support for script, style and messages.
687 * // From startup mdoule
688 * 'version': ############## (unix timestamp)
689 * 'dependencies': ['required.foo', 'bar.also', ...], (or) function () {}
690 * 'group': 'somegroup', (or) null
691 * 'source': 'local', (or) 'anotherwiki'
692 * 'skip': 'return !!window.Example', (or) null
693 * 'state': 'registered', 'loaded', 'loading', 'ready', 'error', or 'missing'
695 * // Added during implementation
699 * 'messages': { 'key': 'value' }
707 // Mapping of sources, keyed by source-id, values are strings.
712 // 'sourceId': 'http://example.org/w/load.php'
717 // List of modules which will be loaded as when ready
720 // List of modules to be loaded
723 // List of callback functions waiting for modules to be ready to be called
726 // Selector cache for the marker element. Use getMarker() to get/use the marker!
729 // Buffer for #addEmbeddedCSS
732 // Callbacks for #addEmbeddedCSS
733 cssCallbacks
= $.Callbacks();
735 function getMarker() {
738 $marker
= $( 'meta[name="ResourceLoaderDynamicStyles"]' );
739 if ( !$marker
.length
) {
740 mw
.log( 'No <meta name="ResourceLoaderDynamicStyles"> found, inserting dynamically' );
741 $marker
= $( '<meta>' ).attr( 'name', 'ResourceLoaderDynamicStyles' ).appendTo( 'head' );
748 * Create a new style element and add it to the DOM.
751 * @param {string} text CSS text
752 * @param {HTMLElement|jQuery} [nextnode=document.head] The element where the style tag
753 * should be inserted before
754 * @return {HTMLElement} Reference to the created style element
756 function newStyleTag( text
, nextnode
) {
757 var s
= document
.createElement( 'style' );
759 // Must attach to document before setting cssText (bug 33305)
761 $( nextnode
).before( s
);
763 document
.getElementsByTagName( 'head' )[0].appendChild( s
);
765 if ( s
.styleSheet
) {
767 // Old IE ignores appended text nodes, access stylesheet directly.
768 s
.styleSheet
.cssText
= text
;
770 // Standard behaviour
771 s
.appendChild( document
.createTextNode( text
) );
777 * Add a bit of CSS text to the current browser page.
779 * The CSS will be appended to an existing ResourceLoader-created `<style>` tag
780 * or create a new one based on whether the given `cssText` is safe for extension.
782 * @param {string} [cssText=cssBuffer] If called without cssText,
783 * the internal buffer will be inserted instead.
784 * @param {Function} [callback]
786 function addEmbeddedCSS( cssText
, callback
) {
790 cssCallbacks
.add( callback
);
793 // Yield once before inserting the <style> tag. There are likely
794 // more calls coming up which we can combine this way.
795 // Appending a stylesheet and waiting for the browser to repaint
796 // is fairly expensive, this reduces that (bug 45810)
798 // Be careful not to extend the buffer with css that needs a new stylesheet.
799 // cssText containing `@import` rules needs to go at the start of a buffer,
800 // since those only work when placed at the start of a stylesheet; bug 35562.
801 if ( !cssBuffer
|| cssText
.slice( 0, '@import'.length
) !== '@import' ) {
802 // Linebreak for somewhat distinguishable sections
803 // (the rl-cachekey comment separating each)
804 cssBuffer
+= '\n' + cssText
;
805 // TODO: Use requestAnimationFrame in the future which will
806 // perform even better by not injecting styles while the browser
808 setTimeout( function () {
809 // Can't pass addEmbeddedCSS to setTimeout directly because Firefox
810 // (below version 13) has the non-standard behaviour of passing a
811 // numerical "lateness" value as first argument to this callback
812 // http://benalman.com/news/2009/07/the-mysterious-firefox-settime/
818 // This is a delayed call and we got a buffer still
819 } else if ( cssBuffer
) {
824 // This is a delayed call, but buffer was already cleared by
825 // another delayed call.
829 // By default, always create a new <style>. Appending text to a <style>
830 // tag is bad as it means the contents have to be re-parsed (bug 45810).
832 // Except, of course, in IE 9 and below. In there we default to re-using and
833 // appending to a <style> tag due to the IE stylesheet limit (bug 31676).
834 if ( 'documentMode' in document
&& document
.documentMode
<= 9 ) {
836 $style
= getMarker().prev();
837 // Verify that the element before the marker actually is a
838 // <style> tag and one that came from ResourceLoader
839 // (not some other style tag or even a `<meta>` or `<script>`).
840 if ( $style
.data( 'ResourceLoaderDynamicStyleTag' ) === true ) {
841 // There's already a dynamic <style> tag present and
842 // we are able to append more to it.
843 styleEl
= $style
.get( 0 );
845 if ( styleEl
.styleSheet
) {
847 styleEl
.styleSheet
.cssText
+= cssText
;
849 mw
.track( 'resourceloader.exception', { exception
: e
, source
: 'stylesheet' } );
852 styleEl
.appendChild( document
.createTextNode( cssText
) );
854 cssCallbacks
.fire().empty();
859 $( newStyleTag( cssText
, getMarker() ) ).data( 'ResourceLoaderDynamicStyleTag', true );
861 cssCallbacks
.fire().empty();
865 * Zero-pad three numbers.
873 function pad( a
, b
, c
) {
875 ( a
< 10 ? '0' : '' ) + a
+
876 ( b
< 10 ? '0' : '' ) + b
+
877 ( c
< 10 ? '0' : '' ) + c
882 * Convert UNIX timestamp to ISO8601 format.
885 * @param {number} timestamp UNIX timestamp
887 function formatVersionNumber( timestamp
) {
889 d
.setTime( timestamp
* 1000 );
891 pad( d
.getUTCFullYear(), d
.getUTCMonth() + 1, d
.getUTCDate() ),
893 pad( d
.getUTCHours(), d
.getUTCMinutes(), d
.getUTCSeconds() ),
899 * Resolve dependencies and detect circular references.
902 * @param {string} module Name of the top-level module whose dependencies shall be
903 * resolved and sorted.
904 * @param {Array} resolved Returns a topological sort of the given module and its
905 * dependencies, such that later modules depend on earlier modules. The array
906 * contains the module names. If the array contains already some module names,
907 * this function appends its result to the pre-existing array.
908 * @param {Object} [unresolved] Hash used to track the current dependency
909 * chain; used to report loops in the dependency graph.
910 * @throws {Error} If any unregistered module or a dependency loop is encountered
912 function sortDependencies( module
, resolved
, unresolved
) {
913 var n
, deps
, len
, skip
;
915 if ( !hasOwn
.call( registry
, module
) ) {
916 throw new Error( 'Unknown dependency: ' + module
);
919 if ( registry
[module
].skip
!== null ) {
920 /*jshint evil:true */
921 skip
= new Function( registry
[module
].skip
);
922 registry
[module
].skip
= null;
924 registry
[module
].skipped
= true;
925 registry
[module
].dependencies
= [];
926 registry
[module
].state
= 'ready';
927 handlePending( module
);
932 // Resolves dynamic loader function and replaces it with its own results
933 if ( $.isFunction( registry
[module
].dependencies
) ) {
934 registry
[module
].dependencies
= registry
[module
].dependencies();
935 // Ensures the module's dependencies are always in an array
936 if ( typeof registry
[module
].dependencies
!== 'object' ) {
937 registry
[module
].dependencies
= [registry
[module
].dependencies
];
940 if ( $.inArray( module
, resolved
) !== -1 ) {
941 // Module already resolved; nothing to do
944 // Create unresolved if not passed in
948 // Tracks down dependencies
949 deps
= registry
[module
].dependencies
;
951 for ( n
= 0; n
< len
; n
+= 1 ) {
952 if ( $.inArray( deps
[n
], resolved
) === -1 ) {
953 if ( unresolved
[deps
[n
]] ) {
955 'Circular reference detected: ' + module
+
961 unresolved
[module
] = true;
962 sortDependencies( deps
[n
], resolved
, unresolved
);
963 delete unresolved
[module
];
966 resolved
[resolved
.length
] = module
;
970 * Get a list of module names that a module depends on in their proper dependency
974 * @param {string[]} module Array of string module names
975 * @return {Array} List of dependencies, including 'module'.
977 function resolve( modules
) {
979 $.each( modules
, function ( idx
, module
) {
980 sortDependencies( module
, resolved
);
986 * Determine whether all dependencies are in state 'ready', which means we may
987 * execute the module or job now.
990 * @param {Array} module Names of modules to be checked
991 * @return {boolean} True if all modules are in state 'ready', false otherwise
993 function allReady( modules
) {
995 for ( i
= 0; i
< modules
.length
; i
++ ) {
996 if ( mw
.loader
.getState( modules
[i
] ) !== 'ready' ) {
1004 * Determine whether all dependencies are in state 'ready', which means we may
1005 * execute the module or job now.
1008 * @param {Array} modules Names of modules to be checked
1009 * @return {boolean} True if no modules are in state 'error' or 'missing', false otherwise
1011 function anyFailed( modules
) {
1013 for ( i
= 0; i
< modules
.length
; i
++ ) {
1014 state
= mw
.loader
.getState( modules
[i
] );
1015 if ( state
=== 'error' || state
=== 'missing' ) {
1023 * A module has entered state 'ready', 'error', or 'missing'. Automatically update
1024 * pending jobs and modules that depend upon this module. If the given module failed,
1025 * propagate the 'error' state up the dependency tree. Otherwise, go ahead an execute
1026 * all jobs/modules now having their dependencies satisfied.
1028 * Jobs that depend on a failed module, will have their error callback ran (if any).
1031 * @param {string} module Name of module that entered one of the states 'ready', 'error', or 'missing'.
1033 function handlePending( module
) {
1034 var j
, job
, hasErrors
, m
, stateChange
;
1036 if ( registry
[module
].state
=== 'error' || registry
[module
].state
=== 'missing' ) {
1037 // If the current module failed, mark all dependent modules also as failed.
1038 // Iterate until steady-state to propagate the error state upwards in the
1041 stateChange
= false;
1042 for ( m
in registry
) {
1043 if ( registry
[m
].state
!== 'error' && registry
[m
].state
!== 'missing' ) {
1044 if ( anyFailed( registry
[m
].dependencies
) ) {
1045 registry
[m
].state
= 'error';
1050 } while ( stateChange
);
1053 // Execute all jobs whose dependencies are either all satisfied or contain at least one failed module.
1054 for ( j
= 0; j
< jobs
.length
; j
+= 1 ) {
1055 hasErrors
= anyFailed( jobs
[j
].dependencies
);
1056 if ( hasErrors
|| allReady( jobs
[j
].dependencies
) ) {
1057 // All dependencies satisfied, or some have errors
1059 jobs
.splice( j
, 1 );
1063 if ( $.isFunction( job
.error
) ) {
1064 job
.error( new Error( 'Module ' + module
+ ' has failed dependencies' ), [module
] );
1067 if ( $.isFunction( job
.ready
) ) {
1072 // A user-defined callback raised an exception.
1073 // Swallow it to protect our state machine!
1074 mw
.track( 'resourceloader.exception', { exception
: e
, module
: module
, source
: 'load-callback' } );
1079 if ( registry
[module
].state
=== 'ready' ) {
1080 // The current module became 'ready'. Set it in the module store, and recursively execute all
1081 // dependent modules that are loaded and now have all dependencies satisfied.
1082 mw
.loader
.store
.set( module
, registry
[module
] );
1083 for ( m
in registry
) {
1084 if ( registry
[m
].state
=== 'loaded' && allReady( registry
[m
].dependencies
) ) {
1092 * Adds a script tag to the DOM, either using document.write or low-level DOM manipulation,
1093 * depending on whether document-ready has occurred yet and whether we are in async mode.
1096 * @param {string} src URL to script, will be used as the src attribute in the script tag
1097 * @param {Function} [callback] Callback which will be run when the script is done
1098 * @param {boolean} [async=false] Whether to load modules asynchronously.
1099 * Ignored (and defaulted to `true`) if the document-ready event has already occurred.
1101 function addScript( src
, callback
, async
) {
1102 // Using isReady directly instead of storing it locally from a $().ready callback (bug 31895)
1103 if ( $.isReady
|| async
) {
1107 // Force jQuery behaviour to be for crossDomain. Otherwise jQuery would use
1108 // XHR for a same domain request instead of <script>, which changes the request
1109 // headers (potentially missing a cache hit), and reduces caching in general
1110 // since browsers cache XHR much less (if at all). And XHR means we retreive
1111 // text, so we'd need to $.globalEval, which then messes up line numbers.
1115 } ).always( callback
);
1117 /*jshint evil:true */
1118 document
.write( mw
.html
.element( 'script', { 'src': src
}, '' ) );
1120 // Document.write is synchronous, so this is called when it's done.
1121 // FIXME: That's a lie. doc.write isn't actually synchronous.
1128 * Executes a loaded module, making it ready to use
1131 * @param {string} module Module name to execute
1133 function execute( module
) {
1134 var key
, value
, media
, i
, urls
, cssHandle
, checkCssHandles
,
1135 cssHandlesRegistered
= false;
1137 if ( !hasOwn
.call( registry
, module
) ) {
1138 throw new Error( 'Module has not been registered yet: ' + module
);
1139 } else if ( registry
[module
].state
=== 'registered' ) {
1140 throw new Error( 'Module has not been requested from the server yet: ' + module
);
1141 } else if ( registry
[module
].state
=== 'loading' ) {
1142 throw new Error( 'Module has not completed loading yet: ' + module
);
1143 } else if ( registry
[module
].state
=== 'ready' ) {
1144 throw new Error( 'Module has already been executed: ' + module
);
1148 * Define loop-function here for efficiency
1149 * and to avoid re-using badly scoped variables.
1152 function addLink( media
, url
) {
1153 var el
= document
.createElement( 'link' );
1155 // Insert in document *before* setting href
1156 getMarker().before( el
);
1157 el
.rel
= 'stylesheet';
1158 if ( media
&& media
!== 'all' ) {
1161 // If you end up here from an IE exception "SCRIPT: Invalid property value.",
1162 // see #addEmbeddedCSS, bug 31676, and bug 47277 for details.
1166 function runScript() {
1167 var script
, markModuleReady
, nestedAddScript
;
1169 script
= registry
[module
].script
;
1170 markModuleReady = function () {
1171 registry
[module
].state
= 'ready';
1172 handlePending( module
);
1174 nestedAddScript = function ( arr
, callback
, async
, i
) {
1175 // Recursively call addScript() in its own callback
1176 // for each element of arr.
1177 if ( i
>= arr
.length
) {
1178 // We're at the end of the array
1183 addScript( arr
[i
], function () {
1184 nestedAddScript( arr
, callback
, async
, i
+ 1 );
1188 if ( $.isArray( script
) ) {
1189 nestedAddScript( script
, markModuleReady
, registry
[module
].async
, 0 );
1190 } else if ( $.isFunction( script
) ) {
1191 registry
[module
].state
= 'ready';
1192 // Pass jQuery twice so that the signature of the closure which wraps
1193 // the script can bind both '$' and 'jQuery'.
1195 handlePending( module
);
1198 // This needs to NOT use mw.log because these errors are common in production mode
1199 // and not in debug mode, such as when a symbol that should be global isn't exported
1200 registry
[module
].state
= 'error';
1201 mw
.track( 'resourceloader.exception', { exception
: e
, module
: module
, source
: 'module-execute' } );
1202 handlePending( module
);
1206 // This used to be inside runScript, but since that is now fired asychronously
1207 // (after CSS is loaded) we need to set it here right away. It is crucial that
1208 // when execute() is called this is set synchronously, otherwise modules will get
1209 // executed multiple times as the registry will state that it isn't loading yet.
1210 registry
[module
].state
= 'loading';
1212 // Add localizations to message system
1213 if ( $.isPlainObject( registry
[module
].messages
) ) {
1214 mw
.messages
.set( registry
[module
].messages
);
1217 // Initialise templates
1218 if ( registry
[module
].templates
) {
1219 mw
.templates
.set( module
, registry
[module
].templates
);
1222 if ( $.isReady
|| registry
[module
].async
) {
1223 // Make sure we don't run the scripts until all (potentially asynchronous)
1224 // stylesheet insertions have completed.
1227 checkCssHandles = function () {
1228 // cssHandlesRegistered ensures we don't take off too soon, e.g. when
1229 // one of the cssHandles is fired while we're still creating more handles.
1230 if ( cssHandlesRegistered
&& pending
=== 0 && runScript
) {
1232 runScript
= undefined; // Revoke
1235 cssHandle = function () {
1236 var check
= checkCssHandles
;
1238 return function () {
1242 check
= undefined; // Revoke
1248 // We are in blocking mode, and so we can't afford to wait for CSS
1249 cssHandle = function () {};
1251 checkCssHandles
= runScript
;
1254 // Process styles (see also mw.loader.implement)
1255 // * back-compat: { <media>: css }
1256 // * back-compat: { <media>: [url, ..] }
1257 // * { "css": [css, ..] }
1258 // * { "url": { <media>: [url, ..] } }
1259 if ( $.isPlainObject( registry
[module
].style
) ) {
1260 for ( key
in registry
[module
].style
) {
1261 value
= registry
[module
].style
[key
];
1264 if ( key
!== 'url' && key
!== 'css' ) {
1265 // Backwards compatibility, key is a media-type
1266 if ( typeof value
=== 'string' ) {
1267 // back-compat: { <media>: css }
1268 // Ignore 'media' because it isn't supported (nor was it used).
1269 // Strings are pre-wrapped in "@media". The media-type was just ""
1270 // (because it had to be set to something).
1271 // This is one of the reasons why this format is no longer used.
1272 addEmbeddedCSS( value
, cssHandle() );
1274 // back-compat: { <media>: [url, ..] }
1280 // Array of css strings in key 'css',
1281 // or back-compat array of urls from media-type
1282 if ( $.isArray( value
) ) {
1283 for ( i
= 0; i
< value
.length
; i
+= 1 ) {
1284 if ( key
=== 'bc-url' ) {
1285 // back-compat: { <media>: [url, ..] }
1286 addLink( media
, value
[i
] );
1287 } else if ( key
=== 'css' ) {
1288 // { "css": [css, ..] }
1289 addEmbeddedCSS( value
[i
], cssHandle() );
1292 // Not an array, but a regular object
1293 // Array of urls inside media-type key
1294 } else if ( typeof value
=== 'object' ) {
1295 // { "url": { <media>: [url, ..] } }
1296 for ( media
in value
) {
1297 urls
= value
[media
];
1298 for ( i
= 0; i
< urls
.length
; i
+= 1 ) {
1299 addLink( media
, urls
[i
] );
1307 cssHandlesRegistered
= true;
1312 * Adds a dependencies to the queue with optional callbacks to be run
1313 * when the dependencies are ready or fail
1316 * @param {string|string[]} dependencies Module name or array of string module names
1317 * @param {Function} [ready] Callback to execute when all dependencies are ready
1318 * @param {Function} [error] Callback to execute when any dependency fails
1319 * @param {boolean} [async=false] Whether to load modules asynchronously.
1320 * Ignored (and defaulted to `true`) if the document-ready event has already occurred.
1322 function request( dependencies
, ready
, error
, async
) {
1323 // Allow calling by single module name
1324 if ( typeof dependencies
=== 'string' ) {
1325 dependencies
= [dependencies
];
1328 // Add ready and error callbacks if they were given
1329 if ( ready
!== undefined || error
!== undefined ) {
1330 jobs
[jobs
.length
] = {
1331 dependencies
: $.grep( dependencies
, function ( module
) {
1332 var state
= mw
.loader
.getState( module
);
1333 return state
=== 'registered' || state
=== 'loaded' || state
=== 'loading';
1340 $.each( dependencies
, function ( idx
, module
) {
1341 var state
= mw
.loader
.getState( module
);
1342 if ( state
=== 'registered' && $.inArray( module
, queue
) === -1 ) {
1343 queue
.push( module
);
1345 registry
[module
].async
= true;
1353 function sortQuery( o
) {
1359 if ( hasOwn
.call( o
, key
) ) {
1364 for ( key
= 0; key
< a
.length
; key
+= 1 ) {
1365 sorted
[a
[key
]] = o
[a
[key
]];
1371 * Converts a module map of the form { foo: [ 'bar', 'baz' ], bar: [ 'baz, 'quux' ] }
1372 * to a query string of the form foo.bar,baz|bar.baz,quux
1375 function buildModulesString( moduleMap
) {
1379 for ( prefix
in moduleMap
) {
1380 p
= prefix
=== '' ? '' : prefix
+ '.';
1381 arr
.push( p
+ moduleMap
[prefix
].join( ',' ) );
1383 return arr
.join( '|' );
1387 * Asynchronously append a script tag to the end of the body
1388 * that invokes load.php
1390 * @param {Object} moduleMap Module map, see #buildModulesString
1391 * @param {Object} currReqBase Object with other parameters (other than 'modules') to use in the request
1392 * @param {string} sourceLoadScript URL of load.php
1393 * @param {boolean} async Whether to load modules asynchronously.
1394 * Ignored (and defaulted to `true`) if the document-ready event has already occurred.
1396 function doRequest( moduleMap
, currReqBase
, sourceLoadScript
, async
) {
1397 var request
= $.extend(
1398 { modules
: buildModulesString( moduleMap
) },
1401 request
= sortQuery( request
);
1403 // Append &* to satisfy load.php's WebRequest::checkUrlExtension test. This script
1404 // isn't actually used in IE6, but MediaWiki enforces it in general.
1405 addScript( sourceLoadScript
+ '?' + $.param( request
) + '&*', null, async
);
1409 * Resolve indexed dependencies.
1411 * ResourceLoader uses an optimization to save space which replaces module names in
1412 * dependency lists with the index of that module within the array of module
1413 * registration data if it exists. The benefit is a significant reduction in the data
1414 * size of the startup module. This function changes those dependency lists back to
1415 * arrays of strings.
1417 * @param {Array} modules Modules array
1419 function resolveIndexedDependencies( modules
) {
1420 $.each( modules
, function ( idx
, module
) {
1422 module
[2] = $.map( module
[2], function ( dep
) {
1423 return typeof dep
=== 'number' ? modules
[dep
][0] : dep
;
1429 /* Public Members */
1432 * The module registry is exposed as an aid for debugging and inspecting page
1433 * state; it is not a public interface for modifying the registry.
1439 moduleRegistry
: registry
,
1442 * @inheritdoc #newStyleTag
1445 addStyleTag
: newStyleTag
,
1448 * Batch-request queued dependencies from the server.
1451 var reqBase
, splits
, maxQueryLength
, q
, b
, bSource
, bGroup
, bSourceGroup
,
1452 source
, concatSource
, origBatch
, group
, g
, i
, modules
, maxVersion
, sourceLoadScript
,
1453 currReqBase
, currReqBaseLength
, moduleMap
, l
,
1454 lastDotIndex
, prefix
, suffix
, bytesAdded
, async
;
1456 // Build a list of request parameters common to all requests.
1458 skin
: mw
.config
.get( 'skin' ),
1459 lang
: mw
.config
.get( 'wgUserLanguage' ),
1460 debug
: mw
.config
.get( 'debug' )
1462 // Split module batch by source and by group.
1464 maxQueryLength
= mw
.config
.get( 'wgResourceLoaderMaxQueryLength', 2000 );
1466 // Appends a list of modules from the queue to the batch
1467 for ( q
= 0; q
< queue
.length
; q
+= 1 ) {
1468 // Only request modules which are registered
1469 if ( hasOwn
.call( registry
, queue
[q
] ) && registry
[queue
[q
]].state
=== 'registered' ) {
1470 // Prevent duplicate entries
1471 if ( $.inArray( queue
[q
], batch
) === -1 ) {
1472 batch
[batch
.length
] = queue
[q
];
1473 // Mark registered modules as loading
1474 registry
[queue
[q
]].state
= 'loading';
1479 mw
.loader
.store
.init();
1480 if ( mw
.loader
.store
.enabled
) {
1483 batch
= $.grep( batch
, function ( module
) {
1484 var source
= mw
.loader
.store
.get( module
);
1486 concatSource
.push( source
);
1492 $.globalEval( concatSource
.join( ';' ) );
1494 // Not good, the cached mw.loader.implement calls failed! This should
1495 // never happen, barring ResourceLoader bugs, browser bugs and PEBKACs.
1496 // Depending on how corrupt the string is, it is likely that some
1497 // modules' implement() succeeded while the ones after the error will
1498 // never run and leave their modules in the 'loading' state forever.
1500 // Since this is an error not caused by an individual module but by
1501 // something that infected the implement call itself, don't take any
1502 // risks and clear everything in this cache.
1503 mw
.loader
.store
.clear();
1504 // Re-add the ones still pending back to the batch and let the server
1505 // repopulate these modules to the cache.
1506 // This means that at most one module will be useless (the one that had
1507 // the error) instead of all of them.
1508 mw
.track( 'resourceloader.exception', { exception
: err
, source
: 'store-eval' } );
1509 origBatch
= $.grep( origBatch
, function ( module
) {
1510 return registry
[module
].state
=== 'loading';
1512 batch
= batch
.concat( origBatch
);
1516 // Early exit if there's nothing to load...
1517 if ( !batch
.length
) {
1521 // The queue has been processed into the batch, clear up the queue.
1524 // Always order modules alphabetically to help reduce cache
1525 // misses for otherwise identical content.
1528 // Split batch by source and by group.
1529 for ( b
= 0; b
< batch
.length
; b
+= 1 ) {
1530 bSource
= registry
[batch
[b
]].source
;
1531 bGroup
= registry
[batch
[b
]].group
;
1532 if ( !hasOwn
.call( splits
, bSource
) ) {
1533 splits
[bSource
] = {};
1535 if ( !hasOwn
.call( splits
[bSource
], bGroup
) ) {
1536 splits
[bSource
][bGroup
] = [];
1538 bSourceGroup
= splits
[bSource
][bGroup
];
1539 bSourceGroup
[bSourceGroup
.length
] = batch
[b
];
1542 // Clear the batch - this MUST happen before we append any
1543 // script elements to the body or it's possible that a script
1544 // will be locally cached, instantly load, and work the batch
1545 // again, all before we've cleared it causing each request to
1546 // include modules which are already loaded.
1549 for ( source
in splits
) {
1551 sourceLoadScript
= sources
[source
];
1553 for ( group
in splits
[source
] ) {
1555 // Cache access to currently selected list of
1556 // modules for this group from this source.
1557 modules
= splits
[source
][group
];
1559 // Calculate the highest timestamp
1561 for ( g
= 0; g
< modules
.length
; g
+= 1 ) {
1562 if ( registry
[modules
[g
]].version
> maxVersion
) {
1563 maxVersion
= registry
[modules
[g
]].version
;
1567 currReqBase
= $.extend( { version
: formatVersionNumber( maxVersion
) }, reqBase
);
1568 // For user modules append a user name to the request.
1569 if ( group
=== 'user' && mw
.config
.get( 'wgUserName' ) !== null ) {
1570 currReqBase
.user
= mw
.config
.get( 'wgUserName' );
1572 currReqBaseLength
= $.param( currReqBase
).length
;
1574 // We may need to split up the request to honor the query string length limit,
1575 // so build it piece by piece.
1576 l
= currReqBaseLength
+ 9; // '&modules='.length == 9
1578 moduleMap
= {}; // { prefix: [ suffixes ] }
1580 for ( i
= 0; i
< modules
.length
; i
+= 1 ) {
1581 // Determine how many bytes this module would add to the query string
1582 lastDotIndex
= modules
[i
].lastIndexOf( '.' );
1584 // If lastDotIndex is -1, substr() returns an empty string
1585 prefix
= modules
[i
].substr( 0, lastDotIndex
);
1586 suffix
= modules
[i
].slice( lastDotIndex
+ 1 );
1588 bytesAdded
= hasOwn
.call( moduleMap
, prefix
)
1589 ? suffix
.length
+ 3 // '%2C'.length == 3
1590 : modules
[i
].length
+ 3; // '%7C'.length == 3
1592 // If the request would become too long, create a new one,
1593 // but don't create empty requests
1594 if ( maxQueryLength
> 0 && !$.isEmptyObject( moduleMap
) && l
+ bytesAdded
> maxQueryLength
) {
1595 // This request would become too long, create a new one
1596 // and fire off the old one
1597 doRequest( moduleMap
, currReqBase
, sourceLoadScript
, async
);
1600 l
= currReqBaseLength
+ 9;
1601 mw
.track( 'resourceloader.splitRequest', { maxQueryLength
: maxQueryLength
} );
1603 if ( !hasOwn
.call( moduleMap
, prefix
) ) {
1604 moduleMap
[prefix
] = [];
1606 moduleMap
[prefix
].push( suffix
);
1607 if ( !registry
[modules
[i
]].async
) {
1608 // If this module is blocking, make the entire request blocking
1609 // This is slightly suboptimal, but in practice mixing of blocking
1610 // and async modules will only occur in debug mode.
1615 // If there's anything left in moduleMap, request that too
1616 if ( !$.isEmptyObject( moduleMap
) ) {
1617 doRequest( moduleMap
, currReqBase
, sourceLoadScript
, async
);
1624 * Register a source.
1626 * The #work method will use this information to split up requests by source.
1628 * mw.loader.addSource( 'mediawikiwiki', '//www.mediawiki.org/w/load.php' );
1630 * @param {string} id Short string representing a source wiki, used internally for
1631 * registered modules to indicate where they should be loaded from (usually lowercase a-z).
1632 * @param {Object|string} loadUrl load.php url, may be an object for backwards-compatibility
1635 addSource: function ( id
, loadUrl
) {
1637 // Allow multiple additions
1638 if ( typeof id
=== 'object' ) {
1639 for ( source
in id
) {
1640 mw
.loader
.addSource( source
, id
[source
] );
1645 if ( hasOwn
.call( sources
, id
) ) {
1646 throw new Error( 'source already registered: ' + id
);
1649 if ( typeof loadUrl
=== 'object' ) {
1650 loadUrl
= loadUrl
.loadScript
;
1653 sources
[id
] = loadUrl
;
1659 * Register a module, letting the system know about it and its
1660 * properties. Startup modules contain calls to this function.
1662 * When using multiple module registration by passing an array, dependencies that
1663 * are specified as references to modules within the array will be resolved before
1664 * the modules are registered.
1666 * @param {string|Array} module Module name or array of arrays, each containing
1667 * a list of arguments compatible with this method
1668 * @param {number} version Module version number as a timestamp (falls backs to 0)
1669 * @param {string|Array|Function} dependencies One string or array of strings of module
1670 * names on which this module depends, or a function that returns that array.
1671 * @param {string} [group=null] Group which the module is in
1672 * @param {string} [source='local'] Name of the source
1673 * @param {string} [skip=null] Script body of the skip function
1675 register: function ( module
, version
, dependencies
, group
, source
, skip
) {
1677 // Allow multiple registration
1678 if ( typeof module
=== 'object' ) {
1679 resolveIndexedDependencies( module
);
1680 for ( i
= 0, len
= module
.length
; i
< len
; i
++ ) {
1681 // module is an array of module names
1682 if ( typeof module
[i
] === 'string' ) {
1683 mw
.loader
.register( module
[i
] );
1684 // module is an array of arrays
1685 } else if ( typeof module
[i
] === 'object' ) {
1686 mw
.loader
.register
.apply( mw
.loader
, module
[i
] );
1692 if ( typeof module
!== 'string' ) {
1693 throw new Error( 'module must be a string, not a ' + typeof module
);
1695 if ( hasOwn
.call( registry
, module
) ) {
1696 throw new Error( 'module already registered: ' + module
);
1698 // List the module as registered
1699 registry
[module
] = {
1700 version
: version
!== undefined ? parseInt( version
, 10 ) : 0,
1702 group
: typeof group
=== 'string' ? group
: null,
1703 source
: typeof source
=== 'string' ? source
: 'local',
1704 state
: 'registered',
1705 skip
: typeof skip
=== 'string' ? skip
: null
1707 if ( typeof dependencies
=== 'string' ) {
1708 // Allow dependencies to be given as a single module name
1709 registry
[module
].dependencies
= [ dependencies
];
1710 } else if ( typeof dependencies
=== 'object' || $.isFunction( dependencies
) ) {
1711 // Allow dependencies to be given as an array of module names
1712 // or a function which returns an array
1713 registry
[module
].dependencies
= dependencies
;
1718 * Implement a module given the components that make up the module.
1720 * When #load or #using requests one or more modules, the server
1721 * response contain calls to this function.
1723 * All arguments are required.
1725 * @param {string} module Name of module
1726 * @param {Function|Array} script Function with module code or Array of URLs to
1727 * be used as the src attribute of a new `<script>` tag.
1728 * @param {Object} [style] Should follow one of the following patterns:
1730 * { "css": [css, ..] }
1731 * { "url": { <media>: [url, ..] } }
1733 * And for backwards compatibility (needs to be supported forever due to caching):
1736 * { <media>: [url, ..] }
1738 * The reason css strings are not concatenated anymore is bug 31676. We now check
1739 * whether it's safe to extend the stylesheet.
1741 * @param {Object} [msgs] List of key/value pairs to be added to mw#messages.
1742 * @param {Object} [templates] List of key/value pairs to be added to mw#templates.
1744 implement: function ( module
, script
, style
, msgs
, templates
) {
1746 if ( typeof module
!== 'string' ) {
1747 throw new Error( 'module must be of type string, not ' + typeof module
);
1749 if ( script
&& !$.isFunction( script
) && !$.isArray( script
) ) {
1750 throw new Error( 'script must be of type function or array, not ' + typeof script
);
1752 if ( style
&& !$.isPlainObject( style
) ) {
1753 throw new Error( 'style must be of type object, not ' + typeof style
);
1755 if ( msgs
&& !$.isPlainObject( msgs
) ) {
1756 throw new Error( 'msgs must be of type object, not a ' + typeof msgs
);
1758 if ( templates
&& !$.isPlainObject( templates
) ) {
1759 throw new Error( 'templates must be of type object, not a ' + typeof templates
);
1761 // Automatically register module
1762 if ( !hasOwn
.call( registry
, module
) ) {
1763 mw
.loader
.register( module
);
1765 // Check for duplicate implementation
1766 if ( hasOwn
.call( registry
, module
) && registry
[module
].script
!== undefined ) {
1767 throw new Error( 'module already implemented: ' + module
);
1769 // Attach components
1770 registry
[module
].script
= script
|| [];
1771 registry
[module
].style
= style
|| {};
1772 registry
[module
].messages
= msgs
|| {};
1773 registry
[module
].templates
= templates
|| {};
1774 // The module may already have been marked as erroneous
1775 if ( $.inArray( registry
[module
].state
, ['error', 'missing'] ) === -1 ) {
1776 registry
[module
].state
= 'loaded';
1777 if ( allReady( registry
[module
].dependencies
) ) {
1784 * Execute a function as soon as one or more required modules are ready.
1786 * Example of inline dependency on OOjs:
1788 * mw.loader.using( 'oojs', function () {
1789 * OO.compare( [ 1 ], [ 1 ] );
1792 * @param {string|Array} dependencies Module name or array of modules names the callback
1793 * dependends on to be ready before executing
1794 * @param {Function} [ready] Callback to execute when all dependencies are ready
1795 * @param {Function} [error] Callback to execute if one or more dependencies failed
1796 * @return {jQuery.Promise}
1797 * @since 1.23 this returns a promise
1799 using: function ( dependencies
, ready
, error
) {
1800 var deferred
= $.Deferred();
1802 // Allow calling with a single dependency as a string
1803 if ( typeof dependencies
=== 'string' ) {
1804 dependencies
= [ dependencies
];
1805 } else if ( !$.isArray( dependencies
) ) {
1807 throw new Error( 'Dependencies must be a string or an array' );
1811 deferred
.done( ready
);
1814 deferred
.fail( error
);
1817 // Resolve entire dependency map
1818 dependencies
= resolve( dependencies
);
1819 if ( allReady( dependencies
) ) {
1820 // Run ready immediately
1822 } else if ( anyFailed( dependencies
) ) {
1823 // Execute error immediately if any dependencies have errors
1825 new Error( 'One or more dependencies failed to load' ),
1829 // Not all dependencies are ready: queue up a request
1830 request( dependencies
, deferred
.resolve
, deferred
.reject
);
1833 return deferred
.promise();
1837 * Load an external script or one or more modules.
1839 * @param {string|Array} modules Either the name of a module, array of modules,
1840 * or a URL of an external script or style
1841 * @param {string} [type='text/javascript'] MIME type to use if calling with a URL of an
1842 * external script or style; acceptable values are "text/css" and
1843 * "text/javascript"; if no type is provided, text/javascript is assumed.
1844 * @param {boolean} [async] Whether to load modules asynchronously.
1845 * Ignored (and defaulted to `true`) if the document-ready event has already occurred.
1846 * Defaults to `true` if loading a URL, `false` otherwise.
1848 load: function ( modules
, type
, async
) {
1852 if ( typeof modules
!== 'object' && typeof modules
!== 'string' ) {
1853 throw new Error( 'modules must be a string or an array, not a ' + typeof modules
);
1855 // Allow calling with an external url or single dependency as a string
1856 if ( typeof modules
=== 'string' ) {
1857 if ( /^(https?:)?\/\//.test( modules
) ) {
1858 if ( async
=== undefined ) {
1859 // Assume async for bug 34542
1862 if ( type
=== 'text/css' ) {
1864 // Use properties instead of attributes as IE throws security
1865 // warnings when inserting a <link> tag with a protocol-relative
1866 // URL set though attributes - when on HTTPS. See bug 41331.
1867 l
= document
.createElement( 'link' );
1868 l
.rel
= 'stylesheet';
1870 $( 'head' ).append( l
);
1873 if ( type
=== 'text/javascript' || type
=== undefined ) {
1874 addScript( modules
, null, async
);
1878 throw new Error( 'invalid type for external url, must be text/css or text/javascript. not ' + type
);
1880 // Called with single module
1881 modules
= [ modules
];
1884 // Filter out undefined modules, otherwise resolve() will throw
1885 // an exception for trying to load an undefined module.
1886 // Undefined modules are acceptable here in load(), because load() takes
1887 // an array of unrelated modules, whereas the modules passed to
1888 // using() are related and must all be loaded.
1889 filtered
= $.grep( modules
, function ( module
) {
1890 var state
= mw
.loader
.getState( module
);
1891 return state
!== null && state
!== 'error' && state
!== 'missing';
1894 if ( filtered
.length
=== 0 ) {
1897 // Resolve entire dependency map
1898 filtered
= resolve( filtered
);
1899 // If all modules are ready, or if any modules have errors, nothing to be done.
1900 if ( allReady( filtered
) || anyFailed( filtered
) ) {
1903 // Since some modules are not yet ready, queue up a request.
1904 request( filtered
, undefined, undefined, async
);
1908 * Change the state of one or more modules.
1910 * @param {string|Object} module Module name or object of module name/state pairs
1911 * @param {string} state State name
1913 state: function ( module
, state
) {
1916 if ( typeof module
=== 'object' ) {
1917 for ( m
in module
) {
1918 mw
.loader
.state( m
, module
[m
] );
1922 if ( !hasOwn
.call( registry
, module
) ) {
1923 mw
.loader
.register( module
);
1925 if ( $.inArray( state
, ['ready', 'error', 'missing'] ) !== -1
1926 && registry
[module
].state
!== state
) {
1927 // Make sure pending modules depending on this one get executed if their
1928 // dependencies are now fulfilled!
1929 registry
[module
].state
= state
;
1930 handlePending( module
);
1932 registry
[module
].state
= state
;
1937 * Get the version of a module.
1939 * @param {string} module Name of module
1940 * @return {string|null} The version, or null if the module (or its version) is not
1943 getVersion: function ( module
) {
1944 if ( !hasOwn
.call( registry
, module
) || registry
[module
].version
=== undefined ) {
1947 return formatVersionNumber( registry
[module
].version
);
1951 * Get the state of a module.
1953 * @param {string} module Name of module
1954 * @return {string|null} The state, or null if the module (or its state) is not
1957 getState: function ( module
) {
1958 if ( !hasOwn
.call( registry
, module
) || registry
[module
].state
=== undefined ) {
1961 return registry
[module
].state
;
1965 * Get the names of all registered modules.
1969 getModuleNames: function () {
1970 return $.map( registry
, function ( i
, key
) {
1976 * @inheritdoc mw.inspect#runReports
1979 inspect: function () {
1980 var args
= slice
.call( arguments
);
1981 mw
.loader
.using( 'mediawiki.inspect', function () {
1982 mw
.inspect
.runReports
.apply( mw
.inspect
, args
);
1987 * On browsers that implement the localStorage API, the module store serves as a
1988 * smart complement to the browser cache. Unlike the browser cache, the module store
1989 * can slice a concatenated response from ResourceLoader into its constituent
1990 * modules and cache each of them separately, using each module's versioning scheme
1991 * to determine when the cache should be invalidated.
1994 * @class mw.loader.store
1997 // Whether the store is in use on this page.
2000 // The contents of the store, mapping '[module name]@[version]' keys
2001 // to module implementations.
2005 stats
: { hits
: 0, misses
: 0, expired
: 0 },
2008 * Construct a JSON-serializable object representing the content of the store.
2009 * @return {Object} Module store contents.
2011 toJSON: function () {
2012 return { items
: mw
.loader
.store
.items
, vary
: mw
.loader
.store
.getVary() };
2016 * Get the localStorage key for the entire module store. The key references
2017 * $wgDBname to prevent clashes between wikis which share a common host.
2019 * @return {string} localStorage item key
2021 getStoreKey: function () {
2022 return 'MediaWikiModuleStore:' + mw
.config
.get( 'wgDBname' );
2026 * Get a key on which to vary the module cache.
2027 * @return {string} String of concatenated vary conditions.
2029 getVary: function () {
2031 mw
.config
.get( 'skin' ),
2032 mw
.config
.get( 'wgResourceLoaderStorageVersion' ),
2033 mw
.config
.get( 'wgUserLanguage' )
2038 * Get a key for a specific module. The key format is '[name]@[version]'.
2040 * @param {string} module Module name
2041 * @return {string|null} Module key or null if module does not exist
2043 getModuleKey: function ( module
) {
2044 return hasOwn
.call( registry
, module
) ?
2045 ( module
+ '@' + registry
[module
].version
) : null;
2049 * Initialize the store.
2051 * Retrieves store from localStorage and (if successfully retrieved) decoding
2052 * the stored JSON value to a plain object.
2054 * The try / catch block is used for JSON & localStorage feature detection.
2055 * See the in-line documentation for Modernizr's localStorage feature detection
2056 * code for a full account of why we need a try / catch:
2057 * <https://github.com/Modernizr/Modernizr/blob/v2.7.1/modernizr.js#L771-L796>.
2062 if ( mw
.loader
.store
.enabled
!== null ) {
2067 if ( !mw
.config
.get( 'wgResourceLoaderStorageEnabled' ) ) {
2068 // Disabled by configuration.
2069 // Clear any previous store to free up space. (T66721)
2070 mw
.loader
.store
.clear();
2071 mw
.loader
.store
.enabled
= false;
2074 if ( mw
.config
.get( 'debug' ) ) {
2075 // Disable module store in debug mode
2076 mw
.loader
.store
.enabled
= false;
2081 raw
= localStorage
.getItem( mw
.loader
.store
.getStoreKey() );
2082 // If we get here, localStorage is available; mark enabled
2083 mw
.loader
.store
.enabled
= true;
2084 data
= JSON
.parse( raw
);
2085 if ( data
&& typeof data
.items
=== 'object' && data
.vary
=== mw
.loader
.store
.getVary() ) {
2086 mw
.loader
.store
.items
= data
.items
;
2090 mw
.track( 'resourceloader.exception', { exception
: e
, source
: 'store-localstorage-init' } );
2093 if ( raw
=== undefined ) {
2094 // localStorage failed; disable store
2095 mw
.loader
.store
.enabled
= false;
2097 mw
.loader
.store
.update();
2102 * Retrieve a module from the store and update cache hit stats.
2104 * @param {string} module Module name
2105 * @return {string|boolean} Module implementation or false if unavailable
2107 get: function ( module
) {
2110 if ( !mw
.loader
.store
.enabled
) {
2114 key
= mw
.loader
.store
.getModuleKey( module
);
2115 if ( key
in mw
.loader
.store
.items
) {
2116 mw
.loader
.store
.stats
.hits
++;
2117 return mw
.loader
.store
.items
[key
];
2119 mw
.loader
.store
.stats
.misses
++;
2124 * Stringify a module and queue it for storage.
2126 * @param {string} module Module name
2127 * @param {Object} descriptor The module's descriptor as set in the registry
2129 set: function ( module
, descriptor
) {
2132 if ( !mw
.loader
.store
.enabled
) {
2136 key
= mw
.loader
.store
.getModuleKey( module
);
2139 // Already stored a copy of this exact version
2140 key
in mw
.loader
.store
.items
||
2141 // Module failed to load
2142 descriptor
.state
!== 'ready' ||
2143 // Unversioned, private, or site-/user-specific
2144 ( !descriptor
.version
|| $.inArray( descriptor
.group
, [ 'private', 'user', 'site' ] ) !== -1 ) ||
2145 // Partial descriptor
2146 $.inArray( undefined, [ descriptor
.script
, descriptor
.style
,
2147 descriptor
.messages
, descriptor
.templates
] ) !== -1
2155 JSON
.stringify( module
),
2156 typeof descriptor
.script
=== 'function' ?
2157 String( descriptor
.script
) :
2158 JSON
.stringify( descriptor
.script
),
2159 JSON
.stringify( descriptor
.style
),
2160 JSON
.stringify( descriptor
.messages
),
2161 JSON
.stringify( descriptor
.templates
)
2163 // Attempted workaround for a possible Opera bug (bug T59567).
2164 // This regex should never match under sane conditions.
2165 if ( /^\s*\(/.test( args
[1] ) ) {
2166 args
[1] = 'function' + args
[1];
2167 mw
.track( 'resourceloader.assert', { source
: 'bug-T59567' } );
2170 mw
.track( 'resourceloader.exception', { exception
: e
, source
: 'store-localstorage-json' } );
2174 mw
.loader
.store
.items
[key
] = 'mw.loader.implement(' + args
.join( ',' ) + ');';
2175 mw
.loader
.store
.update();
2179 * Iterate through the module store, removing any item that does not correspond
2180 * (in name and version) to an item in the module registry.
2182 prune: function () {
2185 if ( !mw
.loader
.store
.enabled
) {
2189 for ( key
in mw
.loader
.store
.items
) {
2190 module
= key
.slice( 0, key
.indexOf( '@' ) );
2191 if ( mw
.loader
.store
.getModuleKey( module
) !== key
) {
2192 mw
.loader
.store
.stats
.expired
++;
2193 delete mw
.loader
.store
.items
[key
];
2199 * Clear the entire module store right now.
2201 clear: function () {
2202 mw
.loader
.store
.items
= {};
2203 localStorage
.removeItem( mw
.loader
.store
.getStoreKey() );
2207 * Sync modules to localStorage.
2209 * This function debounces localStorage updates. When called multiple times in
2210 * quick succession, the calls are coalesced into a single update operation.
2211 * This allows us to call #update without having to consider the module load
2212 * queue; the call to localStorage.setItem will be naturally deferred until the
2213 * page is quiescent.
2215 * Because localStorage is shared by all pages with the same origin, if multiple
2216 * pages are loaded with different module sets, the possibility exists that
2217 * modules saved by one page will be clobbered by another. But the impact would
2218 * be minor and the problem would be corrected by subsequent page views.
2222 update
: ( function () {
2227 key
= mw
.loader
.store
.getStoreKey();
2229 if ( !mw
.loader
.store
.enabled
) {
2232 mw
.loader
.store
.prune();
2234 // Replacing the content of the module store might fail if the new
2235 // contents would exceed the browser's localStorage size limit. To
2236 // avoid clogging the browser with stale data, always remove the old
2237 // value before attempting to set the new one.
2238 localStorage
.removeItem( key
);
2239 data
= JSON
.stringify( mw
.loader
.store
);
2240 localStorage
.setItem( key
, data
);
2242 mw
.track( 'resourceloader.exception', { exception
: e
, source
: 'store-localstorage-update' } );
2246 return function () {
2247 clearTimeout( timer
);
2248 timer
= setTimeout( flush
, 2000 );
2256 * HTML construction helper functions
2263 * output = Html.element( 'div', {}, new Html.Raw(
2264 * Html.element( 'img', { src: '<' } )
2266 * mw.log( output ); // <div><img src="<"/></div>
2271 html
: ( function () {
2272 function escapeCallback( s
) {
2289 * Escape a string for HTML.
2291 * Converts special characters to HTML entities.
2293 * mw.html.escape( '< > \' & "' );
2294 * // Returns < > ' & "
2296 * @param {string} s The string to escape
2297 * @return {string} HTML
2299 escape: function ( s
) {
2300 return s
.replace( /['"<>&]/g, escapeCallback
);
2304 * Create an HTML element string, with safe escaping.
2306 * @param {string} name The tag name.
2307 * @param {Object} attrs An object with members mapping element names to values
2308 * @param {Mixed} contents The contents of the element. May be either:
2310 * - string: The string is escaped.
2311 * - null or undefined: The short closing form is used, e.g. `<br/>`.
2312 * - this.Raw: The value attribute is included without escaping.
2313 * - this.Cdata: The value attribute is included, and an exception is
2314 * thrown if it contains an illegal ETAGO delimiter.
2315 * See <http://www.w3.org/TR/1999/REC-html401-19991224/appendix/notes.html#h-B.3.2>.
2316 * @return {string} HTML
2318 element: function ( name
, attrs
, contents
) {
2319 var v
, attrName
, s
= '<' + name
;
2321 for ( attrName
in attrs
) {
2322 v
= attrs
[attrName
];
2323 // Convert name=true, to name=name
2327 } else if ( v
=== false ) {
2330 s
+= ' ' + attrName
+ '="' + this.escape( String( v
) ) + '"';
2332 if ( contents
=== undefined || contents
=== null ) {
2339 switch ( typeof contents
) {
2342 s
+= this.escape( contents
);
2346 // Convert to string
2347 s
+= String( contents
);
2350 if ( contents
instanceof this.Raw
) {
2351 // Raw HTML inclusion
2352 s
+= contents
.value
;
2353 } else if ( contents
instanceof this.Cdata
) {
2355 if ( /<\/[a-zA-z]/.test( contents
.value
) ) {
2356 throw new Error( 'mw.html.element: Illegal end tag found in CDATA' );
2358 s
+= contents
.value
;
2360 throw new Error( 'mw.html.element: Invalid type of contents' );
2363 s
+= '</' + name
+ '>';
2368 * Wrapper object for raw HTML passed to mw.html.element().
2369 * @class mw.html.Raw
2371 Raw: function ( value
) {
2376 * Wrapper object for CDATA element contents passed to mw.html.element()
2377 * @class mw.html.Cdata
2379 Cdata: function ( value
) {
2385 // Skeleton user object. mediawiki.user.js extends this
2392 * Registry and firing of events.
2394 * MediaWiki has various interface components that are extended, enhanced
2395 * or manipulated in some other way by extensions, gadgets and even
2398 * This framework helps streamlining the timing of when these other
2399 * code paths fire their plugins (instead of using document-ready,
2400 * which can and should be limited to firing only once).
2402 * Features like navigating to other wiki pages, previewing an edit
2403 * and editing itself – without a refresh – can then retrigger these
2404 * hooks accordingly to ensure everything still works as expected.
2408 * mw.hook( 'wikipage.content' ).add( fn ).remove( fn );
2409 * mw.hook( 'wikipage.content' ).fire( $content );
2411 * Handlers can be added and fired for arbitrary event names at any time. The same
2412 * event can be fired multiple times. The last run of an event is memorized
2413 * (similar to `$(document).ready` and `$.Deferred().done`).
2414 * This means if an event is fired, and a handler added afterwards, the added
2415 * function will be fired right away with the last given event data.
2417 * Like Deferreds and Promises, the mw.hook object is both detachable and chainable.
2418 * Thus allowing flexible use and optimal maintainability and authority control.
2419 * You can pass around the `add` and/or `fire` method to another piece of code
2420 * without it having to know the event name (or `mw.hook` for that matter).
2422 * var h = mw.hook( 'bar.ready' );
2423 * new mw.Foo( .. ).fetch( { callback: h.fire } );
2425 * Note: Events are documented with an underscore instead of a dot in the event
2426 * name due to jsduck not supporting dots in that position.
2430 hook
: ( function () {
2434 * Create an instance of mw.hook.
2438 * @param {string} name Name of hook.
2441 return function ( name
) {
2442 var list
= hasOwn
.call( lists
, name
) ?
2444 lists
[name
] = $.Callbacks( 'memory' );
2448 * Register a hook handler
2449 * @param {Function...} handler Function to bind.
2455 * Unregister a hook handler
2456 * @param {Function...} handler Function to unbind.
2459 remove
: list
.remove
,
2463 * @param {Mixed...} data
2467 return list
.fireWith
.call( this, null, slice
.call( arguments
) );
2474 // Alias $j to jQuery for backwards compatibility
2475 // @deprecated since 1.23 Use $ or jQuery instead
2476 mw
.log
.deprecate( window
, '$j', $, 'Use $ or jQuery instead.' );
2479 * Log a message to window.console, if possible.
2481 * Useful to force logging of some errors that are otherwise hard to detect (i.e., this logs
2482 * also in production mode). Gets console references in each invocation instead of caching the
2483 * reference, so that debugging tools loaded later are supported (e.g. Firebug Lite in IE).
2487 * @param {string} topic Stream name passed by mw.track
2488 * @param {Object} data Data passed by mw.track
2489 * @param {Error} [data.exception]
2490 * @param {string} data.source Error source
2491 * @param {string} [data.module] Name of module which caused the error
2493 function log( topic
, data
) {
2496 source
= data
.source
,
2497 module
= data
.module
,
2498 console
= window
.console
;
2500 if ( console
&& console
.log
) {
2501 msg
= ( e
? 'Exception' : 'Error' ) + ' in ' + source
;
2503 msg
+= ' in module ' + module
;
2505 msg
+= ( e
? ':' : '.' );
2508 // If we have an exception object, log it to the error channel to trigger a
2509 // proper stacktraces in browsers that support it. No fallback as we have no browsers
2510 // that don't support error(), but do support log().
2511 if ( e
&& console
.error
) {
2512 console
.error( String( e
), e
);
2517 // subscribe to error streams
2518 mw
.trackSubscribe( 'resourceloader.exception', log
);
2519 mw
.trackSubscribe( 'resourceloader.assert', log
);
2521 // Attach to window and globally alias
2522 window
.mw
= window
.mediaWiki
= mw
;