85df90e912597fe447626d164ac7f0a201991a6d
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
) {
25 data
.query
&& // query is missing on title=""
26 data
.query
.pages
&& // query.pages is missing on title="#" or title="mw:"
27 data
.query
.pages
[ 0 ].categoryinfo
30 .promise( { abort
: apiPromise
.abort
} );
34 * Get a list of categories that match a certain prefix.
36 * E.g. given "Foo", return "Food", "Foolish people", "Foosball tables"...
38 * @param {string} prefix Prefix to match.
39 * @return {jQuery.Promise}
40 * @return {Function} return.done
41 * @return {string[]} return.done.categories Matched categories
43 getCategoriesByPrefix: function ( prefix
) {
44 // Fetch with allpages to only get categories that have a corresponding description page.
45 var apiPromise
= this.get( {
49 apnamespace
: mw
.config
.get( 'wgNamespaceIds' ).category
53 .then( function ( data
) {
54 return data
.query
.allpages
.map( function ( category
) {
55 return new mw
.Title( category
.title
).getMainText();
58 .promise( { abort
: apiPromise
.abort
} );
62 * Get the categories that a particular page on the wiki belongs to.
64 * @param {mw.Title|string} title
65 * @return {jQuery.Promise}
66 * @return {Function} return.done
67 * @return {boolean|mw.Title[]} return.done.categories List of category titles or false
68 * if title was not found.
70 getCategories: function ( title
) {
71 var apiPromise
= this.get( {
74 titles
: [ String( title
) ]
78 .then( function ( data
) {
81 if ( !data
.query
|| !data
.query
.pages
) {
84 page
= data
.query
.pages
[ 0 ];
85 if ( !page
.categories
) {
88 return page
.categories
.map( function ( cat
) {
89 return new mw
.Title( cat
.title
);
92 .promise( { abort
: apiPromise
.abort
} );
98 * @mixins mw.Api.plugin.category
101 }( mediaWiki
, jQuery
) );