2 * Base library for MediaWiki.
4 * Exposed globally as `mediaWiki` with `mw` as shortcut.
7 * @alternateClassName mediaWiki
16 hasOwn
= Object
.prototype.hasOwnProperty
,
17 slice
= Array
.prototype.slice
,
18 trackCallbacks
= $.Callbacks( 'memory' ),
22 * Log a message to window.console, if possible. Useful to force logging of some
23 * errors that are otherwise hard to detect (I.e., this logs also in production mode).
24 * Gets console references in each invocation, so that delayed debugging tools work
25 * fine. No need for optimization here, which would only result in losing logs.
29 * @param {string} msg text for the log entry.
32 function log( msg
, e
) {
33 var console
= window
.console
;
34 if ( console
&& console
.log
) {
36 // If we have an exception object, log it through .error() to trigger
37 // proper stacktraces in browsers that support it. There are no (known)
38 // browsers that don't support .error(), that do support .log() and
39 // have useful exception handling through .log().
40 if ( e
&& console
.error
) {
41 console
.error( String( e
), e
);
46 // String format helper. Replaces $1, $2 .. $N placeholders with positional
47 // args. Used by Message.prototype.parser() and exported as mw.format().
48 function format( formatString
) {
49 var parameters
= slice
.call( arguments
, 1 );
50 return formatString
.replace( /\$(\d+)/g, function ( str
, match
) {
51 var index
= parseInt( match
, 10 ) - 1;
52 return parameters
[index
] !== undefined ? parameters
[index
] : '$' + match
;
56 /* Object constructors */
59 * Creates an object that can be read from or written to from prototype functions
60 * that allow both single and multiple variables at once.
64 * var addies, wanted, results;
66 * // Create your address book
67 * addies = new mw.Map();
69 * // This data could be coming from an external source (eg. API/AJAX)
71 * 'John Doe' : '10 Wall Street, New York, USA',
72 * 'Jane Jackson' : '21 Oxford St, London, UK',
73 * 'Dominique van Halen' : 'Kalverstraat 7, Amsterdam, NL'
76 * wanted = ['Dominique van Halen', 'George Johnson', 'Jane Jackson'];
78 * // You can detect missing keys first
79 * if ( !addies.exists( wanted ) ) {
80 * // One or more are missing (in this case: "George Johnson")
81 * mw.log( 'One or more names were not found in your address book' );
84 * // Or just let it give you what it can
85 * results = addies.get( wanted, 'Middle of Nowhere, Alaska, US' );
86 * mw.log( results['Jane Jackson'] ); // "21 Oxford St, London, UK"
87 * mw.log( results['George Johnson'] ); // "Middle of Nowhere, Alaska, US"
92 * @param {Object|boolean} [values] Value-bearing object to map, defaults to an empty object.
93 * For backwards-compatibility with mw.config, this can also be `true` in which case values
94 * will be copied to the Window object as global variables (T72470). Values are copied in one
95 * direction only. Changes to globals are not reflected in the map.
97 function Map( values
) {
98 if ( values
=== true ) {
101 // Override #set to also set the global variable
102 this.set = function ( selection
, value
) {
105 if ( $.isPlainObject( selection
) ) {
106 for ( s
in selection
) {
107 setGlobalMapValue( this, s
, selection
[s
] );
111 if ( typeof selection
=== 'string' && arguments
.length
) {
112 setGlobalMapValue( this, selection
, value
);
121 this.values
= values
|| {};
125 * Alias property to the global object.
129 * @param {mw.Map} map
130 * @param {string} key
131 * @param {Mixed} value
133 function setGlobalMapValue( map
, key
, value
) {
134 map
.values
[key
] = value
;
139 // Deprecation notice for mw.config globals (T58550, T72470)
140 map
=== mw
.config
&& 'Use mw.config instead.'
146 * Get the value of one or multiple keys.
148 * If called with no arguments, all values will be returned.
150 * @param {string|Array} selection String key or array of keys to get values for.
151 * @param {Mixed} [fallback] Value to use in case key(s) do not exist.
152 * @return mixed If selection was a string returns the value or null,
153 * If selection was an array, returns an object of key/values (value is null if not found),
154 * If selection was not passed or invalid, will return the 'values' object member (be careful as
155 * objects are always passed by reference in JavaScript!).
156 * @return {string|Object|null} Values as a string or object, null if invalid/inexistent.
158 get: function ( selection
, fallback
) {
160 // If we only do this in the `return` block, it'll fail for the
161 // call to get() from the mutli-selection block.
162 fallback
= arguments
.length
> 1 ? fallback
: null;
164 if ( $.isArray( selection
) ) {
165 selection
= slice
.call( selection
);
167 for ( i
= 0; i
< selection
.length
; i
++ ) {
168 results
[selection
[i
]] = this.get( selection
[i
], fallback
);
173 if ( typeof selection
=== 'string' ) {
174 if ( !hasOwn
.call( this.values
, selection
) ) {
177 return this.values
[selection
];
180 if ( selection
=== undefined ) {
184 // invalid selection key
189 * Sets one or multiple key/value pairs.
191 * @param {string|Object} selection String key to set value for, or object mapping keys to values.
192 * @param {Mixed} [value] Value to set (optional, only in use when key is a string)
193 * @return {boolean} This returns true on success, false on failure.
195 set: function ( selection
, value
) {
198 if ( $.isPlainObject( selection
) ) {
199 for ( s
in selection
) {
200 this.values
[s
] = selection
[s
];
204 if ( typeof selection
=== 'string' && arguments
.length
) {
205 this.values
[selection
] = value
;
212 * Checks if one or multiple keys exist.
214 * @param {Mixed} selection String key or array of keys to check
215 * @return {boolean} Existence of key(s)
217 exists: function ( selection
) {
220 if ( $.isArray( selection
) ) {
221 for ( s
= 0; s
< selection
.length
; s
++ ) {
222 if ( typeof selection
[s
] !== 'string' || !hasOwn
.call( this.values
, selection
[s
] ) ) {
228 return typeof selection
=== 'string' && hasOwn
.call( this.values
, selection
);
233 * Object constructor for messages.
235 * Similar to the Message class in MediaWiki PHP.
237 * Format defaults to 'text'.
243 * 'hello': 'Hello world',
244 * 'hello-user': 'Hello, $1!',
245 * 'welcome-user': 'Welcome back to $2, $1! Last visit by $1: $3'
248 * obj = new mw.Message( mw.messages, 'hello' );
249 * mw.log( obj.text() );
252 * obj = new mw.Message( mw.messages, 'hello-user', [ 'John Doe' ] );
253 * mw.log( obj.text() );
254 * // Hello, John Doe!
256 * obj = new mw.Message( mw.messages, 'welcome-user', [ 'John Doe', 'Wikipedia', '2 hours ago' ] );
257 * mw.log( obj.text() );
258 * // Welcome back to Wikipedia, John Doe! Last visit by John Doe: 2 hours ago
260 * // Using mw.message shortcut
261 * obj = mw.message( 'hello-user', 'John Doe' );
262 * mw.log( obj.text() );
263 * // Hello, John Doe!
265 * // Using mw.msg shortcut
266 * str = mw.msg( 'hello-user', 'John Doe' );
268 * // Hello, John Doe!
270 * // Different formats
271 * obj = new mw.Message( mw.messages, 'hello-user', [ 'John "Wiki" <3 Doe' ] );
273 * obj.format = 'text';
274 * str = obj.toString();
279 * // Hello, John "Wiki" <3 Doe!
281 * mw.log( obj.escaped() );
282 * // Hello, John "Wiki" <3 Doe!
287 * @param {mw.Map} map Message storage
288 * @param {string} key
289 * @param {Array} [parameters]
291 function Message( map
, key
, parameters
) {
292 this.format
= 'text';
295 this.parameters
= parameters
=== undefined ? [] : slice
.call( parameters
);
299 Message
.prototype = {
301 * Simple message parser, does $N replacement and nothing else.
303 * This may be overridden to provide a more complex message parser.
305 * The primary override is in mediawiki.jqueryMsg.
307 * This function will not be called for nonexistent messages.
309 parser: function () {
310 return format
.apply( null, [ this.map
.get( this.key
) ].concat( this.parameters
) );
314 * Appends (does not replace) parameters for replacement to the .parameters property.
316 * @param {Array} parameters
319 params: function ( parameters
) {
321 for ( i
= 0; i
< parameters
.length
; i
+= 1 ) {
322 this.parameters
.push( parameters
[i
] );
328 * Converts message object to its string form based on the state of format.
330 * @return {string} Message as a string in the current form or `<key>` if key does not exist.
332 toString: function () {
335 if ( !this.exists() ) {
336 // Use <key> as text if key does not exist
337 if ( this.format
=== 'escaped' || this.format
=== 'parse' ) {
338 // format 'escaped' and 'parse' need to have the brackets and key html escaped
339 return mw
.html
.escape( '<' + this.key
+ '>' );
341 return '<' + this.key
+ '>';
344 if ( this.format
=== 'plain' || this.format
=== 'text' || this.format
=== 'parse' ) {
345 text
= this.parser();
348 if ( this.format
=== 'escaped' ) {
349 text
= this.parser();
350 text
= mw
.html
.escape( text
);
357 * Changes format to 'parse' and converts message to string
359 * If jqueryMsg is loaded, this parses the message text from wikitext
360 * (where supported) to HTML
362 * Otherwise, it is equivalent to plain.
364 * @return {string} String form of parsed message
367 this.format
= 'parse';
368 return this.toString();
372 * Changes format to 'plain' and converts message to string
374 * This substitutes parameters, but otherwise does not change the
377 * @return {string} String form of plain message
380 this.format
= 'plain';
381 return this.toString();
385 * Changes format to 'text' and converts message to string
387 * If jqueryMsg is loaded, {{-transformation is done where supported
388 * (such as {{plural:}}, {{gender:}}, {{int:}}).
390 * Otherwise, it is equivalent to plain.
393 this.format
= 'text';
394 return this.toString();
398 * Changes the format to 'escaped' and converts message to string
400 * This is equivalent to using the 'text' format (see text method), then
401 * HTML-escaping the output.
403 * @return {string} String form of html escaped message
405 escaped: function () {
406 this.format
= 'escaped';
407 return this.toString();
411 * Checks if message exists
416 exists: function () {
417 return this.map
.exists( this.key
);
428 * Get the current time, measured in milliseconds since January 1, 1970 (UTC).
430 * On browsers that implement the Navigation Timing API, this function will produce floating-point
431 * values with microsecond precision that are guaranteed to be monotonic. On all other browsers,
432 * it will fall back to using `Date`.
434 * @return {number} Current time
437 var perf
= window
.performance
,
438 navStart
= perf
&& perf
.timing
&& perf
.timing
.navigationStart
;
439 return navStart
&& typeof perf
.now
=== 'function' ?
440 function () { return navStart
+ perf
.now(); } :
441 function () { return +new Date(); };
445 * Format a string. Replace $1, $2 ... $N with positional arguments.
449 * @param {string} fmt Format string
450 * @param {Mixed...} parameters Substitutions for $N placeholders.
451 * @return {string} Formatted string
456 * Track an analytic event.
458 * This method provides a generic means for MediaWiki JavaScript code to capture state
459 * information for analysis. Each logged event specifies a string topic name that describes
460 * the kind of event that it is. Topic names consist of dot-separated path components,
461 * arranged from most general to most specific. Each path component should have a clear and
462 * well-defined purpose.
464 * Data handlers are registered via `mw.trackSubscribe`, and receive the full set of
465 * events that match their subcription, including those that fired before the handler was
468 * @param {string} topic Topic name
469 * @param {Object} [data] Data describing the event, encoded as an object
471 track: function ( topic
, data
) {
472 trackQueue
.push( { topic
: topic
, timeStamp
: mw
.now(), data
: data
} );
473 trackCallbacks
.fire( trackQueue
);
477 * Register a handler for subset of analytic events, specified by topic
479 * Handlers will be called once for each tracked event, including any events that fired before the
480 * handler was registered; 'this' is set to a plain object with a 'timeStamp' property indicating
481 * the exact time at which the event fired, a string 'topic' property naming the event, and a
482 * 'data' property which is an object of event-specific data. The event topic and event data are
483 * also passed to the callback as the first and second arguments, respectively.
485 * @param {string} topic Handle events whose name starts with this string prefix
486 * @param {Function} callback Handler to call for each matching tracked event
488 trackSubscribe: function ( topic
, callback
) {
491 trackCallbacks
.add( function ( trackQueue
) {
493 for ( ; seen
< trackQueue
.length
; seen
++ ) {
494 event
= trackQueue
[ seen
];
495 if ( event
.topic
.indexOf( topic
) === 0 ) {
496 callback
.call( event
, event
.topic
, event
.data
);
502 // Make the Map constructor publicly available.
505 // Make the Message constructor publicly available.
509 * Map of configuration values
511 * Check out [the complete list of configuration values](https://www.mediawiki.org/wiki/Manual:Interface/JavaScript#mw.config)
514 * If `$wgLegacyJavaScriptGlobals` is true, this Map will add its values to the
515 * global `window` object.
517 * @property {mw.Map} config
519 // Dummy placeholder. Re-assigned in ResourceLoaderStartUpModule to an instance of `mw.Map`.
523 * Empty object that plugins can be installed in.
529 * Access container for deprecated functionality that can be moved from
530 * from their legacy location and attached to this object (e.g. a global
531 * function that is deprecated and as stop-gap can be exposed through here).
533 * This was reserved for future use but never ended up being used.
535 * @deprecated since 1.22 Let deprecated identifiers keep their original name
536 * and use mw.log#deprecate to create an access container for tracking.
542 * Localization system
548 * Templates associated with a module
551 templates
: new Map(),
556 * Get a message object.
558 * Shorcut for `new mw.Message( mw.messages, key, parameters )`.
561 * @param {string} key Key of message to get
562 * @param {Mixed...} parameters Parameters for the $N replacements in messages.
563 * @return {mw.Message}
565 message: function ( key
) {
566 // Variadic arguments
567 var parameters
= slice
.call( arguments
, 1 );
568 return new Message( mw
.messages
, key
, parameters
);
572 * Get a message string using the (default) 'text' format.
574 * Shortcut for `mw.message( key, parameters... ).text()`.
577 * @param {string} key Key of message to get
578 * @param {Mixed...} parameters Parameters for the $N replacements in messages.
582 return mw
.message
.apply( mw
.message
, arguments
).toString();
586 * Dummy placeholder for {@link mw.log}
590 // Also update the restoration of methods in mediawiki.log.js
591 // when adding or removing methods here.
592 var log = function () {};
600 * Write a message the console's warning channel.
601 * Also logs a stacktrace for easier debugging.
602 * Each action is silently ignored if the browser doesn't support it.
604 * @param {string...} msg Messages to output to console
606 log
.warn = function () {
607 var console
= window
.console
;
608 if ( console
&& console
.warn
&& console
.warn
.apply
) {
609 console
.warn
.apply( console
, arguments
);
610 if ( console
.trace
) {
617 * Create a property in a host object that, when accessed, will produce
618 * a deprecation warning in the console with backtrace.
620 * @param {Object} obj Host object of deprecated property
621 * @param {string} key Name of property to create in `obj`
622 * @param {Mixed} val The value this property should return when accessed
623 * @param {string} [msg] Optional text to include in the deprecation message.
625 log
.deprecate
= !Object
.defineProperty
? function ( obj
, key
, val
) {
627 } : function ( obj
, key
, val
, msg
) {
628 msg
= 'Use of "' + key
+ '" is deprecated.' + ( msg
? ( ' ' + msg
) : '' );
630 Object
.defineProperty( obj
, key
, {
634 mw
.track( 'mw.deprecate', key
);
638 set: function ( newVal
) {
639 mw
.track( 'mw.deprecate', key
);
645 // IE8 can throw on Object.defineProperty
646 // Create a copy of the value to the object.
655 * Client-side module loader which integrates with the MediaWiki ResourceLoader
659 loader
: ( function () {
661 /* Private Members */
664 * Mapping of registered modules
666 * The jquery module is pre-registered, because it must have already
667 * been provided for this object to have been built, and in debug mode
668 * jquery would have been provided through a unique loader request,
669 * making it impossible to hold back registration of jquery until after
672 * For exact details on support for script, style and messages, look at
673 * mw.loader.implement.
679 * 'version': ############## (unix timestamp),
680 * 'dependencies': ['required.foo', 'bar.also', ...], (or) function () {}
681 * 'group': 'somegroup', (or) null,
682 * 'source': 'local', 'someforeignwiki', (or) null
683 * 'state': 'registered', 'loaded', 'loading', 'ready', 'error' or 'missing'
684 * 'skip': 'return !!window.Example', (or) null
686 * // Added during implementation
690 * 'messages': { 'key': 'value' },
699 // Mapping of sources, keyed by source-id, values are strings.
702 // 'sourceId': 'http://foo.bar/w/load.php'
706 // List of modules which will be loaded as when ready
708 // List of modules to be loaded
710 // List of callback functions waiting for modules to be ready to be called
712 // Selector cache for the marker element. Use getMarker() to get/use the marker!
714 // Buffer for addEmbeddedCSS.
716 // Callbacks for addEmbeddedCSS.
717 cssCallbacks
= $.Callbacks();
719 /* Private methods */
721 function getMarker() {
724 $marker
= $( 'meta[name="ResourceLoaderDynamicStyles"]' );
725 if ( !$marker
.length
) {
726 mw
.log( 'No <meta name="ResourceLoaderDynamicStyles"> found, inserting dynamically' );
727 $marker
= $( '<meta>' ).attr( 'name', 'ResourceLoaderDynamicStyles' ).appendTo( 'head' );
734 * Create a new style tag and add it to the DOM.
737 * @param {string} text CSS text
738 * @param {HTMLElement|jQuery} [nextnode=document.head] The element where the style tag should be
739 * inserted before. Otherwise it will be appended to `<head>`.
740 * @return {HTMLElement} Reference to the created `<style>` element.
742 function newStyleTag( text
, nextnode
) {
743 var s
= document
.createElement( 'style' );
744 // Insert into document before setting cssText (bug 33305)
746 // Must be inserted with native insertBefore, not $.fn.before.
747 // When using jQuery to insert it, like $nextnode.before( s ),
748 // then IE6 will throw "Access is denied" when trying to append
749 // to .cssText later. Some kind of weird security measure.
750 // http://stackoverflow.com/q/12586482/319266
751 // Works: jsfiddle.net/zJzMy/1
752 // Fails: jsfiddle.net/uJTQz
753 // Works again: http://jsfiddle.net/Azr4w/ (diff: the next 3 lines)
754 if ( nextnode
.jquery
) {
755 nextnode
= nextnode
.get( 0 );
757 nextnode
.parentNode
.insertBefore( s
, nextnode
);
759 document
.getElementsByTagName( 'head' )[0].appendChild( s
);
761 if ( s
.styleSheet
) {
763 s
.styleSheet
.cssText
= text
;
766 // (Safari sometimes borks on non-string values,
767 // play safe by casting to a string, just in case.)
768 s
.appendChild( document
.createTextNode( String( text
) ) );
774 * Checks whether it is safe to add this css to a stylesheet.
777 * @param {string} cssText
778 * @return {boolean} False if a new one must be created.
780 function canExpandStylesheetWith( cssText
) {
781 // Makes sure that cssText containing `@import`
782 // rules will end up in a new stylesheet (as those only work when
783 // placed at the start of a stylesheet; bug 35562).
784 return cssText
.indexOf( '@import' ) === -1;
788 * Add a bit of CSS text to the current browser page.
790 * The CSS will be appended to an existing ResourceLoader-created `<style>` tag
791 * or create a new one based on whether the given `cssText` is safe for extension.
793 * @param {string} [cssText=cssBuffer] If called without cssText,
794 * the internal buffer will be inserted instead.
795 * @param {Function} [callback]
797 function addEmbeddedCSS( cssText
, callback
) {
801 cssCallbacks
.add( callback
);
804 // Yield once before inserting the <style> tag. There are likely
805 // more calls coming up which we can combine this way.
806 // Appending a stylesheet and waiting for the browser to repaint
807 // is fairly expensive, this reduces it (bug 45810)
809 // Be careful not to extend the buffer with css that needs a new stylesheet
810 if ( !cssBuffer
|| canExpandStylesheetWith( cssText
) ) {
811 // Linebreak for somewhat distinguishable sections
812 // (the rl-cachekey comment separating each)
813 cssBuffer
+= '\n' + cssText
;
814 // TODO: Use requestAnimationFrame in the future which will
815 // perform even better by not injecting styles while the browser
817 setTimeout( function () {
818 // Can't pass addEmbeddedCSS to setTimeout directly because Firefox
819 // (below version 13) has the non-standard behaviour of passing a
820 // numerical "lateness" value as first argument to this callback
821 // http://benalman.com/news/2009/07/the-mysterious-firefox-settime/
827 // This is a delayed call and we got a buffer still
828 } else if ( cssBuffer
) {
832 // This is a delayed call, but buffer is already cleared by
833 // another delayed call.
837 // By default, always create a new <style>. Appending text to a <style>
838 // tag is bad as it means the contents have to be re-parsed (bug 45810).
840 // Except, of course, in IE 9 and below. In there we default to re-using and
841 // appending to a <style> tag due to the IE stylesheet limit (bug 31676).
842 if ( 'documentMode' in document
&& document
.documentMode
<= 9 ) {
844 $style
= getMarker().prev();
845 // Verify that the element before Marker actually is a
846 // <style> tag and one that came from ResourceLoader
847 // (not some other style tag or even a `<meta>` or `<script>`).
848 if ( $style
.data( 'ResourceLoaderDynamicStyleTag' ) === true ) {
849 // There's already a dynamic <style> tag present and
850 // canExpandStylesheetWith() gave a green light to append more to it.
851 styleEl
= $style
.get( 0 );
852 if ( styleEl
.styleSheet
) {
854 styleEl
.styleSheet
.cssText
+= cssText
; // IE
856 log( 'Stylesheet error', e
);
859 styleEl
.appendChild( document
.createTextNode( String( cssText
) ) );
861 cssCallbacks
.fire().empty();
866 $( newStyleTag( cssText
, getMarker() ) ).data( 'ResourceLoaderDynamicStyleTag', true );
868 cssCallbacks
.fire().empty();
872 * Convert UNIX timestamp to ISO8601 format
873 * @param {number} timestamp UNIX timestamp
876 function formatVersionNumber( timestamp
) {
878 function pad( a
, b
, c
) {
879 return [a
< 10 ? '0' + a
: a
, b
< 10 ? '0' + b
: b
, c
< 10 ? '0' + c
: c
].join( '' );
881 d
.setTime( timestamp
* 1000 );
883 pad( d
.getUTCFullYear(), d
.getUTCMonth() + 1, d
.getUTCDate() ), 'T',
884 pad( d
.getUTCHours(), d
.getUTCMinutes(), d
.getUTCSeconds() ), 'Z'
889 * Resolves dependencies and detects circular references.
892 * @param {string} module Name of the top-level module whose dependencies shall be
893 * resolved and sorted.
894 * @param {Array} resolved Returns a topological sort of the given module and its
895 * dependencies, such that later modules depend on earlier modules. The array
896 * contains the module names. If the array contains already some module names,
897 * this function appends its result to the pre-existing array.
898 * @param {Object} [unresolved] Hash used to track the current dependency
899 * chain; used to report loops in the dependency graph.
900 * @throws {Error} If any unregistered module or a dependency loop is encountered
902 function sortDependencies( module
, resolved
, unresolved
) {
903 var n
, deps
, len
, skip
;
905 if ( !hasOwn
.call( registry
, module
) ) {
906 throw new Error( 'Unknown dependency: ' + module
);
909 if ( registry
[module
].skip
!== null ) {
910 /*jshint evil:true */
911 skip
= new Function( registry
[module
].skip
);
912 registry
[module
].skip
= null;
914 registry
[module
].skipped
= true;
915 registry
[module
].dependencies
= [];
916 registry
[module
].state
= 'ready';
917 handlePending( module
);
922 // Resolves dynamic loader function and replaces it with its own results
923 if ( $.isFunction( registry
[module
].dependencies
) ) {
924 registry
[module
].dependencies
= registry
[module
].dependencies();
925 // Ensures the module's dependencies are always in an array
926 if ( typeof registry
[module
].dependencies
!== 'object' ) {
927 registry
[module
].dependencies
= [registry
[module
].dependencies
];
930 if ( $.inArray( module
, resolved
) !== -1 ) {
931 // Module already resolved; nothing to do.
934 // unresolved is optional, supply it if not passed in
938 // Tracks down dependencies
939 deps
= registry
[module
].dependencies
;
941 for ( n
= 0; n
< len
; n
+= 1 ) {
942 if ( $.inArray( deps
[n
], resolved
) === -1 ) {
943 if ( unresolved
[deps
[n
]] ) {
945 'Circular reference detected: ' + module
+
951 unresolved
[module
] = true;
952 sortDependencies( deps
[n
], resolved
, unresolved
);
953 delete unresolved
[module
];
956 resolved
[resolved
.length
] = module
;
960 * Gets a list of module names that a module depends on in their proper dependency
964 * @param {string} module Module name or array of string module names
965 * @return {Array} list of dependencies, including 'module'.
966 * @throws {Error} If circular reference is detected
968 function resolve( module
) {
971 // Allow calling with an array of module names
972 if ( $.isArray( module
) ) {
974 for ( m
= 0; m
< module
.length
; m
+= 1 ) {
975 sortDependencies( module
[m
], resolved
);
980 if ( typeof module
=== 'string' ) {
982 sortDependencies( module
, resolved
);
986 throw new Error( 'Invalid module argument: ' + module
);
990 * Narrows a list of module names down to those matching a specific
991 * state (see comment on top of this scope for a list of valid states).
992 * One can also filter for 'unregistered', which will return the
993 * modules names that don't have a registry entry.
996 * @param {string|string[]} states Module states to filter by
997 * @param {Array} [modules] List of module names to filter (optional, by default the entire
999 * @return {Array} List of filtered module names
1001 function filter( states
, modules
) {
1002 var list
, module
, s
, m
;
1004 // Allow states to be given as a string
1005 if ( typeof states
=== 'string' ) {
1008 // If called without a list of modules, build and use a list of all modules
1010 if ( modules
=== undefined ) {
1012 for ( module
in registry
) {
1013 modules
[modules
.length
] = module
;
1016 // Build a list of modules which are in one of the specified states
1017 for ( s
= 0; s
< states
.length
; s
+= 1 ) {
1018 for ( m
= 0; m
< modules
.length
; m
+= 1 ) {
1019 if ( !hasOwn
.call( registry
, modules
[m
] ) ) {
1020 // Module does not exist
1021 if ( states
[s
] === 'unregistered' ) {
1023 list
[list
.length
] = modules
[m
];
1026 // Module exists, check state
1027 if ( registry
[modules
[m
]].state
=== states
[s
] ) {
1028 // OK, correct state
1029 list
[list
.length
] = modules
[m
];
1038 * Determine whether all dependencies are in state 'ready', which means we may
1039 * execute the module or job now.
1042 * @param {Array} dependencies Dependencies (module names) to be checked.
1043 * @return {boolean} True if all dependencies are in state 'ready', false otherwise
1045 function allReady( dependencies
) {
1046 return filter( 'ready', dependencies
).length
=== dependencies
.length
;
1050 * A module has entered state 'ready', 'error', or 'missing'. Automatically update pending jobs
1051 * and modules that depend upon this module. if the given module failed, propagate the 'error'
1052 * state up the dependency tree; otherwise, execute all jobs/modules that now have all their
1053 * dependencies satisfied. On jobs depending on a failed module, run the error callback, if any.
1056 * @param {string} module Name of module that entered one of the states 'ready', 'error', or 'missing'.
1058 function handlePending( module
) {
1059 var j
, job
, hasErrors
, m
, stateChange
;
1062 if ( $.inArray( registry
[module
].state
, ['error', 'missing'] ) !== -1 ) {
1063 // If the current module failed, mark all dependent modules also as failed.
1064 // Iterate until steady-state to propagate the error state upwards in the
1067 stateChange
= false;
1068 for ( m
in registry
) {
1069 if ( $.inArray( registry
[m
].state
, ['error', 'missing'] ) === -1 ) {
1070 if ( filter( ['error', 'missing'], registry
[m
].dependencies
).length
> 0 ) {
1071 registry
[m
].state
= 'error';
1076 } while ( stateChange
);
1079 // Execute all jobs whose dependencies are either all satisfied or contain at least one failed module.
1080 for ( j
= 0; j
< jobs
.length
; j
+= 1 ) {
1081 hasErrors
= filter( ['error', 'missing'], jobs
[j
].dependencies
).length
> 0;
1082 if ( hasErrors
|| allReady( jobs
[j
].dependencies
) ) {
1083 // All dependencies satisfied, or some have errors
1085 jobs
.splice( j
, 1 );
1089 if ( $.isFunction( job
.error
) ) {
1090 job
.error( new Error( 'Module ' + module
+ ' has failed dependencies' ), [module
] );
1093 if ( $.isFunction( job
.ready
) ) {
1098 // A user-defined callback raised an exception.
1099 // Swallow it to protect our state machine!
1100 log( 'Exception thrown by user callback', e
);
1105 if ( registry
[module
].state
=== 'ready' ) {
1106 // The current module became 'ready'. Set it in the module store, and recursively execute all
1107 // dependent modules that are loaded and now have all dependencies satisfied.
1108 mw
.loader
.store
.set( module
, registry
[module
] );
1109 for ( m
in registry
) {
1110 if ( registry
[m
].state
=== 'loaded' && allReady( registry
[m
].dependencies
) ) {
1118 * Adds a script tag to the DOM, either using document.write or low-level DOM manipulation,
1119 * depending on whether document-ready has occurred yet and whether we are in async mode.
1122 * @param {string} src URL to script, will be used as the src attribute in the script tag
1123 * @param {Function} [callback] Callback which will be run when the script is done
1124 * @param {boolean} [async=false] Whether to load modules asynchronously.
1125 * Ignored (and defaulted to `true`) if the document-ready event has already occurred.
1127 function addScript( src
, callback
, async
) {
1128 // Using isReady directly instead of storing it locally from a $().ready callback (bug 31895)
1129 if ( $.isReady
|| async
) {
1133 // Force jQuery behaviour to be for crossDomain. Otherwise jQuery would use
1134 // XHR for a same domain request instead of <script>, which changes the request
1135 // headers (potentially missing a cache hit), and reduces caching in general
1136 // since browsers cache XHR much less (if at all). And XHR means we retreive
1137 // text, so we'd need to $.globalEval, which then messes up line numbers.
1141 } ).always( callback
);
1143 /*jshint evil:true */
1144 document
.write( mw
.html
.element( 'script', { 'src': src
}, '' ) );
1146 // Document.write is synchronous, so this is called when it's done.
1147 // FIXME: That's a lie. doc.write isn't actually synchronous.
1154 * Executes a loaded module, making it ready to use
1157 * @param {string} module Module name to execute
1159 function execute( module
) {
1160 var key
, value
, media
, i
, urls
, cssHandle
, checkCssHandles
,
1161 cssHandlesRegistered
= false;
1163 if ( !hasOwn
.call( registry
, module
) ) {
1164 throw new Error( 'Module has not been registered yet: ' + module
);
1165 } else if ( registry
[module
].state
=== 'registered' ) {
1166 throw new Error( 'Module has not been requested from the server yet: ' + module
);
1167 } else if ( registry
[module
].state
=== 'loading' ) {
1168 throw new Error( 'Module has not completed loading yet: ' + module
);
1169 } else if ( registry
[module
].state
=== 'ready' ) {
1170 throw new Error( 'Module has already been executed: ' + module
);
1174 * Define loop-function here for efficiency
1175 * and to avoid re-using badly scoped variables.
1178 function addLink( media
, url
) {
1179 var el
= document
.createElement( 'link' );
1180 // For IE: Insert in document *before* setting href
1181 getMarker().before( el
);
1182 el
.rel
= 'stylesheet';
1183 if ( media
&& media
!== 'all' ) {
1186 // If you end up here from an IE exception "SCRIPT: Invalid property value.",
1187 // see #addEmbeddedCSS, bug 31676, and bug 47277 for details.
1191 function runScript() {
1192 var script
, markModuleReady
, nestedAddScript
;
1194 script
= registry
[module
].script
;
1195 markModuleReady = function () {
1196 registry
[module
].state
= 'ready';
1197 handlePending( module
);
1199 nestedAddScript = function ( arr
, callback
, async
, i
) {
1200 // Recursively call addScript() in its own callback
1201 // for each element of arr.
1202 if ( i
>= arr
.length
) {
1203 // We're at the end of the array
1208 addScript( arr
[i
], function () {
1209 nestedAddScript( arr
, callback
, async
, i
+ 1 );
1213 if ( $.isArray( script
) ) {
1214 nestedAddScript( script
, markModuleReady
, registry
[module
].async
, 0 );
1215 } else if ( $.isFunction( script
) ) {
1216 registry
[module
].state
= 'ready';
1217 // Pass jQuery twice so that the signature of the closure which wraps
1218 // the script can bind both '$' and 'jQuery'.
1220 handlePending( module
);
1223 // This needs to NOT use mw.log because these errors are common in production mode
1224 // and not in debug mode, such as when a symbol that should be global isn't exported
1225 log( 'Exception thrown by ' + module
, e
);
1226 registry
[module
].state
= 'error';
1227 handlePending( module
);
1231 // This used to be inside runScript, but since that is now fired asychronously
1232 // (after CSS is loaded) we need to set it here right away. It is crucial that
1233 // when execute() is called this is set synchronously, otherwise modules will get
1234 // executed multiple times as the registry will state that it isn't loading yet.
1235 registry
[module
].state
= 'loading';
1237 // Add localizations to message system
1238 if ( $.isPlainObject( registry
[module
].messages
) ) {
1239 mw
.messages
.set( registry
[module
].messages
);
1242 // Initialise templates
1243 if ( registry
[module
].templates
) {
1244 mw
.templates
.set( module
, registry
[module
].templates
);
1247 if ( $.isReady
|| registry
[module
].async
) {
1248 // Make sure we don't run the scripts until all (potentially asynchronous)
1249 // stylesheet insertions have completed.
1252 checkCssHandles = function () {
1253 // cssHandlesRegistered ensures we don't take off too soon, e.g. when
1254 // one of the cssHandles is fired while we're still creating more handles.
1255 if ( cssHandlesRegistered
&& pending
=== 0 && runScript
) {
1257 runScript
= undefined; // Revoke
1260 cssHandle = function () {
1261 var check
= checkCssHandles
;
1263 return function () {
1267 check
= undefined; // Revoke
1273 // We are in blocking mode, and so we can't afford to wait for CSS
1274 cssHandle = function () {};
1276 checkCssHandles
= runScript
;
1279 // Process styles (see also mw.loader.implement)
1280 // * back-compat: { <media>: css }
1281 // * back-compat: { <media>: [url, ..] }
1282 // * { "css": [css, ..] }
1283 // * { "url": { <media>: [url, ..] } }
1284 if ( $.isPlainObject( registry
[module
].style
) ) {
1285 for ( key
in registry
[module
].style
) {
1286 value
= registry
[module
].style
[key
];
1289 if ( key
!== 'url' && key
!== 'css' ) {
1290 // Backwards compatibility, key is a media-type
1291 if ( typeof value
=== 'string' ) {
1292 // back-compat: { <media>: css }
1293 // Ignore 'media' because it isn't supported (nor was it used).
1294 // Strings are pre-wrapped in "@media". The media-type was just ""
1295 // (because it had to be set to something).
1296 // This is one of the reasons why this format is no longer used.
1297 addEmbeddedCSS( value
, cssHandle() );
1299 // back-compat: { <media>: [url, ..] }
1305 // Array of css strings in key 'css',
1306 // or back-compat array of urls from media-type
1307 if ( $.isArray( value
) ) {
1308 for ( i
= 0; i
< value
.length
; i
+= 1 ) {
1309 if ( key
=== 'bc-url' ) {
1310 // back-compat: { <media>: [url, ..] }
1311 addLink( media
, value
[i
] );
1312 } else if ( key
=== 'css' ) {
1313 // { "css": [css, ..] }
1314 addEmbeddedCSS( value
[i
], cssHandle() );
1317 // Not an array, but a regular object
1318 // Array of urls inside media-type key
1319 } else if ( typeof value
=== 'object' ) {
1320 // { "url": { <media>: [url, ..] } }
1321 for ( media
in value
) {
1322 urls
= value
[media
];
1323 for ( i
= 0; i
< urls
.length
; i
+= 1 ) {
1324 addLink( media
, urls
[i
] );
1332 cssHandlesRegistered
= true;
1337 * Adds a dependencies to the queue with optional callbacks to be run
1338 * when the dependencies are ready or fail
1341 * @param {string|string[]} dependencies Module name or array of string module names
1342 * @param {Function} [ready] Callback to execute when all dependencies are ready
1343 * @param {Function} [error] Callback to execute when any dependency fails
1344 * @param {boolean} [async=false] Whether to load modules asynchronously.
1345 * Ignored (and defaulted to `true`) if the document-ready event has already occurred.
1347 function request( dependencies
, ready
, error
, async
) {
1350 // Allow calling by single module name
1351 if ( typeof dependencies
=== 'string' ) {
1352 dependencies
= [dependencies
];
1355 // Add ready and error callbacks if they were given
1356 if ( ready
!== undefined || error
!== undefined ) {
1357 jobs
[jobs
.length
] = {
1358 'dependencies': filter(
1359 ['registered', 'loading', 'loaded'],
1367 // Queue up any dependencies that are registered
1368 dependencies
= filter( ['registered'], dependencies
);
1369 for ( n
= 0; n
< dependencies
.length
; n
+= 1 ) {
1370 if ( $.inArray( dependencies
[n
], queue
) === -1 ) {
1371 queue
[queue
.length
] = dependencies
[n
];
1373 // Mark this module as async in the registry
1374 registry
[dependencies
[n
]].async
= true;
1383 function sortQuery( o
) {
1384 var sorted
= {}, key
, a
= [];
1386 if ( hasOwn
.call( o
, key
) ) {
1391 for ( key
= 0; key
< a
.length
; key
+= 1 ) {
1392 sorted
[a
[key
]] = o
[a
[key
]];
1398 * Converts a module map of the form { foo: [ 'bar', 'baz' ], bar: [ 'baz, 'quux' ] }
1399 * to a query string of the form foo.bar,baz|bar.baz,quux
1402 function buildModulesString( moduleMap
) {
1403 var arr
= [], p
, prefix
;
1404 for ( prefix
in moduleMap
) {
1405 p
= prefix
=== '' ? '' : prefix
+ '.';
1406 arr
.push( p
+ moduleMap
[prefix
].join( ',' ) );
1408 return arr
.join( '|' );
1412 * Asynchronously append a script tag to the end of the body
1413 * that invokes load.php
1415 * @param {Object} moduleMap Module map, see #buildModulesString
1416 * @param {Object} currReqBase Object with other parameters (other than 'modules') to use in the request
1417 * @param {string} sourceLoadScript URL of load.php
1418 * @param {boolean} async Whether to load modules asynchronously.
1419 * Ignored (and defaulted to `true`) if the document-ready event has already occurred.
1421 function doRequest( moduleMap
, currReqBase
, sourceLoadScript
, async
) {
1422 var request
= $.extend(
1423 { modules
: buildModulesString( moduleMap
) },
1426 request
= sortQuery( request
);
1427 // Append &* to avoid triggering the IE6 extension check
1428 addScript( sourceLoadScript
+ '?' + $.param( request
) + '&*', null, async
);
1432 * Resolve indexed dependencies.
1434 * ResourceLoader uses an optimization to save space which replaces module names in
1435 * dependency lists with the index of that module within the array of module
1436 * registration data if it exists. The benefit is a significant reduction in the data
1437 * size of the startup module. This function changes those dependency lists back to
1438 * arrays of strings.
1440 * @param {Array} modules Modules array
1442 function resolveIndexedDependencies( modules
) {
1443 var i
, iLen
, j
, jLen
, module
, dependency
;
1445 // Expand indexed dependency names
1446 for ( i
= 0, iLen
= modules
.length
; i
< iLen
; i
++ ) {
1447 module
= modules
[i
];
1449 for ( j
= 0, jLen
= module
[2].length
; j
< jLen
; j
++ ) {
1450 dependency
= module
[2][j
];
1451 if ( typeof dependency
=== 'number' ) {
1452 module
[2][j
] = modules
[dependency
][0];
1459 /* Public Members */
1462 * The module registry is exposed as an aid for debugging and inspecting page
1463 * state; it is not a public interface for modifying the registry.
1469 moduleRegistry
: registry
,
1472 * @inheritdoc #newStyleTag
1475 addStyleTag
: newStyleTag
,
1478 * Batch-request queued dependencies from the server.
1481 var reqBase
, splits
, maxQueryLength
, q
, b
, bSource
, bGroup
, bSourceGroup
,
1482 source
, concatSource
, origBatch
, group
, g
, i
, modules
, maxVersion
, sourceLoadScript
,
1483 currReqBase
, currReqBaseLength
, moduleMap
, l
,
1484 lastDotIndex
, prefix
, suffix
, bytesAdded
, async
;
1486 // Build a list of request parameters common to all requests.
1488 skin
: mw
.config
.get( 'skin' ),
1489 lang
: mw
.config
.get( 'wgUserLanguage' ),
1490 debug
: mw
.config
.get( 'debug' )
1492 // Split module batch by source and by group.
1494 maxQueryLength
= mw
.config
.get( 'wgResourceLoaderMaxQueryLength', -1 );
1496 // Appends a list of modules from the queue to the batch
1497 for ( q
= 0; q
< queue
.length
; q
+= 1 ) {
1498 // Only request modules which are registered
1499 if ( hasOwn
.call( registry
, queue
[q
] ) && registry
[queue
[q
]].state
=== 'registered' ) {
1500 // Prevent duplicate entries
1501 if ( $.inArray( queue
[q
], batch
) === -1 ) {
1502 batch
[batch
.length
] = queue
[q
];
1503 // Mark registered modules as loading
1504 registry
[queue
[q
]].state
= 'loading';
1509 mw
.loader
.store
.init();
1510 if ( mw
.loader
.store
.enabled
) {
1513 batch
= $.grep( batch
, function ( module
) {
1514 var source
= mw
.loader
.store
.get( module
);
1516 concatSource
.push( source
);
1522 $.globalEval( concatSource
.join( ';' ) );
1524 // Not good, the cached mw.loader.implement calls failed! This should
1525 // never happen, barring ResourceLoader bugs, browser bugs and PEBKACs.
1526 // Depending on how corrupt the string is, it is likely that some
1527 // modules' implement() succeeded while the ones after the error will
1528 // never run and leave their modules in the 'loading' state forever.
1530 // Since this is an error not caused by an individual module but by
1531 // something that infected the implement call itself, don't take any
1532 // risks and clear everything in this cache.
1533 mw
.loader
.store
.clear();
1534 // Re-add the ones still pending back to the batch and let the server
1535 // repopulate these modules to the cache.
1536 // This means that at most one module will be useless (the one that had
1537 // the error) instead of all of them.
1538 log( 'Error while evaluating data from mw.loader.store', err
);
1539 origBatch
= $.grep( origBatch
, function ( module
) {
1540 return registry
[module
].state
=== 'loading';
1542 batch
= batch
.concat( origBatch
);
1546 // Early exit if there's nothing to load...
1547 if ( !batch
.length
) {
1551 // The queue has been processed into the batch, clear up the queue.
1554 // Always order modules alphabetically to help reduce cache
1555 // misses for otherwise identical content.
1558 // Split batch by source and by group.
1559 for ( b
= 0; b
< batch
.length
; b
+= 1 ) {
1560 bSource
= registry
[batch
[b
]].source
;
1561 bGroup
= registry
[batch
[b
]].group
;
1562 if ( !hasOwn
.call( splits
, bSource
) ) {
1563 splits
[bSource
] = {};
1565 if ( !hasOwn
.call( splits
[bSource
], bGroup
) ) {
1566 splits
[bSource
][bGroup
] = [];
1568 bSourceGroup
= splits
[bSource
][bGroup
];
1569 bSourceGroup
[bSourceGroup
.length
] = batch
[b
];
1572 // Clear the batch - this MUST happen before we append any
1573 // script elements to the body or it's possible that a script
1574 // will be locally cached, instantly load, and work the batch
1575 // again, all before we've cleared it causing each request to
1576 // include modules which are already loaded.
1579 for ( source
in splits
) {
1581 sourceLoadScript
= sources
[source
];
1583 for ( group
in splits
[source
] ) {
1585 // Cache access to currently selected list of
1586 // modules for this group from this source.
1587 modules
= splits
[source
][group
];
1589 // Calculate the highest timestamp
1591 for ( g
= 0; g
< modules
.length
; g
+= 1 ) {
1592 if ( registry
[modules
[g
]].version
> maxVersion
) {
1593 maxVersion
= registry
[modules
[g
]].version
;
1597 currReqBase
= $.extend( { version
: formatVersionNumber( maxVersion
) }, reqBase
);
1598 // For user modules append a user name to the request.
1599 if ( group
=== 'user' && mw
.config
.get( 'wgUserName' ) !== null ) {
1600 currReqBase
.user
= mw
.config
.get( 'wgUserName' );
1602 currReqBaseLength
= $.param( currReqBase
).length
;
1604 // We may need to split up the request to honor the query string length limit,
1605 // so build it piece by piece.
1606 l
= currReqBaseLength
+ 9; // '&modules='.length == 9
1608 moduleMap
= {}; // { prefix: [ suffixes ] }
1610 for ( i
= 0; i
< modules
.length
; i
+= 1 ) {
1611 // Determine how many bytes this module would add to the query string
1612 lastDotIndex
= modules
[i
].lastIndexOf( '.' );
1614 // If lastDotIndex is -1, substr() returns an empty string
1615 prefix
= modules
[i
].substr( 0, lastDotIndex
);
1616 suffix
= modules
[i
].slice( lastDotIndex
+ 1 );
1618 bytesAdded
= hasOwn
.call( moduleMap
, prefix
)
1619 ? suffix
.length
+ 3 // '%2C'.length == 3
1620 : modules
[i
].length
+ 3; // '%7C'.length == 3
1622 // If the request would become too long, create a new one,
1623 // but don't create empty requests
1624 if ( maxQueryLength
> 0 && !$.isEmptyObject( moduleMap
) && l
+ bytesAdded
> maxQueryLength
) {
1625 // This request would become too long, create a new one
1626 // and fire off the old one
1627 doRequest( moduleMap
, currReqBase
, sourceLoadScript
, async
);
1630 l
= currReqBaseLength
+ 9;
1632 if ( !hasOwn
.call( moduleMap
, prefix
) ) {
1633 moduleMap
[prefix
] = [];
1635 moduleMap
[prefix
].push( suffix
);
1636 if ( !registry
[modules
[i
]].async
) {
1637 // If this module is blocking, make the entire request blocking
1638 // This is slightly suboptimal, but in practice mixing of blocking
1639 // and async modules will only occur in debug mode.
1644 // If there's anything left in moduleMap, request that too
1645 if ( !$.isEmptyObject( moduleMap
) ) {
1646 doRequest( moduleMap
, currReqBase
, sourceLoadScript
, async
);
1653 * Register a source.
1655 * The #work method will use this information to split up requests by source.
1657 * mw.loader.addSource( 'mediawikiwiki', '//www.mediawiki.org/w/load.php' );
1659 * @param {string} id Short string representing a source wiki, used internally for
1660 * registered modules to indicate where they should be loaded from (usually lowercase a-z).
1661 * @param {Object|string} loadUrl load.php url, may be an object for backwards-compatibility
1664 addSource: function ( id
, loadUrl
) {
1666 // Allow multiple additions
1667 if ( typeof id
=== 'object' ) {
1668 for ( source
in id
) {
1669 mw
.loader
.addSource( source
, id
[source
] );
1674 if ( hasOwn
.call( sources
, id
) ) {
1675 throw new Error( 'source already registered: ' + id
);
1678 if ( typeof loadUrl
=== 'object' ) {
1679 loadUrl
= loadUrl
.loadScript
;
1682 sources
[id
] = loadUrl
;
1688 * Register a module, letting the system know about it and its
1689 * properties. Startup modules contain calls to this function.
1691 * When using multiple module registration by passing an array, dependencies that
1692 * are specified as references to modules within the array will be resolved before
1693 * the modules are registered.
1695 * @param {string|Array} module Module name or array of arrays, each containing
1696 * a list of arguments compatible with this method
1697 * @param {number} version Module version number as a timestamp (falls backs to 0)
1698 * @param {string|Array|Function} dependencies One string or array of strings of module
1699 * names on which this module depends, or a function that returns that array.
1700 * @param {string} [group=null] Group which the module is in
1701 * @param {string} [source='local'] Name of the source
1702 * @param {string} [skip=null] Script body of the skip function
1704 register: function ( module
, version
, dependencies
, group
, source
, skip
) {
1706 // Allow multiple registration
1707 if ( typeof module
=== 'object' ) {
1708 resolveIndexedDependencies( module
);
1709 for ( i
= 0, len
= module
.length
; i
< len
; i
++ ) {
1710 // module is an array of module names
1711 if ( typeof module
[i
] === 'string' ) {
1712 mw
.loader
.register( module
[i
] );
1713 // module is an array of arrays
1714 } else if ( typeof module
[i
] === 'object' ) {
1715 mw
.loader
.register
.apply( mw
.loader
, module
[i
] );
1721 if ( typeof module
!== 'string' ) {
1722 throw new Error( 'module must be a string, not a ' + typeof module
);
1724 if ( hasOwn
.call( registry
, module
) ) {
1725 throw new Error( 'module already registered: ' + module
);
1727 // List the module as registered
1728 registry
[module
] = {
1729 version
: version
!== undefined ? parseInt( version
, 10 ) : 0,
1731 group
: typeof group
=== 'string' ? group
: null,
1732 source
: typeof source
=== 'string' ? source
: 'local',
1733 state
: 'registered',
1734 skip
: typeof skip
=== 'string' ? skip
: null
1736 if ( typeof dependencies
=== 'string' ) {
1737 // Allow dependencies to be given as a single module name
1738 registry
[module
].dependencies
= [ dependencies
];
1739 } else if ( typeof dependencies
=== 'object' || $.isFunction( dependencies
) ) {
1740 // Allow dependencies to be given as an array of module names
1741 // or a function which returns an array
1742 registry
[module
].dependencies
= dependencies
;
1747 * Implement a module given the components that make up the module.
1749 * When #load or #using requests one or more modules, the server
1750 * response contain calls to this function.
1752 * All arguments are required.
1754 * @param {string} module Name of module
1755 * @param {Function|Array} script Function with module code or Array of URLs to
1756 * be used as the src attribute of a new `<script>` tag.
1757 * @param {Object} [style] Should follow one of the following patterns:
1759 * { "css": [css, ..] }
1760 * { "url": { <media>: [url, ..] } }
1762 * And for backwards compatibility (needs to be supported forever due to caching):
1765 * { <media>: [url, ..] }
1767 * The reason css strings are not concatenated anymore is bug 31676. We now check
1768 * whether it's safe to extend the stylesheet (see #canExpandStylesheetWith).
1770 * @param {Object} [msgs] List of key/value pairs to be added to mw#messages.
1771 * @param {Object} [templates] List of key/value pairs to be added to mw#templates.
1773 implement: function ( module
, script
, style
, msgs
, templates
) {
1775 if ( typeof module
!== 'string' ) {
1776 throw new Error( 'module must be of type string, not ' + typeof module
);
1778 if ( !$.isFunction( script
) && !$.isArray( script
) ) {
1779 throw new Error( 'script must be of type function or array, not ' + typeof script
);
1781 if ( style
&& !$.isPlainObject( style
) ) {
1782 throw new Error( 'style must be of type object, not ' + typeof style
);
1784 if ( msgs
&& !$.isPlainObject( msgs
) ) {
1785 throw new Error( 'msgs must be of type object, not a ' + typeof msgs
);
1787 if ( templates
&& !$.isPlainObject( templates
) ) {
1788 throw new Error( 'templates must be of type object, not a ' + typeof templates
);
1790 // Automatically register module
1791 if ( !hasOwn
.call( registry
, module
) ) {
1792 mw
.loader
.register( module
);
1794 // Check for duplicate implementation
1795 if ( hasOwn
.call( registry
, module
) && registry
[module
].script
!== undefined ) {
1796 throw new Error( 'module already implemented: ' + module
);
1798 // Attach components
1799 registry
[module
].script
= script
;
1800 registry
[module
].style
= style
|| {};
1801 registry
[module
].messages
= msgs
|| {};
1802 // Templates are optional (for back-compat)
1803 registry
[module
].templates
= templates
|| {};
1804 // The module may already have been marked as erroneous
1805 if ( $.inArray( registry
[module
].state
, ['error', 'missing'] ) === -1 ) {
1806 registry
[module
].state
= 'loaded';
1807 if ( allReady( registry
[module
].dependencies
) ) {
1814 * Execute a function as soon as one or more required modules are ready.
1816 * Example of inline dependency on OOjs:
1818 * mw.loader.using( 'oojs', function () {
1819 * OO.compare( [ 1 ], [ 1 ] );
1822 * @param {string|Array} dependencies Module name or array of modules names the callback
1823 * dependends on to be ready before executing
1824 * @param {Function} [ready] Callback to execute when all dependencies are ready
1825 * @param {Function} [error] Callback to execute if one or more dependencies failed
1826 * @return {jQuery.Promise}
1827 * @since 1.23 this returns a promise
1829 using: function ( dependencies
, ready
, error
) {
1830 var deferred
= $.Deferred();
1832 // Allow calling with a single dependency as a string
1833 if ( typeof dependencies
=== 'string' ) {
1834 dependencies
= [ dependencies
];
1835 } else if ( !$.isArray( dependencies
) ) {
1837 throw new Error( 'Dependencies must be a string or an array' );
1841 deferred
.done( ready
);
1844 deferred
.fail( error
);
1847 // Resolve entire dependency map
1848 dependencies
= resolve( dependencies
);
1849 if ( allReady( dependencies
) ) {
1850 // Run ready immediately
1852 } else if ( filter( ['error', 'missing'], dependencies
).length
) {
1853 // Execute error immediately if any dependencies have errors
1855 new Error( 'One or more dependencies failed to load' ),
1859 // Not all dependencies are ready: queue up a request
1860 request( dependencies
, deferred
.resolve
, deferred
.reject
);
1863 return deferred
.promise();
1867 * Load an external script or one or more modules.
1869 * @param {string|Array} modules Either the name of a module, array of modules,
1870 * or a URL of an external script or style
1871 * @param {string} [type='text/javascript'] MIME type to use if calling with a URL of an
1872 * external script or style; acceptable values are "text/css" and
1873 * "text/javascript"; if no type is provided, text/javascript is assumed.
1874 * @param {boolean} [async] Whether to load modules asynchronously.
1875 * Ignored (and defaulted to `true`) if the document-ready event has already occurred.
1876 * Defaults to `true` if loading a URL, `false` otherwise.
1878 load: function ( modules
, type
, async
) {
1879 var filtered
, m
, module
, l
;
1882 if ( typeof modules
!== 'object' && typeof modules
!== 'string' ) {
1883 throw new Error( 'modules must be a string or an array, not a ' + typeof modules
);
1885 // Allow calling with an external url or single dependency as a string
1886 if ( typeof modules
=== 'string' ) {
1887 // Support adding arbitrary external scripts
1888 if ( /^(https?:)?\/\//.test( modules
) ) {
1889 if ( async
=== undefined ) {
1890 // Assume async for bug 34542
1893 if ( type
=== 'text/css' ) {
1894 // IE7-8 throws security warnings when inserting a <link> tag
1895 // with a protocol-relative URL set though attributes (instead of
1896 // properties) - when on HTTPS. See also bug 41331.
1897 l
= document
.createElement( 'link' );
1898 l
.rel
= 'stylesheet';
1900 $( 'head' ).append( l
);
1903 if ( type
=== 'text/javascript' || type
=== undefined ) {
1904 addScript( modules
, null, async
);
1908 throw new Error( 'invalid type for external url, must be text/css or text/javascript. not ' + type
);
1910 // Called with single module
1911 modules
= [ modules
];
1914 // Filter out undefined modules, otherwise resolve() will throw
1915 // an exception for trying to load an undefined module.
1916 // Undefined modules are acceptable here in load(), because load() takes
1917 // an array of unrelated modules, whereas the modules passed to
1918 // using() are related and must all be loaded.
1919 for ( filtered
= [], m
= 0; m
< modules
.length
; m
+= 1 ) {
1920 if ( hasOwn
.call( registry
, modules
[m
] ) ) {
1921 module
= registry
[modules
[m
]];
1922 if ( $.inArray( module
.state
, ['error', 'missing'] ) === -1 ) {
1923 filtered
[filtered
.length
] = modules
[m
];
1928 if ( filtered
.length
=== 0 ) {
1931 // Resolve entire dependency map
1932 filtered
= resolve( filtered
);
1933 // If all modules are ready, nothing to be done
1934 if ( allReady( filtered
) ) {
1937 // If any modules have errors: also quit.
1938 if ( filter( ['error', 'missing'], filtered
).length
) {
1941 // Since some modules are not yet ready, queue up a request.
1942 request( filtered
, undefined, undefined, async
);
1946 * Change the state of one or more modules.
1948 * @param {string|Object} module Module name or object of module name/state pairs
1949 * @param {string} state State name
1951 state: function ( module
, state
) {
1954 if ( typeof module
=== 'object' ) {
1955 for ( m
in module
) {
1956 mw
.loader
.state( m
, module
[m
] );
1960 if ( !hasOwn
.call( registry
, module
) ) {
1961 mw
.loader
.register( module
);
1963 if ( $.inArray( state
, ['ready', 'error', 'missing'] ) !== -1
1964 && registry
[module
].state
!== state
) {
1965 // Make sure pending modules depending on this one get executed if their
1966 // dependencies are now fulfilled!
1967 registry
[module
].state
= state
;
1968 handlePending( module
);
1970 registry
[module
].state
= state
;
1975 * Get the version of a module.
1977 * @param {string} module Name of module
1978 * @return {string|null} The version, or null if the module (or its version) is not
1981 getVersion: function ( module
) {
1982 if ( !hasOwn
.call( registry
, module
) || registry
[module
].version
=== undefined ) {
1985 return formatVersionNumber( registry
[module
].version
);
1989 * Get the state of a module.
1991 * @param {string} module Name of module
1992 * @return {string|null} The state, or null if the module (or its state) is not
1995 getState: function ( module
) {
1996 if ( !hasOwn
.call( registry
, module
) || registry
[module
].state
=== undefined ) {
1999 return registry
[module
].state
;
2003 * Get the names of all registered modules.
2007 getModuleNames: function () {
2008 return $.map( registry
, function ( i
, key
) {
2014 * @inheritdoc mw.inspect#runReports
2017 inspect: function () {
2018 var args
= slice
.call( arguments
);
2019 mw
.loader
.using( 'mediawiki.inspect', function () {
2020 mw
.inspect
.runReports
.apply( mw
.inspect
, args
);
2025 * On browsers that implement the localStorage API, the module store serves as a
2026 * smart complement to the browser cache. Unlike the browser cache, the module store
2027 * can slice a concatenated response from ResourceLoader into its constituent
2028 * modules and cache each of them separately, using each module's versioning scheme
2029 * to determine when the cache should be invalidated.
2032 * @class mw.loader.store
2035 // Whether the store is in use on this page.
2038 // The contents of the store, mapping '[module name]@[version]' keys
2039 // to module implementations.
2043 stats
: { hits
: 0, misses
: 0, expired
: 0 },
2046 * Construct a JSON-serializable object representing the content of the store.
2047 * @return {Object} Module store contents.
2049 toJSON: function () {
2050 return { items
: mw
.loader
.store
.items
, vary
: mw
.loader
.store
.getVary() };
2054 * Get the localStorage key for the entire module store. The key references
2055 * $wgDBname to prevent clashes between wikis which share a common host.
2057 * @return {string} localStorage item key
2059 getStoreKey: function () {
2060 return 'MediaWikiModuleStore:' + mw
.config
.get( 'wgDBname' );
2064 * Get a string key on which to vary the module cache.
2065 * @return {string} String of concatenated vary conditions.
2067 getVary: function () {
2069 mw
.config
.get( 'skin' ),
2070 mw
.config
.get( 'wgResourceLoaderStorageVersion' ),
2071 mw
.config
.get( 'wgUserLanguage' )
2076 * Get a string key for a specific module. The key format is '[name]@[version]'.
2078 * @param {string} module Module name
2079 * @return {string|null} Module key or null if module does not exist
2081 getModuleKey: function ( module
) {
2082 return hasOwn
.call( registry
, module
) ?
2083 ( module
+ '@' + registry
[module
].version
) : null;
2087 * Initialize the store.
2089 * Retrieves store from localStorage and (if successfully retrieved) decoding
2090 * the stored JSON value to a plain object.
2092 * The try / catch block is used for JSON & localStorage feature detection.
2093 * See the in-line documentation for Modernizr's localStorage feature detection
2094 * code for a full account of why we need a try / catch:
2095 * <https://github.com/Modernizr/Modernizr/blob/v2.7.1/modernizr.js#L771-L796>.
2100 if ( mw
.loader
.store
.enabled
!== null ) {
2105 if ( !mw
.config
.get( 'wgResourceLoaderStorageEnabled' ) ) {
2106 // Disabled by configuration.
2107 // Clear any previous store to free up space. (T66721)
2108 mw
.loader
.store
.clear();
2109 mw
.loader
.store
.enabled
= false;
2112 if ( mw
.config
.get( 'debug' ) ) {
2113 // Disable module store in debug mode
2114 mw
.loader
.store
.enabled
= false;
2119 raw
= localStorage
.getItem( mw
.loader
.store
.getStoreKey() );
2120 // If we get here, localStorage is available; mark enabled
2121 mw
.loader
.store
.enabled
= true;
2122 data
= JSON
.parse( raw
);
2123 if ( data
&& typeof data
.items
=== 'object' && data
.vary
=== mw
.loader
.store
.getVary() ) {
2124 mw
.loader
.store
.items
= data
.items
;
2128 log( 'Storage error', e
);
2131 if ( raw
=== undefined ) {
2132 // localStorage failed; disable store
2133 mw
.loader
.store
.enabled
= false;
2135 mw
.loader
.store
.update();
2140 * Retrieve a module from the store and update cache hit stats.
2142 * @param {string} module Module name
2143 * @return {string|boolean} Module implementation or false if unavailable
2145 get: function ( module
) {
2148 if ( !mw
.loader
.store
.enabled
) {
2152 key
= mw
.loader
.store
.getModuleKey( module
);
2153 if ( key
in mw
.loader
.store
.items
) {
2154 mw
.loader
.store
.stats
.hits
++;
2155 return mw
.loader
.store
.items
[key
];
2157 mw
.loader
.store
.stats
.misses
++;
2162 * Stringify a module and queue it for storage.
2164 * @param {string} module Module name
2165 * @param {Object} descriptor The module's descriptor as set in the registry
2167 set: function ( module
, descriptor
) {
2170 if ( !mw
.loader
.store
.enabled
) {
2174 key
= mw
.loader
.store
.getModuleKey( module
);
2177 // Already stored a copy of this exact version
2178 key
in mw
.loader
.store
.items
||
2179 // Module failed to load
2180 descriptor
.state
!== 'ready' ||
2181 // Unversioned, private, or site-/user-specific
2182 ( !descriptor
.version
|| $.inArray( descriptor
.group
, [ 'private', 'user', 'site' ] ) !== -1 ) ||
2183 // Partial descriptor
2184 $.inArray( undefined, [ descriptor
.script
, descriptor
.style
,
2185 descriptor
.messages
, descriptor
.templates
] ) !== -1
2193 JSON
.stringify( module
),
2194 typeof descriptor
.script
=== 'function' ?
2195 String( descriptor
.script
) :
2196 JSON
.stringify( descriptor
.script
),
2197 JSON
.stringify( descriptor
.style
),
2198 JSON
.stringify( descriptor
.messages
),
2199 JSON
.stringify( descriptor
.templates
)
2201 // Attempted workaround for a possible Opera bug (bug 57567).
2202 // This regex should never match under sane conditions.
2203 if ( /^\s*\(/.test( args
[1] ) ) {
2204 args
[1] = 'function' + args
[1];
2205 log( 'Detected malformed function stringification (bug 57567)' );
2208 log( 'Storage error', e
);
2212 mw
.loader
.store
.items
[key
] = 'mw.loader.implement(' + args
.join( ',' ) + ');';
2213 mw
.loader
.store
.update();
2217 * Iterate through the module store, removing any item that does not correspond
2218 * (in name and version) to an item in the module registry.
2220 prune: function () {
2223 if ( !mw
.loader
.store
.enabled
) {
2227 for ( key
in mw
.loader
.store
.items
) {
2228 module
= key
.slice( 0, key
.indexOf( '@' ) );
2229 if ( mw
.loader
.store
.getModuleKey( module
) !== key
) {
2230 mw
.loader
.store
.stats
.expired
++;
2231 delete mw
.loader
.store
.items
[key
];
2237 * Clear the entire module store right now.
2239 clear: function () {
2240 mw
.loader
.store
.items
= {};
2241 localStorage
.removeItem( mw
.loader
.store
.getStoreKey() );
2245 * Sync modules to localStorage.
2247 * This function debounces localStorage updates. When called multiple times in
2248 * quick succession, the calls are coalesced into a single update operation.
2249 * This allows us to call #update without having to consider the module load
2250 * queue; the call to localStorage.setItem will be naturally deferred until the
2251 * page is quiescent.
2253 * Because localStorage is shared by all pages with the same origin, if multiple
2254 * pages are loaded with different module sets, the possibility exists that
2255 * modules saved by one page will be clobbered by another. But the impact would
2256 * be minor and the problem would be corrected by subsequent page views.
2260 update
: ( function () {
2265 key
= mw
.loader
.store
.getStoreKey();
2267 if ( !mw
.loader
.store
.enabled
) {
2270 mw
.loader
.store
.prune();
2272 // Replacing the content of the module store might fail if the new
2273 // contents would exceed the browser's localStorage size limit. To
2274 // avoid clogging the browser with stale data, always remove the old
2275 // value before attempting to set the new one.
2276 localStorage
.removeItem( key
);
2277 data
= JSON
.stringify( mw
.loader
.store
);
2278 localStorage
.setItem( key
, data
);
2280 log( 'Storage error', e
);
2284 return function () {
2285 clearTimeout( timer
);
2286 timer
= setTimeout( flush
, 2000 );
2294 * HTML construction helper functions
2301 * output = Html.element( 'div', {}, new Html.Raw(
2302 * Html.element( 'img', { src: '<' } )
2304 * mw.log( output ); // <div><img src="<"/></div>
2309 html
: ( function () {
2310 function escapeCallback( s
) {
2327 * Escape a string for HTML.
2329 * Converts special characters to HTML entities.
2331 * mw.html.escape( '< > \' & "' );
2332 * // Returns < > ' & "
2334 * @param {string} s The string to escape
2335 * @return {string} HTML
2337 escape: function ( s
) {
2338 return s
.replace( /['"<>&]/g, escapeCallback
);
2342 * Create an HTML element string, with safe escaping.
2344 * @param {string} name The tag name.
2345 * @param {Object} attrs An object with members mapping element names to values
2346 * @param {Mixed} contents The contents of the element. May be either:
2348 * - string: The string is escaped.
2349 * - null or undefined: The short closing form is used, e.g. `<br/>`.
2350 * - this.Raw: The value attribute is included without escaping.
2351 * - this.Cdata: The value attribute is included, and an exception is
2352 * thrown if it contains an illegal ETAGO delimiter.
2353 * See <http://www.w3.org/TR/1999/REC-html401-19991224/appendix/notes.html#h-B.3.2>.
2354 * @return {string} HTML
2356 element: function ( name
, attrs
, contents
) {
2357 var v
, attrName
, s
= '<' + name
;
2359 for ( attrName
in attrs
) {
2360 v
= attrs
[attrName
];
2361 // Convert name=true, to name=name
2365 } else if ( v
=== false ) {
2368 s
+= ' ' + attrName
+ '="' + this.escape( String( v
) ) + '"';
2370 if ( contents
=== undefined || contents
=== null ) {
2377 switch ( typeof contents
) {
2380 s
+= this.escape( contents
);
2384 // Convert to string
2385 s
+= String( contents
);
2388 if ( contents
instanceof this.Raw
) {
2389 // Raw HTML inclusion
2390 s
+= contents
.value
;
2391 } else if ( contents
instanceof this.Cdata
) {
2393 if ( /<\/[a-zA-z]/.test( contents
.value
) ) {
2394 throw new Error( 'mw.html.element: Illegal end tag found in CDATA' );
2396 s
+= contents
.value
;
2398 throw new Error( 'mw.html.element: Invalid type of contents' );
2401 s
+= '</' + name
+ '>';
2406 * Wrapper object for raw HTML passed to mw.html.element().
2407 * @class mw.html.Raw
2409 Raw: function ( value
) {
2414 * Wrapper object for CDATA element contents passed to mw.html.element()
2415 * @class mw.html.Cdata
2417 Cdata: function ( value
) {
2423 // Skeleton user object. mediawiki.user.js extends this
2430 * Registry and firing of events.
2432 * MediaWiki has various interface components that are extended, enhanced
2433 * or manipulated in some other way by extensions, gadgets and even
2436 * This framework helps streamlining the timing of when these other
2437 * code paths fire their plugins (instead of using document-ready,
2438 * which can and should be limited to firing only once).
2440 * Features like navigating to other wiki pages, previewing an edit
2441 * and editing itself – without a refresh – can then retrigger these
2442 * hooks accordingly to ensure everything still works as expected.
2446 * mw.hook( 'wikipage.content' ).add( fn ).remove( fn );
2447 * mw.hook( 'wikipage.content' ).fire( $content );
2449 * Handlers can be added and fired for arbitrary event names at any time. The same
2450 * event can be fired multiple times. The last run of an event is memorized
2451 * (similar to `$(document).ready` and `$.Deferred().done`).
2452 * This means if an event is fired, and a handler added afterwards, the added
2453 * function will be fired right away with the last given event data.
2455 * Like Deferreds and Promises, the mw.hook object is both detachable and chainable.
2456 * Thus allowing flexible use and optimal maintainability and authority control.
2457 * You can pass around the `add` and/or `fire` method to another piece of code
2458 * without it having to know the event name (or `mw.hook` for that matter).
2460 * var h = mw.hook( 'bar.ready' );
2461 * new mw.Foo( .. ).fetch( { callback: h.fire } );
2463 * Note: Events are documented with an underscore instead of a dot in the event
2464 * name due to jsduck not supporting dots in that position.
2468 hook
: ( function () {
2472 * Create an instance of mw.hook.
2476 * @param {string} name Name of hook.
2479 return function ( name
) {
2480 var list
= hasOwn
.call( lists
, name
) ?
2482 lists
[name
] = $.Callbacks( 'memory' );
2486 * Register a hook handler
2487 * @param {Function...} handler Function to bind.
2493 * Unregister a hook handler
2494 * @param {Function...} handler Function to unbind.
2497 remove
: list
.remove
,
2501 * @param {Mixed...} data
2505 return list
.fireWith
.call( this, null, slice
.call( arguments
) );
2512 // Alias $j to jQuery for backwards compatibility
2513 // @deprecated since 1.23 Use $ or jQuery instead
2514 mw
.log
.deprecate( window
, '$j', $, 'Use $ or jQuery instead.' );
2516 // Attach to window and globally alias
2517 window
.mw
= window
.mediaWiki
= mw
;
2519 // Auto-register from pre-loaded startup scripts
2520 if ( $.isFunction( window
.startUp
) ) {
2522 window
.startUp
= undefined;