Merge "Update tests/phpunit/MediaWikiTestCase.php with support for apcu"
[mediawiki.git] / resources / src / mediawiki / api / messages.js
bloba1a499960f3d6c0a4fc5b52149ef4a88f2d9468a
1 /**
2 * Allows to retrieve a specific or a set of
3 * messages to be added to mw.messages and returned
4 * by the Api.
6 * @class mw.Api.plugin.messages
7 * @since 1.27
8 */
9 ( function ( mw, $ ) {
10 'use strict';
12 $.extend( mw.Api.prototype, {
13 /**
14 * Get a set of messages.
16 * @param {Array} messages Messages to retrieve
17 * @param {Object} [options] Additional parameters for the API call
18 * @return {jQuery.Promise}
20 getMessages: function ( messages, options ) {
21 options = options || {};
22 return this.get( $.extend( {
23 action: 'query',
24 meta: 'allmessages',
25 ammessages: messages,
26 amlang: mw.config.get( 'wgUserLanguage' ),
27 formatversion: 2
28 }, options ) ).then( function ( data ) {
29 var result = {};
31 $.each( data.query.allmessages, function ( i, obj ) {
32 if ( !obj.missing ) {
33 result[ obj.name ] = obj.content;
35 } );
37 return result;
38 } );
41 /**
42 * Loads a set of messages and add them to mw.messages.
44 * @param {Array} messages Messages to retrieve
45 * @param {Object} [options] Additional parameters for the API call
46 * @return {jQuery.Promise}
48 loadMessages: function ( messages, options ) {
49 return this.getMessages( messages, options ).then( $.proxy( mw.messages, 'set' ) );
52 /**
53 * Loads a set of messages and add them to mw.messages. Only messages that are not already known
54 * are loaded. If all messages are known, the returned promise is resolved immediately.
56 * @param {Array} messages Messages to retrieve
57 * @param {Object} [options] Additional parameters for the API call
58 * @return {jQuery.Promise}
60 loadMessagesIfMissing: function ( messages, options ) {
61 var missing = messages.filter( function ( msg ) {
62 return !mw.message( msg ).exists();
63 } );
65 if ( missing.length === 0 ) {
66 return $.Deferred().resolve();
69 return this.getMessages( missing, options ).then( $.proxy( mw.messages, 'set' ) );
71 } );
73 /**
74 * @class mw.Api
75 * @mixins mw.Api.plugin.messages
78 }( mediaWiki, jQuery ) );