From: Fomafix Date: Mon, 29 Jan 2018 11:57:00 +0000 (+0100) Subject: mediawiki.api.category: Avoid exceptions X-Git-Tag: 1.31.0-rc.0~724^2 X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=1b525abda4b56bafb531092cadb735ea9a11973c mediawiki.api.category: Avoid exceptions This change avoids some exceptions in processing the API response * Ensure that a "|" in the title is not interpreted as multi value separator. * The key query is missing on empty title. * The key query.pages is missing on title="#" or title="mw:". Change-Id: Idc82412337d0733cc58a78b1e202f5fdfd8f31cd --- diff --git a/resources/src/mediawiki/api/category.js b/resources/src/mediawiki/api/category.js index 04462e3e1d..85df90e912 100644 --- a/resources/src/mediawiki/api/category.js +++ b/resources/src/mediawiki/api/category.js @@ -16,12 +16,16 @@ var apiPromise = this.get( { formatversion: 2, prop: 'categoryinfo', - titles: String( title ) + titles: [ String( title ) ] } ); return apiPromise .then( function ( data ) { - return !!data.query.pages[ 0 ].categoryinfo; + 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 } ); }, @@ -67,13 +71,17 @@ var apiPromise = this.get( { formatversion: 2, prop: 'categories', - titles: String( title ) + titles: [ String( title ) ] } ); return apiPromise .then( function ( data ) { - var page = data.query.pages[ 0 ]; + var page; + if ( !data.query || !data.query.pages ) { + return false; + } + page = data.query.pages[ 0 ]; if ( !page.categories ) { return false; } diff --git a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.category.test.js b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.category.test.js index 8ad1290016..50fa6d1509 100644 --- a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.category.test.js +++ b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.category.test.js @@ -22,4 +22,93 @@ ); } ); } ); + + QUnit.test( '.isCategory("")', function ( assert ) { + this.server.respondWith( /titles=$/, [ + 200, + { 'Content-Type': 'application/json' }, + '{"batchcomplete":true}' + ] ); + return new mw.Api().isCategory( '' ).then( function ( response ) { + assert.equal( response, false ); + } ); + } ); + + QUnit.test( '.isCategory("#")', function ( assert ) { + this.server.respondWith( /titles=%23$/, [ + 200, + { 'Content-Type': 'application/json' }, + '{"batchcomplete":true,"query":{"normalized":[{"fromencoded":false,"from":"#","to":""}]}}' + ] ); + return new mw.Api().isCategory( '#' ).then( function ( response ) { + assert.equal( response, false ); + } ); + } ); + + QUnit.test( '.isCategory("mw:")', function ( assert ) { + this.server.respondWith( /titles=mw%3A$/, [ + 200, + { 'Content-Type': 'application/json' }, + '{"batchcomplete":true,"query":{"interwiki":[{"title":"mw:","iw":"mw"}]}}' + ] ); + return new mw.Api().isCategory( 'mw:' ).then( function ( response ) { + assert.equal( response, false ); + } ); + } ); + + QUnit.test( '.isCategory("|")', function ( assert ) { + this.server.respondWith( /titles=%1F%7C$/, [ + 200, + { 'Content-Type': 'application/json' }, + '{"batchcomplete":true,"query":{"pages":[{"title":"|","invalidreason":"The requested page title contains invalid characters: \\"|\\".","invalid":true}]}}' + ] ); + return new mw.Api().isCategory( '|' ).then( function ( response ) { + assert.equal( response, false ); + } ); + } ); + + QUnit.test( '.getCategories("")', function ( assert ) { + this.server.respondWith( /titles=$/, [ + 200, + { 'Content-Type': 'application/json' }, + '{"batchcomplete":true}' + ] ); + return new mw.Api().getCategories( '' ).then( function ( response ) { + assert.equal( response, false ); + } ); + } ); + + QUnit.test( '.getCategories("#")', function ( assert ) { + this.server.respondWith( /titles=%23$/, [ + 200, + { 'Content-Type': 'application/json' }, + '{"batchcomplete":true,"query":{"normalized":[{"fromencoded":false,"from":"#","to":""}]}}' + ] ); + return new mw.Api().getCategories( '#' ).then( function ( response ) { + assert.equal( response, false ); + } ); + } ); + + QUnit.test( '.getCategories("mw:")', function ( assert ) { + this.server.respondWith( /titles=mw%3A$/, [ + 200, + { 'Content-Type': 'application/json' }, + '{"batchcomplete":true,"query":{"interwiki":[{"title":"mw:","iw":"mw"}]}}' + ] ); + return new mw.Api().getCategories( 'mw:' ).then( function ( response ) { + assert.equal( response, false ); + } ); + } ); + + QUnit.test( '.getCategories("|")', function ( assert ) { + this.server.respondWith( /titles=%1F%7C$/, [ + 200, + { 'Content-Type': 'application/json' }, + '{"batchcomplete":true,"query":{"pages":[{"title":"|","invalidreason":"The requested page title contains invalid characters: \\"|\\".","invalid":true}]}}' + ] ); + return new mw.Api().getCategories( '|' ).then( function ( response ) { + assert.equal( response, false ); + } ); + } ); + }( mediaWiki ) );