Add methods to get raw request in WebRequest
[mediawiki.git] / resources / mediawiki / mediawiki.js
blob46b74f1daf072fcb5bb9ca3c57511b3c486374ec
1 /*
2  * Core MediaWiki JavaScript Library
3  */
5 var mw = ( function ( $, undefined ) {
6         'use strict';
8         /* Private Members */
10         var hasOwn = Object.prototype.hasOwnProperty,
11                 slice = Array.prototype.slice;
13         /* Object constructors */
15         /**
16          * Creates an object that can be read from or written to from prototype functions
17          * that allow both single and multiple variables at once.
18          *
19          *     @example
20          *
21          *     var addies, wanted, results;
22          *
23          *     // Create your address book
24          *     addies = new mw.Map();
25          *
26          *     // This data could be coming from an external source (eg. API/AJAX)
27          *     addies.set( {
28          *         'John Doe' : '10 Wall Street, New York, USA',
29          *         'Jane Jackson' : '21 Oxford St, London, UK',
30          *         'Dominique van Halen' : 'Kalverstraat 7, Amsterdam, NL'
31          *     } );
32          *
33          *     wanted = ['Dominique van Halen', 'George Johnson', 'Jane Jackson'];
34          *
35          *     // You can detect missing keys first
36          *     if ( !addies.exists( wanted ) ) {
37          *         // One or more are missing (in this case: "George Johnson")
38          *         mw.log( 'One or more names were not found in your address book' );
39          *     }
40          *
41          *     // Or just let it give you what it can
42          *     results = addies.get( wanted, 'Middle of Nowhere, Alaska, US' );
43          *     mw.log( results['Jane Jackson'] ); // "21 Oxford St, London, UK"
44          *     mw.log( results['George Johnson'] ); // "Middle of Nowhere, Alaska, US"
45          *
46          * @class mw.Map
47          *
48          * @constructor
49          * @param {boolean} [global=false] Whether to store the values in the global window
50          *  object or a exclusively in the object property 'values'.
51          */
52         function Map( global ) {
53                 this.values = global === true ? window : {};
54                 return this;
55         }
57         Map.prototype = {
58                 /**
59                  * Get the value of one or multiple a keys.
60                  *
61                  * If called with no arguments, all values will be returned.
62                  *
63                  * @param {string|Array} selection String key or array of keys to get values for.
64                  * @param {Mixed} [fallback] Value to use in case key(s) do not exist.
65                  * @return mixed If selection was a string returns the value or null,
66                  *  If selection was an array, returns an object of key/values (value is null if not found),
67                  *  If selection was not passed or invalid, will return the 'values' object member (be careful as
68                  *  objects are always passed by reference in JavaScript!).
69                  * @return {string|Object|null} Values as a string or object, null if invalid/inexistant.
70                  */
71                 get: function ( selection, fallback ) {
72                         var results, i;
73                         // If we only do this in the `return` block, it'll fail for the
74                         // call to get() from the mutli-selection block.
75                         fallback = arguments.length > 1 ? fallback : null;
77                         if ( $.isArray( selection ) ) {
78                                 selection = slice.call( selection );
79                                 results = {};
80                                 for ( i = 0; i < selection.length; i++ ) {
81                                         results[selection[i]] = this.get( selection[i], fallback );
82                                 }
83                                 return results;
84                         }
86                         if ( typeof selection === 'string' ) {
87                                 if ( !hasOwn.call( this.values, selection ) ) {
88                                         return fallback;
89                                 }
90                                 return this.values[selection];
91                         }
93                         if ( selection === undefined ) {
94                                 return this.values;
95                         }
97                         // invalid selection key
98                         return null;
99                 },
101                 /**
102                  * Sets one or multiple key/value pairs.
103                  *
104                  * @param {string|Object} selection String key to set value for, or object mapping keys to values.
105                  * @param {Mixed} [value] Value to set (optional, only in use when key is a string)
106                  * @return {Boolean} This returns true on success, false on failure.
107                  */
108                 set: function ( selection, value ) {
109                         var s;
111                         if ( $.isPlainObject( selection ) ) {
112                                 for ( s in selection ) {
113                                         this.values[s] = selection[s];
114                                 }
115                                 return true;
116                         }
117                         if ( typeof selection === 'string' && arguments.length > 1 ) {
118                                 this.values[selection] = value;
119                                 return true;
120                         }
121                         return false;
122                 },
124                 /**
125                  * Checks if one or multiple keys exist.
126                  *
127                  * @param {Mixed} selection String key or array of keys to check
128                  * @return {boolean} Existence of key(s)
129                  */
130                 exists: function ( selection ) {
131                         var s;
133                         if ( $.isArray( selection ) ) {
134                                 for ( s = 0; s < selection.length; s++ ) {
135                                         if ( typeof selection[s] !== 'string' || !hasOwn.call( this.values, selection[s] ) ) {
136                                                 return false;
137                                         }
138                                 }
139                                 return true;
140                         }
141                         return typeof selection === 'string' && hasOwn.call( this.values, selection );
142                 }
143         };
145         /**
146          * Object constructor for messages,
147          * similar to the Message class in MediaWiki PHP.
148          * @class mw.Message
149          *
150          * @constructor
151          * @param {mw.Map} map Message storage
152          * @param {string} key
153          * @param {Array} [parameters]
154          */
155         function Message( map, key, parameters ) {
156                 this.format = 'text';
157                 this.map = map;
158                 this.key = key;
159                 this.parameters = parameters === undefined ? [] : slice.call( parameters );
160                 return this;
161         }
163         Message.prototype = {
164                 /**
165                  * Simple message parser, does $N replacement and nothing else.
166                  *
167                  * This may be overridden to provide a more complex message parser.
168                  *
169                  * The primary override is in mediawiki.jqueryMsg.
170                  *
171                  * This function will not be called for nonexistent messages.
172                  */
173                 parser: function () {
174                         var parameters = this.parameters;
175                         return this.map.get( this.key ).replace( /\$(\d+)/g, function ( str, match ) {
176                                 var index = parseInt( match, 10 ) - 1;
177                                 return parameters[index] !== undefined ? parameters[index] : '$' + match;
178                         } );
179                 },
181                 /**
182                  * Appends (does not replace) parameters for replacement to the .parameters property.
183                  *
184                  * @param {Array} parameters
185                  * @chainable
186                  */
187                 params: function ( parameters ) {
188                         var i;
189                         for ( i = 0; i < parameters.length; i += 1 ) {
190                                 this.parameters.push( parameters[i] );
191                         }
192                         return this;
193                 },
195                 /**
196                  * Converts message object to it's string form based on the state of format.
197                  *
198                  * @return {string} Message as a string in the current form or `<key>` if key does not exist.
199                  */
200                 toString: function () {
201                         var text;
203                         if ( !this.exists() ) {
204                                 // Use <key> as text if key does not exist
205                                 if ( this.format === 'escaped' || this.format === 'parse' ) {
206                                         // format 'escaped' and 'parse' need to have the brackets and key html escaped
207                                         return mw.html.escape( '<' + this.key + '>' );
208                                 }
209                                 return '<' + this.key + '>';
210                         }
212                         if ( this.format === 'plain' || this.format === 'text' || this.format === 'parse' ) {
213                                 text = this.parser();
214                         }
216                         if ( this.format === 'escaped' ) {
217                                 text = this.parser();
218                                 text = mw.html.escape( text );
219                         }
221                         return text;
222                 },
224                 /**
225                  * Changes format to 'parse' and converts message to string
226                  *
227                  * If jqueryMsg is loaded, this parses the message text from wikitext
228                  * (where supported) to HTML
229                  *
230                  * Otherwise, it is equivalent to plain.
231                  *
232                  * @return {string} String form of parsed message
233                  */
234                 parse: function () {
235                         this.format = 'parse';
236                         return this.toString();
237                 },
239                 /**
240                  * Changes format to 'plain' and converts message to string
241                  *
242                  * This substitutes parameters, but otherwise does not change the
243                  * message text.
244                  *
245                  * @return {string} String form of plain message
246                  */
247                 plain: function () {
248                         this.format = 'plain';
249                         return this.toString();
250                 },
252                 /**
253                  * Changes format to 'text' and converts message to string
254                  *
255                  * If jqueryMsg is loaded, {{-transformation is done where supported
256                  * (such as {{plural:}}, {{gender:}}, {{int:}}).
257                  *
258                  * Otherwise, it is equivalent to plain.
259                  */
260                 text: function () {
261                         this.format = 'text';
262                         return this.toString();
263                 },
265                 /**
266                  * Changes the format to 'escaped' and converts message to string
267                  *
268                  * This is equivalent to using the 'text' format (see text method), then
269                  * HTML-escaping the output.
270                  *
271                  * @return {string} String form of html escaped message
272                  */
273                 escaped: function () {
274                         this.format = 'escaped';
275                         return this.toString();
276                 },
278                 /**
279                  * Checks if message exists
280                  *
281                  * @see mw.Map#exists
282                  * @return {boolean}
283                  */
284                 exists: function () {
285                         return this.map.exists( this.key );
286                 }
287         };
289         /**
290          * Base library for MediaWiki.
291          *
292          * @class mw
293          * @alternateClassName mediaWiki
294          * @singleton
295          */
296         return {
297                 /* Public Members */
299                 /**
300                  * Dummy placeholder for {@link mw.log}
301                  * @method
302                  */
303                 log: ( function () {
304                         var log = function () {};
305                         log.warn = function () {};
306                         log.deprecate = function ( obj, key, val ) {
307                                 obj[key] = val;
308                         };
309                         return log;
310                 }() ),
312                 // Make the Map constructor publicly available.
313                 Map: Map,
315                 // Make the Message constructor publicly available.
316                 Message: Message,
318                 /**
319                  * Map of configuration values
320                  *
321                  * Check out [the complete list of configuration values](https://www.mediawiki.org/wiki/Manual:Interface/JavaScript#mw.config)
322                  * on MediaWiki.org.
323                  *
324                  * If `$wgLegacyJavaScriptGlobals` is true, this Map will put its values in the
325                  * global window object.
326                  *
327                  * @property {mw.Map} config
328                  */
329                 // Dummy placeholder. Re-assigned in ResourceLoaderStartupModule with an instance of `mw.Map`.
330                 config: null,
332                 /**
333                  * Empty object that plugins can be installed in.
334                  * @property
335                  */
336                 libs: {},
338                 /**
339                  * Access container for deprecated functionality that can be moved from
340                  * from their legacy location and attached to this object (e.g. a global
341                  * function that is deprecated and as stop-gap can be exposed through here).
342                  *
343                  * This was reserved for future use but never ended up being used.
344                  *
345                  * @deprecated since 1.22: Let deprecated identifiers keep their original name
346                  * and use mw.log#deprecate to create an access container for tracking.
347                  * @property
348                  */
349                 legacy: {},
351                 /**
352                  * Localization system
353                  * @property {mw.Map}
354                  */
355                 messages: new Map(),
357                 /* Public Methods */
359                 /**
360                  * Gets a message object, similar to wfMessage().
361                  *
362                  * @param {string} key Key of message to get
363                  * @param {Mixed...} parameters Parameters for the $N replacements in messages.
364                  * @return {mw.Message}
365                  */
366                 message: function ( key ) {
367                         // Variadic arguments
368                         var parameters = slice.call( arguments, 1 );
369                         return new Message( mw.messages, key, parameters );
370                 },
372                 /**
373                  * Gets a message string, similar to wfMessage()
374                  *
375                  * @see mw.Message#toString
376                  * @param {string} key Key of message to get
377                  * @param {Mixed...} parameters Parameters for the $N replacements in messages.
378                  * @return {string}
379                  */
380                 msg: function ( /* key, parameters... */ ) {
381                         return mw.message.apply( mw.message, arguments ).toString();
382                 },
384                 /**
385                  * Client-side module loader which integrates with the MediaWiki ResourceLoader
386                  * @class mw.loader
387                  * @singleton
388                  */
389                 loader: ( function () {
391                         /* Private Members */
393                         /**
394                          * Mapping of registered modules
395                          *
396                          * The jquery module is pre-registered, because it must have already
397                          * been provided for this object to have been built, and in debug mode
398                          * jquery would have been provided through a unique loader request,
399                          * making it impossible to hold back registration of jquery until after
400                          * mediawiki.
401                          *
402                          * For exact details on support for script, style and messages, look at
403                          * mw.loader.implement.
404                          *
405                          * Format:
406                          *     {
407                          *         'moduleName': {
408                          *             'version': ############## (unix timestamp),
409                          *             'dependencies': ['required.foo', 'bar.also', ...], (or) function () {}
410                          *             'group': 'somegroup', (or) null,
411                          *             'source': 'local', 'someforeignwiki', (or) null
412                          *             'state': 'registered', 'loaded', 'loading', 'ready', 'error' or 'missing'
413                          *             'script': ...,
414                          *             'style': ...,
415                          *             'messages': { 'key': 'value' },
416                          *         }
417                          *     }
418                          *
419                          * @property
420                          * @private
421                          */
422                         var registry = {},
423                                 //
424                                 // Mapping of sources, keyed by source-id, values are objects.
425                                 // Format:
426                                 //      {
427                                 //              'sourceId': {
428                                 //                      'loadScript': 'http://foo.bar/w/load.php'
429                                 //              }
430                                 //      }
431                                 //
432                                 sources = {},
433                                 // List of modules which will be loaded as when ready
434                                 batch = [],
435                                 // List of modules to be loaded
436                                 queue = [],
437                                 // List of callback functions waiting for modules to be ready to be called
438                                 jobs = [],
439                                 // Selector cache for the marker element. Use getMarker() to get/use the marker!
440                                 $marker = null,
441                                 // Buffer for addEmbeddedCSS.
442                                 cssBuffer = '',
443                                 // Callbacks for addEmbeddedCSS.
444                                 cssCallbacks = $.Callbacks();
446                         /* Private methods */
448                         function getMarker() {
449                                 // Cached ?
450                                 if ( $marker ) {
451                                         return $marker;
452                                 }
454                                 $marker = $( 'meta[name="ResourceLoaderDynamicStyles"]' );
455                                 if ( $marker.length ) {
456                                         return $marker;
457                                 }
458                                 mw.log( 'getMarker> No <meta name="ResourceLoaderDynamicStyles"> found, inserting dynamically.' );
459                                 $marker = $( '<meta>' ).attr( 'name', 'ResourceLoaderDynamicStyles' ).appendTo( 'head' );
461                                 return $marker;
462                         }
464                         /**
465                          * Create a new style tag and add it to the DOM.
466                          *
467                          * @private
468                          * @param {string} text CSS text
469                          * @param {HTMLElement|jQuery} [nextnode=document.head] The element where the style tag should be
470                          *  inserted before. Otherwise it will be appended to `<head>`.
471                          * @return {HTMLElement} Reference to the created `<style>` element.
472                          */
473                         function newStyleTag( text, nextnode ) {
474                                 var s = document.createElement( 'style' );
475                                 // Insert into document before setting cssText (bug 33305)
476                                 if ( nextnode ) {
477                                         // Must be inserted with native insertBefore, not $.fn.before.
478                                         // When using jQuery to insert it, like $nextnode.before( s ),
479                                         // then IE6 will throw "Access is denied" when trying to append
480                                         // to .cssText later. Some kind of weird security measure.
481                                         // http://stackoverflow.com/q/12586482/319266
482                                         // Works: jsfiddle.net/zJzMy/1
483                                         // Fails: jsfiddle.net/uJTQz
484                                         // Works again: http://jsfiddle.net/Azr4w/ (diff: the next 3 lines)
485                                         if ( nextnode.jquery ) {
486                                                 nextnode = nextnode.get( 0 );
487                                         }
488                                         nextnode.parentNode.insertBefore( s, nextnode );
489                                 } else {
490                                         document.getElementsByTagName( 'head' )[0].appendChild( s );
491                                 }
492                                 if ( s.styleSheet ) {
493                                         // IE
494                                         s.styleSheet.cssText = text;
495                                 } else {
496                                         // Other browsers.
497                                         // (Safari sometimes borks on non-string values,
498                                         // play safe by casting to a string, just in case.)
499                                         s.appendChild( document.createTextNode( String( text ) ) );
500                                 }
501                                 return s;
502                         }
504                         /**
505                          * Checks whether it is safe to add this css to a stylesheet.
506                          *
507                          * @private
508                          * @param {string} cssText
509                          * @return {boolean} False if a new one must be created.
510                          */
511                         function canExpandStylesheetWith( cssText ) {
512                                 // Makes sure that cssText containing `@import`
513                                 // rules will end up in a new stylesheet (as those only work when
514                                 // placed at the start of a stylesheet; bug 35562).
515                                 return cssText.indexOf( '@import' ) === -1;
516                         }
518                         /**
519                          * Add a bit of CSS text to the current browser page.
520                          *
521                          * The CSS will be appended to an existing ResourceLoader-created `<style>` tag
522                          * or create a new one based on whether the given `cssText` is safe for extension.
523                          *
524                          * @param {string} [cssText=cssBuffer] If called without cssText,
525                          *  the internal buffer will be inserted instead.
526                          * @param {Function} [callback]
527                          */
528                         function addEmbeddedCSS( cssText, callback ) {
529                                 var $style, styleEl;
531                                 if ( callback ) {
532                                         cssCallbacks.add( callback );
533                                 }
535                                 // Yield once before inserting the <style> tag. There are likely
536                                 // more calls coming up which we can combine this way.
537                                 // Appending a stylesheet and waiting for the browser to repaint
538                                 // is fairly expensive, this reduces it (bug 45810)
539                                 if ( cssText ) {
540                                         // Be careful not to extend the buffer with css that needs a new stylesheet
541                                         if ( !cssBuffer || canExpandStylesheetWith( cssText ) ) {
542                                                 // Linebreak for somewhat distinguishable sections
543                                                 // (the rl-cachekey comment separating each)
544                                                 cssBuffer += '\n' + cssText;
545                                                 // TODO: Use requestAnimationFrame in the future which will
546                                                 // perform even better by not injecting styles while the browser
547                                                 // is paiting.
548                                                 setTimeout( function () {
549                                                         // Can't pass addEmbeddedCSS to setTimeout directly because Firefox
550                                                         // (below version 13) has the non-standard behaviour of passing a
551                                                         // numerical "lateness" value as first argument to this callback
552                                                         // http://benalman.com/news/2009/07/the-mysterious-firefox-settime/
553                                                         addEmbeddedCSS();
554                                                 } );
555                                                 return;
556                                         }
558                                 // This is a delayed call and we got a buffer still
559                                 } else if ( cssBuffer ) {
560                                         cssText = cssBuffer;
561                                         cssBuffer = '';
562                                 } else {
563                                         // This is a delayed call, but buffer is already cleared by
564                                         // another delayed call.
565                                         return;
566                                 }
568                                 // By default, always create a new <style>. Appending text
569                                 // to a <style> tag means the contents have to be re-parsed (bug 45810).
570                                 // Except, of course, in IE below 9, in there we default to
571                                 // re-using and appending to a <style> tag due to the
572                                 // IE stylesheet limit (bug 31676).
573                                 if ( 'documentMode' in document && document.documentMode <= 9 ) {
575                                         $style = getMarker().prev();
576                                         // Verify that the the element before Marker actually is a
577                                         // <style> tag and one that came from ResourceLoader
578                                         // (not some other style tag or even a `<meta>` or `<script>`).
579                                         if ( $style.data( 'ResourceLoaderDynamicStyleTag' ) === true ) {
580                                                 // There's already a dynamic <style> tag present and
581                                                 // canExpandStylesheetWith() gave a green light to append more to it.
582                                                 styleEl = $style.get( 0 );
583                                                 if ( styleEl.styleSheet ) {
584                                                         try {
585                                                                 styleEl.styleSheet.cssText += cssText; // IE
586                                                         } catch ( e ) {
587                                                                 log( 'addEmbeddedCSS fail\ne.message: ' + e.message, e );
588                                                         }
589                                                 } else {
590                                                         styleEl.appendChild( document.createTextNode( String( cssText ) ) );
591                                                 }
592                                                 cssCallbacks.fire().empty();
593                                                 return;
594                                         }
595                                 }
597                                 $( newStyleTag( cssText, getMarker() ) ).data( 'ResourceLoaderDynamicStyleTag', true );
599                                 cssCallbacks.fire().empty();
600                         }
602                         /**
603                          * Generates an ISO8601 "basic" string from a UNIX timestamp
604                          * @private
605                          */
606                         function formatVersionNumber( timestamp ) {
607                                 var     d = new Date();
608                                 function pad( a, b, c ) {
609                                         return [a < 10 ? '0' + a : a, b < 10 ? '0' + b : b, c < 10 ? '0' + c : c].join( '' );
610                                 }
611                                 d.setTime( timestamp * 1000 );
612                                 return [
613                                         pad( d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate() ), 'T',
614                                         pad( d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds() ), 'Z'
615                                 ].join( '' );
616                         }
618                         /**
619                          * Resolves dependencies and detects circular references.
620                          *
621                          * @private
622                          * @param {string} module Name of the top-level module whose dependencies shall be
623                          *   resolved and sorted.
624                          * @param {Array} resolved Returns a topological sort of the given module and its
625                          *   dependencies, such that later modules depend on earlier modules. The array
626                          *   contains the module names. If the array contains already some module names,
627                          *   this function appends its result to the pre-existing array.
628                          * @param {Object} [unresolved] Hash used to track the current dependency
629                          *   chain; used to report loops in the dependency graph.
630                          * @throws {Error} If any unregistered module or a dependency loop is encountered
631                          */
632                         function sortDependencies( module, resolved, unresolved ) {
633                                 var n, deps, len;
635                                 if ( registry[module] === undefined ) {
636                                         throw new Error( 'Unknown dependency: ' + module );
637                                 }
638                                 // Resolves dynamic loader function and replaces it with its own results
639                                 if ( $.isFunction( registry[module].dependencies ) ) {
640                                         registry[module].dependencies = registry[module].dependencies();
641                                         // Ensures the module's dependencies are always in an array
642                                         if ( typeof registry[module].dependencies !== 'object' ) {
643                                                 registry[module].dependencies = [registry[module].dependencies];
644                                         }
645                                 }
646                                 if ( $.inArray( module, resolved ) !== -1 ) {
647                                         // Module already resolved; nothing to do.
648                                         return;
649                                 }
650                                 // unresolved is optional, supply it if not passed in
651                                 if ( !unresolved ) {
652                                         unresolved = {};
653                                 }
654                                 // Tracks down dependencies
655                                 deps = registry[module].dependencies;
656                                 len = deps.length;
657                                 for ( n = 0; n < len; n += 1 ) {
658                                         if ( $.inArray( deps[n], resolved ) === -1 ) {
659                                                 if ( unresolved[deps[n]] ) {
660                                                         throw new Error(
661                                                                 'Circular reference detected: ' + module +
662                                                                 ' -> ' + deps[n]
663                                                         );
664                                                 }
666                                                 // Add to unresolved
667                                                 unresolved[module] = true;
668                                                 sortDependencies( deps[n], resolved, unresolved );
669                                                 delete unresolved[module];
670                                         }
671                                 }
672                                 resolved[resolved.length] = module;
673                         }
675                         /**
676                          * Gets a list of module names that a module depends on in their proper dependency
677                          * order.
678                          *
679                          * @private
680                          * @param {string} module Module name or array of string module names
681                          * @return {Array} list of dependencies, including 'module'.
682                          * @throws {Error} If circular reference is detected
683                          */
684                         function resolve( module ) {
685                                 var m, resolved;
687                                 // Allow calling with an array of module names
688                                 if ( $.isArray( module ) ) {
689                                         resolved = [];
690                                         for ( m = 0; m < module.length; m += 1 ) {
691                                                 sortDependencies( module[m], resolved );
692                                         }
693                                         return resolved;
694                                 }
696                                 if ( typeof module === 'string' ) {
697                                         resolved = [];
698                                         sortDependencies( module, resolved );
699                                         return resolved;
700                                 }
702                                 throw new Error( 'Invalid module argument: ' + module );
703                         }
705                         /**
706                          * Narrows a list of module names down to those matching a specific
707                          * state (see comment on top of this scope for a list of valid states).
708                          * One can also filter for 'unregistered', which will return the
709                          * modules names that don't have a registry entry.
710                          *
711                          * @private
712                          * @param {string|string[]} states Module states to filter by
713                          * @param {Array} [modules] List of module names to filter (optional, by default the entire
714                          * registry is used)
715                          * @return {Array} List of filtered module names
716                          */
717                         function filter( states, modules ) {
718                                 var list, module, s, m;
720                                 // Allow states to be given as a string
721                                 if ( typeof states === 'string' ) {
722                                         states = [states];
723                                 }
724                                 // If called without a list of modules, build and use a list of all modules
725                                 list = [];
726                                 if ( modules === undefined ) {
727                                         modules = [];
728                                         for ( module in registry ) {
729                                                 modules[modules.length] = module;
730                                         }
731                                 }
732                                 // Build a list of modules which are in one of the specified states
733                                 for ( s = 0; s < states.length; s += 1 ) {
734                                         for ( m = 0; m < modules.length; m += 1 ) {
735                                                 if ( registry[modules[m]] === undefined ) {
736                                                         // Module does not exist
737                                                         if ( states[s] === 'unregistered' ) {
738                                                                 // OK, undefined
739                                                                 list[list.length] = modules[m];
740                                                         }
741                                                 } else {
742                                                         // Module exists, check state
743                                                         if ( registry[modules[m]].state === states[s] ) {
744                                                                 // OK, correct state
745                                                                 list[list.length] = modules[m];
746                                                         }
747                                                 }
748                                         }
749                                 }
750                                 return list;
751                         }
753                         /**
754                          * Determine whether all dependencies are in state 'ready', which means we may
755                          * execute the module or job now.
756                          *
757                          * @private
758                          * @param {Array} dependencies Dependencies (module names) to be checked.
759                          * @return {boolean} True if all dependencies are in state 'ready', false otherwise
760                          */
761                         function allReady( dependencies ) {
762                                 return filter( 'ready', dependencies ).length === dependencies.length;
763                         }
765                         /**
766                          * Log a message to window.console, if possible. Useful to force logging of some
767                          * errors that are otherwise hard to detect (I.e., this logs also in production mode).
768                          * Gets console references in each invocation, so that delayed debugging tools work
769                          * fine. No need for optimization here, which would only result in losing logs.
770                          *
771                          * @private
772                          * @param {string} msg text for the log entry.
773                          * @param {Error} [e]
774                          */
775                         function log( msg, e ) {
776                                 var console = window.console;
777                                 if ( console && console.log ) {
778                                         console.log( msg );
779                                         // If we have an exception object, log it through .error() to trigger
780                                         // proper stacktraces in browsers that support it. There are no (known)
781                                         // browsers that don't support .error(), that do support .log() and
782                                         // have useful exception handling through .log().
783                                         if ( e && console.error ) {
784                                                 console.error( e );
785                                         }
786                                 }
787                         }
789                         /**
790                          * A module has entered state 'ready', 'error', or 'missing'. Automatically update pending jobs
791                          * and modules that depend upon this module. if the given module failed, propagate the 'error'
792                          * state up the dependency tree; otherwise, execute all jobs/modules that now have all their
793                          * dependencies satisfied. On jobs depending on a failed module, run the error callback, if any.
794                          *
795                          * @private
796                          * @param {string} module Name of module that entered one of the states 'ready', 'error', or 'missing'.
797                          */
798                         function handlePending( module ) {
799                                 var j, job, hasErrors, m, stateChange;
801                                 // Modules.
802                                 if ( $.inArray( registry[module].state, ['error', 'missing'] ) !== -1 ) {
803                                         // If the current module failed, mark all dependent modules also as failed.
804                                         // Iterate until steady-state to propagate the error state upwards in the
805                                         // dependency tree.
806                                         do {
807                                                 stateChange = false;
808                                                 for ( m in registry ) {
809                                                         if ( $.inArray( registry[m].state, ['error', 'missing'] ) === -1 ) {
810                                                                 if ( filter( ['error', 'missing'], registry[m].dependencies ).length > 0 ) {
811                                                                         registry[m].state = 'error';
812                                                                         stateChange = true;
813                                                                 }
814                                                         }
815                                                 }
816                                         } while ( stateChange );
817                                 }
819                                 // Execute all jobs whose dependencies are either all satisfied or contain at least one failed module.
820                                 for ( j = 0; j < jobs.length; j += 1 ) {
821                                         hasErrors = filter( ['error', 'missing'], jobs[j].dependencies ).length > 0;
822                                         if ( hasErrors || allReady( jobs[j].dependencies ) ) {
823                                                 // All dependencies satisfied, or some have errors
824                                                 job = jobs[j];
825                                                 jobs.splice( j, 1 );
826                                                 j -= 1;
827                                                 try {
828                                                         if ( hasErrors ) {
829                                                                 throw new Error( 'Module ' + module + ' failed.');
830                                                         } else {
831                                                                 if ( $.isFunction( job.ready ) ) {
832                                                                         job.ready();
833                                                                 }
834                                                         }
835                                                 } catch ( e ) {
836                                                         if ( $.isFunction( job.error ) ) {
837                                                                 try {
838                                                                         job.error( e, [module] );
839                                                                 } catch ( ex ) {
840                                                                         // A user-defined operation raised an exception. Swallow to protect
841                                                                         // our state machine!
842                                                                         log( 'Exception thrown by job.error()', ex );
843                                                                 }
844                                                         }
845                                                 }
846                                         }
847                                 }
849                                 if ( registry[module].state === 'ready' ) {
850                                         // The current module became 'ready'. Recursively execute all dependent modules that are loaded
851                                         // and now have all dependencies satisfied.
852                                         for ( m in registry ) {
853                                                 if ( registry[m].state === 'loaded' && allReady( registry[m].dependencies ) ) {
854                                                         execute( m );
855                                                 }
856                                         }
857                                 }
858                         }
860                         /**
861                          * Adds a script tag to the DOM, either using document.write or low-level DOM manipulation,
862                          * depending on whether document-ready has occurred yet and whether we are in async mode.
863                          *
864                          * @private
865                          * @param {string} src URL to script, will be used as the src attribute in the script tag
866                          * @param {Function} [callback] Callback which will be run when the script is done
867                          */
868                         function addScript( src, callback, async ) {
869                                 /*jshint evil:true */
870                                 var script, head, done;
872                                 // Using isReady directly instead of storing it locally from
873                                 // a $.fn.ready callback (bug 31895).
874                                 if ( $.isReady || async ) {
875                                         // Can't use jQuery.getScript because that only uses <script> for cross-domain,
876                                         // it uses XHR and eval for same-domain scripts, which we don't want because it
877                                         // messes up line numbers.
878                                         // The below is based on jQuery ([jquery@1.8.2]/src/ajax/script.js)
880                                         // IE-safe way of getting the <head>. document.head isn't supported
881                                         // in old IE, and doesn't work when in the <head>.
882                                         done = false;
883                                         head = document.getElementsByTagName( 'head' )[0] || document.body;
885                                         script = document.createElement( 'script' );
886                                         script.async = true;
887                                         script.src = src;
888                                         if ( $.isFunction( callback ) ) {
889                                                 script.onload = script.onreadystatechange = function () {
890                                                         if (
891                                                                 !done
892                                                                 && (
893                                                                         !script.readyState
894                                                                         || /loaded|complete/.test( script.readyState )
895                                                                 )
896                                                         ) {
897                                                                 done = true;
899                                                                 // Handle memory leak in IE
900                                                                 script.onload = script.onreadystatechange = null;
902                                                                 // Detach the element from the document
903                                                                 if ( script.parentNode ) {
904                                                                         script.parentNode.removeChild( script );
905                                                                 }
907                                                                 // Dereference the element from javascript
908                                                                 script = undefined;
910                                                                 callback();
911                                                         }
912                                                 };
913                                         }
915                                         if ( window.opera ) {
916                                                 // Appending to the <head> blocks rendering completely in Opera,
917                                                 // so append to the <body> after document ready. This means the
918                                                 // scripts only start loading after the document has been rendered,
919                                                 // but so be it. Opera users don't deserve faster web pages if their
920                                                 // browser makes it impossible.
921                                                 $( function () {
922                                                         document.body.appendChild( script );
923                                                 } );
924                                         } else {
925                                                 head.appendChild( script );
926                                         }
927                                 } else {
928                                         document.write( mw.html.element( 'script', { 'src': src }, '' ) );
929                                         if ( $.isFunction( callback ) ) {
930                                                 // Document.write is synchronous, so this is called when it's done
931                                                 // FIXME: that's a lie. doc.write isn't actually synchronous
932                                                 callback();
933                                         }
934                                 }
935                         }
937                         /**
938                          * Executes a loaded module, making it ready to use
939                          *
940                          * @private
941                          * @param {string} module Module name to execute
942                          */
943                         function execute( module ) {
944                                 var key, value, media, i, urls, cssHandle, checkCssHandles,
945                                         cssHandlesRegistered = false;
947                                 if ( registry[module] === undefined ) {
948                                         throw new Error( 'Module has not been registered yet: ' + module );
949                                 } else if ( registry[module].state === 'registered' ) {
950                                         throw new Error( 'Module has not been requested from the server yet: ' + module );
951                                 } else if ( registry[module].state === 'loading' ) {
952                                         throw new Error( 'Module has not completed loading yet: ' + module );
953                                 } else if ( registry[module].state === 'ready' ) {
954                                         throw new Error( 'Module has already been executed: ' + module );
955                                 }
957                                 /**
958                                  * Define loop-function here for efficiency
959                                  * and to avoid re-using badly scoped variables.
960                                  * @ignore
961                                  */
962                                 function addLink( media, url ) {
963                                         var el = document.createElement( 'link' );
964                                         getMarker().before( el ); // IE: Insert in dom before setting href
965                                         el.rel = 'stylesheet';
966                                         if ( media && media !== 'all' ) {
967                                                 el.media = media;
968                                         }
969                                         el.href = url;
970                                 }
972                                 function runScript() {
973                                         var script, markModuleReady, nestedAddScript;
974                                         try {
975                                                 script = registry[module].script;
976                                                 markModuleReady = function () {
977                                                         registry[module].state = 'ready';
978                                                         handlePending( module );
979                                                 };
980                                                 nestedAddScript = function ( arr, callback, async, i ) {
981                                                         // Recursively call addScript() in its own callback
982                                                         // for each element of arr.
983                                                         if ( i >= arr.length ) {
984                                                                 // We're at the end of the array
985                                                                 callback();
986                                                                 return;
987                                                         }
989                                                         addScript( arr[i], function () {
990                                                                 nestedAddScript( arr, callback, async, i + 1 );
991                                                         }, async );
992                                                 };
994                                                 if ( $.isArray( script ) ) {
995                                                         nestedAddScript( script, markModuleReady, registry[module].async, 0 );
996                                                 } else if ( $.isFunction( script ) ) {
997                                                         registry[module].state = 'ready';
998                                                         script( $ );
999                                                         handlePending( module );
1000                                                 }
1001                                         } catch ( e ) {
1002                                                 // This needs to NOT use mw.log because these errors are common in production mode
1003                                                 // and not in debug mode, such as when a symbol that should be global isn't exported
1004                                                 log( 'Exception thrown by ' + module + ': ' + e.message, e );
1005                                                 registry[module].state = 'error';
1006                                                 handlePending( module );
1007                                         }
1008                                 }
1010                                 // This used to be inside runScript, but since that is now fired asychronously
1011                                 // (after CSS is loaded) we need to set it here right away. It is crucial that
1012                                 // when execute() is called this is set synchronously, otherwise modules will get
1013                                 // executed multiple times as the registry will state that it isn't loading yet.
1014                                 registry[module].state = 'loading';
1016                                 // Add localizations to message system
1017                                 if ( $.isPlainObject( registry[module].messages ) ) {
1018                                         mw.messages.set( registry[module].messages );
1019                                 }
1021                                 // Make sure we don't run the scripts until all (potentially asynchronous)
1022                                 // stylesheet insertions have completed.
1023                                 ( function () {
1024                                         var pending = 0;
1025                                         checkCssHandles = function () {
1026                                                 // cssHandlesRegistered ensures we don't take off too soon, e.g. when
1027                                                 // one of the cssHandles is fired while we're still creating more handles.
1028                                                 if ( cssHandlesRegistered && pending === 0 && runScript ) {
1029                                                         runScript();
1030                                                         runScript = undefined; // Revoke
1031                                                 }
1032                                         };
1033                                         cssHandle = function () {
1034                                                 var check = checkCssHandles;
1035                                                 pending++;
1036                                                 return function () {
1037                                                         if (check) {
1038                                                                 pending--;
1039                                                                 check();
1040                                                                 check = undefined; // Revoke
1041                                                         }
1042                                                 };
1043                                         };
1044                                 }() );
1046                                 // Process styles (see also mw.loader.implement)
1047                                 // * back-compat: { <media>: css }
1048                                 // * back-compat: { <media>: [url, ..] }
1049                                 // * { "css": [css, ..] }
1050                                 // * { "url": { <media>: [url, ..] } }
1051                                 if ( $.isPlainObject( registry[module].style ) ) {
1052                                         for ( key in registry[module].style ) {
1053                                                 value = registry[module].style[key];
1054                                                 media = undefined;
1056                                                 if ( key !== 'url' && key !== 'css' ) {
1057                                                         // Backwards compatibility, key is a media-type
1058                                                         if ( typeof value === 'string' ) {
1059                                                                 // back-compat: { <media>: css }
1060                                                                 // Ignore 'media' because it isn't supported (nor was it used).
1061                                                                 // Strings are pre-wrapped in "@media". The media-type was just ""
1062                                                                 // (because it had to be set to something).
1063                                                                 // This is one of the reasons why this format is no longer used.
1064                                                                 addEmbeddedCSS( value, cssHandle() );
1065                                                         } else {
1066                                                                 // back-compat: { <media>: [url, ..] }
1067                                                                 media = key;
1068                                                                 key = 'bc-url';
1069                                                         }
1070                                                 }
1072                                                 // Array of css strings in key 'css',
1073                                                 // or back-compat array of urls from media-type
1074                                                 if ( $.isArray( value ) ) {
1075                                                         for ( i = 0; i < value.length; i += 1 ) {
1076                                                                 if ( key === 'bc-url' ) {
1077                                                                         // back-compat: { <media>: [url, ..] }
1078                                                                         addLink( media, value[i] );
1079                                                                 } else if ( key === 'css' ) {
1080                                                                         // { "css": [css, ..] }
1081                                                                         addEmbeddedCSS( value[i], cssHandle() );
1082                                                                 }
1083                                                         }
1084                                                 // Not an array, but a regular object
1085                                                 // Array of urls inside media-type key
1086                                                 } else if ( typeof value === 'object' ) {
1087                                                         // { "url": { <media>: [url, ..] } }
1088                                                         for ( media in value ) {
1089                                                                 urls = value[media];
1090                                                                 for ( i = 0; i < urls.length; i += 1 ) {
1091                                                                         addLink( media, urls[i] );
1092                                                                 }
1093                                                         }
1094                                                 }
1095                                         }
1096                                 }
1098                                 // Kick off.
1099                                 cssHandlesRegistered = true;
1100                                 checkCssHandles();
1101                         }
1103                         /**
1104                          * Adds a dependencies to the queue with optional callbacks to be run
1105                          * when the dependencies are ready or fail
1106                          *
1107                          * @private
1108                          * @param {string|string[]} dependencies Module name or array of string module names
1109                          * @param {Function} [ready] Callback to execute when all dependencies are ready
1110                          * @param {Function} [error] Callback to execute when any dependency fails
1111                          * @param {boolean} [async] If true, load modules asynchronously even if
1112                          *  document ready has not yet occurred.
1113                          */
1114                         function request( dependencies, ready, error, async ) {
1115                                 var n;
1117                                 // Allow calling by single module name
1118                                 if ( typeof dependencies === 'string' ) {
1119                                         dependencies = [dependencies];
1120                                 }
1122                                 // Add ready and error callbacks if they were given
1123                                 if ( ready !== undefined || error !== undefined ) {
1124                                         jobs[jobs.length] = {
1125                                                 'dependencies': filter(
1126                                                         ['registered', 'loading', 'loaded'],
1127                                                         dependencies
1128                                                 ),
1129                                                 'ready': ready,
1130                                                 'error': error
1131                                         };
1132                                 }
1134                                 // Queue up any dependencies that are registered
1135                                 dependencies = filter( ['registered'], dependencies );
1136                                 for ( n = 0; n < dependencies.length; n += 1 ) {
1137                                         if ( $.inArray( dependencies[n], queue ) === -1 ) {
1138                                                 queue[queue.length] = dependencies[n];
1139                                                 if ( async ) {
1140                                                         // Mark this module as async in the registry
1141                                                         registry[dependencies[n]].async = true;
1142                                                 }
1143                                         }
1144                                 }
1146                                 // Work the queue
1147                                 mw.loader.work();
1148                         }
1150                         function sortQuery(o) {
1151                                 var sorted = {}, key, a = [];
1152                                 for ( key in o ) {
1153                                         if ( hasOwn.call( o, key ) ) {
1154                                                 a.push( key );
1155                                         }
1156                                 }
1157                                 a.sort();
1158                                 for ( key = 0; key < a.length; key += 1 ) {
1159                                         sorted[a[key]] = o[a[key]];
1160                                 }
1161                                 return sorted;
1162                         }
1164                         /**
1165                          * Converts a module map of the form { foo: [ 'bar', 'baz' ], bar: [ 'baz, 'quux' ] }
1166                          * to a query string of the form foo.bar,baz|bar.baz,quux
1167                          * @private
1168                          */
1169                         function buildModulesString( moduleMap ) {
1170                                 var arr = [], p, prefix;
1171                                 for ( prefix in moduleMap ) {
1172                                         p = prefix === '' ? '' : prefix + '.';
1173                                         arr.push( p + moduleMap[prefix].join( ',' ) );
1174                                 }
1175                                 return arr.join( '|' );
1176                         }
1178                         /**
1179                          * Asynchronously append a script tag to the end of the body
1180                          * that invokes load.php
1181                          * @private
1182                          * @param {Object} moduleMap Module map, see #buildModulesString
1183                          * @param {Object} currReqBase Object with other parameters (other than 'modules') to use in the request
1184                          * @param {string} sourceLoadScript URL of load.php
1185                          * @param {boolean} async If true, use an asynchrounous request even if document ready has not yet occurred
1186                          */
1187                         function doRequest( moduleMap, currReqBase, sourceLoadScript, async ) {
1188                                 var request = $.extend(
1189                                         { modules: buildModulesString( moduleMap ) },
1190                                         currReqBase
1191                                 );
1192                                 request = sortQuery( request );
1193                                 // Asynchronously append a script tag to the end of the body
1194                                 // Append &* to avoid triggering the IE6 extension check
1195                                 addScript( sourceLoadScript + '?' + $.param( request ) + '&*', null, async );
1196                         }
1198                         /* Public Methods */
1199                         return {
1200                                 /**
1201                                  * @inheritdoc #newStyleTag
1202                                  * @method
1203                                  */
1204                                 addStyleTag: newStyleTag,
1206                                 /**
1207                                  * Batch-request queued dependencies from the server.
1208                                  */
1209                                 work: function () {
1210                                         var     reqBase, splits, maxQueryLength, q, b, bSource, bGroup, bSourceGroup,
1211                                                 source, group, g, i, modules, maxVersion, sourceLoadScript,
1212                                                 currReqBase, currReqBaseLength, moduleMap, l,
1213                                                 lastDotIndex, prefix, suffix, bytesAdded, async;
1215                                         // Build a list of request parameters common to all requests.
1216                                         reqBase = {
1217                                                 skin: mw.config.get( 'skin' ),
1218                                                 lang: mw.config.get( 'wgUserLanguage' ),
1219                                                 debug: mw.config.get( 'debug' )
1220                                         };
1221                                         // Split module batch by source and by group.
1222                                         splits = {};
1223                                         maxQueryLength = mw.config.get( 'wgResourceLoaderMaxQueryLength', -1 );
1225                                         // Appends a list of modules from the queue to the batch
1226                                         for ( q = 0; q < queue.length; q += 1 ) {
1227                                                 // Only request modules which are registered
1228                                                 if ( registry[queue[q]] !== undefined && registry[queue[q]].state === 'registered' ) {
1229                                                         // Prevent duplicate entries
1230                                                         if ( $.inArray( queue[q], batch ) === -1 ) {
1231                                                                 batch[batch.length] = queue[q];
1232                                                                 // Mark registered modules as loading
1233                                                                 registry[queue[q]].state = 'loading';
1234                                                         }
1235                                                 }
1236                                         }
1237                                         // Early exit if there's nothing to load...
1238                                         if ( !batch.length ) {
1239                                                 return;
1240                                         }
1242                                         // The queue has been processed into the batch, clear up the queue.
1243                                         queue = [];
1245                                         // Always order modules alphabetically to help reduce cache
1246                                         // misses for otherwise identical content.
1247                                         batch.sort();
1249                                         // Split batch by source and by group.
1250                                         for ( b = 0; b < batch.length; b += 1 ) {
1251                                                 bSource = registry[batch[b]].source;
1252                                                 bGroup = registry[batch[b]].group;
1253                                                 if ( splits[bSource] === undefined ) {
1254                                                         splits[bSource] = {};
1255                                                 }
1256                                                 if ( splits[bSource][bGroup] === undefined ) {
1257                                                         splits[bSource][bGroup] = [];
1258                                                 }
1259                                                 bSourceGroup = splits[bSource][bGroup];
1260                                                 bSourceGroup[bSourceGroup.length] = batch[b];
1261                                         }
1263                                         // Clear the batch - this MUST happen before we append any
1264                                         // script elements to the body or it's possible that a script
1265                                         // will be locally cached, instantly load, and work the batch
1266                                         // again, all before we've cleared it causing each request to
1267                                         // include modules which are already loaded.
1268                                         batch = [];
1270                                         for ( source in splits ) {
1272                                                 sourceLoadScript = sources[source].loadScript;
1274                                                 for ( group in splits[source] ) {
1276                                                         // Cache access to currently selected list of
1277                                                         // modules for this group from this source.
1278                                                         modules = splits[source][group];
1280                                                         // Calculate the highest timestamp
1281                                                         maxVersion = 0;
1282                                                         for ( g = 0; g < modules.length; g += 1 ) {
1283                                                                 if ( registry[modules[g]].version > maxVersion ) {
1284                                                                         maxVersion = registry[modules[g]].version;
1285                                                                 }
1286                                                         }
1288                                                         currReqBase = $.extend( { version: formatVersionNumber( maxVersion ) }, reqBase );
1289                                                         // For user modules append a user name to the request.
1290                                                         if ( group === 'user' && mw.config.get( 'wgUserName' ) !== null ) {
1291                                                                 currReqBase.user = mw.config.get( 'wgUserName' );
1292                                                         }
1293                                                         currReqBaseLength = $.param( currReqBase ).length;
1294                                                         async = true;
1295                                                         // We may need to split up the request to honor the query string length limit,
1296                                                         // so build it piece by piece.
1297                                                         l = currReqBaseLength + 9; // '&modules='.length == 9
1299                                                         moduleMap = {}; // { prefix: [ suffixes ] }
1301                                                         for ( i = 0; i < modules.length; i += 1 ) {
1302                                                                 // Determine how many bytes this module would add to the query string
1303                                                                 lastDotIndex = modules[i].lastIndexOf( '.' );
1304                                                                 // Note that these substr() calls work even if lastDotIndex == -1
1305                                                                 prefix = modules[i].substr( 0, lastDotIndex );
1306                                                                 suffix = modules[i].substr( lastDotIndex + 1 );
1307                                                                 bytesAdded = moduleMap[prefix] !== undefined
1308                                                                         ? suffix.length + 3 // '%2C'.length == 3
1309                                                                         : modules[i].length + 3; // '%7C'.length == 3
1311                                                                 // If the request would become too long, create a new one,
1312                                                                 // but don't create empty requests
1313                                                                 if ( maxQueryLength > 0 && !$.isEmptyObject( moduleMap ) && l + bytesAdded > maxQueryLength ) {
1314                                                                         // This request would become too long, create a new one
1315                                                                         // and fire off the old one
1316                                                                         doRequest( moduleMap, currReqBase, sourceLoadScript, async );
1317                                                                         moduleMap = {};
1318                                                                         async = true;
1319                                                                         l = currReqBaseLength + 9;
1320                                                                 }
1321                                                                 if ( moduleMap[prefix] === undefined ) {
1322                                                                         moduleMap[prefix] = [];
1323                                                                 }
1324                                                                 moduleMap[prefix].push( suffix );
1325                                                                 if ( !registry[modules[i]].async ) {
1326                                                                         // If this module is blocking, make the entire request blocking
1327                                                                         // This is slightly suboptimal, but in practice mixing of blocking
1328                                                                         // and async modules will only occur in debug mode.
1329                                                                         async = false;
1330                                                                 }
1331                                                                 l += bytesAdded;
1332                                                         }
1333                                                         // If there's anything left in moduleMap, request that too
1334                                                         if ( !$.isEmptyObject( moduleMap ) ) {
1335                                                                 doRequest( moduleMap, currReqBase, sourceLoadScript, async );
1336                                                         }
1337                                                 }
1338                                         }
1339                                 },
1341                                 /**
1342                                  * Register a source.
1343                                  *
1344                                  * @param {string} id Short lowercase a-Z string representing a source, only used internally.
1345                                  * @param {Object} props Object containing only the loadScript property which is a url to
1346                                  *  the load.php location of the source.
1347                                  * @return {boolean}
1348                                  */
1349                                 addSource: function ( id, props ) {
1350                                         var source;
1351                                         // Allow multiple additions
1352                                         if ( typeof id === 'object' ) {
1353                                                 for ( source in id ) {
1354                                                         mw.loader.addSource( source, id[source] );
1355                                                 }
1356                                                 return true;
1357                                         }
1359                                         if ( sources[id] !== undefined ) {
1360                                                 throw new Error( 'source already registered: ' + id );
1361                                         }
1363                                         sources[id] = props;
1365                                         return true;
1366                                 },
1368                                 /**
1369                                  * Register a module, letting the system know about it and its
1370                                  * properties. Startup modules contain calls to this function.
1371                                  *
1372                                  * @param {string} module Module name
1373                                  * @param {number} version Module version number as a timestamp (falls backs to 0)
1374                                  * @param {string|Array|Function} dependencies One string or array of strings of module
1375                                  *  names on which this module depends, or a function that returns that array.
1376                                  * @param {string} [group=null] Group which the module is in
1377                                  * @param {string} [source='local'] Name of the source
1378                                  */
1379                                 register: function ( module, version, dependencies, group, source ) {
1380                                         var m;
1381                                         // Allow multiple registration
1382                                         if ( typeof module === 'object' ) {
1383                                                 for ( m = 0; m < module.length; m += 1 ) {
1384                                                         // module is an array of module names
1385                                                         if ( typeof module[m] === 'string' ) {
1386                                                                 mw.loader.register( module[m] );
1387                                                         // module is an array of arrays
1388                                                         } else if ( typeof module[m] === 'object' ) {
1389                                                                 mw.loader.register.apply( mw.loader, module[m] );
1390                                                         }
1391                                                 }
1392                                                 return;
1393                                         }
1394                                         // Validate input
1395                                         if ( typeof module !== 'string' ) {
1396                                                 throw new Error( 'module must be a string, not a ' + typeof module );
1397                                         }
1398                                         if ( registry[module] !== undefined ) {
1399                                                 throw new Error( 'module already registered: ' + module );
1400                                         }
1401                                         // List the module as registered
1402                                         registry[module] = {
1403                                                 version: version !== undefined ? parseInt( version, 10 ) : 0,
1404                                                 dependencies: [],
1405                                                 group: typeof group === 'string' ? group : null,
1406                                                 source: typeof source === 'string' ? source: 'local',
1407                                                 state: 'registered'
1408                                         };
1409                                         if ( typeof dependencies === 'string' ) {
1410                                                 // Allow dependencies to be given as a single module name
1411                                                 registry[module].dependencies = [ dependencies ];
1412                                         } else if ( typeof dependencies === 'object' || $.isFunction( dependencies ) ) {
1413                                                 // Allow dependencies to be given as an array of module names
1414                                                 // or a function which returns an array
1415                                                 registry[module].dependencies = dependencies;
1416                                         }
1417                                 },
1419                                 /**
1420                                  * Implement a module given the components that make up the module.
1421                                  *
1422                                  * When #load or #using requests one or more modules, the server
1423                                  * response contain calls to this function.
1424                                  *
1425                                  * All arguments are required.
1426                                  *
1427                                  * @param {string} module Name of module
1428                                  * @param {Function|Array} script Function with module code or Array of URLs to
1429                                  *  be used as the src attribute of a new `<script>` tag.
1430                                  * @param {Object} style Should follow one of the following patterns:
1431                                  *     { "css": [css, ..] }
1432                                  *     { "url": { <media>: [url, ..] } }
1433                                  * And for backwards compatibility (needs to be supported forever due to caching):
1434                                  *     { <media>: css }
1435                                  *     { <media>: [url, ..] }
1436                                  *
1437                                  * The reason css strings are not concatenated anymore is bug 31676. We now check
1438                                  * whether it's safe to extend the stylesheet (see #canExpandStylesheetWith).
1439                                  *
1440                                  * @param {Object} msgs List of key/value pairs to be added to {@link mw#messages}.
1441                                  */
1442                                 implement: function ( module, script, style, msgs ) {
1443                                         // Validate input
1444                                         if ( typeof module !== 'string' ) {
1445                                                 throw new Error( 'module must be a string, not a ' + typeof module );
1446                                         }
1447                                         if ( !$.isFunction( script ) && !$.isArray( script ) ) {
1448                                                 throw new Error( 'script must be a function or an array, not a ' + typeof script );
1449                                         }
1450                                         if ( !$.isPlainObject( style ) ) {
1451                                                 throw new Error( 'style must be an object, not a ' + typeof style );
1452                                         }
1453                                         if ( !$.isPlainObject( msgs ) ) {
1454                                                 throw new Error( 'msgs must be an object, not a ' + typeof msgs );
1455                                         }
1456                                         // Automatically register module
1457                                         if ( registry[module] === undefined ) {
1458                                                 mw.loader.register( module );
1459                                         }
1460                                         // Check for duplicate implementation
1461                                         if ( registry[module] !== undefined && registry[module].script !== undefined ) {
1462                                                 throw new Error( 'module already implemented: ' + module );
1463                                         }
1464                                         // Attach components
1465                                         registry[module].script = script;
1466                                         registry[module].style = style;
1467                                         registry[module].messages = msgs;
1468                                         // The module may already have been marked as erroneous
1469                                         if ( $.inArray( registry[module].state, ['error', 'missing'] ) === -1 ) {
1470                                                 registry[module].state = 'loaded';
1471                                                 if ( allReady( registry[module].dependencies ) ) {
1472                                                         execute( module );
1473                                                 }
1474                                         }
1475                                 },
1477                                 /**
1478                                  * Execute a function as soon as one or more required modules are ready.
1479                                  *
1480                                  * @param {string|Array} dependencies Module name or array of modules names the callback
1481                                  *  dependends on to be ready before executing
1482                                  * @param {Function} [ready] callback to execute when all dependencies are ready
1483                                  * @param {Function} [error] callback to execute when if dependencies have a errors
1484                                  */
1485                                 using: function ( dependencies, ready, error ) {
1486                                         var tod = typeof dependencies;
1487                                         // Validate input
1488                                         if ( tod !== 'object' && tod !== 'string' ) {
1489                                                 throw new Error( 'dependencies must be a string or an array, not a ' + tod );
1490                                         }
1491                                         // Allow calling with a single dependency as a string
1492                                         if ( tod === 'string' ) {
1493                                                 dependencies = [ dependencies ];
1494                                         }
1495                                         // Resolve entire dependency map
1496                                         dependencies = resolve( dependencies );
1497                                         if ( allReady( dependencies ) ) {
1498                                                 // Run ready immediately
1499                                                 if ( $.isFunction( ready ) ) {
1500                                                         ready();
1501                                                 }
1502                                         } else if ( filter( ['error', 'missing'], dependencies ).length ) {
1503                                                 // Execute error immediately if any dependencies have errors
1504                                                 if ( $.isFunction( error ) ) {
1505                                                         error( new Error( 'one or more dependencies have state "error" or "missing"' ),
1506                                                                 dependencies );
1507                                                 }
1508                                         } else {
1509                                                 // Not all dependencies are ready: queue up a request
1510                                                 request( dependencies, ready, error );
1511                                         }
1512                                 },
1514                                 /**
1515                                  * Load an external script or one or more modules.
1516                                  *
1517                                  * @param {string|Array} modules Either the name of a module, array of modules,
1518                                  *  or a URL of an external script or style
1519                                  * @param {string} [type='text/javascript'] mime-type to use if calling with a URL of an
1520                                  *  external script or style; acceptable values are "text/css" and
1521                                  *  "text/javascript"; if no type is provided, text/javascript is assumed.
1522                                  * @param {boolean} [async] If true, load modules asynchronously
1523                                  *  even if document ready has not yet occurred. If false, block before
1524                                  *  document ready and load async after. If not set, true will be
1525                                  *  assumed if loading a URL, and false will be assumed otherwise.
1526                                  */
1527                                 load: function ( modules, type, async ) {
1528                                         var filtered, m, module, l;
1530                                         // Validate input
1531                                         if ( typeof modules !== 'object' && typeof modules !== 'string' ) {
1532                                                 throw new Error( 'modules must be a string or an array, not a ' + typeof modules );
1533                                         }
1534                                         // Allow calling with an external url or single dependency as a string
1535                                         if ( typeof modules === 'string' ) {
1536                                                 // Support adding arbitrary external scripts
1537                                                 if ( /^(https?:)?\/\//.test( modules ) ) {
1538                                                         if ( async === undefined ) {
1539                                                                 // Assume async for bug 34542
1540                                                                 async = true;
1541                                                         }
1542                                                         if ( type === 'text/css' ) {
1543                                                                 // IE7-8 throws security warnings when inserting a <link> tag
1544                                                                 // with a protocol-relative URL set though attributes (instead of
1545                                                                 // properties) - when on HTTPS. See also bug #.
1546                                                                 l = document.createElement( 'link' );
1547                                                                 l.rel = 'stylesheet';
1548                                                                 l.href = modules;
1549                                                                 $( 'head' ).append( l );
1550                                                                 return;
1551                                                         }
1552                                                         if ( type === 'text/javascript' || type === undefined ) {
1553                                                                 addScript( modules, null, async );
1554                                                                 return;
1555                                                         }
1556                                                         // Unknown type
1557                                                         throw new Error( 'invalid type for external url, must be text/css or text/javascript. not ' + type );
1558                                                 }
1559                                                 // Called with single module
1560                                                 modules = [ modules ];
1561                                         }
1563                                         // Filter out undefined modules, otherwise resolve() will throw
1564                                         // an exception for trying to load an undefined module.
1565                                         // Undefined modules are acceptable here in load(), because load() takes
1566                                         // an array of unrelated modules, whereas the modules passed to
1567                                         // using() are related and must all be loaded.
1568                                         for ( filtered = [], m = 0; m < modules.length; m += 1 ) {
1569                                                 module = registry[modules[m]];
1570                                                 if ( module !== undefined ) {
1571                                                         if ( $.inArray( module.state, ['error', 'missing'] ) === -1 ) {
1572                                                                 filtered[filtered.length] = modules[m];
1573                                                         }
1574                                                 }
1575                                         }
1577                                         if ( filtered.length === 0 ) {
1578                                                 return;
1579                                         }
1580                                         // Resolve entire dependency map
1581                                         filtered = resolve( filtered );
1582                                         // If all modules are ready, nothing to be done
1583                                         if ( allReady( filtered ) ) {
1584                                                 return;
1585                                         }
1586                                         // If any modules have errors: also quit.
1587                                         if ( filter( ['error', 'missing'], filtered ).length ) {
1588                                                 return;
1589                                         }
1590                                         // Since some modules are not yet ready, queue up a request.
1591                                         request( filtered, undefined, undefined, async );
1592                                 },
1594                                 /**
1595                                  * Change the state of one or more modules.
1596                                  *
1597                                  * @param {string|Object} module module name or object of module name/state pairs
1598                                  * @param {string} state state name
1599                                  */
1600                                 state: function ( module, state ) {
1601                                         var m;
1603                                         if ( typeof module === 'object' ) {
1604                                                 for ( m in module ) {
1605                                                         mw.loader.state( m, module[m] );
1606                                                 }
1607                                                 return;
1608                                         }
1609                                         if ( registry[module] === undefined ) {
1610                                                 mw.loader.register( module );
1611                                         }
1612                                         if ( $.inArray( state, ['ready', 'error', 'missing'] ) !== -1
1613                                                 && registry[module].state !== state ) {
1614                                                 // Make sure pending modules depending on this one get executed if their
1615                                                 // dependencies are now fulfilled!
1616                                                 registry[module].state = state;
1617                                                 handlePending( module );
1618                                         } else {
1619                                                 registry[module].state = state;
1620                                         }
1621                                 },
1623                                 /**
1624                                  * Get the version of a module.
1625                                  *
1626                                  * @param {string} module Name of module to get version for
1627                                  */
1628                                 getVersion: function ( module ) {
1629                                         if ( registry[module] !== undefined && registry[module].version !== undefined ) {
1630                                                 return formatVersionNumber( registry[module].version );
1631                                         }
1632                                         return null;
1633                                 },
1635                                 /**
1636                                  * @inheritdoc #getVersion
1637                                  * @deprecated since 1.18 use #getVersion instead
1638                                  */
1639                                 version: function () {
1640                                         return mw.loader.getVersion.apply( mw.loader, arguments );
1641                                 },
1643                                 /**
1644                                  * Get the state of a module.
1645                                  *
1646                                  * @param {string} module name of module to get state for
1647                                  */
1648                                 getState: function ( module ) {
1649                                         if ( registry[module] !== undefined && registry[module].state !== undefined ) {
1650                                                 return registry[module].state;
1651                                         }
1652                                         return null;
1653                                 },
1655                                 /**
1656                                  * Get names of all registered modules.
1657                                  *
1658                                  * @return {Array}
1659                                  */
1660                                 getModuleNames: function () {
1661                                         return $.map( registry, function ( i, key ) {
1662                                                 return key;
1663                                         } );
1664                                 },
1666                                 /**
1667                                  * Load the `mediawiki.user` module.
1668                                  *
1669                                  * For backwards-compatibility with cached pages from before 2013 where:
1670                                  *
1671                                  * - the `mediawiki.user` module didn't exist yet
1672                                  * - `mw.user` was still part of mediawiki.js
1673                                  * - `mw.loader.go` still existed and called after `mw.loader.load()`
1674                                  */
1675                                 go: function () {
1676                                         mw.loader.load( 'mediawiki.user' );
1677                                 }
1678                         };
1679                 }() ),
1681                 /**
1682                  * HTML construction helper functions
1683                  *
1684                  *     @example
1685                  *
1686                  *     var Html, output;
1687                  *
1688                  *     Html = mw.html;
1689                  *     output = Html.element( 'div', {}, new Html.Raw(
1690                  *         Html.element( 'img', { src: '<' } )
1691                  *     ) );
1692                  *     mw.log( output ); // <div><img src="&lt;"/></div>
1693                  *
1694                  * @class mw.html
1695                  * @singleton
1696                  */
1697                 html: ( function () {
1698                         function escapeCallback( s ) {
1699                                 switch ( s ) {
1700                                         case '\'':
1701                                                 return '&#039;';
1702                                         case '"':
1703                                                 return '&quot;';
1704                                         case '<':
1705                                                 return '&lt;';
1706                                         case '>':
1707                                                 return '&gt;';
1708                                         case '&':
1709                                                 return '&amp;';
1710                                 }
1711                         }
1713                         return {
1714                                 /**
1715                                  * Escape a string for HTML. Converts special characters to HTML entities.
1716                                  * @param {string} s The string to escape
1717                                  */
1718                                 escape: function ( s ) {
1719                                         return s.replace( /['"<>&]/g, escapeCallback );
1720                                 },
1722                                 /**
1723                                  * Create an HTML element string, with safe escaping.
1724                                  *
1725                                  * @param {string} name The tag name.
1726                                  * @param {Object} attrs An object with members mapping element names to values
1727                                  * @param {Mixed} contents The contents of the element. May be either:
1728                                  *  - string: The string is escaped.
1729                                  *  - null or undefined: The short closing form is used, e.g. <br/>.
1730                                  *  - this.Raw: The value attribute is included without escaping.
1731                                  *  - this.Cdata: The value attribute is included, and an exception is
1732                                  *   thrown if it contains an illegal ETAGO delimiter.
1733                                  *   See http://www.w3.org/TR/1999/REC-html401-19991224/appendix/notes.html#h-B.3.2
1734                                  */
1735                                 element: function ( name, attrs, contents ) {
1736                                         var v, attrName, s = '<' + name;
1738                                         for ( attrName in attrs ) {
1739                                                 v = attrs[attrName];
1740                                                 // Convert name=true, to name=name
1741                                                 if ( v === true ) {
1742                                                         v = attrName;
1743                                                 // Skip name=false
1744                                                 } else if ( v === false ) {
1745                                                         continue;
1746                                                 }
1747                                                 s += ' ' + attrName + '="' + this.escape( String( v ) ) + '"';
1748                                         }
1749                                         if ( contents === undefined || contents === null ) {
1750                                                 // Self close tag
1751                                                 s += '/>';
1752                                                 return s;
1753                                         }
1754                                         // Regular open tag
1755                                         s += '>';
1756                                         switch ( typeof contents ) {
1757                                                 case 'string':
1758                                                         // Escaped
1759                                                         s += this.escape( contents );
1760                                                         break;
1761                                                 case 'number':
1762                                                 case 'boolean':
1763                                                         // Convert to string
1764                                                         s += String( contents );
1765                                                         break;
1766                                                 default:
1767                                                         if ( contents instanceof this.Raw ) {
1768                                                                 // Raw HTML inclusion
1769                                                                 s += contents.value;
1770                                                         } else if ( contents instanceof this.Cdata ) {
1771                                                                 // CDATA
1772                                                                 if ( /<\/[a-zA-z]/.test( contents.value ) ) {
1773                                                                         throw new Error( 'mw.html.element: Illegal end tag found in CDATA' );
1774                                                                 }
1775                                                                 s += contents.value;
1776                                                         } else {
1777                                                                 throw new Error( 'mw.html.element: Invalid type of contents' );
1778                                                         }
1779                                         }
1780                                         s += '</' + name + '>';
1781                                         return s;
1782                                 },
1784                                 /**
1785                                  * Wrapper object for raw HTML passed to mw.html.element().
1786                                  * @class mw.html.Raw
1787                                  */
1788                                 Raw: function ( value ) {
1789                                         this.value = value;
1790                                 },
1792                                 /**
1793                                  * Wrapper object for CDATA element contents passed to mw.html.element()
1794                                  * @class mw.html.Cdata
1795                                  */
1796                                 Cdata: function ( value ) {
1797                                         this.value = value;
1798                                 }
1799                         };
1800                 }() ),
1802                 // Skeleton user object. mediawiki.user.js extends this
1803                 user: {
1804                         options: new Map(),
1805                         tokens: new Map()
1806                 },
1808                 /**
1809                  * Registry and firing of events.
1810                  *
1811                  * MediaWiki has various interface components that are extended, enhanced
1812                  * or manipulated in some other way by extensions, gadgets and even
1813                  * in core itself.
1814                  *
1815                  * This framework helps streamlining the timing of when these other
1816                  * code paths fire their plugins (instead of using document-ready,
1817                  * which can and should be limited to firing only once).
1818                  *
1819                  * Features like navigating to other wiki pages, previewing an edit
1820                  * and editing itself – without a refresh – can then retrigger these
1821                  * hooks accordingly to ensure everything still works as expected.
1822                  *
1823                  * Example usage:
1824                  *
1825                  *     mw.hook( 'wikipage.content' ).add( fn ).remove( fn );
1826                  *     mw.hook( 'wikipage.content' ).fire( $content );
1827                  *
1828                  * Handlers can be added and fired for arbitrary event names at any time. The same
1829                  * event can be fired multiple times. The last run of an event is memorized
1830                  * (similar to `$(document).ready` and `$.Deferred().done`).
1831                  * This means if an event is fired, and a handler added afterwards, the added
1832                  * function will be fired right away with the last given event data.
1833                  *
1834                  * Like Deferreds and Promises, the mw.hook object is both detachable and chainable.
1835                  * Thus allowing flexible use and optimal maintainability and authority control.
1836                  * You can pass around the `add` and/or `fire` method to another piece of code
1837                  * without it having to know the event name (or `mw.hook` for that matter).
1838                  *
1839                  *     var h = mw.hook( 'bar.ready' );
1840                  *     new mw.Foo( .. ).fetch( { callback: h.fire } );
1841                  *
1842                  * Note: Events are documented with an underscore instead of a dot in the event
1843                  * name due to jsduck not supporting dots in that position.
1844                  *
1845                  * @class mw.hook
1846                  */
1847                 hook: ( function () {
1848                         var lists = {};
1850                         /**
1851                          * Create an instance of mw.hook.
1852                          *
1853                          * @method hook
1854                          * @member mw
1855                          * @param {string} name Name of hook.
1856                          * @return {mw.hook}
1857                          */
1858                         return function ( name ) {
1859                                 var list = lists[name] || ( lists[name] = $.Callbacks( 'memory' ) );
1861                                 return {
1862                                         /**
1863                                          * Register a hook handler
1864                                          * @param {Function...} handler Function to bind.
1865                                          * @chainable
1866                                          */
1867                                         add: list.add,
1869                                         /**
1870                                          * Unregister a hook handler
1871                                          * @param {Function...} handler Function to unbind.
1872                                          * @chainable
1873                                          */
1874                                         remove: list.remove,
1876                                         /**
1877                                          * Run a hook.
1878                                          * @param {Mixed...} data
1879                                          * @chainable
1880                                          */
1881                                         fire: function () {
1882                                                 return list.fireWith( null, slice.call( arguments ) );
1883                                         }
1884                                 };
1885                         };
1886                 }() )
1887         };
1889 }( jQuery ) );
1891 // Alias $j to jQuery for backwards compatibility
1892 window.$j = jQuery;
1894 // Attach to window and globally alias
1895 window.mw = window.mediaWiki = mw;
1897 // Auto-register from pre-loaded startup scripts
1898 if ( jQuery.isFunction( window.startUp ) ) {
1899         window.startUp();
1900         window.startUp = undefined;