2 * This file is currently loaded as part of the 'mediawiki' module and therefore
3 * concatenated to mediawiki.js and executed at the same time. This file exists
4 * to help prepare for splitting up the 'mediawiki' module.
5 * This effort is tracked at https://phabricator.wikimedia.org/T192623
9 * - mediawiki.js will be reduced to the minimum needed to define mw.loader and
10 * mw.config, and then moved to its own private "mediawiki.loader" module that
11 * can be embedded within the StartupModule response.
13 * - mediawiki.base.js and other files in this directory will remain part of the
14 * "mediawiki" module, and will remain a default/implicit dependency for all
15 * regular modules, just like jquery and wikibits already are.
21 var slice
= Array
.prototype.slice
,
22 mwLoaderTrack
= mw
.track
,
23 trackCallbacks
= $.Callbacks( 'memory' ),
25 hasOwn
= Object
.prototype.hasOwnProperty
,
29 * Object constructor for messages.
31 * Similar to the Message class in MediaWiki PHP.
33 * Format defaults to 'text'.
39 * 'hello': 'Hello world',
40 * 'hello-user': 'Hello, $1!',
41 * 'welcome-user': 'Welcome back to $2, $1! Last visit by $1: $3'
44 * obj = new mw.Message( mw.messages, 'hello' );
45 * mw.log( obj.text() );
48 * obj = new mw.Message( mw.messages, 'hello-user', [ 'John Doe' ] );
49 * mw.log( obj.text() );
52 * obj = new mw.Message( mw.messages, 'welcome-user', [ 'John Doe', 'Wikipedia', '2 hours ago' ] );
53 * mw.log( obj.text() );
54 * // Welcome back to Wikipedia, John Doe! Last visit by John Doe: 2 hours ago
56 * // Using mw.message shortcut
57 * obj = mw.message( 'hello-user', 'John Doe' );
58 * mw.log( obj.text() );
61 * // Using mw.msg shortcut
62 * str = mw.msg( 'hello-user', 'John Doe' );
66 * // Different formats
67 * obj = new mw.Message( mw.messages, 'hello-user', [ 'John "Wiki" <3 Doe' ] );
69 * obj.format = 'text';
70 * str = obj.toString();
75 * // Hello, John "Wiki" <3 Doe!
77 * mw.log( obj.escaped() );
78 * // Hello, John "Wiki" <3 Doe!
83 * @param {mw.Map} map Message store
85 * @param {Array} [parameters]
87 function Message( map
, key
, parameters
) {
91 this.parameters
= parameters
=== undefined ? [] : slice
.call( parameters
);
97 * Get parsed contents of the message.
99 * The default parser does simple $N replacements and nothing else.
100 * This may be overridden to provide a more complex message parser.
101 * The primary override is in the mediawiki.jqueryMsg module.
103 * This function will not be called for nonexistent messages.
105 * @return {string} Parsed message
107 parser: function () {
108 return mw
.format
.apply( null, [ this.map
.get( this.key
) ].concat( this.parameters
) );
112 * Add (does not replace) parameters for `$N` placeholder values.
114 * @param {Array} parameters
115 * @return {mw.Message}
118 params: function ( parameters
) {
120 for ( i
= 0; i
< parameters
.length
; i
++ ) {
121 this.parameters
.push( parameters
[ i
] );
127 * Convert message object to its string form based on current format.
129 * @return {string} Message as a string in the current form, or `<key>` if key
132 toString: function () {
135 if ( !this.exists() ) {
136 // Use ⧼key⧽ as text if key does not exist
137 // Err on the side of safety, ensure that the output
138 // is always html safe in the event the message key is
139 // missing, since in that case its highly likely the
140 // message key is user-controlled.
141 // '⧼' is used instead of '<' to side-step any
142 // double-escaping issues.
143 // (Keep synchronised with Message::toString() in PHP.)
144 return '⧼' + mw
.html
.escape( this.key
) + '⧽';
147 if ( this.format
=== 'plain' || this.format
=== 'text' || this.format
=== 'parse' ) {
148 text
= this.parser();
151 if ( this.format
=== 'escaped' ) {
152 text
= this.parser();
153 text
= mw
.html
.escape( text
);
160 * Change format to 'parse' and convert message to string
162 * If jqueryMsg is loaded, this parses the message text from wikitext
163 * (where supported) to HTML
165 * Otherwise, it is equivalent to plain.
167 * @return {string} String form of parsed message
170 this.format
= 'parse';
171 return this.toString();
175 * Change format to 'plain' and convert message to string
177 * This substitutes parameters, but otherwise does not change the
180 * @return {string} String form of plain message
183 this.format
= 'plain';
184 return this.toString();
188 * Change format to 'text' and convert message to string
190 * If jqueryMsg is loaded, {{-transformation is done where supported
191 * (such as {{plural:}}, {{gender:}}, {{int:}}).
193 * Otherwise, it is equivalent to plain
195 * @return {string} String form of text message
198 this.format
= 'text';
199 return this.toString();
203 * Change the format to 'escaped' and convert message to string
205 * This is equivalent to using the 'text' format (see #text), then
206 * HTML-escaping the output.
208 * @return {string} String form of html escaped message
210 escaped: function () {
211 this.format
= 'escaped';
212 return this.toString();
216 * Check if a message exists
221 exists: function () {
222 return this.map
.exists( this.key
);
232 * @inheritdoc mw.inspect#runReports
235 mw
.inspect = function () {
236 var args
= arguments
;
238 mw
.loader
.using( 'mediawiki.inspect', function () {
239 mw
.inspect
.runReports
.apply( mw
.inspect
, args
);
244 * Format a string. Replace $1, $2 ... $N with positional arguments.
246 * Used by Message#parser().
249 * @param {string} formatString Format string
250 * @param {...Mixed} parameters Values for $N replacements
251 * @return {string} Formatted string
253 mw
.format = function ( formatString
) {
254 var parameters
= slice
.call( arguments
, 1 );
255 return formatString
.replace( /\$(\d+)/g, function ( str
, match
) {
256 var index
= parseInt( match
, 10 ) - 1;
257 return parameters
[ index
] !== undefined ? parameters
[ index
] : '$' + match
;
261 // Expose Message constructor
262 mw
.Message
= Message
;
265 * Get a message object.
267 * Shortcut for `new mw.Message( mw.messages, key, parameters )`.
270 * @param {string} key Key of message to get
271 * @param {...Mixed} parameters Values for $N replacements
272 * @return {mw.Message}
274 mw
.message = function ( key
) {
275 var parameters
= slice
.call( arguments
, 1 );
276 return new Message( mw
.messages
, key
, parameters
);
280 * Get a message string using the (default) 'text' format.
282 * Shortcut for `mw.message( key, parameters... ).text()`.
285 * @param {string} key Key of message to get
286 * @param {...Mixed} parameters Values for $N replacements
289 mw
.msg = function () {
290 return mw
.message
.apply( mw
.message
, arguments
).toString();
294 * Track an analytic event.
296 * This method provides a generic means for MediaWiki JavaScript code to capture state
297 * information for analysis. Each logged event specifies a string topic name that describes
298 * the kind of event that it is. Topic names consist of dot-separated path components,
299 * arranged from most general to most specific. Each path component should have a clear and
300 * well-defined purpose.
302 * Data handlers are registered via `mw.trackSubscribe`, and receive the full set of
303 * events that match their subcription, including those that fired before the handler was
306 * @param {string} topic Topic name
307 * @param {Object} [data] Data describing the event, encoded as an object
309 mw
.track = function ( topic
, data
) {
310 mwLoaderTrack( topic
, data
);
311 trackCallbacks
.fire( mw
.trackQueue
);
315 * Register a handler for subset of analytic events, specified by topic.
317 * Handlers will be called once for each tracked event, including any events that fired before the
318 * handler was registered; 'this' is set to a plain object with a 'timeStamp' property indicating
319 * the exact time at which the event fired, a string 'topic' property naming the event, and a
320 * 'data' property which is an object of event-specific data. The event topic and event data are
321 * also passed to the callback as the first and second arguments, respectively.
323 * @param {string} topic Handle events whose name starts with this string prefix
324 * @param {Function} callback Handler to call for each matching tracked event
325 * @param {string} callback.topic
326 * @param {Object} [callback.data]
328 mw
.trackSubscribe = function ( topic
, callback
) {
330 function handler( trackQueue
) {
332 for ( ; seen
< trackQueue
.length
; seen
++ ) {
333 event
= trackQueue
[ seen
];
334 if ( event
.topic
.indexOf( topic
) === 0 ) {
335 callback
.call( event
, event
.topic
, event
.data
);
340 trackHandlers
.push( [ handler
, callback
] );
342 trackCallbacks
.add( handler
);
346 * Stop handling events for a particular handler
348 * @param {Function} callback
350 mw
.trackUnsubscribe = function ( callback
) {
351 trackHandlers
= trackHandlers
.filter( function ( fns
) {
352 if ( fns
[ 1 ] === callback
) {
353 trackCallbacks
.remove( fns
[ 0 ] );
354 // Ensure the tuple is removed to avoid holding on to closures
361 // Fire events from before track() triggred fire()
362 trackCallbacks
.fire( mw
.trackQueue
);
365 * Registry and firing of events.
367 * MediaWiki has various interface components that are extended, enhanced
368 * or manipulated in some other way by extensions, gadgets and even
371 * This framework helps streamlining the timing of when these other
372 * code paths fire their plugins (instead of using document-ready,
373 * which can and should be limited to firing only once).
375 * Features like navigating to other wiki pages, previewing an edit
376 * and editing itself – without a refresh – can then retrigger these
377 * hooks accordingly to ensure everything still works as expected.
381 * mw.hook( 'wikipage.content' ).add( fn ).remove( fn );
382 * mw.hook( 'wikipage.content' ).fire( $content );
384 * Handlers can be added and fired for arbitrary event names at any time. The same
385 * event can be fired multiple times. The last run of an event is memorized
386 * (similar to `$(document).ready` and `$.Deferred().done`).
387 * This means if an event is fired, and a handler added afterwards, the added
388 * function will be fired right away with the last given event data.
390 * Like Deferreds and Promises, the mw.hook object is both detachable and chainable.
391 * Thus allowing flexible use and optimal maintainability and authority control.
392 * You can pass around the `add` and/or `fire` method to another piece of code
393 * without it having to know the event name (or `mw.hook` for that matter).
395 * var h = mw.hook( 'bar.ready' );
396 * new mw.Foo( .. ).fetch( { callback: h.fire } );
398 * Note: Events are documented with an underscore instead of a dot in the event
399 * name due to jsduck not supporting dots in that position.
403 mw
.hook
= ( function () {
407 * Create an instance of mw.hook.
411 * @param {string} name Name of hook.
414 return function ( name
) {
415 var list
= hasOwn
.call( lists
, name
) ?
417 lists
[ name
] = $.Callbacks( 'memory' );
421 * Register a hook handler
423 * @param {...Function} handler Function to bind.
429 * Unregister a hook handler
431 * @param {...Function} handler Function to unbind.
439 * @param {...Mixed} data
444 return list
.fireWith
.call( this, null, slice
.call( arguments
) );
451 * HTML construction helper functions
458 * output = Html.element( 'div', {}, new Html.Raw(
459 * Html.element( 'img', { src: '<' } )
461 * mw.log( output ); // <div><img src="<"/></div>
466 mw
.html
= ( function () {
467 function escapeCallback( s
) {
484 * Escape a string for HTML.
486 * Converts special characters to HTML entities.
488 * mw.html.escape( '< > \' & "' );
489 * // Returns < > ' & "
491 * @param {string} s The string to escape
492 * @return {string} HTML
494 escape: function ( s
) {
495 return s
.replace( /['"<>&]/g, escapeCallback
);
499 * Create an HTML element string, with safe escaping.
501 * @param {string} name The tag name.
502 * @param {Object} [attrs] An object with members mapping element names to values
503 * @param {string|mw.html.Raw|mw.html.Cdata|null} [contents=null] The contents of the element.
505 * - string: Text to be escaped.
506 * - null: The element is treated as void with short closing form, e.g. `<br/>`.
507 * - this.Raw: The raw value is directly included.
508 * - this.Cdata: The raw value is directly included. An exception is
509 * thrown if it contains any illegal ETAGO delimiter.
510 * See <https://www.w3.org/TR/html401/appendix/notes.html#h-B.3.2>.
511 * @return {string} HTML
513 element: function ( name
, attrs
, contents
) {
514 var v
, attrName
, s
= '<' + name
;
517 for ( attrName
in attrs
) {
518 v
= attrs
[ attrName
];
519 // Convert name=true, to name=name
523 } else if ( v
=== false ) {
526 s
+= ' ' + attrName
+ '="' + this.escape( String( v
) ) + '"';
529 if ( contents
=== undefined || contents
=== null ) {
536 switch ( typeof contents
) {
539 s
+= this.escape( contents
);
544 s
+= String( contents
);
547 if ( contents
instanceof this.Raw
) {
548 // Raw HTML inclusion
550 } else if ( contents
instanceof this.Cdata
) {
552 if ( /<\/[a-zA-z]/.test( contents
.value
) ) {
553 throw new Error( 'mw.html.element: Illegal end tag found in CDATA' );
557 throw new Error( 'mw.html.element: Invalid type of contents' );
560 s
+= '</' + name
+ '>';
565 * Wrapper object for raw HTML passed to mw.html.element().
569 * @param {string} value
571 Raw: function ( value
) {
576 * Wrapper object for CDATA element contents passed to mw.html.element()
578 * @class mw.html.Cdata
580 * @param {string} value
582 Cdata: function ( value
) {
589 * Execute a function as soon as one or more required modules are ready.
591 * Example of inline dependency on OOjs:
593 * mw.loader.using( 'oojs', function () {
594 * OO.compare( [ 1 ], [ 1 ] );
597 * Example of inline dependency obtained via `require()`:
599 * mw.loader.using( [ 'mediawiki.util' ], function ( require ) {
600 * var util = require( 'mediawiki.util' );
603 * Since MediaWiki 1.23 this also returns a promise.
605 * Since MediaWiki 1.28 the promise is resolved with a `require` function.
608 * @param {string|Array} dependencies Module name or array of modules names the
609 * callback depends on to be ready before executing
610 * @param {Function} [ready] Callback to execute when all dependencies are ready
611 * @param {Function} [error] Callback to execute if one or more dependencies failed
612 * @return {jQuery.Promise} With a `require` function
614 mw
.loader
.using = function ( dependencies
, ready
, error
) {
615 var deferred
= $.Deferred();
617 // Allow calling with a single dependency as a string
618 if ( typeof dependencies
=== 'string' ) {
619 dependencies
= [ dependencies
];
623 deferred
.done( ready
);
626 deferred
.fail( error
);
630 // Resolve entire dependency map
631 dependencies
= mw
.loader
.resolve( dependencies
);
633 return deferred
.reject( e
).promise();
636 mw
.loader
.enqueue( dependencies
, function () {
637 deferred
.resolve( mw
.loader
.require
);
638 }, deferred
.reject
);
640 return deferred
.promise();
643 // Alias $j to jQuery for backwards compatibility
644 // @deprecated since 1.23 Use $ or jQuery instead
645 mw
.log
.deprecate( window
, '$j', $, 'Use $ or jQuery instead.' );
647 // Process callbacks for Grade A that require modules.
648 // Plain ones were already processed by startup.js.
650 // Redefine publicly to capture any late arrivals
652 push: function ( entry
) {
653 mw
.loader
.using( entry
[ 0 ], entry
[ 1 ] );
656 while ( queue
[ 0 ] ) {
657 window
.RLQ
.push( queue
.shift() );