summaryrefslogtreecommitdiff
path: root/www/wiki/resources/src/mediawiki/api/category.js
blob: 85df90e912597fe447626d164ac7f0a201991a6d (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
/**
 * @class mw.Api.plugin.category
 */
( function ( mw, $ ) {

	$.extend( mw.Api.prototype, {
		/**
		 * Determine if a category exists.
		 *
		 * @param {mw.Title|string} title
		 * @return {jQuery.Promise}
		 * @return {Function} return.done
		 * @return {boolean} return.done.isCategory Whether the category exists.
		 */
		isCategory: function ( title ) {
			var apiPromise = this.get( {
				formatversion: 2,
				prop: 'categoryinfo',
				titles: [ String( title ) ]
			} );

			return apiPromise
				.then( function ( data ) {
					return !!(
						data.query && // query is missing on title=""
						data.query.pages && // query.pages is missing on title="#" or title="mw:"
						data.query.pages[ 0 ].categoryinfo
					);
				} )
				.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.
		 * @return {jQuery.Promise}
		 * @return {Function} return.done
		 * @return {string[]} return.done.categories Matched categories
		 */
		getCategoriesByPrefix: function ( prefix ) {
			// Fetch with allpages to only get categories that have a corresponding description page.
			var apiPromise = this.get( {
				formatversion: 2,
				list: 'allpages',
				apprefix: prefix,
				apnamespace: mw.config.get( 'wgNamespaceIds' ).category
			} );

			return apiPromise
				.then( function ( data ) {
					return data.query.allpages.map( function ( category ) {
						return new mw.Title( category.title ).getMainText();
					} );
				} )
				.promise( { abort: apiPromise.abort } );
		},

		/**
		 * Get the categories that a particular page on the wiki belongs to.
		 *
		 * @param {mw.Title|string} title
		 * @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 ) {
			var apiPromise = this.get( {
				formatversion: 2,
				prop: 'categories',
				titles: [ String( title ) ]
			} );

			return apiPromise
				.then( function ( data ) {
					var page;

					if ( !data.query || !data.query.pages ) {
						return false;
					}
					page = data.query.pages[ 0 ];
					if ( !page.categories ) {
						return false;
					}
					return page.categories.map( function ( cat ) {
						return new mw.Title( cat.title );
					} );
				} )
				.promise( { abort: apiPromise.abort } );
		}
	} );

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

}( mediaWiki, jQuery ) );