43ee202f9322df0eef264a38bf19150f900a7ef7
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
;
24 * Object constructor for messages.
26 * Similar to the Message class in MediaWiki PHP.
28 * Format defaults to 'text'.
34 * 'hello': 'Hello world',
35 * 'hello-user': 'Hello, $1!',
36 * 'welcome-user': 'Welcome back to $2, $1! Last visit by $1: $3'
39 * obj = new mw.Message( mw.messages, 'hello' );
40 * mw.log( obj.text() );
43 * obj = new mw.Message( mw.messages, 'hello-user', [ 'John Doe' ] );
44 * mw.log( obj.text() );
47 * obj = new mw.Message( mw.messages, 'welcome-user', [ 'John Doe', 'Wikipedia', '2 hours ago' ] );
48 * mw.log( obj.text() );
49 * // Welcome back to Wikipedia, John Doe! Last visit by John Doe: 2 hours ago
51 * // Using mw.message shortcut
52 * obj = mw.message( 'hello-user', 'John Doe' );
53 * mw.log( obj.text() );
56 * // Using mw.msg shortcut
57 * str = mw.msg( 'hello-user', 'John Doe' );
61 * // Different formats
62 * obj = new mw.Message( mw.messages, 'hello-user', [ 'John "Wiki" <3 Doe' ] );
64 * obj.format = 'text';
65 * str = obj.toString();
70 * // Hello, John "Wiki" <3 Doe!
72 * mw.log( obj.escaped() );
73 * // Hello, John "Wiki" <3 Doe!
78 * @param {mw.Map} map Message store
80 * @param {Array} [parameters]
82 function Message( map
, key
, parameters
) {
86 this.parameters
= parameters
=== undefined ? [] : slice
.call( parameters
);
92 * Get parsed contents of the message.
94 * The default parser does simple $N replacements and nothing else.
95 * This may be overridden to provide a more complex message parser.
96 * The primary override is in the mediawiki.jqueryMsg module.
98 * This function will not be called for nonexistent messages.
100 * @return {string} Parsed message
102 parser: function () {
103 return mw
.format
.apply( null, [ this.map
.get( this.key
) ].concat( this.parameters
) );
107 * Add (does not replace) parameters for `$N` placeholder values.
109 * @param {Array} parameters
110 * @return {mw.Message}
113 params: function ( parameters
) {
115 for ( i
= 0; i
< parameters
.length
; i
++ ) {
116 this.parameters
.push( parameters
[ i
] );
122 * Convert message object to its string form based on current format.
124 * @return {string} Message as a string in the current form, or `<key>` if key
127 toString: function () {
130 if ( !this.exists() ) {
131 // Use ⧼key⧽ as text if key does not exist
132 // Err on the side of safety, ensure that the output
133 // is always html safe in the event the message key is
134 // missing, since in that case its highly likely the
135 // message key is user-controlled.
136 // '⧼' is used instead of '<' to side-step any
137 // double-escaping issues.
138 // (Keep synchronised with Message::toString() in PHP.)
139 return '⧼' + mw
.html
.escape( this.key
) + '⧽';
142 if ( this.format
=== 'plain' || this.format
=== 'text' || this.format
=== 'parse' ) {
143 text
= this.parser();
146 if ( this.format
=== 'escaped' ) {
147 text
= this.parser();
148 text
= mw
.html
.escape( text
);
155 * Change format to 'parse' and convert message to string
157 * If jqueryMsg is loaded, this parses the message text from wikitext
158 * (where supported) to HTML
160 * Otherwise, it is equivalent to plain.
162 * @return {string} String form of parsed message
165 this.format
= 'parse';
166 return this.toString();
170 * Change format to 'plain' and convert message to string
172 * This substitutes parameters, but otherwise does not change the
175 * @return {string} String form of plain message
178 this.format
= 'plain';
179 return this.toString();
183 * Change format to 'text' and convert message to string
185 * If jqueryMsg is loaded, {{-transformation is done where supported
186 * (such as {{plural:}}, {{gender:}}, {{int:}}).
188 * Otherwise, it is equivalent to plain
190 * @return {string} String form of text message
193 this.format
= 'text';
194 return this.toString();
198 * Change the format to 'escaped' and convert message to string
200 * This is equivalent to using the 'text' format (see #text), then
201 * HTML-escaping the output.
203 * @return {string} String form of html escaped message
205 escaped: function () {
206 this.format
= 'escaped';
207 return this.toString();
211 * Check if a message exists
216 exists: function () {
217 return this.map
.exists( this.key
);
227 * @inheritdoc mw.inspect#runReports
230 mw
.inspect = function () {
231 var args
= arguments
;
232 mw
.loader
.using( 'mediawiki.inspect', function () {
233 mw
.inspect
.runReports
.apply( mw
.inspect
, args
);
238 * Format a string. Replace $1, $2 ... $N with positional arguments.
240 * Used by Message#parser().
243 * @param {string} formatString Format string
244 * @param {...Mixed} parameters Values for $N replacements
245 * @return {string} Formatted string
247 mw
.format = function ( formatString
) {
248 var parameters
= slice
.call( arguments
, 1 );
249 return formatString
.replace( /\$(\d+)/g, function ( str
, match
) {
250 var index
= parseInt( match
, 10 ) - 1;
251 return parameters
[ index
] !== undefined ? parameters
[ index
] : '$' + match
;
255 // Expose Message constructor
256 mw
.Message
= Message
;
259 * Get a message object.
261 * Shortcut for `new mw.Message( mw.messages, key, parameters )`.
264 * @param {string} key Key of message to get
265 * @param {...Mixed} parameters Values for $N replacements
266 * @return {mw.Message}
268 mw
.message = function ( key
) {
269 var parameters
= slice
.call( arguments
, 1 );
270 return new Message( mw
.messages
, key
, parameters
);
274 * Get a message string using the (default) 'text' format.
276 * Shortcut for `mw.message( key, parameters... ).text()`.
279 * @param {string} key Key of message to get
280 * @param {...Mixed} parameters Values for $N replacements
283 mw
.msg = function () {
284 return mw
.message
.apply( mw
.message
, arguments
).toString();