jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/328357 )
Change subject: Use ChangeOpDeserializer callbacks in Api\EditEntity ...................................................................... Use ChangeOpDeserializer callbacks in Api\EditEntity Bug: T152491 Change-Id: Id1e5a8f71924fced1a852de86d4e9aee070d1cf6 --- M lib/includes/EntityTypeDefinitions.php M lib/tests/phpunit/EntityTypeDefinitionsTest.php M repo/includes/Api/EditEntity.php M repo/includes/WikibaseRepo.php M repo/tests/phpunit/includes/WikibaseRepoTest.php 5 files changed, 79 insertions(+), 17 deletions(-) Approvals: WMDE-leszek: Looks good to me, but someone else must approve jenkins-bot: Verified Thiemo Mättig (WMDE): Looks good to me, approved diff --git a/lib/includes/EntityTypeDefinitions.php b/lib/includes/EntityTypeDefinitions.php index aae731b..336f534 100644 --- a/lib/includes/EntityTypeDefinitions.php +++ b/lib/includes/EntityTypeDefinitions.php @@ -153,4 +153,15 @@ return $this->getMapForDefinitionField( 'entity-id-composer-callback' ); } + /** + * @return callable[] An array mapping entity type identifiers + * to callables instantiating ChangeOpDeserializer objects + * capable of turning serialization of the change (in array form) + * to a ChangeOp object representing the change. + * Not guaranteed to contain all entity types. + */ + public function getChangeOpDeserializerCallbacks() { + return $this->getMapForDefinitionField( 'changeop-deserializer-callback' ); + } + } diff --git a/lib/tests/phpunit/EntityTypeDefinitionsTest.php b/lib/tests/phpunit/EntityTypeDefinitionsTest.php index 5c4ca91..d5a8fb0 100644 --- a/lib/tests/phpunit/EntityTypeDefinitionsTest.php +++ b/lib/tests/phpunit/EntityTypeDefinitionsTest.php @@ -32,6 +32,7 @@ 'entity-id-pattern' => 'foo-id-pattern', 'entity-id-builder' => 'new-foo-id', 'entity-id-composer-callback' => 'new-composed-foo-id', + 'changeop-deserializer-callback' => 'new-changeop-deserializer-callback' ), 'bar' => array( 'serializer-factory-callback' => 'bar-serializer', @@ -157,4 +158,13 @@ ], $definitions->getEntityIdComposers() ); } + public function testGetChangeOpDeserializerCallbacks() { + $definitions = new EntityTypeDefinitions( $this->getDefinitions() ); + + $this->assertSame( + [ 'foo' => 'new-changeop-deserializer-callback' ], + $definitions->getChangeOpDeserializerCallbacks() + ); + } + } diff --git a/repo/includes/Api/EditEntity.php b/repo/includes/Api/EditEntity.php index 8d22edb..8210dfd 100644 --- a/repo/includes/Api/EditEntity.php +++ b/repo/includes/Api/EditEntity.php @@ -87,6 +87,11 @@ private $entityFactory; /** + * @var callable[] + */ + private $changeOpDeserializerCallbacks; + + /** * @see ModifyEntity::__construct * * @param ApiMain $mainModule @@ -109,6 +114,7 @@ $this->termChangeOpFactory = $changeOpFactoryProvider->getFingerprintChangeOpFactory(); $this->statementChangeOpFactory = $changeOpFactoryProvider->getStatementChangeOpFactory(); $this->siteLinkChangeOpFactory = $changeOpFactoryProvider->getSiteLinkChangeOpFactory(); + $this->changeOpDeserializerCallbacks = $wikibaseRepo->getChangeOpDeserializerCallbacks(); } /** @@ -274,56 +280,67 @@ } /** - * @param array $data + * @param array $changeRequest an array of data to apply. For example: + * [ 'label' => [ 'zh' => [ 'remove' ], 'de' => [ 'value' => 'Foo' ] ] ] * @param EntityDocument $entity * * @throws ApiUsageException * @return ChangeOps */ - private function getChangeOps( array $data, EntityDocument $entity ) { + private function getChangeOps( array $changeRequest, EntityDocument $entity ) { $changeOps = new ChangeOps(); + $type = $entity->getType(); + if ( isset( $this->changeOpDeserializerCallbacks[$type] ) ) { + $changeOpDeserializer = call_user_func( + $this->changeOpDeserializerCallbacks[$type] + ); + $changeOp = $changeOpDeserializer->createEntityChangeOp( $changeRequest ); + $changeOps->add( $changeOp ); + // Shorten out + return $changeOps; + } //FIXME: Use a ChangeOpBuilder so we can batch fingerprint ops etc, // for more efficient validation! - if ( array_key_exists( 'labels', $data ) ) { + if ( array_key_exists( 'labels', $changeRequest ) ) { if ( !( $entity instanceof LabelsProvider ) ) { $this->errorReporter->dieError( 'The given entity cannot contain labels', 'not-supported' ); } - $this->assertArray( $data['labels'], 'List of labels must be an array' ); - $changeOps->add( $this->getLabelChangeOps( $data['labels'] ) ); + $this->assertArray( $changeRequest['labels'], 'List of labels must be an array' ); + $changeOps->add( $this->getLabelChangeOps( $changeRequest['labels'] ) ); } - if ( array_key_exists( 'descriptions', $data ) ) { + if ( array_key_exists( 'descriptions', $changeRequest ) ) { if ( !( $entity instanceof DescriptionsProvider ) ) { $this->errorReporter->dieError( 'The given entity cannot contain descriptions', 'not-supported' ); } - $this->assertArray( $data['descriptions'], 'List of descriptions must be an array' ); - $changeOps->add( $this->getDescriptionChangeOps( $data['descriptions'] ) ); + $this->assertArray( $changeRequest['descriptions'], 'List of descriptions must be an array' ); + $changeOps->add( $this->getDescriptionChangeOps( $changeRequest['descriptions'] ) ); } - if ( array_key_exists( 'aliases', $data ) ) { + if ( array_key_exists( 'aliases', $changeRequest ) ) { if ( !( $entity instanceof AliasesProvider ) ) { $this->errorReporter->dieError( 'The given entity cannot contain aliases', 'not-supported' ); } - $this->assertArray( $data['aliases'], 'List of aliases must be an array' ); - $changeOps->add( $this->getAliasesChangeOps( $data['aliases'] ) ); + $this->assertArray( $changeRequest['aliases'], 'List of aliases must be an array' ); + $changeOps->add( $this->getAliasesChangeOps( $changeRequest['aliases'] ) ); } - if ( array_key_exists( 'sitelinks', $data ) ) { + if ( array_key_exists( 'sitelinks', $changeRequest ) ) { if ( !( $entity instanceof Item ) ) { $this->errorReporter->dieError( 'Non Items cannot have sitelinks', 'not-supported' ); } - $this->assertArray( $data['sitelinks'], 'List of sitelinks must be an array' ); - $changeOps->add( $this->getSiteLinksChangeOps( $data['sitelinks'], $entity ) ); + $this->assertArray( $changeRequest['sitelinks'], 'List of sitelinks must be an array' ); + $changeOps->add( $this->getSiteLinksChangeOps( $changeRequest['sitelinks'], $entity ) ); } - if ( array_key_exists( 'claims', $data ) ) { + if ( array_key_exists( 'claims', $changeRequest ) ) { if ( !( $entity instanceof StatementListProvider ) ) { $this->errorReporter->dieError( 'The given entity cannot contain statements', 'not-supported' ); } - $this->assertArray( $data['claims'], 'List of claims must be an array' ); - $changeOps->add( $this->getClaimsChangeOps( $data['claims'] ) ); + $this->assertArray( $changeRequest['claims'], 'List of claims must be an array' ); + $changeOps->add( $this->getClaimsChangeOps( $changeRequest['claims'] ) ); } return $changeOps; diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php index f89ddcc..3d9762c 100644 --- a/repo/includes/WikibaseRepo.php +++ b/repo/includes/WikibaseRepo.php @@ -1839,4 +1839,13 @@ return $storage; } + /** + * @see EntityTypeDefinitions::getChangeOpDeserializerCallbacks + * + * @return callable[] + */ + public function getChangeOpDeserializerCallbacks() { + return $this->entityTypeDefinitions->getChangeOpDeserializerCallbacks(); + } + } diff --git a/repo/tests/phpunit/includes/WikibaseRepoTest.php b/repo/tests/phpunit/includes/WikibaseRepoTest.php index 55ed17c..d929722 100644 --- a/repo/tests/phpunit/includes/WikibaseRepoTest.php +++ b/repo/tests/phpunit/includes/WikibaseRepoTest.php @@ -614,4 +614,19 @@ $this->assertEquals( $expected, $deserialized ); } + public function testGetChangeOpDeserializerCallbacks() { + $wikibaseRepo = $this->getWikibaseRepo( + [ + 'foo' => [ + 'changeop-deserializer-callback' => 'new-changeop-deserializer-callback' + ] + ] + ); + $changeOpsCallbacks = $wikibaseRepo->getChangeOpDeserializerCallbacks(); + $expected = [ + 'foo' => 'new-changeop-deserializer-callback' + ]; + $this->assertSame( $expected, $changeOpsCallbacks ); + } + } -- To view, visit https://gerrit.wikimedia.org/r/328357 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Id1e5a8f71924fced1a852de86d4e9aee070d1cf6 Gerrit-PatchSet: 19 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Ladsgroup <ladsgr...@gmail.com> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Jakob <jakob.warkot...@wikimedia.de> Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com> Gerrit-Reviewer: Ladsgroup <ladsgr...@gmail.com> Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: WMDE-leszek <leszek.mani...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits