Bene has uploaded a new change for review. https://gerrit.wikimedia.org/r/156044
Change subject: Refactor ReferencedEntitiesFinder ...................................................................... Refactor ReferencedEntitiesFinder This patch improves the readability and structure in ReferencedEntitiesFinder. It was split from https://gerrit.wikimedia.org/r/#/c/155896/ Change-Id: Ia8ccfe2db6fc857b3d10147326adaefd3055e255 --- M lib/includes/ReferencedEntitiesFinder.php M lib/tests/phpunit/ReferencedEntitiesFinderTest.php M repo/includes/EntityView.php 3 files changed, 28 insertions(+), 91 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/44/156044/1 diff --git a/lib/includes/ReferencedEntitiesFinder.php b/lib/includes/ReferencedEntitiesFinder.php index b629658..6ead007 100644 --- a/lib/includes/ReferencedEntitiesFinder.php +++ b/lib/includes/ReferencedEntitiesFinder.php @@ -15,8 +15,14 @@ * @author Daniel Werner < daniel.a.r.wer...@gmail.com > * @author Katie Filbert * @author Daniel Kinzler + * @author Bene* < benestar.wikime...@gmail.com > */ class ReferencedEntitiesFinder { + + /** + * @var EntityId[] serialized entity ids pointing to entity id objects + */ + private $foundEntityIds; /** * Finds linked entities within a set of snaks. @@ -26,51 +32,33 @@ * @return EntityId[] */ public function findSnakLinks( array $snaks ) { - $foundEntities = array(); + $this->foundEntityIds = array(); foreach ( $snaks as $snak ) { - // all of the Snak's properties are referenced entities, add them: - $propertyId = $snak->getPropertyId(); - $foundEntities[ $propertyId->getSerialization() ] = $propertyId; - - // PropertyValueSnaks might have a value referencing an Entity, find those as well: - if( $snak instanceof PropertyValueSnak ) { - $snakValue = $snak->getDataValue(); - - if( $snakValue === null ) { - // shouldn't ever run into this, but make sure! - continue; - } - - $entitiesInSnakDataValue = $this->findDataValueLinks( $snakValue ); - $foundEntities = array_merge( $foundEntities, $entitiesInSnakDataValue ); - } + $this->handleSnak( $snak ); } - return $foundEntities; + return $this->foundEntityIds; } - /** - * Finds linked entities within a given data value. - * - * @since 0.5 - * - * @param DataValue $dataValue - * @return EntityId[] - */ - public function findDataValueLinks( DataValue $dataValue ) { - switch( $dataValue->getType() ) { - case 'wikibase-entityid': - if( $dataValue instanceof EntityIdValue ) { - $entityId = $dataValue->getEntityId(); - return array( - $entityId->getSerialization() => $entityId ); - } - break; + private function handleSnak( Snak $snak ) { + // all of the Snak's properties are referenced entities + $this->addEntityId( $snak->getPropertyId() ); - // TODO: we might want to allow extensions to add handling for their custom - // data value types here. Either use a hook or a proper registration for that. + // PropertyValueSnaks might have a value referencing an Entity + if( $snak instanceof PropertyValueSnak ) { + $this->handleDataValue( $snak->getDataValue() ); } - return array(); } + + private function handleDataValue( DataValue $dataValue ) { + if ( $dataValue instanceof EntityIdValue ) { + $this->addEntityId( $dataValue->getEntityId() ); + } + } + + private function addEntityId( EntityId $entityId ) { + $this->foundEntityIds[$entityId->getSerialization()] = $entityId; + } + } diff --git a/lib/tests/phpunit/ReferencedEntitiesFinderTest.php b/lib/tests/phpunit/ReferencedEntitiesFinderTest.php index 2b01ebd..3c4012d 100644 --- a/lib/tests/phpunit/ReferencedEntitiesFinderTest.php +++ b/lib/tests/phpunit/ReferencedEntitiesFinderTest.php @@ -103,55 +103,4 @@ $this->assertEquals( $expected, $actual, $message ); } - public function dataValuesProvider() { - $p21 = new PropertyId( 'p11' ); - $q42 = new ItemId( 'q42' ); - $stringValue = new StringValue( 'q1337' ); - - return array( - $this->dataValuesTestCaseFromEntity( $p21 ), - $this->dataValuesTestCaseFromEntity( $q42 ), - array( - $stringValue, - array(), - 'StringValue without references' - ) - ); - } - - /** - * Returns a test definition suitable for "testFindDataValueLinks". - * - * @param string $entity - * @return array - */ - private function dataValuesTestCaseFromEntity( $entity ) { - $definition = array( - new EntityIdValue( $entity ), - array( $entity ), - $entity->getEntityType() - ); - return $definition; - } - - /** - * @dataProvider dataValuesProvider - * - * @param DataValue $dataValue - * @param array $expected - * @param string $message - */ - public function testFindDataValueLinks( DataValue $dataValue, array $expected, $message = '' ) { - $linkFinder = new ReferencedEntitiesFinder(); - - $actual = $linkFinder->findDataValueLinks( $dataValue ); - - $expected = array_values( $expected ); - $actual = array_values( $actual ); - - asort( $expected ); - asort( $actual ); - - $this->assertEquals( $expected, $actual, $message ); - } } diff --git a/repo/includes/EntityView.php b/repo/includes/EntityView.php index dda755f..ce7fb5f 100644 --- a/repo/includes/EntityView.php +++ b/repo/includes/EntityView.php @@ -358,8 +358,8 @@ $allSnaks = $entityRevision->getEntity()->getAllSnaks(); // treat referenced entities as page links ------ - $refFinder = new ReferencedEntitiesFinder(); - $usedEntityIds = $refFinder->findSnakLinks( $allSnaks ); + $entitiesFinder = new ReferencedEntitiesFinder(); + $usedEntityIds = $entitiesFinder->findSnakLinks( $allSnaks ); foreach ( $usedEntityIds as $entityId ) { $pout->addLink( $this->entityTitleLookup->getTitleForId( $entityId ) ); -- To view, visit https://gerrit.wikimedia.org/r/156044 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ia8ccfe2db6fc857b3d10147326adaefd3055e255 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Bene <benestar.wikime...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits