Debian Codegraph @

resources/src/mediawiki.api/messages.js

raw symbols SCIP
1 ( function () {
2 'use strict';
3
4 Object.assign( mw.Api.prototype, /** @lends mw.Api.prototype */ {
5 /**
6 * Get a set of messages.
7 *
8 * @since 1.27
9 * @param {string|string[]} messages Messages to retrieve
10 * @param {Object} [options] Additional parameters for the API call
11 * @return {jQuery.Promise<Object.<string, string>>}
12 */
13 getMessages: function ( messages, options ) {
14 const that = this;
15 options = options || {};
16 messages = Array.isArray( messages ) ? messages : [ messages ];
17 return this.get( Object.assign( {
18 action: 'query',
19 meta: 'allmessages',
20 ammessages: messages.slice( 0, 50 ),
21 amlang: mw.config.get( 'wgUserLanguage' ),
22 formatversion: 2
23 }, options ) ).then( ( data ) => {
24 const result = {};
25
26 data.query.allmessages.forEach( ( obj ) => {
27 if ( !obj.missing ) {
28 result[ obj.name ] = obj.content;
29 }
30 } );
31
32 // If no more messages are needed, return now, otherwise calls
33 // itself recursively, because only 50 messages can be loaded
34 // at a time. This limit of 50 comes from ApiBase::LIMIT_SML1;
35 // ApiQueryAllMessages sets the 'ammessages' parameter to include
36 // multiple values, and for users without the `apihighlimits` right
37 // LIMIT_SML1 is the limit imposed on the number of values.
38 if ( messages.length <= 50 ) {
39 return result;
40 }
41
42 return that.getMessages( messages.slice( 50 ), options ).then(
43 // Merge result objects
44 ( innerResult ) => Object.assign( result, innerResult )
45 );
46 } );
47 },
48
49 /**
50 * Loads a set of messages and add them to {@link mw.messages}.
51 *
52 * @param {string|string[]} messages Messages to retrieve
53 * @param {Object} [options] Additional parameters for the API call
54 * @return {jQuery.Promise}
55 */
56 loadMessages: function ( messages, options ) {
57 return this.getMessages( messages, options ).then( mw.messages.set.bind( mw.messages ) );
58 },
59
60 /**
61 * Loads a set of messages and add them to {@link mw.messages}. Only messages that are not already known
62 * are loaded. If all messages are known, the returned promise is resolved immediately.
63 *
64 * @since 1.27
65 * @param {string|string[]} messages Messages to retrieve
66 * @param {Object} [options] Additional parameters for the API call
67 * @return {jQuery.Promise}
68 */
69 loadMessagesIfMissing: function ( messages, options ) {
70 messages = Array.isArray( messages ) ? messages : [ messages ];
71 const missing = messages.filter(
72 // eslint-disable-next-line mediawiki/msg-doc
73 ( msg ) => !mw.message( msg ).exists()
74 );
75
76 if ( missing.length === 0 ) {
77 return $.Deferred().resolve();
78 }
79
80 return this.loadMessages( missing, options );
81 }
82 } );
83
84 }() );