Daniel Kinzler has uploaded a new change for review. https://gerrit.wikimedia.org/r/72962
Change subject: Make ReferencedEntityFinder use Entity::getAllSnaks. ...................................................................... Make ReferencedEntityFinder use Entity::getAllSnaks. IMPORTANT: this needs Iae5f7817cfe in WikibaseDataModel. Note how this reduces the complexity of ReferencedEntityFinder in preparation of the creation of more such classes, e.g. ReferencedUrlFinder, ReferencedImageFinder, etc. Change-Id: Ie9fcbb315f858e36566bc6f702c659d241d5679d --- M lib/includes/ReferencedEntitiesFinder.php M lib/tests/phpunit/ReferencedEntitiesFinderTest.php M repo/includes/EntityView.php 3 files changed, 50 insertions(+), 149 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/62/72962/1 diff --git a/lib/includes/ReferencedEntitiesFinder.php b/lib/includes/ReferencedEntitiesFinder.php index d97d2ec..3a8b11d 100644 --- a/lib/includes/ReferencedEntitiesFinder.php +++ b/lib/includes/ReferencedEntitiesFinder.php @@ -28,58 +28,16 @@ * @licence GNU GPL v2+ * @author Jeroen De Dauw < [email protected] > * @author Daniel Werner < [email protected] > + * @author Daniel Kinzler */ class ReferencedEntitiesFinder { - - /** - * @since 0.4 - * - * @var EntityLookup - */ - protected $entityLoader; - - /** - * @since 0.4 - * - * @param EntityLookup $entityLoader - */ - public function __construct( EntityLookup $entityLoader ) { - $this->entityLoader = $entityLoader; - } - - /** - * @since 0.4 - * - * @param Claim[]|Claims $claims - * - * @return EntityId[] - */ - public function findClaimLinks( $claims ) { - $snaks = array(); - - /** - * @var Claim $claim - */ - foreach ( $claims as $claim ) { - $snaks[] = $claim->getMainSnak(); - $snaks = array_merge( $snaks, iterator_to_array( $claim->getQualifiers() ) ); - - if( $claim instanceof Statement ) { - foreach( $claim->getReferences() as $reference ) { - $snaks = array_merge( $snaks, iterator_to_array( $reference->getSnaks() ) ); - } - } - } - - return $this->findSnakLinks( $snaks ); - } /** * @param Snak[] $snaks * * @return EntityId[] */ - protected function findSnakLinks( array $snaks ) { + public function findSnakLinks( array $snaks ) { $foundEntities = array(); foreach ( $snaks as $snak ) { diff --git a/lib/tests/phpunit/ReferencedEntitiesFinderTest.php b/lib/tests/phpunit/ReferencedEntitiesFinderTest.php index 8453a5a..caf87d3 100644 --- a/lib/tests/phpunit/ReferencedEntitiesFinderTest.php +++ b/lib/tests/phpunit/ReferencedEntitiesFinderTest.php @@ -14,6 +14,7 @@ use Wikibase\Property; use Wikibase\Item; use Wikibase\SnakList; +use Wikibase\Snak; use DataValues\StringValue; use Wikibase\LibRegistry; use Wikibase\Settings; @@ -50,95 +51,55 @@ * * @licence GNU GPL v2+ * @author Jeroen De Dauw < [email protected] > + * @author Daniel Kinzler */ class ReferencedEntitiesFinderTest extends \MediaWikiTestCase { public function claimsProvider() { $argLists = array(); - $argLists[] = array( array(), array() ); + $p11 = new EntityId( Property::ENTITY_TYPE, 11 ); + $p42 = new EntityId( Property::ENTITY_TYPE, 42 ); + $p44 = new EntityId( Property::ENTITY_TYPE, 44 ); + + $q23 = new EntityId( Item::ENTITY_TYPE, 23 ); + $q24 = new EntityId( Item::ENTITY_TYPE, 24 ); + + $argLists[] = array( + array(), + array(), + "empty" ); + + $argLists[] = array( + array( new PropertyNoValueSnak( $p42 ) ), + array( $p42 ), + "Property" ); + + $argLists[] = array( + array( new PropertySomeValueSnak( $p42 ) ), + array( $p42 ), + "PropertySomeValueSnak" ); + + $argLists[] = array( + array( new PropertyValueSnak( $p42, new StringValue( 'onoez' ) ) ), + array( $p42 ), + "PropertyValueSnak with string value" ); + + $argLists[] = array( + array( new PropertyValueSnak( $p42, $q23 ) ), + array( $p42, $q23 ), + "PropertyValueSnak with EntityId" ); $argLists[] = array( array( - new Claim( new PropertyNoValueSnak( 42 ) ) + new PropertyValueSnak( $p11, $q23 ), + new PropertyNoValueSnak( $p42 ), + new PropertySomeValueSnak( $p44 ), + new PropertyValueSnak( $p44, new StringValue( 'onoez' ) ), + new PropertyValueSnak( $p44, $q24 ), ), - array( - new EntityId( Property::ENTITY_TYPE, 42 ) - ) - ); - - $argLists[] = array( - array( - new Statement( - new PropertyNoValueSnak( 42 ), - null, - new ReferenceList( array( new Reference( new SnakList( array( - new PropertyNoValueSnak( 24 ) ) ) ) ) ) - ) - ), - array( - new EntityId( Property::ENTITY_TYPE, 42 ), - new EntityId( Property::ENTITY_TYPE, 24 ) - ) - ); - - $argLists[] = array( - array( - new Claim( new PropertyNoValueSnak( 42 ) ), - new Claim( new PropertyNoValueSnak( 43 ) ), - ), - array( - new EntityId( Property::ENTITY_TYPE, 42 ), - new EntityId( Property::ENTITY_TYPE, 43 ), - ) - ); - - $argLists[] = array( - array( - new Claim( - new PropertyNoValueSnak( 42 ), - new SnakList( array( - new PropertyNoValueSnak( 42 ), - new PropertySomeValueSnak( 43 ), - new PropertyValueSnak( 1, new StringValue( 'onoez' ) ), - ) ) - ), - new Claim( new PropertyNoValueSnak( 44 ) ), - ), - array( - new EntityId( Property::ENTITY_TYPE, 42 ), - new EntityId( Property::ENTITY_TYPE, 43 ), - new EntityId( Property::ENTITY_TYPE, 44 ), - new EntityId( Property::ENTITY_TYPE, 1 ), - ) - ); - - $id9001 = new EntityId( Item::ENTITY_TYPE, 9001 ); - $id1 = new EntityId( Item::ENTITY_TYPE, 1 ); - - $argLists[] = array( - array( - new Claim( - new PropertyValueSnak( 2, $id9001 ), - new SnakList( array( - new PropertyNoValueSnak( 42 ), - new PropertySomeValueSnak( 43 ), - new PropertyValueSnak( 1, new StringValue( 'onoez' ) ), - new PropertyValueSnak( 2, $id1 ), - ) ) - ), - new Claim( new PropertyNoValueSnak( 44 ) ), - ), - array( - new EntityId( Property::ENTITY_TYPE, 2 ), - new EntityId( Property::ENTITY_TYPE, 42 ), - new EntityId( Property::ENTITY_TYPE, 43 ), - new EntityId( Property::ENTITY_TYPE, 44 ), - new EntityId( Property::ENTITY_TYPE, 1 ), - new EntityId( Item::ENTITY_TYPE, 9001 ), - new EntityId( Item::ENTITY_TYPE, 1 ), - ) - ); + array( $p11, $q23, $p42, $p44, $q24 ), + "PropertyValueSnak with EntityId" ); return $argLists; } @@ -146,34 +107,16 @@ /** * @dataProvider claimsProvider * - * @param Claim[] $claims + * @param Snak[] $snaks * @param EntityId[] $expected + * @param $message */ - public function testFindClaimLinks( array $claims, array $expected ) { - $linkFinder = new ReferencedEntitiesFinder( $this->getMockEntityLoader() ); + public function testFindSnakLinks( array $snaks, array $expected, $message ) { + $linkFinder = new ReferencedEntitiesFinder(); - $actual = $linkFinder->findClaimLinks( new Claims( $claims ) ); + $actual = $linkFinder->findSnakLinks( $snaks ); - $this->assertArrayEquals( $expected, $actual ); - } - - /** - * @return \Wikibase\EntityLookup - */ - protected function getMockEntityLoader() { - $entityLoader = new \Wikibase\Test\MockRepository(); - - $stringProp = Property::newEmpty(); - $stringProp->setId( 1 ); - $stringProp->setDataTypeId( 'commonsMedia' ); - $entityLoader->putEntity( $stringProp ); - - $itemProp = Property::newEmpty(); - $itemProp->setId( 2 ); - $itemProp->setDataTypeId( 'wikibase-item' ); - $entityLoader->putEntity( $itemProp ); - - return $entityLoader; + $this->assertArrayEquals( $expected, $actual ); // assertArrayEquals doesn't take a message :( } } diff --git a/repo/includes/EntityView.php b/repo/includes/EntityView.php index 803bd6c..70e9e5a 100644 --- a/repo/includes/EntityView.php +++ b/repo/includes/EntityView.php @@ -247,7 +247,7 @@ $refFinder = new ReferencedEntitiesFinder( $entityLoader ); $contentFactory = EntityContentFactory::singleton(); - $usedEntityIds = $refFinder->findClaimLinks( $entity->getEntity()->getClaims() ); + $usedEntityIds = $refFinder->findSnakLinks( $entity->getEntity()->getAllSnaks() ); foreach ( $usedEntityIds as $entityId ) { $pout->addLink( $contentFactory->getTitleForId( $entityId ) ); @@ -838,7 +838,7 @@ $entityLoader = StoreFactory::getStore()->getEntityLookup(); $refFinder = new ReferencedEntitiesFinder( $entityLoader ); - $usedEntityIds = $refFinder->findClaimLinks( $entity->getClaims() ); + $usedEntityIds = $refFinder->findSnakLinks( $entity->getAllSnaks() ); $basicEntityInfo = static::getBasicEntityInfo( $entityLoader, $usedEntityIds, $langCode ); $out->addJsConfigVars( -- To view, visit https://gerrit.wikimedia.org/r/72962 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie9fcbb315f858e36566bc6f702c659d241d5679d Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Daniel Kinzler <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
