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( {
18 titles
: String( title
)
22 .then( function ( data
) {
24 if ( data
.query
&& data
.query
.pages
) {
25 $.each( data
.query
.pages
, function ( id
, page
) {
26 if ( page
.categoryinfo
) {
33 .promise( { abort
: apiPromise
.abort
} );
37 * Get a list of categories that match a certain prefix.
39 * E.g. given "Foo", return "Food", "Foolish people", "Foosball tables"...
41 * @param {string} prefix Prefix to match.
42 * @return {jQuery.Promise}
43 * @return {Function} return.done
44 * @return {string[]} return.done.categories Matched categories
46 getCategoriesByPrefix: function ( prefix
) {
47 // Fetch with allpages to only get categories that have a corresponding description page.
48 var apiPromise
= this.get( {
51 apnamespace
: mw
.config
.get( 'wgNamespaceIds' ).category
55 .then( function ( data
) {
57 if ( data
.query
&& data
.query
.allpages
) {
58 $.each( data
.query
.allpages
, function ( i
, category
) {
59 texts
.push( new mw
.Title( category
.title
).getMainText() );
64 .promise( { abort
: apiPromise
.abort
} );
68 * Get the categories that a particular page on the wiki belongs to.
70 * @param {mw.Title|string} title
71 * @return {jQuery.Promise}
72 * @return {Function} return.done
73 * @return {boolean|mw.Title[]} return.done.categories List of category titles or false
74 * if title was not found.
76 getCategories: function ( title
) {
77 var apiPromise
= this.get( {
79 titles
: String( title
)
83 .then( function ( data
) {
85 if ( data
.query
&& data
.query
.pages
) {
86 $.each( data
.query
.pages
, function ( id
, page
) {
87 if ( page
.categories
) {
88 if ( titles
=== false ) {
91 $.each( page
.categories
, function ( i
, cat
) {
92 titles
.push( new mw
.Title( cat
.title
) );
99 .promise( { abort
: apiPromise
.abort
} );
105 * @mixins mw.Api.plugin.category
108 }( mediaWiki
, jQuery
) );