Debian Codegraph @

resources/src/mediawiki.api/category.js

raw symbols SCIP
1 ( function () {
2
3 Object.assign( mw.Api.prototype, /** @lends mw.Api.prototype */ {
4 /**
5 * Determine if a category exists.
6 *
7 * @param {mw.Title|string} title
8 * @return {jQuery.Promise<boolean>} Promise that resolves with a boolean indicating
9 * whether the category exists.
10 */
11 isCategory: function ( title ) {
12 const apiPromise = this.get( {
13 formatversion: 2,
14 prop: 'categoryinfo',
15 titles: [ String( title ) ]
16 } );
17
18 return apiPromise
19 .then( ( data ) => !!(
20 data.query && // query is missing on title=""
21 data.query.pages && // query.pages is missing on title="#" or title="mw:"
22 data.query.pages[ 0 ].categoryinfo
23 ) )
24 .promise( { abort: apiPromise.abort } );
25 },
26
27 /**
28 * Get a list of categories that match a certain prefix.
29 *
30 * E.g. given "Foo", return "Food", "Foolish people", "Foosball tables"...
31 *
32 * @param {string} prefix Prefix to match.
33 * @return {jQuery.Promise<string[]>} Promise that resolves with an array of matched categories
34 */
35 getCategoriesByPrefix: function ( prefix ) {
36 // Fetch with allpages to only get categories that have a corresponding description page.
37 const apiPromise = this.get( {
38 formatversion: 2,
39 list: 'allpages',
40 apprefix: prefix,
41 apnamespace: mw.config.get( 'wgNamespaceIds' ).category
42 } );
43
44 return apiPromise
45 .then( ( data ) => data.query.allpages.map( ( category ) => new mw.Title( category.title ).getMainText() ) )
46 .promise( { abort: apiPromise.abort } );
47 },
48
49 /**
50 * Get the categories that a particular page on the wiki belongs to.
51 *
52 * @param {mw.Title|string} title
53 * @return {jQuery.Promise<mw.Title[]|false>} Promise that resolves with an array of
54 * category titles, or with false if the title was not found.
55 */
56 getCategories: function ( title ) {
57 const apiPromise = this.get( {
58 formatversion: 2,
59 prop: 'categories',
60 titles: [ String( title ) ]
61 } );
62
63 return apiPromise
64 .then( ( data ) => {
65 if ( !data.query || !data.query.pages ) {
66 return false;
67 }
68 const page = data.query.pages[ 0 ];
69 if ( !page.categories ) {
70 return false;
71 }
72 return page.categories.map( ( cat ) => new mw.Title( cat.title ) );
73 } )
74 .promise( { abort: apiPromise.abort } );
75 }
76 } );
77
78 }() );