| 1 |
( function () { |
| 2 |
|
| 3 |
Object.assign( mw.Api.prototype, { |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 && |
| 21 |
data.query.pages && |
| 22 |
data.query.pages[ 0 ].categoryinfo |
| 23 |
) ) |
| 24 |
.promise( { abort: apiPromise.abort } ); |
| 25 |
}, |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
getCategoriesByPrefix: function ( prefix ) { |
| 36 |
|
| 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 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 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 |
}() ); |