2 * @class mw.Api.plugin.category
6 $.extend( mw.Api.prototype, {
8 * Determine if a category exists.
9 * @param {mw.Title} title
10 * @param {Function} [ok] Success callback (deprecated)
11 * @param {Function} [err] Error callback (deprecated)
12 * @return {jQuery.Promise}
13 * @return {Function} return.done
14 * @return {boolean} return.done.isCategory Whether the category exists.
16 isCategory: function ( title, ok, err ) {
19 // Backwards compatibility (< MW 1.20)
23 apiPromise = this.get( {
25 titles: title.toString()
27 .done( function ( data ) {
29 if ( data.query && data.query.pages ) {
30 $.each( data.query.pages, function ( id, page ) {
31 if ( page.categoryinfo ) {
40 return d.promise( { abort: apiPromise.abort } );
44 * Get a list of categories that match a certain prefix.
45 * e.g. given "Foo", return "Food", "Foolish people", "Foosball tables" ...
46 * @param {string} prefix Prefix to match.
47 * @param {Function} [ok] Success callback (deprecated)
48 * @param {Function} [err] Error callback (deprecated)
49 * @return {jQuery.Promise}
50 * @return {Function} return.done
51 * @return {String[]} return.done.categories Matched categories
53 getCategoriesByPrefix: function ( prefix, ok, err ) {
56 // Backwards compatibility (< MW 1.20)
60 // Fetch with allpages to only get categories that have a corresponding description page.
61 apiPromise = this.get( {
64 apnamespace: mw.config.get('wgNamespaceIds').category
66 .done( function ( data ) {
68 if ( data.query && data.query.allpages ) {
69 $.each( data.query.allpages, function ( i, category ) {
70 texts.push( new mw.Title( category.title ).getNameText() );
77 return d.promise( { abort: apiPromise.abort } );
82 * Get the categories that a particular page on the wiki belongs to
83 * @param {mw.Title} title
84 * @param {Function} [ok] Success callback (deprecated)
85 * @param {Function} [err] Error callback (deprecated)
86 * @param {boolean} [async=true] Asynchronousness
87 * @return {jQuery.Promise}
88 * @return {Function} return.done
89 * @return {boolean|mw.Title[]} return.done.categories List of category titles or false
90 * if title was not found.
92 getCategories: function ( title, ok, err, async ) {
95 // Backwards compatibility (< MW 1.20)
99 apiPromise = this.get( {
101 titles: title.toString()
103 async: async === undefined ? true : async
105 .done( function ( data ) {
107 if ( data.query && data.query.pages ) {
108 $.each( data.query.pages, function ( id, page ) {
109 if ( page.categories ) {
110 if ( typeof ret !== 'object' ) {
113 $.each( page.categories, function ( i, cat ) {
114 ret.push( new mw.Title( cat.title ) );
123 return d.promise( { abort: apiPromise.abort } );
130 * @mixins mw.Api.plugin.category
133 }( mediaWiki, jQuery ) );