fetchLanguageNames: fallback to default instead of false
[lhc/web/wiklou.git] / resources / mediawiki / mediawiki.js
index 585483b..4f345ab 100644 (file)
@@ -1,9 +1,11 @@
+/*jslint browser: true, continue: true, white: true, forin: true*/
+/*global jQuery*/
 /*
  * Core MediaWiki JavaScript Library
  */
 
 var mw = ( function ( $, undefined ) {
-"use strict";
+       "use strict";
 
        /* Private Members */
 
@@ -49,7 +51,9 @@ var mw = ( function ( $, undefined ) {
                                        results[selection[i]] = this.get( selection[i], fallback );
                                }
                                return results;
-                       } else if ( typeof selection === 'string' ) {
+                       }
+
+                       if ( typeof selection === 'string' ) {
                                if ( this.values[selection] === undefined ) {
                                        if ( fallback !== undefined ) {
                                                return fallback;
@@ -58,11 +62,13 @@ var mw = ( function ( $, undefined ) {
                                }
                                return this.values[selection];
                        }
+
                        if ( selection === undefined ) {
                                return this.values;
-                       } else {
-                               return null; // invalid selection key
                        }
+
+                       // invalid selection key
+                       return null;
                },
 
                /**
@@ -80,7 +86,8 @@ var mw = ( function ( $, undefined ) {
                                        this.values[s] = selection[s];
                                }
                                return true;
-                       } else if ( typeof selection === 'string' && value !== undefined ) {
+                       }
+                       if ( typeof selection === 'string' && value !== undefined ) {
                                this.values[selection] = value;
                                return true;
                        }
@@ -103,9 +110,8 @@ var mw = ( function ( $, undefined ) {
                                        }
                                }
                                return true;
-                       } else {
-                               return this.values[selection] !== undefined;
                        }
+                       return this.values[selection] !== undefined;
                }
        };
 
@@ -132,7 +138,7 @@ var mw = ( function ( $, undefined ) {
                /**
                 * Simple message parser, does $N replacement and nothing else.
                 * This may be overridden to provide a more complex message parser.
-                * 
+                *
                 * This function will not be called for nonexistent messages.
                 */
                parser: function() {
@@ -142,7 +148,7 @@ var mw = ( function ( $, undefined ) {
                                return parameters[index] !== undefined ? parameters[index] : '$' + match;
                        } );
                },
-               
+
                /**
                 * Appends (does not replace) parameters for replacement to the .parameters property.
                 *
@@ -186,7 +192,7 @@ var mw = ( function ( $, undefined ) {
                                text = this.parser();
                                text = mw.html.escape( text );
                        }
-                       
+
                        if ( this.format === 'parse' ) {
                                text = this.parser();
                        }
@@ -242,7 +248,7 @@ var mw = ( function ( $, undefined ) {
                 * emulates console.log in console-less environments.
                 */
                log: function() { },
-       
+
                /**
                 * @var constructor Make the Map constructor publicly available.
                 */
@@ -252,7 +258,7 @@ var mw = ( function ( $, undefined ) {
                 * @var constructor Make the Message constructor publicly available.
                 */
                Message: Message,
-       
+
                /**
                 * List of configuration values
                 *
@@ -261,25 +267,25 @@ var mw = ( function ( $, undefined ) {
                 * in the global window object.
                 */
                config: null,
-       
+
                /**
                 * @var object
                 *
                 * Empty object that plugins can be installed in.
                 */
                libs: {},
-       
+
                /* Extension points */
-       
+
                legacy: {},
-       
+
                /**
                 * Localization system
                 */
                messages: new Map(),
-       
+
                /* Public Methods */
-       
+
                /**
                 * Gets a message object, similar to wfMessage()
                 *
@@ -299,7 +305,7 @@ var mw = ( function ( $, undefined ) {
                        }
                        return new Message( mw.messages, key, parameters );
                },
-       
+
                /**
                 * Gets a message string, similar to wfMsg()
                 *
@@ -308,17 +314,17 @@ var mw = ( function ( $, undefined ) {
                 *  each a parameter for $N replacement in messages.
                 * @return String.
                 */
-               msg: function ( key, parameters ) {
+               msg: function ( /* key, parameter_1, parameter_2, .. */ ) {
                        return mw.message.apply( mw.message, arguments ).toString();
                },
-       
+
                /**
                 * Client-side module loader which integrates with the MediaWiki ResourceLoader
                 */
-               loader: ( function() {
-       
+               loader: ( function () {
+
                        /* Private Members */
-       
+
                        /**
                         * Mapping of registered modules
                         *
@@ -364,35 +370,83 @@ var mw = ( function ( $, undefined ) {
                                jobs = [],
                                // Flag indicating that document ready has occured
                                ready = false,
-                               // Whether we should try to load scripts in a blocking way 
-                               // Set with setBlocking()
-                               blocking = false,
                                // Selector cache for the marker element. Use getMarker() to get/use the marker!
                                $marker = null;
-       
+
                        /* Cache document ready status */
-       
+
                        $(document).ready( function () {
                                ready = true;
                        } );
-       
+
                        /* Private methods */
-       
-                       function getMarker(){
+
+                       function getMarker() {
                                // Cached ?
                                if ( $marker ) {
                                        return $marker;
+                               }
+
+                               $marker = $( 'meta[name="ResourceLoaderDynamicStyles"]' );
+                               if ( $marker.length ) {
+                                       return $marker;
+                               }
+                               mw.log( 'getMarker> No <meta name="ResourceLoaderDynamicStyles"> found, inserting dynamically.' );
+                               $marker = $( '<meta>' ).attr( 'name', 'ResourceLoaderDynamicStyles' ).appendTo( 'head' );
+
+                               return $marker;
+                       }
+
+                       /**
+                        * Create a new style tag and add it to the DOM.
+                        *
+                        * @param text String: CSS text
+                        * @param $nextnode mixed: [optional] An Element or jQuery object for an element where
+                        * the style tag should be inserted before. Otherwise appended to the <head>.
+                        * @return HTMLStyleElement
+                        */
+                       function addStyleTag( text, $nextnode ) {
+                               var s = document.createElement( 'style' );
+                               s.type = 'text/css';
+                               s.rel = 'stylesheet';
+                               // Insert into document before setting cssText (bug 33305)
+                               if ( $nextnode ) {
+                                       // If a raw element, create a jQuery object, otherwise use directly
+                                       if ( $nextnode.nodeType ) {
+                                               $nextnode = $( $nextnode );
+                                       }
+                                       $nextnode.before( s );
                                } else {
-                                       $marker = $( 'meta[name="ResourceLoaderDynamicStyles"]' );
-                                       if ( $marker.length ) {
-                                               return $marker;
+                                       document.getElementsByTagName('head')[0].appendChild( s );
+                               }
+                               if ( s.styleSheet ) {
+                                       s.styleSheet.cssText = text; // IE
+                               } else {
+                                       // Safari sometimes borks on null
+                                       s.appendChild( document.createTextNode( String( text ) ) );
+                               }
+                               return s;
+                       }
+
+                       function addInlineCSS( css ) {
+                               var $style, style, $newStyle;
+                               $style = getMarker().prev();
+                               // Disable <style> tag recycling/concatenation because of bug 34669
+                               if ( false && $style.is( 'style' ) && $style.data( 'ResourceLoaderDynamicStyleTag' ) === true ) {
+                                       // There's already a dynamic <style> tag present, append to it. This recycling of
+                                       // <style> tags is for bug 31676 (can't have more than 32 <style> tags in IE)
+                                       style = $style.get( 0 );
+                                       if ( style.styleSheet ) {
+                                               style.styleSheet.cssText += css; // IE
+                                       } else {
+                                               style.appendChild( document.createTextNode( String( css ) ) );
                                        }
-                                       mw.log( 'getMarker> No <meta name="ResourceLoaderDynamicStyles"> found, inserting dynamically.' );
-                                       $marker = $( '<meta>' ).attr( 'name', 'ResourceLoaderDynamicStyles' ).appendTo( 'head' );
-                                       return $marker;
+                               } else {
+                                       $newStyle = $( addStyleTag( css, getMarker() ) )
+                                               .data( 'ResourceLoaderDynamicStyleTag', true );
                                }
                        }
-       
+
                        function compare( a, b ) {
                                var i;
                                if ( a.length !== b.length ) {
@@ -410,7 +464,7 @@ var mw = ( function ( $, undefined ) {
                                }
                                return true;
                        }
-       
+
                        /**
                         * Generates an ISO8601 "basic" string from a UNIX timestamp
                         */
@@ -425,13 +479,13 @@ var mw = ( function ( $, undefined ) {
                                        pad( d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds() ), 'Z'
                                ].join( '' );
                        }
-       
+
                        /**
                         * Recursively resolves dependencies and detects circular references
                         */
                        function recurse( module, resolved, unresolved ) {
                                var n, deps, len;
-       
+
                                if ( registry[module] === undefined ) {
                                        throw new Error( 'Unknown dependency: ' + module );
                                }
@@ -464,7 +518,7 @@ var mw = ( function ( $, undefined ) {
                                }
                                resolved[resolved.length] = module;
                        }
-       
+
                        /**
                         * Gets a list of module names that a module depends on in their proper dependency order
                         *
@@ -474,7 +528,7 @@ var mw = ( function ( $, undefined ) {
                         */
                        function resolve( module ) {
                                var modules, m, deps, n, resolved;
-       
+
                                // Allow calling with an array of module names
                                if ( $.isArray( module ) ) {
                                        modules = [];
@@ -485,14 +539,17 @@ var mw = ( function ( $, undefined ) {
                                                }
                                        }
                                        return modules;
-                               } else if ( typeof module === 'string' ) {
+                               }
+
+                               if ( typeof module === 'string' ) {
                                        resolved = [];
                                        recurse( module, resolved, [] );
                                        return resolved;
                                }
+
                                throw new Error( 'Invalid module argument: ' + module );
                        }
-       
+
                        /**
                         * Narrows a list of module names down to those matching a specific
                         * state (see comment on top of this scope for a list of valid states).
@@ -506,7 +563,7 @@ var mw = ( function ( $, undefined ) {
                         */
                        function filter( states, modules ) {
                                var list, module, s, m;
-       
+
                                // Allow states to be given as a string
                                if ( typeof states === 'string' ) {
                                        states = [states];
@@ -539,7 +596,7 @@ var mw = ( function ( $, undefined ) {
                                }
                                return list;
                        }
-       
+
                        /**
                         * Automatically executes jobs and modules which are pending with satistifed dependencies.
                         *
@@ -547,22 +604,23 @@ var mw = ( function ( $, undefined ) {
                         */
                        function handlePending( module ) {
                                var j, r;
-       
+
                                try {
-                                       // Run jobs who's dependencies have just been met
+                                       // Run jobs whose dependencies have just been met
                                        for ( j = 0; j < jobs.length; j += 1 ) {
                                                if ( compare(
                                                        filter( 'ready', jobs[j].dependencies ),
                                                        jobs[j].dependencies ) )
                                                {
-                                                       if ( $.isFunction( jobs[j].ready ) ) {
-                                                               jobs[j].ready();
-                                                       }
+                                                       var callback = jobs[j].ready;
                                                        jobs.splice( j, 1 );
                                                        j -= 1;
+                                                       if ( $.isFunction( callback ) ) {
+                                                               callback();
+                                                       }
                                                }
                                        }
-                                       // Execute modules who's dependencies have just been met
+                                       // Execute modules whose dependencies have just been met
                                        for ( r in registry ) {
                                                if ( registry[r].state === 'loaded' ) {
                                                        if ( compare(
@@ -584,19 +642,20 @@ var mw = ( function ( $, undefined ) {
                                                        j -= 1;
                                                }
                                        }
+                                       throw e;
                                }
                        }
-       
+
                        /**
                         * Adds a script tag to the DOM, either using document.write or low-level DOM manipulation,
-                        * depending on whether document-ready has occured yet and whether we are in blocking mode.
+                        * depending on whether document-ready has occured yet and whether we are in async mode.
                         *
                         * @param src String: URL to script, will be used as the src attribute in the script tag
                         * @param callback Function: Optional callback which will be run when the script is done
                         */
-                       function addScript( src, callback ) {
+                       function addScript( src, callback, async ) {
                                var done = false, script, head;
-                               if ( ready || !blocking ) {
+                               if ( ready || async ) {
                                        // jQuery's getScript method is NOT better than doing this the old-fashioned way
                                        // because jQuery will eval the script's code, and errors will not have sane
                                        // line numbers.
@@ -606,7 +665,7 @@ var mw = ( function ( $, undefined ) {
                                        if ( $.isFunction( callback ) ) {
                                                // Attach handlers for all browsers (based on jQuery.ajax)
                                                script.onload = script.onreadystatechange = function() {
-       
+
                                                        if (
                                                                !done
                                                                && (
@@ -614,24 +673,28 @@ var mw = ( function ( $, undefined ) {
                                                                        || /loaded|complete/.test( script.readyState )
                                                                )
                                                        ) {
-       
+
                                                                done = true;
-       
-                                                               // Handle memory leak in IE
-                                                               script.onload = script.onreadystatechange = null;
-       
+
                                                                callback();
-       
-                                                               if ( script.parentNode ) {
-                                                                       script.parentNode.removeChild( script );
-                                                               }
-       
-                                                               // Dereference the script
-                                                               script = undefined;
+
+                                                               // Handle memory leak in IE. This seems to fail in
+                                                               // IE7 sometimes (Permission Denied error when
+                                                               // accessing script.parentNode) so wrap it in
+                                                               // a try catch.
+                                                               try {
+                                                                       script.onload = script.onreadystatechange = null;
+                                                                       if ( script.parentNode ) {
+                                                                               script.parentNode.removeChild( script );
+                                                                       }
+
+                                                                       // Dereference the script
+                                                                       script = undefined;
+                                                               } catch ( e ) { }
                                                        }
                                                };
                                        }
-                                       
+
                                        if ( window.opera ) {
                                                // Appending to the <head> blocks rendering completely in Opera,
                                                // so append to the <body> after document ready. This means the
@@ -656,7 +719,7 @@ var mw = ( function ( $, undefined ) {
                                        }
                                }
                        }
-       
+
                        /**
                         * Executes a loaded module, making it ready to use
                         *
@@ -664,7 +727,7 @@ var mw = ( function ( $, undefined ) {
                         */
                        function execute( module, callback ) {
                                var style, media, i, script, markModuleReady, nestedAddScript;
-       
+
                                if ( registry[module] === undefined ) {
                                        throw new Error( 'Module has not been registered yet: ' + module );
                                } else if ( registry[module].state === 'registered' ) {
@@ -674,25 +737,22 @@ var mw = ( function ( $, undefined ) {
                                } else if ( registry[module].state === 'ready' ) {
                                        throw new Error( 'Module has already been loaded: ' + module );
                                }
-       
+
                                // Add styles
                                if ( $.isPlainObject( registry[module].style ) ) {
+                                       // 'media' type ignored, see documentation of mw.loader.implement
                                        for ( media in registry[module].style ) {
                                                style = registry[module].style[media];
                                                if ( $.isArray( style ) ) {
                                                        for ( i = 0; i < style.length; i += 1 ) {
                                                                getMarker().before( mw.html.element( 'link', {
                                                                        'type': 'text/css',
-                                                                       'media': media,
                                                                        'rel': 'stylesheet',
                                                                        'href': style[i]
                                                                } ) );
                                                        }
                                                } else if ( typeof style === 'string' ) {
-                                                       getMarker().before( mw.html.element( 'style', {
-                                                               'type': 'text/css',
-                                                               'media': media
-                                                       }, new mw.html.Cdata( style ) ) );
+                                                       addInlineCSS( style );
                                                }
                                        }
                                }
@@ -710,7 +770,7 @@ var mw = ( function ( $, undefined ) {
                                                        callback();
                                                }
                                        };
-                                       nestedAddScript = function ( arr, callback, i ) {
+                                       nestedAddScript = function ( arr, callback, async, i ) {
                                                // Recursively call addScript() in its own callback
                                                // for each element of arr.
                                                if ( i >= arr.length ) {
@@ -718,15 +778,15 @@ var mw = ( function ( $, undefined ) {
                                                        callback();
                                                        return;
                                                }
-       
+
                                                addScript( arr[i], function() {
-                                                       nestedAddScript( arr, callback, i + 1 );
-                                               } );
+                                                       nestedAddScript( arr, callback, async, i + 1 );
+                                               }, async );
                                        };
-       
+
                                        if ( $.isArray( script ) ) {
                                                registry[module].state = 'loading';
-                                               nestedAddScript( script, markModuleReady, 0 );
+                                               nestedAddScript( script, markModuleReady, registry[module].async, 0 );
                                        } else if ( $.isFunction( script ) ) {
                                                script( $ );
                                                markModuleReady();
@@ -736,12 +796,12 @@ var mw = ( function ( $, undefined ) {
                                        // and not in debug mode, such as when a symbol that should be global isn't exported
                                        if ( window.console && typeof window.console.log === 'function' ) {
                                                console.log( 'mw.loader::execute> Exception thrown by ' + module + ': ' + e.message );
+                                               console.log( e );
                                        }
                                        registry[module].state = 'error';
-                                       throw e;
                                }
                        }
-       
+
                        /**
                         * Adds a dependencies to the queue with optional callbacks to be run
                         * when the dependencies are ready or fail
@@ -749,10 +809,12 @@ var mw = ( function ( $, undefined ) {
                         * @param dependencies string module name or array of string module names
                         * @param ready function callback to execute when all dependencies are ready
                         * @param error function callback to execute when any dependency fails
+                        * @param async (optional) If true, load modules asynchronously even if
+                        *  document ready has not yet occurred
                         */
-                       function request( dependencies, ready, error ) {
+                       function request( dependencies, ready, error, async ) {
                                var regItemDeps, regItemDepLen, n;
-       
+
                                // Allow calling by single module name
                                if ( typeof dependencies === 'string' ) {
                                        dependencies = [dependencies];
@@ -766,8 +828,9 @@ var mw = ( function ( $, undefined ) {
                                                }
                                        }
                                }
+
                                // Add ready and error callbacks if they were given
-                               if ( arguments.length > 1 ) {
+                               if ( ready !== undefined || error !== undefined ) {
                                        jobs[jobs.length] = {
                                                'dependencies': filter(
                                                        ['registered', 'loading', 'loaded'],
@@ -777,17 +840,23 @@ var mw = ( function ( $, undefined ) {
                                                'error': error
                                        };
                                }
+
                                // Queue up any dependencies that are registered
                                dependencies = filter( ['registered'], dependencies );
                                for ( n = 0; n < dependencies.length; n += 1 ) {
                                        if ( $.inArray( dependencies[n], queue ) === -1 ) {
                                                queue[queue.length] = dependencies[n];
+                                               if ( async ) {
+                                                       // Mark this module as async in the registry
+                                                       registry[dependencies[n]].async = true;
+                                               }
                                        }
                                }
+
                                // Work the queue
                                mw.loader.work();
                        }
-       
+
                        function sortQuery(o) {
                                var sorted = {}, key, a = [];
                                for ( key in o ) {
@@ -801,7 +870,7 @@ var mw = ( function ( $, undefined ) {
                                }
                                return sorted;
                        }
-       
+
                        /**
                         * Converts a module map of the form { foo: [ 'bar', 'baz' ], bar: [ 'baz, 'quux' ] }
                         * to a query string of the form foo.bar,baz|bar.baz,quux
@@ -814,15 +883,16 @@ var mw = ( function ( $, undefined ) {
                                }
                                return arr.join( '|' );
                        }
-       
+
                        /**
                         * Asynchronously append a script tag to the end of the body
                         * that invokes load.php
                         * @param moduleMap {Object}: Module map, see buildModulesString()
                         * @param currReqBase {Object}: Object with other parameters (other than 'modules') to use in the request
                         * @param sourceLoadScript {String}: URL of load.php
+                        * @param async {Boolean}: If true, use an asynchrounous request even if document ready has not yet occurred
                         */
-                       function doRequest( moduleMap, currReqBase, sourceLoadScript ) {
+                       function doRequest( moduleMap, currReqBase, sourceLoadScript, async ) {
                                var request = $.extend(
                                        { 'modules': buildModulesString( moduleMap ) },
                                        currReqBase
@@ -830,11 +900,13 @@ var mw = ( function ( $, undefined ) {
                                request = sortQuery( request );
                                // Asynchronously append a script tag to the end of the body
                                // Append &* to avoid triggering the IE6 extension check
-                               addScript( sourceLoadScript + '?' + $.param( request ) + '&*' );
+                               addScript( sourceLoadScript + '?' + $.param( request ) + '&*', null, async );
                        }
-       
+
                        /* Public Methods */
                        return {
+                               addStyleTag: addStyleTag,
+
                                /**
                                 * Requests dependencies from server, loading and executing when things when ready.
                                 */
@@ -842,8 +914,8 @@ var mw = ( function ( $, undefined ) {
                                        var     reqBase, splits, maxQueryLength, q, b, bSource, bGroup, bSourceGroup,
                                                source, group, g, i, modules, maxVersion, sourceLoadScript,
                                                currReqBase, currReqBaseLength, moduleMap, l,
-                                               lastDotIndex, prefix, suffix, bytesAdded;
-               
+                                               lastDotIndex, prefix, suffix, bytesAdded, async;
+
                                        // Build a list of request parameters common to all requests.
                                        reqBase = {
                                                skin: mw.config.get( 'skin' ),
@@ -853,7 +925,7 @@ var mw = ( function ( $, undefined ) {
                                        // Split module batch by source and by group.
                                        splits = {};
                                        maxQueryLength = mw.config.get( 'wgResourceLoaderMaxQueryLength', -1 );
-               
+
                                        // Appends a list of modules from the queue to the batch
                                        for ( q = 0; q < queue.length; q += 1 ) {
                                                // Only request modules which are registered
@@ -870,14 +942,14 @@ var mw = ( function ( $, undefined ) {
                                        if ( !batch.length ) {
                                                return;
                                        }
-               
+
                                        // The queue has been processed into the batch, clear up the queue.
                                        queue = [];
-               
+
                                        // Always order modules alphabetically to help reduce cache
                                        // misses for otherwise identical content.
                                        batch.sort();
-               
+
                                        // Split batch by source and by group.
                                        for ( b = 0; b < batch.length; b += 1 ) {
                                                bSource = registry[batch[b]].source;
@@ -891,24 +963,24 @@ var mw = ( function ( $, undefined ) {
                                                bSourceGroup = splits[bSource][bGroup];
                                                bSourceGroup[bSourceGroup.length] = batch[b];
                                        }
-               
+
                                        // Clear the batch - this MUST happen before we append any
                                        // script elements to the body or it's possible that a script
                                        // will be locally cached, instantly load, and work the batch
                                        // again, all before we've cleared it causing each request to
                                        // include modules which are already loaded.
                                        batch = [];
-               
+
                                        for ( source in splits ) {
-               
+
                                                sourceLoadScript = sources[source].loadScript;
-               
+
                                                for ( group in splits[source] ) {
-               
+
                                                        // Cache access to currently selected list of
                                                        // modules for this group from this source.
                                                        modules = splits[source][group];
-               
+
                                                        // Calculate the highest timestamp
                                                        maxVersion = 0;
                                                        for ( g = 0; g < modules.length; g += 1 ) {
@@ -916,16 +988,16 @@ var mw = ( function ( $, undefined ) {
                                                                        maxVersion = registry[modules[g]].version;
                                                                }
                                                        }
-               
+
                                                        currReqBase = $.extend( { 'version': formatVersionNumber( maxVersion ) }, reqBase );
                                                        currReqBaseLength = $.param( currReqBase ).length;
-                                                       moduleMap = {};
+                                                       async = true;
                                                        // We may need to split up the request to honor the query string length limit,
                                                        // so build it piece by piece.
                                                        l = currReqBaseLength + 9; // '&modules='.length == 9
-               
+
                                                        moduleMap = {}; // { prefix: [ suffixes ] }
-               
+
                                                        for ( i = 0; i < modules.length; i += 1 ) {
                                                                // Determine how many bytes this module would add to the query string
                                                                lastDotIndex = modules[i].lastIndexOf( '.' );
@@ -935,30 +1007,37 @@ var mw = ( function ( $, undefined ) {
                                                                bytesAdded = moduleMap[prefix] !== undefined
                                                                        ? suffix.length + 3 // '%2C'.length == 3
                                                                        : modules[i].length + 3; // '%7C'.length == 3
-               
+
                                                                // If the request would become too long, create a new one,
                                                                // but don't create empty requests
                                                                if ( maxQueryLength > 0 && !$.isEmptyObject( moduleMap ) && l + bytesAdded > maxQueryLength ) {
                                                                        // This request would become too long, create a new one
                                                                        // and fire off the old one
-                                                                       doRequest( moduleMap, currReqBase, sourceLoadScript );
+                                                                       doRequest( moduleMap, currReqBase, sourceLoadScript, async );
                                                                        moduleMap = {};
+                                                                       async = true;
                                                                        l = currReqBaseLength + 9;
                                                                }
                                                                if ( moduleMap[prefix] === undefined ) {
                                                                        moduleMap[prefix] = [];
                                                                }
                                                                moduleMap[prefix].push( suffix );
+                                                               if ( !registry[modules[i]].async ) {
+                                                                       // If this module is blocking, make the entire request blocking
+                                                                       // This is slightly suboptimal, but in practice mixing of blocking
+                                                                       // and async modules will only occur in debug mode.
+                                                                       async = false;
+                                                               }
                                                                l += bytesAdded;
                                                        }
                                                        // If there's anything left in moduleMap, request that too
                                                        if ( !$.isEmptyObject( moduleMap ) ) {
-                                                               doRequest( moduleMap, currReqBase, sourceLoadScript );
+                                                               doRequest( moduleMap, currReqBase, sourceLoadScript, async );
                                                        }
                                                }
                                        }
                                },
-               
+
                                /**
                                 * Register a source.
                                 *
@@ -976,16 +1055,16 @@ var mw = ( function ( $, undefined ) {
                                                }
                                                return true;
                                        }
-               
+
                                        if ( sources[id] !== undefined ) {
                                                throw new Error( 'source already registered: ' + id );
                                        }
-               
+
                                        sources[id] = props;
-               
+
                                        return true;
                                },
-               
+
                                /**
                                 * Registers a module, letting the system know about it and its
                                 * properties. Startup modules contain calls to this function.
@@ -1036,7 +1115,7 @@ var mw = ( function ( $, undefined ) {
                                                registry[module].dependencies = dependencies;
                                        }
                                },
-               
+
                                /**
                                 * Implements a module, giving the system a course of action to take
                                 * upon loading. Results of a request for one or more modules contain
@@ -1048,7 +1127,12 @@ var mw = ( function ( $, undefined ) {
                                 * @param script Mixed: Function of module code or String of URL to be used as the src
                                 *  attribute when adding a script element to the body
                                 * @param style Object: Object of CSS strings keyed by media-type or Object of lists of URLs
-                                *  keyed by media-type
+                                *  keyed by media-type. Media-type should be "all" or "", actual types are not supported
+                                *  right now due to the way execute() processes the stylesheets (they are concatenated
+                                *  into a single <style> tag). In the past these weren't concatenated together (which is
+                                *  these are keyed by media-type),  but bug 31676 forces us to. In practice this is not a
+                                *  problem because ResourceLoader only generates stylesheets for media-type all (e.g. print
+                                *  stylesheets are wrapped in @media print {} and concatenated with the others).
                                 * @param msgs Object: List of key/value pairs to be passed through mw.messages.set
                                 */
                                implement: function ( module, script, style, msgs ) {
@@ -1087,7 +1171,7 @@ var mw = ( function ( $, undefined ) {
                                                execute( module );
                                        }
                                },
-               
+
                                /**
                                 * Executes a function as soon as one or more required modules are ready
                                 *
@@ -1126,7 +1210,7 @@ var mw = ( function ( $, undefined ) {
                                                request( dependencies, ready, error );
                                        }
                                },
-               
+
                                /**
                                 * Loads an external script or one or more modules for future use
                                 *
@@ -1135,8 +1219,12 @@ var mw = ( function ( $, undefined ) {
                                 * @param type {String} mime-type to use if calling with a URL of an
                                 *  external script or style; acceptable values are "text/css" and
                                 *  "text/javascript"; if no type is provided, text/javascript is assumed.
+                                * @param async {Boolean} (optional) If true, load modules asynchronously
+                                *  even if document ready has not yet occurred. If false (default),
+                                *  block before document ready and load async after. If not set, true will
+                                *  be assumed if loading a URL, and false will be assumed otherwise.
                                 */
-                               load: function ( modules, type ) {
+                               load: function ( modules, type, async ) {
                                        var filtered, m;
 
                                        // Validate input
@@ -1147,6 +1235,10 @@ var mw = ( function ( $, undefined ) {
                                        if ( typeof modules === 'string' ) {
                                                // Support adding arbitrary external scripts
                                                if ( /^(https?:)?\/\//.test( modules ) ) {
+                                                       if ( async === undefined ) {
+                                                               // Assume async for bug 34542
+                                                               async = true;
+                                                       }
                                                        if ( type === 'text/css' ) {
                                                                $( 'head' ).append( $( '<link>', {
                                                                        rel: 'stylesheet',
@@ -1154,8 +1246,9 @@ var mw = ( function ( $, undefined ) {
                                                                        href: modules
                                                                } ) );
                                                                return;
-                                                       } else if ( type === 'text/javascript' || type === undefined ) {
-                                                               addScript( modules );
+                                                       }
+                                                       if ( type === 'text/javascript' || type === undefined ) {
+                                                               addScript( modules, null, async );
                                                                return;
                                                        }
                                                        // Unknown type
@@ -1183,16 +1276,14 @@ var mw = ( function ( $, undefined ) {
                                                return;
                                        }
                                        // If any modules have errors
-                                       else if ( filter( ['error'], filtered ).length ) {
+                                       if ( filter( ['error'], filtered ).length ) {
                                                return;
                                        }
                                        // Since some modules are not yet ready, queue up a request
-                                       else {
-                                               request( filtered );
-                                               return;
-                                       }
+                                       request( filtered, null, null, async );
+                                       return;
                                },
-               
+
                                /**
                                 * Changes the state of a module
                                 *
@@ -1201,6 +1292,7 @@ var mw = ( function ( $, undefined ) {
                                 */
                                state: function ( module, state ) {
                                        var m;
+
                                        if ( typeof module === 'object' ) {
                                                for ( m in module ) {
                                                        mw.loader.state( m, module[m] );
@@ -1210,9 +1302,16 @@ var mw = ( function ( $, undefined ) {
                                        if ( registry[module] === undefined ) {
                                                mw.loader.register( module );
                                        }
-                                       registry[module].state = state;
+                                       if ( state === 'ready' && registry[module].state !== state) {
+                                               // Make sure pending modules depending on this one get executed if their
+                                               // dependencies are now fulfilled!
+                                               registry[module].state = state;
+                                               handlePending( module );
+                                       } else {
+                                               registry[module].state = state;
+                                       }
                                },
-               
+
                                /**
                                 * Gets the version of a module
                                 *
@@ -1224,14 +1323,14 @@ var mw = ( function ( $, undefined ) {
                                        }
                                        return null;
                                },
-               
+
                                /**
                                 * @deprecated since 1.18 use mw.loader.getVersion() instead
                                 */
                                version: function () {
                                        return mw.loader.getVersion.apply( mw.loader, arguments );
                                },
-               
+
                                /**
                                 * Gets the state of a module
                                 *
@@ -1243,7 +1342,7 @@ var mw = ( function ( $, undefined ) {
                                        }
                                        return null;
                                },
-               
+
                                /**
                                 * Get names of all registered modules.
                                 *
@@ -1255,18 +1354,6 @@ var mw = ( function ( $, undefined ) {
                                        } );
                                },
 
-                               /**
-                                * Enable or disable blocking. If blocking is enabled and
-                                * document ready has not yet occurred, scripts will be loaded
-                                * in a blocking way (using document.write) rather than
-                                * asynchronously using DOM manipulation
-                                * 
-                                * @param b {Boolean} True to enable blocking, false to disable it
-                                */
-                               setBlocking: function( b ) {
-                                       blocking = b;
-                               },
-               
                                /**
                                 * For backwards-compatibility with Squid-cached pages. Loads mw.user
                                 */
@@ -1275,7 +1362,7 @@ var mw = ( function ( $, undefined ) {
                                }
                        };
                }() ),
-       
+
                /** HTML construction helper functions */
                html: ( function () {
                        function escapeCallback( s ) {
@@ -1301,7 +1388,7 @@ var mw = ( function ( $, undefined ) {
                                escape: function ( s ) {
                                        return s.replace( /['"<>&]/g, escapeCallback );
                                },
-               
+
                                /**
                                 * Wrapper object for raw HTML passed to mw.html.element().
                                 * @constructor
@@ -1309,7 +1396,7 @@ var mw = ( function ( $, undefined ) {
                                Raw: function ( value ) {
                                        this.value = value;
                                },
-               
+
                                /**
                                 * Wrapper object for CDATA element contents passed to mw.html.element()
                                 * @constructor
@@ -1317,7 +1404,7 @@ var mw = ( function ( $, undefined ) {
                                Cdata: function ( value ) {
                                        this.value = value;
                                },
-               
+
                                /**
                                 * Create an HTML element string, with safe escaping.
                                 *
@@ -1339,7 +1426,7 @@ var mw = ( function ( $, undefined ) {
                                 */
                                element: function ( name, attrs, contents ) {
                                        var v, attrName, s = '<' + name;
-               
+
                                        for ( attrName in attrs ) {
                                                v = attrs[attrName];
                                                // Convert name=true, to name=name
@@ -1386,10 +1473,16 @@ var mw = ( function ( $, undefined ) {
                                        return s;
                                }
                        };
-               })()
+               }() ),
+
+               // Skeleton user object. mediawiki.user.js extends this
+               user: {
+                       options: new Map(),
+                       tokens: new Map()
+               }
        };
-       
-})( jQuery );
+
+}( jQuery ) );
 
 // Alias $j to jQuery for backwards compatibility
 window.$j = jQuery;
@@ -1398,7 +1491,7 @@ window.$j = jQuery;
 window.mw = window.mediaWiki = mw;
 
 // Auto-register from pre-loaded startup scripts
-if ( typeof startUp !== 'undefined' && jQuery.isFunction( startUp ) ) {
-       startUp();
-       startUp = undefined;
+if ( jQuery.isFunction( window.startUp ) ) {
+       window.startUp();
+       window.startUp = undefined;
 }