summaryrefslogtreecommitdiff
path: root/resources/src/mediawiki.api
diff options
context:
space:
mode:
Diffstat (limited to 'resources/src/mediawiki.api')
-rw-r--r--resources/src/mediawiki.api/mediawiki.api.category.js44
-rw-r--r--resources/src/mediawiki.api/mediawiki.api.edit.js43
-rw-r--r--resources/src/mediawiki.api/mediawiki.api.js87
-rw-r--r--resources/src/mediawiki.api/mediawiki.api.login.js45
-rw-r--r--resources/src/mediawiki.api/mediawiki.api.options.js89
-rw-r--r--resources/src/mediawiki.api/mediawiki.api.parse.js12
-rw-r--r--resources/src/mediawiki.api/mediawiki.api.watch.js21
7 files changed, 168 insertions, 173 deletions
diff --git a/resources/src/mediawiki.api/mediawiki.api.category.js b/resources/src/mediawiki.api/mediawiki.api.category.js
index 7dd9730f..14077e02 100644
--- a/resources/src/mediawiki.api/mediawiki.api.category.js
+++ b/resources/src/mediawiki.api/mediawiki.api.category.js
@@ -3,29 +3,21 @@
*/
( function ( mw, $ ) {
- var msg = 'Use of mediawiki.api callback params is deprecated. Use the Promise instead.';
$.extend( mw.Api.prototype, {
/**
* Determine if a category exists.
*
* @param {mw.Title|string} title
- * @param {Function} [ok] Success callback (deprecated)
- * @param {Function} [err] Error callback (deprecated)
* @return {jQuery.Promise}
* @return {Function} return.done
* @return {boolean} return.done.isCategory Whether the category exists.
*/
- isCategory: function ( title, ok, err ) {
+ isCategory: function ( title ) {
var apiPromise = this.get( {
prop: 'categoryinfo',
titles: String( title )
} );
- if ( ok || err ) {
- mw.track( 'mw.deprecate', 'api.cbParam' );
- mw.log.warn( msg );
- }
-
return apiPromise
.then( function ( data ) {
var exists = false;
@@ -38,8 +30,6 @@
}
return exists;
} )
- .done( ok )
- .fail( err )
.promise( { abort: apiPromise.abort } );
},
@@ -49,13 +39,11 @@
* E.g. given "Foo", return "Food", "Foolish people", "Foosball tables"...
*
* @param {string} prefix Prefix to match.
- * @param {Function} [ok] Success callback (deprecated)
- * @param {Function} [err] Error callback (deprecated)
* @return {jQuery.Promise}
* @return {Function} return.done
* @return {string[]} return.done.categories Matched categories
*/
- getCategoriesByPrefix: function ( prefix, ok, err ) {
+ getCategoriesByPrefix: function ( prefix ) {
// Fetch with allpages to only get categories that have a corresponding description page.
var apiPromise = this.get( {
list: 'allpages',
@@ -63,11 +51,6 @@
apnamespace: mw.config.get( 'wgNamespaceIds' ).category
} );
- if ( ok || err ) {
- mw.track( 'mw.deprecate', 'api.cbParam' );
- mw.log.warn( msg );
- }
-
return apiPromise
.then( function ( data ) {
var texts = [];
@@ -78,8 +61,6 @@
}
return texts;
} )
- .done( ok )
- .fail( err )
.promise( { abort: apiPromise.abort } );
},
@@ -87,34 +68,17 @@
* Get the categories that a particular page on the wiki belongs to.
*
* @param {mw.Title|string} title
- * @param {Function} [ok] Success callback (deprecated)
- * @param {Function} [err] Error callback (deprecated)
- * @param {boolean} [async=true] Asynchronousness (deprecated)
* @return {jQuery.Promise}
* @return {Function} return.done
* @return {boolean|mw.Title[]} return.done.categories List of category titles or false
* if title was not found.
*/
- getCategories: function ( title, ok, err, async ) {
+ getCategories: function ( title ) {
var apiPromise = this.get( {
prop: 'categories',
titles: String( title )
- }, {
- async: async === undefined ? true : async
} );
- if ( ok || err ) {
- mw.track( 'mw.deprecate', 'api.cbParam' );
- mw.log.warn( msg );
- }
- if ( async !== undefined ) {
- mw.track( 'mw.deprecate', 'api.async' );
- mw.log.warn(
- 'Use of mediawiki.api async=false param is deprecated. ' +
- 'The sychronous mode will be removed in the future.'
- );
- }
-
return apiPromise
.then( function ( data ) {
var titles = false;
@@ -132,8 +96,6 @@
}
return titles;
} )
- .done( ok )
- .fail( err )
.promise( { abort: apiPromise.abort } );
}
} );
diff --git a/resources/src/mediawiki.api/mediawiki.api.edit.js b/resources/src/mediawiki.api/mediawiki.api.edit.js
index e88ae5e2..dbe45bf6 100644
--- a/resources/src/mediawiki.api/mediawiki.api.edit.js
+++ b/resources/src/mediawiki.api/mediawiki.api.edit.js
@@ -3,7 +3,6 @@
*/
( function ( mw, $ ) {
- var msg = 'Use of mediawiki.api callback params is deprecated. Use the Promise instead.';
$.extend( mw.Api.prototype, {
/**
@@ -12,35 +11,21 @@
* cached token and start over.
*
* @param {Object} params API parameters
- * @param {Function} [ok] Success callback (deprecated)
- * @param {Function} [err] Error callback (deprecated)
* @return {jQuery.Promise} See #post
*/
- postWithEditToken: function ( params, ok, err ) {
- if ( ok || err ) {
- mw.track( 'mw.deprecate', 'api.cbParam' );
- mw.log.warn( msg );
- }
-
- return this.postWithToken( 'edit', params ).done( ok ).fail( err );
+ postWithEditToken: function ( params ) {
+ return this.postWithToken( 'edit', params );
},
/**
* API helper to grab an edit token.
*
- * @param {Function} [ok] Success callback (deprecated)
- * @param {Function} [err] Error callback (deprecated)
* @return {jQuery.Promise}
* @return {Function} return.done
* @return {string} return.done.token Received token.
*/
- getEditToken: function ( ok, err ) {
- if ( ok || err ) {
- mw.track( 'mw.deprecate', 'api.cbParam' );
- mw.log.warn( msg );
- }
-
- return this.getToken( 'edit' ).done( ok ).fail( err );
+ getEditToken: function () {
+ return this.getToken( 'edit' );
},
/**
@@ -50,32 +35,16 @@
* @param {string} header
* @param {string} message wikitext message
* @param {Object} [additionalParams] Additional API parameters, e.g. `{ redirect: true }`
- * @param {Function} [ok] Success handler (deprecated)
- * @param {Function} [err] Error handler (deprecated)
* @return {jQuery.Promise}
*/
- newSection: function ( title, header, message, additionalParams, ok, err ) {
- // Until we remove 'ok' and 'err' parameters, we have to support code that passes them,
- // but not additionalParams...
- if ( $.isFunction( additionalParams ) ) {
- err = ok;
- ok = additionalParams;
- additionalParams = undefined;
- }
-
- if ( ok || err ) {
- mw.track( 'mw.deprecate', 'api.cbParam' );
- mw.log.warn( msg );
- }
-
+ newSection: function ( title, header, message, additionalParams ) {
return this.postWithEditToken( $.extend( {
action: 'edit',
section: 'new',
- format: 'json',
title: String( title ),
summary: header,
text: message
- }, additionalParams ) ).done( ok ).fail( err );
+ }, additionalParams ) );
}
} );
diff --git a/resources/src/mediawiki.api/mediawiki.api.js b/resources/src/mediawiki.api/mediawiki.api.js
index 51b3238c..3a19e021 100644
--- a/resources/src/mediawiki.api/mediawiki.api.js
+++ b/resources/src/mediawiki.api/mediawiki.api.js
@@ -49,6 +49,16 @@
* console.log( data );
* } );
*
+ * Multiple values for a parameter can be specified using an array (since MW 1.25):
+ *
+ * var api = new mw.Api();
+ * api.get( {
+ * action: 'query',
+ * meta: [ 'userinfo', 'siteinfo' ] // same effect as 'userinfo|siteinfo'
+ * } ).done ( function ( data ) {
+ * console.log( data );
+ * } );
+ *
* @class
*
* @constructor
@@ -75,31 +85,14 @@
mw.Api.prototype = {
/**
- * Normalize the ajax options for compatibility and/or convenience methods.
- *
- * @param {Object} [arg] An object contaning one or more of options.ajax.
- * @return {Object} Normalized ajax options.
- */
- normalizeAjaxOptions: function ( arg ) {
- // Arg argument is usually empty
- // (before MW 1.20 it was used to pass ok callbacks)
- var opts = arg || {};
- // Options can also be a success callback handler
- if ( typeof arg === 'function' ) {
- opts = { ok: arg };
- }
- return opts;
- },
-
- /**
* Perform API get request
*
* @param {Object} parameters
- * @param {Object|Function} [ajaxOptions]
+ * @param {Object} [ajaxOptions]
* @return {jQuery.Promise}
*/
get: function ( parameters, ajaxOptions ) {
- ajaxOptions = this.normalizeAjaxOptions( ajaxOptions );
+ ajaxOptions = ajaxOptions || {};
ajaxOptions.type = 'GET';
return this.ajax( parameters, ajaxOptions );
},
@@ -110,11 +103,11 @@
* TODO: Post actions for non-local hostnames will need proxy.
*
* @param {Object} parameters
- * @param {Object|Function} [ajaxOptions]
+ * @param {Object} [ajaxOptions]
* @return {jQuery.Promise}
*/
post: function ( parameters, ajaxOptions ) {
- ajaxOptions = this.normalizeAjaxOptions( ajaxOptions );
+ ajaxOptions = ajaxOptions || {};
ajaxOptions.type = 'POST';
return this.ajax( parameters, ajaxOptions );
},
@@ -130,7 +123,6 @@
ajax: function ( parameters, ajaxOptions ) {
var token,
apiDeferred = $.Deferred(),
- msg = 'Use of mediawiki.api callback params is deprecated. Use the Promise instead.',
xhr, key, formData;
parameters = $.extend( {}, this.defaults.parameters, parameters );
@@ -142,6 +134,12 @@
delete parameters.token;
}
+ for ( key in parameters ) {
+ if ( $.isArray( parameters[key] ) ) {
+ parameters[key] = parameters[key].join( '|' );
+ }
+ }
+
// If multipart/form-data has been requested and emulation is possible, emulate it
if (
ajaxOptions.type === 'POST' &&
@@ -183,21 +181,6 @@
}
}
- // Backwards compatibility: Before MediaWiki 1.20,
- // callbacks were done with the 'ok' and 'err' property in ajaxOptions.
- if ( ajaxOptions.ok ) {
- mw.track( 'mw.deprecate', 'api.cbParam' );
- mw.log.warn( msg );
- apiDeferred.done( ajaxOptions.ok );
- delete ajaxOptions.ok;
- }
- if ( ajaxOptions.err ) {
- mw.track( 'mw.deprecate', 'api.cbParam' );
- mw.log.warn( msg );
- apiDeferred.fail( ajaxOptions.err );
- delete ajaxOptions.err;
- }
-
// Make the AJAX request
xhr = $.ajax( ajaxOptions )
// If AJAX fails, reject API call with error code 'http'
@@ -251,13 +234,7 @@
postWithToken: function ( tokenType, params, ajaxOptions ) {
var api = this;
- // Do not allow deprecated ok-callback
- // FIXME: Remove this check when the deprecated ok-callback is removed in #post
- if ( $.isFunction( ajaxOptions ) ) {
- ajaxOptions = undefined;
- }
-
- return api.getToken( tokenType ).then( function ( token ) {
+ return api.getToken( tokenType, params.assert ).then( function ( token ) {
params.token = token;
return api.post( params, ajaxOptions ).then(
// If no error, return to caller as-is
@@ -270,7 +247,7 @@
params.token = undefined;
// Try again, once
- return api.getToken( tokenType ).then( function ( token ) {
+ return api.getToken( tokenType, params.assert ).then( function ( token ) {
params.token = token;
return api.post( params, ajaxOptions );
} );
@@ -286,19 +263,21 @@
/**
* Get a token for a certain action from the API.
*
+ * The assert parameter is only for internal use by postWithToken.
+ *
* @param {string} type Token type
* @return {jQuery.Promise}
* @return {Function} return.done
* @return {string} return.done.token Received token.
* @since 1.22
*/
- getToken: function ( type ) {
+ getToken: function ( type, assert ) {
var apiPromise,
promiseGroup = promises[ this.defaults.ajax.url ],
d = promiseGroup && promiseGroup[ type + 'Token' ];
if ( !d ) {
- apiPromise = this.get( { action: 'tokens', type: type } );
+ apiPromise = this.get( { action: 'tokens', type: type, assert: assert } );
d = apiPromise
.then( function ( data ) {
@@ -357,7 +336,6 @@
'nomodule',
'mustbeposted',
'badaccess-groups',
- 'stashfailed',
'missingresult',
'missingparam',
'invalid-file-key',
@@ -379,7 +357,18 @@
'fetchfileerror',
'fileexists-shared-forbidden',
'invalidtitle',
- 'notloggedin'
+ 'notloggedin',
+
+ // Stash-specific errors - expanded
+ 'stashfailed',
+ 'stasherror',
+ 'stashedfilenotfound',
+ 'stashpathinvalid',
+ 'stashfilestorage',
+ 'stashzerolength',
+ 'stashnotloggedin',
+ 'stashwrongowner',
+ 'stashnosuchfilekey'
];
/**
diff --git a/resources/src/mediawiki.api/mediawiki.api.login.js b/resources/src/mediawiki.api/mediawiki.api.login.js
index ccbae06c..25257927 100644
--- a/resources/src/mediawiki.api/mediawiki.api.login.js
+++ b/resources/src/mediawiki.api/mediawiki.api.login.js
@@ -14,8 +14,7 @@
* @return {jQuery.Promise} See mw.Api#post
*/
login: function ( username, password ) {
- var params, request,
- deferred = $.Deferred(),
+ var params, apiPromise, innerPromise,
api = this;
params = {
@@ -24,25 +23,31 @@
lgpassword: password
};
- request = api.post( params );
- request.fail( deferred.reject );
- request.done( function ( data ) {
- params.lgtoken = data.login.token;
- api.post( params )
- .fail( deferred.reject )
- .done( function ( data ) {
- var code;
- if ( data.login && data.login.result === 'Success' ) {
- deferred.resolve( data );
- } else {
- // Set proper error code whenever possible
- code = data.error && data.error.code || 'unknown';
- deferred.reject( code, data );
- }
- } );
- } );
+ apiPromise = api.post( params );
- return deferred.promise( { abort: request.abort } );
+ return apiPromise
+ .then( function ( data ) {
+ params.lgtoken = data.login.token;
+ innerPromise = api.post( params )
+ .then( function ( data ) {
+ var code;
+ if ( data.login.result !== 'Success' ) {
+ // Set proper error code whenever possible
+ code = data.error && data.error.code || 'unknown';
+ return $.Deferred().reject( code, data );
+ }
+ return data;
+ } );
+ return innerPromise;
+ } )
+ .promise( {
+ abort: function () {
+ apiPromise.abort();
+ if ( innerPromise ) {
+ innerPromise.abort();
+ }
+ }
+ } );
}
} );
diff --git a/resources/src/mediawiki.api/mediawiki.api.options.js b/resources/src/mediawiki.api/mediawiki.api.options.js
new file mode 100644
index 00000000..b839fbdc
--- /dev/null
+++ b/resources/src/mediawiki.api/mediawiki.api.options.js
@@ -0,0 +1,89 @@
+/**
+ * @class mw.Api.plugin.options
+ */
+( function ( mw, $ ) {
+
+ $.extend( mw.Api.prototype, {
+
+ /**
+ * Asynchronously save the value of a single user option using the API. See #saveOptions.
+ *
+ * @param {string} name
+ * @param {string|null} value
+ * @return {jQuery.Promise}
+ */
+ saveOption: function ( name, value ) {
+ var param = {};
+ param[name] = value;
+ return this.saveOptions( param );
+ },
+
+ /**
+ * Asynchronously save the values of user options using the API.
+ *
+ * If a value of `null` is provided, the given option will be reset to the default value.
+ *
+ * Any warnings returned by the API, including warnings about invalid option names or values,
+ * are ignored. However, do not rely on this behavior.
+ *
+ * If necessary, the options will be saved using several parallel API requests. Only one promise
+ * is always returned that will be resolved when all requests complete.
+ *
+ * @param {Object} options Options as a `{ name: value, … }` object
+ * @return {jQuery.Promise}
+ */
+ saveOptions: function ( options ) {
+ var name, value, bundleable,
+ grouped = [],
+ deferreds = [];
+
+ for ( name in options ) {
+ value = options[name] === null ? null : String( options[name] );
+
+ // Can we bundle this option, or does it need a separate request?
+ bundleable =
+ ( value === null || value.indexOf( '|' ) === -1 ) &&
+ ( name.indexOf( '|' ) === -1 && name.indexOf( '=' ) === -1 );
+
+ if ( bundleable ) {
+ if ( value !== null ) {
+ grouped.push( name + '=' + value );
+ } else {
+ // Omitting value resets the option
+ grouped.push( name );
+ }
+ } else {
+ if ( value !== null ) {
+ deferreds.push( this.postWithToken( 'options', {
+ action: 'options',
+ optionname: name,
+ optionvalue: value
+ } ) );
+ } else {
+ // Omitting value resets the option
+ deferreds.push( this.postWithToken( 'options', {
+ action: 'options',
+ optionname: name
+ } ) );
+ }
+ }
+ }
+
+ if ( grouped.length ) {
+ deferreds.push( this.postWithToken( 'options', {
+ action: 'options',
+ change: grouped.join( '|' )
+ } ) );
+ }
+
+ return $.when.apply( $, deferreds );
+ }
+
+ } );
+
+ /**
+ * @class mw.Api
+ * @mixins mw.Api.plugin.options
+ */
+
+}( mediaWiki, jQuery ) );
diff --git a/resources/src/mediawiki.api/mediawiki.api.parse.js b/resources/src/mediawiki.api/mediawiki.api.parse.js
index b1f1d2b0..2dcf8078 100644
--- a/resources/src/mediawiki.api/mediawiki.api.parse.js
+++ b/resources/src/mediawiki.api/mediawiki.api.parse.js
@@ -8,31 +8,21 @@
* Convenience method for 'action=parse'.
*
* @param {string} wikitext
- * @param {Function} [ok] Success callback (deprecated)
- * @param {Function} [err] Error callback (deprecated)
* @return {jQuery.Promise}
* @return {Function} return.done
* @return {string} return.done.data Parsed HTML of `wikitext`.
*/
- parse: function ( wikitext, ok, err ) {
+ parse: function ( wikitext ) {
var apiPromise = this.get( {
action: 'parse',
contentmodel: 'wikitext',
text: wikitext
} );
- // Backwards compatibility (< MW 1.20)
- if ( ok || err ) {
- mw.track( 'mw.deprecate', 'api.cbParam' );
- mw.log.warn( 'Use of mediawiki.api callback params is deprecated. Use the Promise instead.' );
- }
-
return apiPromise
.then( function ( data ) {
return data.parse.text['*'];
} )
- .done( ok )
- .fail( err )
.promise( { abort: apiPromise.abort } );
}
} );
diff --git a/resources/src/mediawiki.api/mediawiki.api.watch.js b/resources/src/mediawiki.api/mediawiki.api.watch.js
index af2dee10..40ba136d 100644
--- a/resources/src/mediawiki.api/mediawiki.api.watch.js
+++ b/resources/src/mediawiki.api/mediawiki.api.watch.js
@@ -12,8 +12,6 @@
* @param {string|mw.Title|string[]|mw.Title[]} pages Full page name or instance of mw.Title, or an
* array thereof. If an array is passed, the return value passed to the promise will also be an
* array of appropriate objects.
- * @param {Function} [ok] Success callback (deprecated)
- * @param {Function} [err] Error callback (deprecated)
* @return {jQuery.Promise}
* @return {Function} return.done
* @return {Object|Object[]} return.done.watch Object or list of objects (depends on the `pages`
@@ -22,7 +20,7 @@
* @return {boolean} return.done.watch.watched Whether the page is now watched or unwatched
* @return {string} return.done.watch.message Parsed HTML of the confirmational interface message
*/
- function doWatchInternal( pages, ok, err, addParams ) {
+ function doWatchInternal( pages, addParams ) {
// XXX: Parameter addParams is undocumented because we inherit this
// documentation in the public method...
var apiPromise = this.postWithToken( 'watch',
@@ -36,19 +34,11 @@
)
);
- // Backwards compatibility (< MW 1.20)
- if ( ok || err ) {
- mw.track( 'mw.deprecate', 'api.cbParam' );
- mw.log.warn( 'Use of mediawiki.api callback params is deprecated. Use the Promise instead.' );
- }
-
return apiPromise
.then( function ( data ) {
// If a single page was given (not an array) respond with a single item as well.
return $.isArray( pages ) ? data.watch : data.watch[0];
} )
- .done( ok )
- .fail( err )
.promise( { abort: apiPromise.abort } );
}
@@ -58,16 +48,17 @@
*
* @inheritdoc #doWatchInternal
*/
- watch: function ( pages, ok, err ) {
- return doWatchInternal.call( this, pages, ok, err );
+ watch: function ( pages ) {
+ return doWatchInternal.call( this, pages );
},
+
/**
* Convenience method for `action=watch&unwatch=1`.
*
* @inheritdoc #doWatchInternal
*/
- unwatch: function ( pages, ok, err ) {
- return doWatchInternal.call( this, pages, ok, err, { unwatch: 1 } );
+ unwatch: function ( pages ) {
+ return doWatchInternal.call( this, pages, { unwatch: 1 } );
}
} );