2 * @class mw.Api.plugin.category
6 $.extend( mw
.Api
.prototype, {
8 * Determine if a category exists.
10 * @param {mw.Title|string} title
11 * @return {jQuery.Promise}
12 * @return {Function} return.done
13 * @return {boolean} return.done.isCategory Whether the category exists.
15 isCategory: function ( title
) {
16 var apiPromise
= this.get( {
19 titles
: String( title
)
23 .then( function ( data
) {
24 return !!data
.query
.pages
[ 0 ].categoryinfo
;
26 .promise( { abort
: apiPromise
.abort
} );
30 * Get a list of categories that match a certain prefix.
32 * E.g. given "Foo", return "Food", "Foolish people", "Foosball tables"...
34 * @param {string} prefix Prefix to match.
35 * @return {jQuery.Promise}
36 * @return {Function} return.done
37 * @return {string[]} return.done.categories Matched categories
39 getCategoriesByPrefix: function ( prefix
) {
40 // Fetch with allpages to only get categories that have a corresponding description page.
41 var apiPromise
= this.get( {
45 apnamespace
: mw
.config
.get( 'wgNamespaceIds' ).category
49 .then( function ( data
) {
50 return data
.query
.allpages
.map( function ( category
) {
51 return new mw
.Title( category
.title
).getMainText();
54 .promise( { abort
: apiPromise
.abort
} );
58 * Get the categories that a particular page on the wiki belongs to.
60 * @param {mw.Title|string} title
61 * @return {jQuery.Promise}
62 * @return {Function} return.done
63 * @return {boolean|mw.Title[]} return.done.categories List of category titles or false
64 * if title was not found.
66 getCategories: function ( title
) {
67 var apiPromise
= this.get( {
70 titles
: String( title
)
74 .then( function ( data
) {
75 var page
= data
.query
.pages
[ 0 ];
77 if ( !page
.categories
) {
80 return page
.categories
.map( function ( cat
) {
81 return new mw
.Title( cat
.title
);
84 .promise( { abort
: apiPromise
.abort
} );
90 * @mixins mw.Api.plugin.category
93 }( mediaWiki
, jQuery
) );