Debian Codegraph @

resources/src/mediawiki.api/options.js

raw symbols SCIP
1 ( function () {
2
3 const saveOptionsRequests = {};
4
5 Object.assign( mw.Api.prototype, /** @lends mw.Api.prototype */ {
6
7 /**
8 * Asynchronously save the value of a single user option using the API.
9 * See [saveOptions()]{@link mw.Api#saveOptions}.
10 *
11 * @param {string} name
12 * @param {string|null} value
13 * @param {Object} [params] additional parameters for API.
14 * @return {jQuery.Promise}
15 */
16 saveOption: function ( name, value, params ) {
17 const options = {};
18 options[ name ] = value;
19 return this.saveOptions( options, params );
20 },
21
22 /**
23 * Asynchronously save the values of user options using the [Options API](https://www.mediawiki.org/wiki/API:Options).
24 *
25 * If a value of `null` is provided, the given option will be reset to the default value.
26 *
27 * Any warnings returned by the API, including warnings about invalid option names or values,
28 * are ignored. However, do not rely on this behavior.
29 *
30 * If necessary, the options will be saved using several sequential API requests. Only one promise
31 * is always returned that will be resolved when all requests complete.
32 *
33 * If a request from a previous `saveOptions()` call is still pending, this will wait for it to be
34 * completed, otherwise MediaWiki gets sad. No requests are sent for anonymous users, as they
35 * would fail anyway. See T214963.
36 *
37 * @param {Object} options Options as a `{ name: value, … }` object
38 * @param {Object} [params] additional parameters for API.
39 * @return {jQuery.Promise}
40 */
41 saveOptions: function ( options, params ) {
42 const grouped = [];
43
44 // Logged-out users can't have user options; we can't depend on mw.user, that'd be circular
45 if ( mw.config.get( 'wgUserName' ) === null || mw.config.get( 'wgUserIsTemp' ) ) {
46 return $.Deferred().reject( 'notloggedin' ).promise();
47 }
48
49 let promise;
50 // If another options request to this API is pending, wait for it first
51 if (
52 saveOptionsRequests[ this.defaults.ajax.url ] &&
53 // Avoid long chains of promises, they may cause memory leaks
54 saveOptionsRequests[ this.defaults.ajax.url ].state() === 'pending'
55 ) {
56 promise = saveOptionsRequests[ this.defaults.ajax.url ].then(
57 // Don't expose the old promise's result, it would be confusing
58 () => $.Deferred().resolve(),
59 () => $.Deferred().resolve()
60 );
61 } else {
62 promise = $.Deferred().resolve();
63 }
64
65 for ( const name in options ) {
66 const value = options[ name ] === null ? null : String( options[ name ] );
67
68 let bundleable;
69 // Can we bundle this option, or does it need a separate request?
70 if ( this.defaults.useUS ) {
71 bundleable = name.indexOf( '=' ) === -1;
72 } else {
73 bundleable =
74 ( value === null || value.indexOf( '|' ) === -1 ) &&
75 ( name.indexOf( '|' ) === -1 && name.indexOf( '=' ) === -1 );
76 }
77
78 if ( bundleable ) {
79 if ( value !== null ) {
80 grouped.push( name + '=' + value );
81 } else {
82 // Omitting value resets the option
83 grouped.push( name );
84 }
85 } else {
86 if ( value !== null ) {
87 promise = promise.then( function ( n, v ) {
88 return this.postWithToken( 'csrf', Object.assign( {
89 formatversion: 2,
90 action: 'options',
91 optionname: n,
92 optionvalue: v
93 }, params ) );
94 }.bind( this, name, value ) );
95 } else {
96 // Omitting value resets the option
97 promise = promise.then( function ( n ) {
98 return this.postWithToken( 'csrf', Object.assign( {
99 formatversion: 2,
100 action: 'options',
101 optionname: n
102 }, params ) );
103 }.bind( this, name ) );
104 }
105 }
106 }
107
108 if ( grouped.length ) {
109 promise = promise.then( () => this.postWithToken( 'csrf', Object.assign( {
110 formatversion: 2,
111 action: 'options',
112 change: grouped
113 }, params ) ) );
114 }
115
116 saveOptionsRequests[ this.defaults.ajax.url ] = promise;
117
118 return promise;
119 }
120
121 } );
122
123 }() );