2 * Additional mw.Api methods to assist with API calls related to categories.
5 ( function( $, mw
, undefined ) {
7 $.extend( mw
.Api
.prototype, {
9 * Determine if a category exists.
10 * @param title {mw.Title}
11 * @param success {Function} callback to pass boolean of category's existence
12 * @param err {Function} optional callback to run if api error
13 * @return ajax call object
15 isCategory: function( title
, success
, err
) {
18 titles
: title
.toString()
20 ok = function( data
) {
22 if ( data
.query
&& data
.query
.pages
) {
23 $.each( data
.query
.pages
, function( id
, page
) {
24 if ( page
.categoryinfo
) {
32 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 prefix {String} prefix to match
39 * @param success {Function} callback to pass matched categories to
40 * @param err {Function} optional callback to run if api error
43 getCategoriesByPrefix: function( prefix
, success
, 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
} );
67 * Get the categories that a particular page on the wiki belongs to
68 * @param title {mw.Title}
69 * @param success {Function} callback to pass categories to (or false, if title not found)
70 * @param err {Function} optional callback to run if api error
71 * @param async {Boolean} optional asynchronousness (default = true = async)
74 getCategories: function( title
, success
, err
, async
) {
78 titles
: title
.toString()
80 if ( async
=== undefined ) {
83 ok = function( data
) {
85 if ( data
.query
&& data
.query
.pages
) {
86 $.each( data
.query
.pages
, function( id
, page
) {
87 if ( page
.categories
) {
88 if ( typeof ret
!== 'object' ) {
91 $.each( page
.categories
, function( i
, cat
) {
92 ret
.push( new mw
.Title( cat
.title
) );
100 return this.get( params
, { ok
: ok
, err
: err
, async
: async
} );
105 } )( jQuery
, mediaWiki
);