2 * @class mw.Api.plugin.category
6 var msg = 'Use of mediawiki.api callback params is deprecated. Use the Promise instead.';
7 $.extend( mw.Api.prototype, {
9 * Determine if a category exists.
10 * @param {mw.Title} title
11 * @param {Function} [ok] Success callback (deprecated)
12 * @param {Function} [err] Error callback (deprecated)
13 * @return {jQuery.Promise}
14 * @return {Function} return.done
15 * @return {boolean} return.done.isCategory Whether the category exists.
17 isCategory: function ( title, ok, err ) {
21 // Backwards compatibility (< MW 1.20)
23 mw.track( 'mw.deprecate', 'api.cbParam' );
25 d.done( ok ).fail( err );
28 apiPromise = this.get( {
30 titles: title.toString()
32 .done( function ( data ) {
34 if ( data.query && data.query.pages ) {
35 $.each( data.query.pages, function ( id, page ) {
36 if ( page.categoryinfo ) {
45 return d.promise( { abort: apiPromise.abort } );
49 * Get a list of categories that match a certain prefix.
50 * e.g. given "Foo", return "Food", "Foolish people", "Foosball tables" ...
51 * @param {string} prefix Prefix to match.
52 * @param {Function} [ok] Success callback (deprecated)
53 * @param {Function} [err] Error callback (deprecated)
54 * @return {jQuery.Promise}
55 * @return {Function} return.done
56 * @return {String[]} return.done.categories Matched categories
58 getCategoriesByPrefix: function ( prefix, ok, err ) {
62 // Backwards compatibility (< MW 1.20)
64 mw.track( 'mw.deprecate', 'api.cbParam' );
66 d.done( ok ).fail( err );
69 // Fetch with allpages to only get categories that have a corresponding description page.
70 apiPromise = this.get( {
73 apnamespace: mw.config.get( 'wgNamespaceIds' ).category
75 .done( function ( data ) {
77 if ( data.query && data.query.allpages ) {
78 $.each( data.query.allpages, function ( i, category ) {
79 texts.push( new mw.Title( category.title ).getNameText() );
86 return d.promise( { abort: apiPromise.abort } );
91 * Get the categories that a particular page on the wiki belongs to
92 * @param {mw.Title} title
93 * @param {Function} [ok] Success callback (deprecated)
94 * @param {Function} [err] Error callback (deprecated)
95 * @param {boolean} [async=true] Asynchronousness
96 * @return {jQuery.Promise}
97 * @return {Function} return.done
98 * @return {boolean|mw.Title[]} return.done.categories List of category titles or false
99 * if title was not found.
101 getCategories: function ( title, ok, err, async ) {
102 var d = $.Deferred(),
105 // Backwards compatibility (< MW 1.20)
107 mw.track( 'mw.deprecate', 'api.cbParam' );
109 d.done( ok ).fail( err );
112 apiPromise = this.get( {
114 titles: title.toString()
116 async: async === undefined ? true : async
118 .done( function ( data ) {
120 if ( data.query && data.query.pages ) {
121 $.each( data.query.pages, function ( id, page ) {
122 if ( page.categories ) {
123 if ( typeof ret !== 'object' ) {
126 $.each( page.categories, function ( i, cat ) {
127 ret.push( new mw.Title( cat.title ) );
136 return d.promise( { abort: apiPromise.abort } );
143 * @mixins mw.Api.plugin.category
146 }( mediaWiki, jQuery ) );