2 * Additional mw.Api methods to assist with API calls related to categories.
6 $.extend( mw
.Api
.prototype, {
8 * Determine if a category exists.
9 * @param title {mw.Title}
10 * @param success {Function} callback to pass boolean of category's existence
11 * @param err {Function} optional callback to run if api error
12 * @return ajax call object
14 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
) {
44 // Fetch with allpages to only get categories that have a corresponding description page.
49 'apnamespace': mw
.config
.get('wgNamespaceIds').category
51 ok = function ( data
) {
53 if ( data
.query
&& data
.query
.allpages
) {
54 $.each( data
.query
.allpages
, function ( i
, category
) {
55 texts
.push( new mw
.Title( category
.title
).getNameText() );
61 return this.get( params
, { ok
: ok
, err
: err
} );
66 * Get the categories that a particular page on the wiki belongs to
67 * @param title {mw.Title}
68 * @param success {Function} callback to pass categories to (or false, if title not found)
69 * @param err {Function} optional callback to run if api error
70 * @param async {Boolean} optional asynchronousness (default = true = async)
73 getCategories: function ( title
, success
, err
, async
) {
77 titles
: title
.toString()
79 if ( async
=== undefined ) {
82 ok = function ( data
) {
84 if ( data
.query
&& data
.query
.pages
) {
85 $.each( data
.query
.pages
, function ( id
, page
) {
86 if ( page
.categories
) {
87 if ( typeof ret
!== 'object' ) {
90 $.each( page
.categories
, function ( i
, cat
) {
91 ret
.push( new mw
.Title( cat
.title
) );
99 return this.get( params
, { ok
: ok
, err
: err
, async
: async
} );
104 }( mediaWiki
, jQuery
) );