Merge "ActiveUsersPager: Fix ordering and return 0-action users"
[lhc/web/wiklou.git] / resources / src / startup / mediawiki.js
index 03f02b4..28f57db 100644 (file)
@@ -13,7 +13,7 @@
        'use strict';
 
        var mw, StringSet, log,
-               trackQueue = [];
+               hasOwn = Object.prototype.hasOwnProperty;
 
        /**
         * FNV132 hash function
         * @return {string} hash as an seven-character base 36 string
         */
        function fnv132( str ) {
-               /* eslint-disable no-bitwise */
                var hash = 0x811C9DC5,
                        i;
 
+               /* eslint-disable no-bitwise */
                for ( i = 0; i < str.length; i++ ) {
                        hash += ( hash << 1 ) + ( hash << 4 ) + ( hash << 7 ) + ( hash << 8 ) + ( hash << 24 );
                        hash ^= str.charCodeAt( i );
@@ -41,9 +41,9 @@
                while ( hash.length < 7 ) {
                        hash = '0' + hash;
                }
+               /* eslint-enable no-bitwise */
 
                return hash;
-               /* eslint-enable no-bitwise */
        }
 
        function defineFallbacks() {
        function logError( topic, data ) {
                var msg,
                        e = data.exception,
-                       source = data.source,
-                       module = data.module,
                        console = window.console;
 
                if ( console && console.log ) {
-                       msg = ( e ? 'Exception' : 'Error' ) + ' in ' + source;
-                       if ( module ) {
-                               msg += ' in module ' + module;
-                       }
-                       msg += ( e ? ':' : '.' );
+                       msg = ( e ? 'Exception' : 'Error' ) +
+                               ' in ' + data.source +
+                               ( data.module ? ' in module ' + data.module : '' ) +
+                               ( e ? ':' : '.' );
+
                        console.log( msg );
 
                        // If we have an exception object, log it to the warning channel to trigger
                        this.set = function ( selection, value ) {
                                var s;
                                if ( arguments.length > 1 ) {
-                                       if ( typeof selection !== 'string' ) {
-                                               return false;
+                                       if ( typeof selection === 'string' ) {
+                                               setGlobalMapValue( this, selection, value );
+                                               return true;
                                        }
-                                       setGlobalMapValue( this, selection, value );
-                                       return true;
-                               }
-                               if ( typeof selection === 'object' ) {
+                               } else if ( typeof selection === 'object' ) {
                                        for ( s in selection ) {
                                                setGlobalMapValue( this, s, selection[ s ] );
                                        }
                        var s;
                        // Use `arguments.length` because `undefined` is also a valid value.
                        if ( arguments.length > 1 ) {
-                               if ( typeof selection !== 'string' ) {
-                                       return false;
+                               // Set one key
+                               if ( typeof selection === 'string' ) {
+                                       this.values[ selection ] = value;
+                                       return true;
                                }
-                               this.values[ selection ] = value;
-                               return true;
-                       }
-                       if ( typeof selection === 'object' ) {
+                       } else if ( typeof selection === 'object' ) {
+                               // Set multiple keys
                                for ( s in selection ) {
                                        this.values[ s ] = selection[ s ];
                                }
                 * @param {string} key Name of property to create in `obj`
                 * @param {Mixed} val The value this property should return when accessed
                 * @param {string} [msg] Optional text to include in the deprecation message
-                * @param {string} [logName=key] Optional custom name for the feature.
-                *  This is used instead of `key` in the message and `mw.deprecate` tracking.
+                * @param {string} [logName] Name for the feature for logging and tracking
+                *  purposes. Except for properties of the window object, tracking is only
+                *  enabled if logName is set.
                 */
                log.deprecate = function ( obj, key, val, msg, logName ) {
                        var stacks;
                        function maybeLog() {
-                               var name,
+                               var name = logName || key,
                                        trace = new Error().stack;
                                if ( !stacks ) {
                                        stacks = new StringSet();
                                }
                                if ( !stacks.has( trace ) ) {
                                        stacks.add( trace );
-                                       name = logName || key;
-                                       mw.track( 'mw.deprecate', name );
+                                       if ( logName || obj === window ) {
+                                               mw.track( 'mw.deprecate', name );
+                                       }
                                        mw.log.warn(
-                                               'Use of "' + name + '" is deprecated.' + ( msg ? ( ' ' + msg ) : '' )
+                                               'Use of "' + name + '" is deprecated.' + ( msg ? ' ' + msg : '' )
                                        );
                                }
                        }
        mw = {
                redefineFallbacksForTest: function () {
                        if ( !window.QUnit ) {
-                               throw new Error( 'Reset not allowed outside unit tests' );
+                               throw new Error( 'Not allowed' );
                        }
                        defineFallbacks();
                },
                 * @return {number} Current time
                 */
                now: function () {
-                       // Optimisation: Define the shortcut on first call, not at module definition.
+                       // Optimisation: Make startup initialisation faster by defining the
+                       // shortcut on first call, not at module definition.
                        var perf = window.performance,
                                navStart = perf && perf.timing && perf.timing.navigationStart;
 
                        // Define the relevant shortcut
-                       mw.now = navStart && typeof perf.now === 'function' ?
+                       mw.now = navStart && perf.now ?
                                function () { return navStart + perf.now(); } :
                                Date.now;
 
                /**
                 * List of all analytic events emitted so far.
                 *
+                * Exposed only for use by mediawiki.base.
+                *
                 * @private
                 * @property {Array}
                 */
-               trackQueue: trackQueue,
+               trackQueue: [],
 
                track: function ( topic, data ) {
-                       trackQueue.push( { topic: topic, timeStamp: mw.now(), data: data } );
-                       // The base module extends this method to fire events here
+                       mw.trackQueue.push( { topic: topic, timeStamp: mw.now(), data: data } );
+                       // This method is extended by mediawiki.base to also fire events.
                },
 
                /**
                                                        i -= 1;
                                                        try {
                                                                if ( failed && job.error ) {
-                                                                       job.error( new Error( 'Module has failed dependencies' ), job.dependencies );
+                                                                       job.error( new Error( 'Failed dependencies' ), job.dependencies );
                                                                } else if ( !failed && job.ready ) {
                                                                        job.ready();
                                                                }
 
                                // Add base modules
                                if ( baseModules.indexOf( module ) === -1 ) {
-                                       baseModules.forEach( function ( baseModule ) {
-                                               if ( resolved.indexOf( baseModule ) === -1 ) {
-                                                       resolved.push( baseModule );
+                                       for ( i = 0; i < baseModules.length; i++ ) {
+                                               if ( resolved.indexOf( baseModules[ i ] ) === -1 ) {
+                                                       resolved.push( baseModules[ i ] );
                                                }
-                                       } );
+                                       }
                                }
 
                                // Tracks down dependencies
                                return resolved;
                        }
 
+                       /**
+                        * Resolve a relative file path.
+                        *
+                        * For example, resolveRelativePath( '../foo.js', 'resources/src/bar/bar.js' )
+                        * returns 'resources/src/foo.js'.
+                        *
+                        * @param {string} relativePath Relative file path, starting with ./ or ../
+                        * @param {string} basePath Path of the file (not directory) relativePath is relative to
+                        * @return {string|null} Resolved path, or null if relativePath does not start with ./ or ../
+                        */
+                       function resolveRelativePath( relativePath, basePath ) {
+                               var prefixes, prefix, baseDirParts,
+                                       relParts = relativePath.match( /^((?:\.\.?\/)+)(.*)$/ );
+
+                               if ( !relParts ) {
+                                       return null;
+                               }
+
+                               baseDirParts = basePath.split( '/' );
+                               // basePath looks like 'foo/bar/baz.js', so baseDirParts looks like [ 'foo', 'bar, 'baz.js' ]
+                               // Remove the file component at the end, so that we are left with only the directory path
+                               baseDirParts.pop();
+
+                               prefixes = relParts[ 1 ].split( '/' );
+                               // relParts[ 1 ] looks like '../../', so prefixes looks like [ '..', '..', '' ]
+                               // Remove the empty element at the end
+                               prefixes.pop();
+
+                               // For every ../ in the path prefix, remove one directory level from baseDirParts
+                               while ( ( prefix = prefixes.pop() ) !== undefined ) {
+                                       if ( prefix === '..' ) {
+                                               baseDirParts.pop();
+                                       }
+                               }
+
+                               // If there's anything left of the base path, prepend it to the file path
+                               return ( baseDirParts.length ? baseDirParts.join( '/' ) + '/' : '' ) + relParts[ 2 ];
+                       }
+
+                       /**
+                        * Make a require() function scoped to a package file
+                        * @private
+                        * @param {Object} moduleObj Module object from the registry
+                        * @param {string} basePath Path of the file this is scoped to. Used for relative paths.
+                        * @return {Function}
+                        */
+                       function makeRequireFunction( moduleObj, basePath ) {
+                               return function require( moduleName ) {
+                                       var fileName, fileContent, result, moduleParam,
+                                               scriptFiles = moduleObj.script.files;
+                                       fileName = resolveRelativePath( moduleName, basePath );
+                                       if ( fileName === null ) {
+                                               // Not a relative path, so it's a module name
+                                               return mw.loader.require( moduleName );
+                                       }
+
+                                       if ( !hasOwn.call( scriptFiles, fileName ) ) {
+                                               throw new Error( 'Cannot require() undefined file ' + fileName );
+                                       }
+                                       if ( hasOwn.call( moduleObj.packageExports, fileName ) ) {
+                                               // File has already been executed, return the cached result
+                                               return moduleObj.packageExports[ fileName ];
+                                       }
+
+                                       fileContent = scriptFiles[ fileName ];
+                                       if ( typeof fileContent === 'function' ) {
+                                               moduleParam = { exports: {} };
+                                               fileContent( makeRequireFunction( moduleObj, fileName ), moduleParam );
+                                               result = moduleParam.exports;
+                                       } else {
+                                               // fileContent is raw data, just pass it through
+                                               result = fileContent;
+                                       }
+                                       moduleObj.packageExports[ fileName ] = result;
+                                       return result;
+                               };
+                       }
+
                        /**
                         * Load and execute a script.
                         *
                         * @param {Function} [callback] Callback to run after request resolution
                         */
                        function addScript( src, callback ) {
+                               // Use a <script> element rather than XHR. Using XHR changes the request
+                               // headers (potentially missing a cache hit), and reduces caching in general
+                               // since browsers cache XHR much less (if at all). And XHR means we retrieve
+                               // text, so we'd need to eval, which then messes up line numbers.
+                               // The drawback is that <script> does not offer progress events, feedback is
+                               // only given after downloading, parsing, and execution have completed.
                                var script = document.createElement( 'script' );
                                script.src = src;
                                script.onload = script.onerror = function () {
                                                // these as the server will deny them anyway (T101806).
                                                if ( registry[ module ].group === 'private' ) {
                                                        setAndPropagate( module, 'error' );
-                                                       return;
+                                               } else {
+                                                       queue.push( module );
                                                }
-                                               queue.push( module );
                                        }
                                } );
 
                                $CODE.profileExecuteStart();
 
                                runScript = function () {
-                                       var script, markModuleReady, nestedAddScript;
+                                       var script, markModuleReady, nestedAddScript, mainScript;
 
                                        $CODE.profileScriptStart();
                                        script = registry[ module ].script;
                                        try {
                                                if ( Array.isArray( script ) ) {
                                                        nestedAddScript( script, markModuleReady, 0 );
-                                               } else if ( typeof script === 'function' ) {
-                                                       // Keep in sync with queueModuleScript() for debug mode
-                                                       if ( module === 'jquery' ) {
-                                                               // This is a special case for when 'jquery' itself is being loaded.
-                                                               // - The standard jquery.js distribution does not set `window.jQuery`
-                                                               //   in CommonJS-compatible environments (Node.js, AMD, RequireJS, etc.).
-                                                               // - MediaWiki's 'jquery' module also bundles jquery.migrate.js, which
-                                                               //   in a CommonJS-compatible environment, will use require('jquery'),
-                                                               //   but that can't work when we're still inside that module.
-                                                               script();
+                                               } else if (
+                                                       typeof script === 'function' || (
+                                                               typeof script === 'object' &&
+                                                               script !== null
+                                                       )
+                                               ) {
+                                                       if ( typeof script === 'function' ) {
+                                                               // Keep in sync with queueModuleScript() for debug mode
+                                                               if ( module === 'jquery' ) {
+                                                                       // This is a special case for when 'jquery' itself is being loaded.
+                                                                       // - The standard jquery.js distribution does not set `window.jQuery`
+                                                                       //   in CommonJS-compatible environments (Node.js, AMD, RequireJS, etc.).
+                                                                       // - MediaWiki's 'jquery' module also bundles jquery.migrate.js, which
+                                                                       //   in a CommonJS-compatible environment, will use require('jquery'),
+                                                                       //   but that can't work when we're still inside that module.
+                                                                       script();
+                                                               } else {
+                                                                       // Pass jQuery twice so that the signature of the closure which wraps
+                                                                       // the script can bind both '$' and 'jQuery'.
+                                                                       script( window.$, window.$, mw.loader.require, registry[ module ].module );
+                                                               }
                                                        } else {
-                                                               // Pass jQuery twice so that the signature of the closure which wraps
-                                                               // the script can bind both '$' and 'jQuery'.
-                                                               script( window.$, window.$, mw.loader.require, registry[ module ].module );
+                                                               mainScript = script.files[ script.main ];
+                                                               if ( typeof mainScript !== 'function' ) {
+                                                                       throw new Error( 'Main file ' + script.main + ' in module ' + module +
+                                                                               ' must be of type function, found ' + typeof mainScript );
+                                                               }
+                                                               // jQuery parameters are not passed for multi-file modules
+                                                               mainScript(
+                                                                       makeRequireFunction( registry[ module ], script.main ),
+                                                                       registry[ module ].module
+                                                               );
                                                        }
                                                        markModuleReady();
-
                                                } else if ( typeof script === 'string' ) {
                                                        // Site and user modules are legacy scripts that run in the global scope.
                                                        // This is transported as a string instead of a function to avoid needing
                         * @param {string[]} batch
                         */
                        function batchRequest( batch ) {
-                               var reqBase, splits, maxQueryLength, b, bSource, bGroup,
+                               var reqBase, splits, b, bSource, bGroup,
                                        source, group, i, modules, sourceLoadScript,
                                        currReqBase, currReqBaseLength, moduleMap, currReqModules, l,
                                        lastDotIndex, prefix, suffix, bytesAdded;
                                        lang: mw.config.get( 'wgUserLanguage' ),
                                        debug: mw.config.get( 'debug' )
                                };
-                               maxQueryLength = mw.config.get( 'wgResourceLoaderMaxQueryLength', 2000 );
 
                                // Split module list by source and by group.
                                splits = Object.create( null );
                                                                modules[ i ].length + 3; // '%7C'.length == 3
 
                                                        // If the url would become too long, create a new one, but don't create empty requests
-                                                       if ( maxQueryLength > 0 && currReqModules.length && l + bytesAdded > maxQueryLength ) {
+                                                       if ( currReqModules.length && l + bytesAdded > mw.loader.maxQueryLength ) {
                                                                // Dispatch what we've got...
                                                                doRequest();
                                                                // .. and start again.
                                                                moduleMap = Object.create( null );
                                                                currReqModules = [];
 
-                                                               mw.track( 'resourceloader.splitRequest', { maxQueryLength: maxQueryLength } );
+                                                               mw.track( 'resourceloader.splitRequest', { maxQueryLength: mw.loader.maxQueryLength } );
                                                        }
                                                        if ( !moduleMap[ prefix ] ) {
                                                                moduleMap[ prefix ] = [];
                                        module: {
                                                exports: {}
                                        },
+                                       // module.export objects for each package file inside this module
+                                       packageExports: {},
                                        version: String( version || '' ),
                                        dependencies: dependencies || [],
                                        group: typeof group === 'string' ? group : null,
                                 */
                                moduleRegistry: registry,
 
+                               /**
+                                * Exposed for testing and debugging only.
+                                *
+                                * @see #batchRequest
+                                * @property
+                                * @private
+                                */
+                               maxQueryLength: $VARS.maxQueryLength,
+
                                /**
                                 * @inheritdoc #newStyleTag
                                 * @method
                                 *  as '`[name]@[version]`". This version should match the requested version
                                 *  (from #batchRequest and #registry). This avoids race conditions (T117587).
                                 *  For back-compat with MediaWiki 1.27 and earlier, the version may be omitted.
-                                * @param {Function|Array|string} [script] Function with module code, list of URLs
-                                *  to load via `<script src>`, or string of module code for `$.globalEval()`.
+                                * @param {Function|Array|string|Object} [script] Module code. This can be a function,
+                                *  a list of URLs to load via `<script src>`, a string for `$.globalEval()`, or an
+                                *  object like {"files": {"foo.js":function, "bar.js": function, ...}, "main": "foo.js"}.
+                                *  If an object is provided, the main file will be executed immediately, and the other
+                                *  files will only be executed if loaded via require(). If a function or string is
+                                *  provided, it will be executed/evaluated immediately. If an array is provided, all
+                                *  URLs in the array will be loaded immediately, and executed as soon as they arrive.
                                 * @param {Object} [style] Should follow one of the following patterns:
                                 *
                                 *     { "css": [css, ..] }
                                                                return;
                                                        }
                                                        // Unknown type
-                                                       throw new Error( 'invalid type for external url, must be text/css or text/javascript. not ' + type );
+                                                       throw new Error( 'type must be text/css or text/javascript, found ' + type );
                                                }
                                                // Called with single module
                                                modules = [ modules ];
                                        // Only ready modules can be required
                                        if ( state !== 'ready' ) {
                                                // Module may've forgotten to declare a dependency
-                                               throw new Error( 'Module "' + moduleName + '" is not loaded.' );
+                                               throw new Error( 'Module "' + moduleName + '" is not loaded' );
                                        }
 
                                        return registry[ moduleName ].module.exports;
                                                                return;
                                                        }
                                                } catch ( e ) {
-                                                       mw.trackError( 'resourceloader.exception', {
-                                                               exception: e,
-                                                               source: 'store-localstorage-init'
-                                                       } );
+                                                       // Perhaps localStorage was disabled by the user, or got corrupted.
+                                                       // See point 3 and 4 below. (T195647)
                                                }
 
                                                // If we get here, one of four things happened:
                                                        this.stats.hits++;
                                                        return this.items[ key ];
                                                }
+
                                                this.stats.misses++;
                                                return false;
                                        },
                                         */
                                        set: function ( module ) {
                                                var key, args, src,
+                                                       encodedScript,
                                                        descriptor = mw.loader.moduleRegistry[ module ];
 
                                                key = getModuleKey( module );
                                                }
 
                                                try {
+                                                       if ( typeof descriptor.script === 'function' ) {
+                                                               // Function literal: cast to string
+                                                               encodedScript = String( descriptor.script );
+                                                       } else if (
+                                                               // Plain object: serialise as object literal (not JSON),
+                                                               // making sure to preserve the functions.
+                                                               typeof descriptor.script === 'object' &&
+                                                               descriptor.script &&
+                                                               !Array.isArray( descriptor.script )
+                                                       ) {
+                                                               encodedScript = '{' +
+                                                                       'main:' + JSON.stringify( descriptor.script.main ) + ',' +
+                                                                       'files:{' +
+                                                                       Object.keys( descriptor.script.files ).map( function ( key ) {
+                                                                               var value = descriptor.script.files[ key ];
+                                                                               return JSON.stringify( key ) + ':' +
+                                                                                       ( typeof value === 'function' ? value : JSON.stringify( value ) );
+                                                                       } ).join( ',' ) +
+                                                                       '}}';
+                                                       } else {
+                                                               // Array of urls, or null.
+                                                               encodedScript = JSON.stringify( descriptor.script );
+                                                       }
                                                        args = [
                                                                JSON.stringify( key ),
-                                                               typeof descriptor.script === 'function' ?
-                                                                       String( descriptor.script ) :
-                                                                       JSON.stringify( descriptor.script ),
+                                                               encodedScript,
                                                                JSON.stringify( descriptor.style ),
                                                                JSON.stringify( descriptor.messages ),
                                                                JSON.stringify( descriptor.templates )