From b8f97035cefbe63f2cb8ce1aa2f2d47f57c008c9 Mon Sep 17 00:00:00 2001 From: Fomafix Date: Sun, 21 Jan 2018 21:58:59 +0100 Subject: [PATCH] API: Allow to pass whitespaces in MultiValue This allows to response with an invalidreason instead silently ignore the parameter. Example request: api.php?format=json&action=query&titles=%20 Response before this change: { "batchcomplete": "" } Response with this change: { "batchcomplete": "", "query": { "pages": { "-1": { "title": " ", "invalidreason": "The requested page title is empty or contains only the name of a namespace.", "invalid": "" } } } } Bug: T185846 Change-Id: I6fdaf32792a0e6e37b08176f975c10607093351b --- includes/api/ApiBase.php | 2 +- tests/phpunit/includes/api/query/ApiQueryTest.php | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index 1a126dba52..2d2f48d592 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -1408,7 +1408,7 @@ abstract class ApiBase extends ContextSource { protected function parseMultiValue( $valueName, $value, $allowMultiple, $allowedValues, $allSpecifier = null, $limit1 = null, $limit2 = null ) { - if ( ( trim( $value ) === '' || trim( $value ) === "\x1f" ) && $allowMultiple ) { + if ( ( $value === '' || $value === "\x1f" ) && $allowMultiple ) { return []; } $limit1 = $limit1 ?: self::LIMIT_SML1; diff --git a/tests/phpunit/includes/api/query/ApiQueryTest.php b/tests/phpunit/includes/api/query/ApiQueryTest.php index 8026e5444e..88a2e62f46 100644 --- a/tests/phpunit/includes/api/query/ApiQueryTest.php +++ b/tests/phpunit/includes/api/query/ApiQueryTest.php @@ -81,6 +81,19 @@ class ApiQueryTest extends ApiTestCase { $this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] ); } + public function testTitlesWithWhitespaces() { + $data = $this->doApiRequest( [ + 'action' => 'query', + 'titles' => ' ' + ] ); + + $this->assertArrayHasKey( 'query', $data[0] ); + $this->assertArrayHasKey( 'pages', $data[0]['query'] ); + $this->assertEquals( 1, count( $data[0]['query']['pages'] ) ); + $this->assertArrayHasKey( -1, $data[0]['query']['pages'] ); + $this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] ); + } + /** * Test the ApiBase::titlePartToKey function * -- 2.20.1