From: Fomafix Date: Thu, 18 Jan 2018 19:23:00 +0000 (+0100) Subject: mediawiki.api.category: Use formatversion=2 and simplify code X-Git-Tag: 1.31.0-rc.0~838^2 X-Git-Url: http://git.cyclocoop.org/%22.%20generer_url_ecrire%28%22sites%22%2C%22%22%29.%20%22?a=commitdiff_plain;h=882293798ba3b903d93b2ac70f73ac5b971d9611;p=lhc%2Fweb%2Fwiklou.git mediawiki.api.category: Use formatversion=2 and simplify code Change-Id: Icc8a193d16cb375593196e9290968f6dac1bc021 --- diff --git a/resources/src/mediawiki/api/category.js b/resources/src/mediawiki/api/category.js index 14077e022f..04462e3e1d 100644 --- a/resources/src/mediawiki/api/category.js +++ b/resources/src/mediawiki/api/category.js @@ -14,21 +14,14 @@ */ isCategory: function ( title ) { var apiPromise = this.get( { + formatversion: 2, prop: 'categoryinfo', titles: String( title ) } ); return apiPromise .then( function ( data ) { - var exists = false; - if ( data.query && data.query.pages ) { - $.each( data.query.pages, function ( id, page ) { - if ( page.categoryinfo ) { - exists = true; - } - } ); - } - return exists; + return !!data.query.pages[ 0 ].categoryinfo; } ) .promise( { abort: apiPromise.abort } ); }, @@ -46,6 +39,7 @@ 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 @@ -53,13 +47,9 @@ return apiPromise .then( function ( data ) { - var texts = []; - if ( data.query && data.query.allpages ) { - $.each( data.query.allpages, function ( i, category ) { - texts.push( new mw.Title( category.title ).getMainText() ); - } ); - } - return texts; + return data.query.allpages.map( function ( category ) { + return new mw.Title( category.title ).getMainText(); + } ); } ) .promise( { abort: apiPromise.abort } ); }, @@ -75,26 +65,21 @@ */ getCategories: function ( title ) { var apiPromise = this.get( { + formatversion: 2, prop: 'categories', titles: String( title ) } ); return apiPromise .then( function ( data ) { - var titles = false; - if ( data.query && data.query.pages ) { - $.each( data.query.pages, function ( id, page ) { - if ( page.categories ) { - if ( titles === false ) { - titles = []; - } - $.each( page.categories, function ( i, cat ) { - titles.push( new mw.Title( cat.title ) ); - } ); - } - } ); + var page = data.query.pages[ 0 ]; + + if ( !page.categories ) { + return false; } - return titles; + return page.categories.map( function ( cat ) { + return new mw.Title( cat.title ); + } ); } ) .promise( { abort: apiPromise.abort } ); }