* @return {boolean} return.done.isCategory Whether the category exists.
*/
isCategory: function ( title, ok, err ) {
- var d = $.Deferred(),
- apiPromise;
+ var apiPromise = this.get( {
+ prop: 'categoryinfo',
+ titles: String( title )
+ } );
// Backwards compatibility (< MW 1.20)
if ( ok || err ) {
mw.track( 'mw.deprecate', 'api.cbParam' );
mw.log.warn( msg );
- d.done( ok ).fail( err );
}
- apiPromise = this.get( {
- prop: 'categoryinfo',
- titles: title.toString()
- } )
- .done( function ( data ) {
+ return apiPromise
+ .then( function ( data ) {
var exists = false;
if ( data.query && data.query.pages ) {
$.each( data.query.pages, function ( id, page ) {
}
} );
}
- d.resolve( exists );
+ return exists;
} )
- .fail( d.reject );
-
- return d.promise( { abort: apiPromise.abort } );
+ .done( ok )
+ .fail( err )
+ .promise( { abort: apiPromise.abort } );
},
/**
* Get a list of categories that match a certain prefix.
- * e.g. given "Foo", return "Food", "Foolish people", "Foosball tables" ...
+ *
+ * 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
+ * @return {string[]} return.done.categories Matched categories
*/
getCategoriesByPrefix: function ( prefix, ok, err ) {
- var d = $.Deferred(),
- apiPromise;
+ // Fetch with allpages to only get categories that have a corresponding description page.
+ var apiPromise = this.get( {
+ list: 'allpages',
+ apprefix: prefix,
+ apnamespace: mw.config.get( 'wgNamespaceIds' ).category
+ } );
// Backwards compatibility (< MW 1.20)
if ( ok || err ) {
mw.track( 'mw.deprecate', 'api.cbParam' );
mw.log.warn( msg );
- d.done( ok ).fail( err );
}
- // Fetch with allpages to only get categories that have a corresponding description page.
- apiPromise = this.get( {
- list: 'allpages',
- apprefix: prefix,
- apnamespace: mw.config.get( 'wgNamespaceIds' ).category
- } )
- .done( function ( data ) {
+ return apiPromise
+ .then( function ( data ) {
var texts = [];
if ( data.query && data.query.allpages ) {
$.each( data.query.allpages, function ( i, category ) {
texts.push( new mw.Title( category.title ).getNameText() );
} );
}
- d.resolve( texts );
+ return texts;
} )
- .fail( d.reject );
-
- return d.promise( { abort: apiPromise.abort } );
+ .done( ok )
+ .fail( err )
+ .promise( { abort: apiPromise.abort } );
},
* if title was not found.
*/
getCategories: function ( title, ok, err, async ) {
- var d = $.Deferred(),
- apiPromise;
+ var apiPromise = this.get( {
+ prop: 'categories',
+ titles: String( title )
+ }, {
+ async: async === undefined ? true : async
+ } );
// Backwards compatibility (< MW 1.20)
if ( ok || err ) {
mw.track( 'mw.deprecate', 'api.cbParam' );
mw.log.warn( msg );
- d.done( ok ).fail( err );
}
- apiPromise = this.get( {
- prop: 'categories',
- titles: title.toString()
- }, {
- async: async === undefined ? true : async
- } )
- .done( function ( data ) {
- var ret = false;
+ return apiPromise
+ .then( function ( data ) {
+ var titles = false;
if ( data.query && data.query.pages ) {
$.each( data.query.pages, function ( id, page ) {
if ( page.categories ) {
- if ( typeof ret !== 'object' ) {
- ret = [];
+ if ( titles === false ) {
+ titles = [];
}
$.each( page.categories, function ( i, cat ) {
- ret.push( new mw.Title( cat.title ) );
+ titles.push( new mw.Title( cat.title ) );
} );
}
} );
}
- d.resolve( ret );
+ return titles;
} )
- .fail( d.reject );
-
- return d.promise( { abort: apiPromise.abort } );
+ .done( ok )
+ .fail( err )
+ .promise( { abort: apiPromise.abort } );
}
} );
* @return {string} return.done.data Parsed HTML of `wikitext`.
*/
parse: function ( wikitext, ok, err ) {
- var d = $.Deferred(),
- apiPromise;
+ 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.' );
- d.done( ok ).fail( err );
}
- apiPromise = this.get( {
- action: 'parse',
- contentmodel: 'wikitext',
- text: wikitext
+ return apiPromise
+ .then( function ( data ) {
+ return data.parse.text['*'];
} )
- .done( function ( data ) {
- if ( data.parse && data.parse.text && data.parse.text['*'] ) {
- d.resolve( data.parse.text['*'] );
- }
- } )
- .fail( d.reject );
-
- return d.promise( { abort: apiPromise.abort } );
+ .done( ok )
+ .fail( err )
+ .promise( { abort: apiPromise.abort } );
}
} );
/**
* @private
+ * @static
* @context mw.Api
*
* @param {string|mw.Title|string[]|mw.Title[]} pages Full page name or instance of mw.Title, or an
function doWatchInternal( pages, ok, err, addParams ) {
// XXX: Parameter addParams is undocumented because we inherit this
// documentation in the public method..
- var params, apiPromise,
- d = $.Deferred();
+ var apiPromise = this.post(
+ $.extend(
+ {
+ action: 'watch',
+ titles: $.isArray( pages ) ? pages.join( '|' ) : String( pages ),
+ token: mw.user.tokens.get( 'watchToken' ),
+ uselang: mw.config.get( 'wgUserLanguage' )
+ },
+ addParams
+ )
+ );
// 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.' );
- d.done( ok ).fail( err );
}
- params = {
- action: 'watch',
- token: mw.user.tokens.get( 'watchToken' ),
- uselang: mw.config.get( 'wgUserLanguage' ),
- titles: $.isArray( pages ) ? pages.join( '|' ) : String( pages )
- };
-
- if ( addParams ) {
- $.extend( params, addParams );
- }
-
- apiPromise = this.post( params )
- .done( function ( data ) {
+ return apiPromise
+ .then( function ( data ) {
// If a single page was given (not an array) respond with a single item as well.
- d.resolve( $.isArray( pages ) ? data.watch : data.watch[0] );
+ return $.isArray( pages ) ? data.watch : data.watch[0];
} )
- .fail( d.reject );
-
- return d.promise( { abort: apiPromise.abort } );
+ .done( ok )
+ .fail( err )
+ .promise( { abort: apiPromise.abort } );
}
$.extend( mw.Api.prototype, {