1 // library to assist with API calls on categories
5 $.extend( mw
.Api
.prototype, {
7 * Determine if a category exists
9 * @param {Function} callback to pass boolean of category's existence
10 * @param {Function} optional callback to run if api error
11 * @return ajax call object
13 isCategory: function( title
, callback
, err
) {
15 'prop': 'categoryinfo',
16 'titles': title
.toString()
19 var ok = function( data
) {
21 if ( data
.query
&& data
.query
.pages
) {
22 $.each( data
.query
.pages
, function( id
, page
) {
23 if ( page
.categoryinfo
) {
31 return this.get( params
, { ok
: ok
, err
: err
} );
36 * Get a list of categories that match a certain prefix.
37 * e.g. given "Foo", return "Food", "Foolish people", "Foosball tables" ...
38 * @param {String} prefix to match
39 * @param {Function} callback to pass matched categories to
40 * @param {Function} optional callback to run if api error
41 * @return ajax call object
43 getCategoriesByPrefix: function( prefix
, callback
, err
) {
45 // fetch with allpages to only get categories that have a corresponding description page.
49 'apnamespace': mw
.config
.get('wgNamespaceIds').category
52 var ok = function( data
) {
54 if ( data
.query
&& data
.query
.allpages
) {
55 $.each( data
.query
.allpages
, function( i
, category
) {
56 texts
.push( new mw
.Title( category
.title
).getNameText() );
62 return this.get( params
, { ok
: ok
, err
: err
} );
68 * Get the categories that a particular page on the wiki belongs to
70 * @param {Function} callback to pass categories to (or false, if title not found)
71 * @param {Function} optional callback to run if api error
72 * @param {Boolean} optional asynchronousness (default = true = async)
73 * @return ajax call object
75 getCategories: function( title
, callback
, err
, async
) {
78 titles
: title
.toString()
80 if ( async
=== undefined ) {
84 var ok = function( data
) {
86 if ( data
.query
&& data
.query
.pages
) {
87 $.each( data
.query
.pages
, function( id
, page
) {
88 if ( page
.categories
) {
89 if ( typeof ret
!== 'object' ) {
92 $.each( page
.categories
, function( i
, cat
) {
93 ret
.push( new mw
.Title( cat
.title
) );
101 return this.get( params
, { ok
: ok
, err
: err
, async
: async
} );
106 } )( window
.mediaWiki
, jQuery
);