Jeroen De Dauw has uploaded a new change for review. https://gerrit.wikimedia.org/r/58874
Change subject: Remove usage of ValueParser Result interface ...................................................................... Remove usage of ValueParser Result interface This depends on https://gerrit.wikimedia.org/r/#/c/58867/ Change-Id: I03bc7f385264007c606fa4466f6fba0f0e221258 --- M DataModel/DataModel/Entity/EntityId.php M client/includes/WikibaseLibrary.php M lib/includes/parsers/EntityIdParser.php M lib/tests/phpunit/DataTypesTest.php M lib/tests/phpunit/parsers/EntityIdParserTest.php M repo/includes/ClaimSaver.php M repo/includes/api/CreateClaim.php M repo/includes/api/SetQualifier.php 8 files changed, 76 insertions(+), 50 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/74/58874/1 diff --git a/DataModel/DataModel/Entity/EntityId.php b/DataModel/DataModel/Entity/EntityId.php index 2e32072..d7f4a46 100644 --- a/DataModel/DataModel/Entity/EntityId.php +++ b/DataModel/DataModel/Entity/EntityId.php @@ -3,6 +3,7 @@ namespace Wikibase; use MWException; +use ValueParsers\ParseException; /** * Represents an ID of an Entity. @@ -62,8 +63,13 @@ */ public static function newFromPrefixedId( $prefixedId ) { $libRegistry = new LibRegistry( Settings::singleton() ); - $result = $libRegistry->getEntityIdParser()->parse( $prefixedId ); - return $result->isValid() ? $result->getValue() : null; + + try { + return $libRegistry->getEntityIdParser()->parse( $prefixedId ); + } + catch ( ParseException $parseException ) { + return null; + } } /** diff --git a/client/includes/WikibaseLibrary.php b/client/includes/WikibaseLibrary.php index ed0efe7..cfb0045 100644 --- a/client/includes/WikibaseLibrary.php +++ b/client/includes/WikibaseLibrary.php @@ -24,6 +24,8 @@ * @author Jens Ohlig < jens.oh...@wikimedia.de > */ +use ValueParsers\ParseException; + class Scribunto_LuaWikibaseLibrary extends Scribunto_LuaLibraryBase { /** @@ -52,15 +54,18 @@ $this->checkType( 'getEntity', 1, $prefixedEntityId, 'string' ); $prefixedEntityId = trim( $prefixedEntityId ); $libRegistry = new \Wikibase\LibRegistry( \Wikibase\Settings::singleton() ); - $parseResult = $libRegistry->getEntityIdParser()->parse( $prefixedEntityId ); - if ( !$parseResult->isValid() ) { + try { + $entityId = $libRegistry->getEntityIdParser()->parse( $prefixedEntityId ); + } + catch ( ParseException $parseException ) { throw $this->getEngine()->newException( 'wikibase-error-invalid-entity-id' ); } $entityObject = Wikibase\ClientStoreFactory::getStore()->getEntityLookup()->getEntity( - Wikibase\EntityId::newFromPrefixedId( $prefixedEntityId ) + $entityId ); + if ( $entityObject == null ) { return array( null ); } diff --git a/lib/includes/parsers/EntityIdParser.php b/lib/includes/parsers/EntityIdParser.php index 4323f1d..d2e5681 100644 --- a/lib/includes/parsers/EntityIdParser.php +++ b/lib/includes/parsers/EntityIdParser.php @@ -1,9 +1,10 @@ <?php namespace Wikibase\Lib; -use ValueParsers\Result; +use ValueParsers\ParseException; use ValueParsers\StringValueParser; use ValueParsers\ParserOptions; +use Wikibase\EntityId; /** * Parser that parses entity id strings into EntityId objects. @@ -72,22 +73,23 @@ * * @param string $value * - * @return Result + * @return EntityId + * @throws ParseException */ protected function stringParse( $value ) { $idParts = $this->getIdParts( $value ); if ( count( $idParts ) < 3 || !ctype_digit( $idParts[2] ) ) { - return Result::newErrorText( 'Not an EntityId' ); + throw new ParseException( 'Not an EntityId' ); } $entityType = $this->getEntityTypeForPrefix( $idParts[1] ); if ( $entityType === null ) { - return Result::newErrorText( 'EntityId has an invalid prefix' ); + throw new ParseException( 'EntityId has an invalid prefix' ); } - return Result::newSuccess( new \Wikibase\EntityId( $entityType, (int)$idParts[2] ) ); + return new EntityId( $entityType, (int)$idParts[2] ); } /** diff --git a/lib/tests/phpunit/DataTypesTest.php b/lib/tests/phpunit/DataTypesTest.php index e1e56ea..978fe10 100644 --- a/lib/tests/phpunit/DataTypesTest.php +++ b/lib/tests/phpunit/DataTypesTest.php @@ -49,9 +49,7 @@ $result = $parser->parse( $expected->getPrefixedId() ); - $this->assertTrue( $result->isValid() ); - - $this->assertTrue( $expected->equals( $result->getValue() ) ); + $this->assertTrue( $expected->equals( $result ) ); } } diff --git a/lib/tests/phpunit/parsers/EntityIdParserTest.php b/lib/tests/phpunit/parsers/EntityIdParserTest.php index d5d2b02..bbf7eec 100644 --- a/lib/tests/phpunit/parsers/EntityIdParserTest.php +++ b/lib/tests/phpunit/parsers/EntityIdParserTest.php @@ -1,7 +1,10 @@ <?php namespace Wikibase\Lib\Test; -use ValueParsers\Result; + +use ValueParsers\ParserOptions; +use Wikibase\EntityId; +use Wikibase\Lib\EntityIdParser; /** * Unit test Wikibase\Lib\EntityIdParser class. @@ -40,11 +43,11 @@ * * @since 0.4 * - * @return \ValueParsers\ParserOptions + * @return ParserOptions */ protected function newParserOptions() { - return new \ValueParsers\ParserOptions( array( - \Wikibase\Lib\EntityIdParser::OPT_PREFIX_MAP => array( + return new ParserOptions( array( + EntityIdParser::OPT_PREFIX_MAP => array( 'a' => 'entity-type-a', 'b' => 'entity-type-b', 'x' => 'entity-type-a', @@ -63,10 +66,10 @@ * * @return array */ - public function parseProvider() { + public function validInputProvider() { $argLists = array(); - $parser = new \Wikibase\Lib\EntityIdParser( $this->newParserOptions() ); + $parser = new EntityIdParser( $this->newParserOptions() ); $valid = array( 'a1' => array( 'entity-type-a', 1 ), @@ -84,9 +87,15 @@ ); foreach ( $valid as $value => $expected ) { - $expected = new \Wikibase\EntityId( $expected[0], $expected[1] ); - $argLists[] = array( (string)$value, Result::newSuccess( $expected ), $parser ); + $expected = new EntityId( $expected[0], $expected[1] ); + $argLists[] = array( (string)$value, $expected, $parser ); } + + return array_merge( $argLists ); + } + + public function invalidInputProvider() { + $argLists = parent::invalidInputProvider(); $invalid = array( 'foo', @@ -101,10 +110,10 @@ ); foreach ( $invalid as $value ) { - $argLists[] = array( $value, Result::newErrorText( '' ), $parser ); + $argLists[] = array( $value ); } - return array_merge( $argLists ); + return $argLists; } /** diff --git a/repo/includes/ClaimSaver.php b/repo/includes/ClaimSaver.php index a9b1968..6f54708 100644 --- a/repo/includes/ClaimSaver.php +++ b/repo/includes/ClaimSaver.php @@ -5,6 +5,7 @@ use User; use MWException; use Status; +use ValueParsers\ParseException; use Wikibase\ExceptionWithCode; /** @@ -99,15 +100,15 @@ $libRegistry = new \Wikibase\LibRegistry( \Wikibase\Settings::singleton() ); $idParser = $libRegistry->getEntityIdParser(); - $parseResult = $idParser->parse( $entityId ); - - if ( $parseResult->isValid() ) { - $entityId = $parseResult->getValue(); - assert( $entityId instanceof EntityId ); - return $entityId; + try { + $entityId = $idParser->parse( $entityId ); + } + catch ( ParseException $parseException ) { + throw new ExceptionWithCode( $parseException->getMessage(), 'setclaim-invalid-guid' ); } - throw new ExceptionWithCode( $parseResult->getError()->getText(), 'setclaim-invalid-guid' ); + assert( $entityId instanceof EntityId ); + return $entityId; } /** diff --git a/repo/includes/api/CreateClaim.php b/repo/includes/api/CreateClaim.php index 6f59e9c..80da322 100644 --- a/repo/includes/api/CreateClaim.php +++ b/repo/includes/api/CreateClaim.php @@ -4,6 +4,7 @@ use ApiBase, MWException; +use ValueParsers\ParseException; use Wikibase\EntityId; use Wikibase\Entity; use Wikibase\EntityContent; @@ -172,16 +173,17 @@ $params = $this->extractRequestParams(); $factory = new SnakFactory(); - $libRegistry = new LibRegistry( Settings::singleton() ); - $parseResult = $libRegistry->getEntityIdParser()->parse( $params['property'] ); - if ( !$parseResult->isValid() ) { - throw new MWException( $parseResult->getError()->getText() ); + try { + $entityId = $libRegistry->getEntityIdParser()->parse( $params['property'] ); + } + catch ( ParseException $parseException ) { + throw new MWException( $parseException->getMessage(), 'setclaim-invalid-guid' ); } return $factory->newSnak( - $parseResult->getValue(), + $entityId, $params['snaktype'], isset( $params['value'] ) ? \FormatJson::decode( $params['value'], true ) : null ); diff --git a/repo/includes/api/SetQualifier.php b/repo/includes/api/SetQualifier.php index d533a39..15dec96 100644 --- a/repo/includes/api/SetQualifier.php +++ b/repo/includes/api/SetQualifier.php @@ -5,6 +5,7 @@ use ApiBase; use MWException; +use ValueParsers\ParseException; use Wikibase\EntityContent; use Wikibase\EntityId; use Wikibase\Entity; @@ -194,14 +195,7 @@ $propertyId = isset( $params['property'] ) ? $params['property'] : $snak->getPropertyId(); if ( is_string( $propertyId ) ) { - $libRegistry = new LibRegistry( Settings::singleton() ); - $parseResult = $libRegistry->getEntityIdParser()->parse( $propertyId ); - - if ( !$parseResult->isValid() ) { - $this->dieUsage( $parseResult->getError()->getText(), 'invalid-property-id' ); - } - - $propertyId = $parseResult->getValue(); + $propertyId = $this->getParsedEntityId( $propertyId, 'invalid-property-id' ); } $snakType = isset( $params['snaktype'] ) ? $params['snaktype'] : $snak->getType(); @@ -222,6 +216,20 @@ return $qualifiers->addSnak( $newQualifier ); } + protected function getParsedEntityId( $prefixedId, $errorCode ) { + $libRegistry = new LibRegistry( Settings::singleton() ); + + try { + $entityId = $libRegistry->getEntityIdParser()->parse( $prefixedId ); + } + catch ( ParseException $parseException ) { + $this->dieUsage( $parseException->getMessage(), $errorCode ); + return null; + } + + return $entityId; + } + /** * @since 0.3 * @@ -235,15 +243,10 @@ $params = $this->extractRequestParams(); $factory = new SnakFactory(); - $libRegistry = new LibRegistry( Settings::singleton() ); - $parseResult = $libRegistry->getEntityIdParser()->parse( $params['property'] ); - - if ( !$parseResult->isValid() ) { - throw new MWException( $parseResult->getError()->getText() ); - } + $propertyId = $this->getParsedEntityId( $params['property'], 'invalid-property-id' ); $newQualifier = $factory->newSnak( - $parseResult->getValue(), + $propertyId, $params['snaktype'], isset( $params['value'] ) ? $params['value'] : null ); -- To view, visit https://gerrit.wikimedia.org/r/58874 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I03bc7f385264007c606fa4466f6fba0f0e221258 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Jeroen De Dauw <jeroended...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits