From: Aaron Schulz Date: Wed, 6 Jun 2018 00:13:10 +0000 (-0700) Subject: resourceloader: move Message methods from mediawiki.js to base module X-Git-Tag: 1.34.0-rc.0~5167 X-Git-Url: http://git.cyclocoop.org/%7B%24admin_url%7Dcompta/comptes/%28%5B%5E/%27%20.%20%24_credit%5B%27url%27%5D%20.%20%27?a=commitdiff_plain;h=b566f66107962a0926cdfb0866454b52961a49f6;p=lhc%2Fweb%2Fwiklou.git resourceloader: move Message methods from mediawiki.js to base module Also set "use strict" in the base module. Bug: T192623 Change-Id: I18a710e167df8be47a70da93a7aba0784fc62aef --- diff --git a/maintenance/jsduck/eg-iframe.html b/maintenance/jsduck/eg-iframe.html index 4c02998d19..f19a69b34b 100644 --- a/maintenance/jsduck/eg-iframe.html +++ b/maintenance/jsduck/eg-iframe.html @@ -43,6 +43,7 @@ + diff --git a/resources/src/mediawiki/mediawiki.base.js b/resources/src/mediawiki/mediawiki.base.js index b68d779358..43ee202f93 100644 --- a/resources/src/mediawiki/mediawiki.base.js +++ b/resources/src/mediawiki/mediawiki.base.js @@ -16,6 +16,208 @@ */ /* globals mw */ ( function () { + 'use strict'; + + var slice = Array.prototype.slice; + + /** + * Object constructor for messages. + * + * Similar to the Message class in MediaWiki PHP. + * + * Format defaults to 'text'. + * + * @example + * + * var obj, str; + * mw.messages.set( { + * 'hello': 'Hello world', + * 'hello-user': 'Hello, $1!', + * 'welcome-user': 'Welcome back to $2, $1! Last visit by $1: $3' + * } ); + * + * obj = new mw.Message( mw.messages, 'hello' ); + * mw.log( obj.text() ); + * // Hello world + * + * obj = new mw.Message( mw.messages, 'hello-user', [ 'John Doe' ] ); + * mw.log( obj.text() ); + * // Hello, John Doe! + * + * obj = new mw.Message( mw.messages, 'welcome-user', [ 'John Doe', 'Wikipedia', '2 hours ago' ] ); + * mw.log( obj.text() ); + * // Welcome back to Wikipedia, John Doe! Last visit by John Doe: 2 hours ago + * + * // Using mw.message shortcut + * obj = mw.message( 'hello-user', 'John Doe' ); + * mw.log( obj.text() ); + * // Hello, John Doe! + * + * // Using mw.msg shortcut + * str = mw.msg( 'hello-user', 'John Doe' ); + * mw.log( str ); + * // Hello, John Doe! + * + * // Different formats + * obj = new mw.Message( mw.messages, 'hello-user', [ 'John "Wiki" <3 Doe' ] ); + * + * obj.format = 'text'; + * str = obj.toString(); + * // Same as: + * str = obj.text(); + * + * mw.log( str ); + * // Hello, John "Wiki" <3 Doe! + * + * mw.log( obj.escaped() ); + * // Hello, John "Wiki" <3 Doe! + * + * @class mw.Message + * + * @constructor + * @param {mw.Map} map Message store + * @param {string} key + * @param {Array} [parameters] + */ + function Message( map, key, parameters ) { + this.format = 'text'; + this.map = map; + this.key = key; + this.parameters = parameters === undefined ? [] : slice.call( parameters ); + return this; + } + + Message.prototype = { + /** + * Get parsed contents of the message. + * + * The default parser does simple $N replacements and nothing else. + * This may be overridden to provide a more complex message parser. + * The primary override is in the mediawiki.jqueryMsg module. + * + * This function will not be called for nonexistent messages. + * + * @return {string} Parsed message + */ + parser: function () { + return mw.format.apply( null, [ this.map.get( this.key ) ].concat( this.parameters ) ); + }, + + /** + * Add (does not replace) parameters for `$N` placeholder values. + * + * @param {Array} parameters + * @return {mw.Message} + * @chainable + */ + params: function ( parameters ) { + var i; + for ( i = 0; i < parameters.length; i++ ) { + this.parameters.push( parameters[ i ] ); + } + return this; + }, + + /** + * Convert message object to its string form based on current format. + * + * @return {string} Message as a string in the current form, or `` if key + * does not exist. + */ + toString: function () { + var text; + + if ( !this.exists() ) { + // Use ⧼key⧽ as text if key does not exist + // Err on the side of safety, ensure that the output + // is always html safe in the event the message key is + // missing, since in that case its highly likely the + // message key is user-controlled. + // '⧼' is used instead of '<' to side-step any + // double-escaping issues. + // (Keep synchronised with Message::toString() in PHP.) + return '⧼' + mw.html.escape( this.key ) + '⧽'; + } + + if ( this.format === 'plain' || this.format === 'text' || this.format === 'parse' ) { + text = this.parser(); + } + + if ( this.format === 'escaped' ) { + text = this.parser(); + text = mw.html.escape( text ); + } + + return text; + }, + + /** + * Change format to 'parse' and convert message to string + * + * If jqueryMsg is loaded, this parses the message text from wikitext + * (where supported) to HTML + * + * Otherwise, it is equivalent to plain. + * + * @return {string} String form of parsed message + */ + parse: function () { + this.format = 'parse'; + return this.toString(); + }, + + /** + * Change format to 'plain' and convert message to string + * + * This substitutes parameters, but otherwise does not change the + * message text. + * + * @return {string} String form of plain message + */ + plain: function () { + this.format = 'plain'; + return this.toString(); + }, + + /** + * Change format to 'text' and convert message to string + * + * If jqueryMsg is loaded, {{-transformation is done where supported + * (such as {{plural:}}, {{gender:}}, {{int:}}). + * + * Otherwise, it is equivalent to plain + * + * @return {string} String form of text message + */ + text: function () { + this.format = 'text'; + return this.toString(); + }, + + /** + * Change the format to 'escaped' and convert message to string + * + * This is equivalent to using the 'text' format (see #text), then + * HTML-escaping the output. + * + * @return {string} String form of html escaped message + */ + escaped: function () { + this.format = 'escaped'; + return this.toString(); + }, + + /** + * Check if a message exists + * + * @see mw.Map#exists + * @return {boolean} + */ + exists: function () { + return this.map.exists( this.key ); + } + }; + /** * @class mw * @singleton @@ -31,4 +233,54 @@ mw.inspect.runReports.apply( mw.inspect, args ); } ); }; + + /** + * Format a string. Replace $1, $2 ... $N with positional arguments. + * + * Used by Message#parser(). + * + * @since 1.25 + * @param {string} formatString Format string + * @param {...Mixed} parameters Values for $N replacements + * @return {string} Formatted string + */ + mw.format = function ( formatString ) { + var parameters = slice.call( arguments, 1 ); + return formatString.replace( /\$(\d+)/g, function ( str, match ) { + var index = parseInt( match, 10 ) - 1; + return parameters[ index ] !== undefined ? parameters[ index ] : '$' + match; + } ); + }; + + // Expose Message constructor + mw.Message = Message; + + /** + * Get a message object. + * + * Shortcut for `new mw.Message( mw.messages, key, parameters )`. + * + * @see mw.Message + * @param {string} key Key of message to get + * @param {...Mixed} parameters Values for $N replacements + * @return {mw.Message} + */ + mw.message = function ( key ) { + var parameters = slice.call( arguments, 1 ); + return new Message( mw.messages, key, parameters ); + }; + + /** + * Get a message string using the (default) 'text' format. + * + * Shortcut for `mw.message( key, parameters... ).text()`. + * + * @see mw.Message + * @param {string} key Key of message to get + * @param {...Mixed} parameters Values for $N replacements + * @return {string} + */ + mw.msg = function () { + return mw.message.apply( mw.message, arguments ).toString(); + }; }() ); diff --git a/resources/src/mediawiki/mediawiki.js b/resources/src/mediawiki/mediawiki.js index c6a3bded12..9d34bf1058 100644 --- a/resources/src/mediawiki/mediawiki.js +++ b/resources/src/mediawiki/mediawiki.js @@ -220,204 +220,6 @@ } }; - /** - * Object constructor for messages. - * - * Similar to the Message class in MediaWiki PHP. - * - * Format defaults to 'text'. - * - * @example - * - * var obj, str; - * mw.messages.set( { - * 'hello': 'Hello world', - * 'hello-user': 'Hello, $1!', - * 'welcome-user': 'Welcome back to $2, $1! Last visit by $1: $3' - * } ); - * - * obj = new mw.Message( mw.messages, 'hello' ); - * mw.log( obj.text() ); - * // Hello world - * - * obj = new mw.Message( mw.messages, 'hello-user', [ 'John Doe' ] ); - * mw.log( obj.text() ); - * // Hello, John Doe! - * - * obj = new mw.Message( mw.messages, 'welcome-user', [ 'John Doe', 'Wikipedia', '2 hours ago' ] ); - * mw.log( obj.text() ); - * // Welcome back to Wikipedia, John Doe! Last visit by John Doe: 2 hours ago - * - * // Using mw.message shortcut - * obj = mw.message( 'hello-user', 'John Doe' ); - * mw.log( obj.text() ); - * // Hello, John Doe! - * - * // Using mw.msg shortcut - * str = mw.msg( 'hello-user', 'John Doe' ); - * mw.log( str ); - * // Hello, John Doe! - * - * // Different formats - * obj = new mw.Message( mw.messages, 'hello-user', [ 'John "Wiki" <3 Doe' ] ); - * - * obj.format = 'text'; - * str = obj.toString(); - * // Same as: - * str = obj.text(); - * - * mw.log( str ); - * // Hello, John "Wiki" <3 Doe! - * - * mw.log( obj.escaped() ); - * // Hello, John "Wiki" <3 Doe! - * - * @class mw.Message - * - * @constructor - * @param {mw.Map} map Message store - * @param {string} key - * @param {Array} [parameters] - */ - function Message( map, key, parameters ) { - this.format = 'text'; - this.map = map; - this.key = key; - this.parameters = parameters === undefined ? [] : slice.call( parameters ); - return this; - } - - Message.prototype = { - /** - * Get parsed contents of the message. - * - * The default parser does simple $N replacements and nothing else. - * This may be overridden to provide a more complex message parser. - * The primary override is in the mediawiki.jqueryMsg module. - * - * This function will not be called for nonexistent messages. - * - * @return {string} Parsed message - */ - parser: function () { - return mw.format.apply( null, [ this.map.get( this.key ) ].concat( this.parameters ) ); - }, - - /** - * Add (does not replace) parameters for `$N` placeholder values. - * - * @param {Array} parameters - * @return {mw.Message} - * @chainable - */ - params: function ( parameters ) { - var i; - for ( i = 0; i < parameters.length; i++ ) { - this.parameters.push( parameters[ i ] ); - } - return this; - }, - - /** - * Convert message object to its string form based on current format. - * - * @return {string} Message as a string in the current form, or `` if key - * does not exist. - */ - toString: function () { - var text; - - if ( !this.exists() ) { - // Use ⧼key⧽ as text if key does not exist - // Err on the side of safety, ensure that the output - // is always html safe in the event the message key is - // missing, since in that case its highly likely the - // message key is user-controlled. - // '⧼' is used instead of '<' to side-step any - // double-escaping issues. - // (Keep synchronised with Message::toString() in PHP.) - return '⧼' + mw.html.escape( this.key ) + '⧽'; - } - - if ( this.format === 'plain' || this.format === 'text' || this.format === 'parse' ) { - text = this.parser(); - } - - if ( this.format === 'escaped' ) { - text = this.parser(); - text = mw.html.escape( text ); - } - - return text; - }, - - /** - * Change format to 'parse' and convert message to string - * - * If jqueryMsg is loaded, this parses the message text from wikitext - * (where supported) to HTML - * - * Otherwise, it is equivalent to plain. - * - * @return {string} String form of parsed message - */ - parse: function () { - this.format = 'parse'; - return this.toString(); - }, - - /** - * Change format to 'plain' and convert message to string - * - * This substitutes parameters, but otherwise does not change the - * message text. - * - * @return {string} String form of plain message - */ - plain: function () { - this.format = 'plain'; - return this.toString(); - }, - - /** - * Change format to 'text' and convert message to string - * - * If jqueryMsg is loaded, {{-transformation is done where supported - * (such as {{plural:}}, {{gender:}}, {{int:}}). - * - * Otherwise, it is equivalent to plain - * - * @return {string} String form of text message - */ - text: function () { - this.format = 'text'; - return this.toString(); - }, - - /** - * Change the format to 'escaped' and convert message to string - * - * This is equivalent to using the 'text' format (see #text), then - * HTML-escaping the output. - * - * @return {string} String form of html escaped message - */ - escaped: function () { - this.format = 'escaped'; - return this.toString(); - }, - - /** - * Check if a message exists - * - * @see mw.Map#exists - * @return {boolean} - */ - exists: function () { - return this.map.exists( this.key ); - } - }; - defineFallbacks(); /* eslint-disable no-console */ @@ -557,24 +359,6 @@ function () { return Date.now(); }; }() ), - /** - * Format a string. Replace $1, $2 ... $N with positional arguments. - * - * Used by Message#parser(). - * - * @since 1.25 - * @param {string} formatString Format string - * @param {...Mixed} parameters Values for $N replacements - * @return {string} Formatted string - */ - format: function ( formatString ) { - var parameters = slice.call( arguments, 1 ); - return formatString.replace( /\$(\d+)/g, function ( str, match ) { - var index = parseInt( match, 10 ) - 1; - return parameters[ index ] !== undefined ? parameters[ index ] : '$' + match; - } ); - }, - /** * Track an analytic event. * @@ -646,9 +430,6 @@ // Expose Map constructor Map: Map, - // Expose Message constructor - Message: Message, - /** * Map of configuration values. * @@ -699,35 +480,6 @@ */ templates: new Map(), - /** - * Get a message object. - * - * Shortcut for `new mw.Message( mw.messages, key, parameters )`. - * - * @see mw.Message - * @param {string} key Key of message to get - * @param {...Mixed} parameters Values for $N replacements - * @return {mw.Message} - */ - message: function ( key ) { - var parameters = slice.call( arguments, 1 ); - return new Message( mw.messages, key, parameters ); - }, - - /** - * Get a message string using the (default) 'text' format. - * - * Shortcut for `mw.message( key, parameters... ).text()`. - * - * @see mw.Message - * @param {string} key Key of message to get - * @param {...Mixed} parameters Values for $N replacements - * @return {string} - */ - msg: function () { - return mw.message.apply( mw.message, arguments ).toString(); - }, - // Expose mw.log log: log, @@ -1135,11 +887,9 @@ for ( i = 0; i < deps.length; i++ ) { if ( resolved.indexOf( deps[ i ] ) === -1 ) { if ( unresolved.has( deps[ i ] ) ) { - throw new Error( mw.format( - 'Circular reference detected: $1 -> $2', - module, - deps[ i ] - ) ); + throw new Error( + 'Circular reference detected: ' + module + ' -> ' + deps[ i ] + ); } unresolved.add( module );