summaryrefslogtreecommitdiff
path: root/resources/mediawiki.api/mediawiki.api.category.js
blob: 98a9c54b21b48ba808f2d959d8f8fbbd0ea3ff38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
 * @class mw.Api.plugin.category
 */
( function ( mw, $ ) {

	$.extend( mw.Api.prototype, {
		/**
		 * Determine if a category exists.
		 * @param {mw.Title} title
		 * @param {Function} [ok] Success callback (deprecated)
		 * @param {Function} [err] Error callback (deprecated)
		 * @return {jQuery.Promise}
		 * @return {Function} return.done
		 * @return {boolean} return.done.isCategory Whether the category exists.
		 */
		isCategory: function ( title, ok, err ) {
			var d = $.Deferred(),
				apiPromise;

			// Backwards compatibility (< MW 1.20)
			d.done( ok ).fail( err );

			apiPromise = this.get( {
					prop: 'categoryinfo',
					titles: title.toString()
				} )
				.done( function ( data ) {
					var exists = false;
					if ( data.query && data.query.pages ) {
						$.each( data.query.pages, function ( id, page ) {
							if ( page.categoryinfo ) {
								exists = true;
							}
						} );
					}
					d.resolve( exists );
				})
				.fail( d.reject );

			return d.promise( { abort: apiPromise.abort } );
		},

		/**
		 * Get a list of categories that match a certain prefix.
		 *   e.g. given "Foo", return "Food", "Foolish people", "Foosball tables" ...
		 * @param {string} prefix Prefix to match.
		 * @param {Function} [ok] Success callback (deprecated)
		 * @param {Function} [err] Error callback (deprecated)
		 * @return {jQuery.Promise}
		 * @return {Function} return.done
		 * @return {String[]} return.done.categories Matched categories
		 */
		getCategoriesByPrefix: function ( prefix, ok, err ) {
			var d = $.Deferred(),
				apiPromise;

			// Backwards compatibility (< MW 1.20)
			d.done( ok ).fail( err );

			// Fetch with allpages to only get categories that have a corresponding description page.
			apiPromise = this.get( {
					list: 'allpages',
					apprefix: prefix,
					apnamespace: mw.config.get('wgNamespaceIds').category
				} )
				.done( function ( data ) {
					var texts = [];
					if ( data.query && data.query.allpages ) {
						$.each( data.query.allpages, function ( i, category ) {
							texts.push( new mw.Title( category.title ).getNameText() );
						} );
					}
					d.resolve( texts );
				})
				.fail( d.reject );

			return d.promise( { abort: apiPromise.abort } );
		},


		/**
		 * Get the categories that a particular page on the wiki belongs to
		 * @param {mw.Title} title
		 * @param {Function} [ok] Success callback (deprecated)
		 * @param {Function} [err] Error callback (deprecated)
		 * @param {boolean} [async=true] Asynchronousness
		 * @return {jQuery.Promise}
		 * @return {Function} return.done
		 * @return {boolean|mw.Title[]} return.done.categories List of category titles or false
		 *  if title was not found.
		 */
		getCategories: function ( title, ok, err, async ) {
			var d = $.Deferred(),
				apiPromise;

			// Backwards compatibility (< MW 1.20)
			d.done( ok ).fail( err );

			apiPromise = this.get( {
					prop: 'categories',
					titles: title.toString()
				}, {
					async: async === undefined ? true : async
				} )
				.done( function ( data ) {
					var ret = false;
					if ( data.query && data.query.pages ) {
						$.each( data.query.pages, function ( id, page ) {
							if ( page.categories ) {
								if ( typeof ret !== 'object' ) {
									ret = [];
								}
								$.each( page.categories, function ( i, cat ) {
									ret.push( new mw.Title( cat.title ) );
								} );
							}
						} );
					}
					d.resolve( ret );
				} )
				.fail( d.reject );

			return d.promise( { abort: apiPromise.abort } );
		}

	} );

	/**
	 * @class mw.Api
	 * @mixins mw.Api.plugin.category
	 */

}( mediaWiki, jQuery ) );