'use strict';
var mw, StringSet, log,
- hasOwn = Object.prototype.hasOwnProperty,
- 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 );
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
* @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 );
}
} );
} else {
mainScript = script.files[ script.main ];
if ( typeof mainScript !== 'function' ) {
- throw new Error( 'Main script file ' + script.main + ' in module ' + module +
- 'must be of type function, is of type ' + typeof mainScript );
+ 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(
* @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 ] = [];
*/
moduleRegistry: registry,
+ /**
+ * Exposed for testing and debugging only.
+ *
+ * @see #batchRequest
+ * @property
+ * @private
+ */
+ maxQueryLength: $VARS.maxQueryLength,
+
/**
* @inheritdoc #newStyleTag
* @method
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;
},
try {
if ( typeof descriptor.script === 'function' ) {
+ // Function literal: cast to string
encodedScript = String( descriptor.script );
} else if (
- // Plain object: an object that is not null and is not an array
+ // 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 = '{' +
- Object.keys( descriptor.script ).map( function ( key ) {
- var value = descriptor.script[ key ];
+ '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 = [