From 882293798ba3b903d93b2ac70f73ac5b971d9611 Mon Sep 17 00:00:00 2001 From: Fomafix Date: Thu, 18 Jan 2018 20:23:00 +0100 Subject: [PATCH] mediawiki.api.category: Use formatversion=2 and simplify code Change-Id: Icc8a193d16cb375593196e9290968f6dac1bc021 --- resources/src/mediawiki/api/category.js | 43 ++++++++----------------- 1 file changed, 14 insertions(+), 29 deletions(-) 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 } ); } -- 2.20.1