Hoo man has uploaded a new change for review. https://gerrit.wikimedia.org/r/190809
Change subject: Generalize client's SnaksFinder ...................................................................... Generalize client's SnaksFinder Change-Id: I1321a70aa1396319ff7640fd12ffe1ec69dd4ebc --- M client/includes/DataAccess/PropertyParserFunction/LanguageAwareRenderer.php M client/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactory.php D client/includes/DataAccess/PropertyParserFunction/SnaksFinder.php A client/includes/DataAccess/SnaksFinder.php M client/includes/WikibaseClient.php M client/tests/phpunit/includes/DataAccess/PropertyParserFunction/LanguageAwareRendererTest.php M client/tests/phpunit/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactoryTest.php R client/tests/phpunit/includes/DataAccess/SnaksFinderTest.php 8 files changed, 114 insertions(+), 89 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/09/190809/1 diff --git a/client/includes/DataAccess/PropertyParserFunction/LanguageAwareRenderer.php b/client/includes/DataAccess/PropertyParserFunction/LanguageAwareRenderer.php index 8e4b000..af188ca 100644 --- a/client/includes/DataAccess/PropertyParserFunction/LanguageAwareRenderer.php +++ b/client/includes/DataAccess/PropertyParserFunction/LanguageAwareRenderer.php @@ -7,6 +7,7 @@ use Status; use Wikibase\Client\Usage\UsageAccumulator; use Wikibase\DataAccess\PropertyIdResolver; +use Wikibase\DataAccess\SnaksFinder; use Wikibase\DataModel\Entity\EntityId; use Wikibase\DataModel\Entity\EntityIdValue; use Wikibase\DataModel\Entity\PropertyId; diff --git a/client/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactory.php b/client/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactory.php index be10b15..b817809 100644 --- a/client/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactory.php +++ b/client/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactory.php @@ -4,6 +4,7 @@ use Language; use Parser; +use Wikibase\DataAccess\SnaksFinder; use ValueFormatters\FormatterOptions; use Wikibase\Client\Usage\ParserOutputUsageAccumulator; use Wikibase\Client\Usage\UsageAccumulator; diff --git a/client/includes/DataAccess/PropertyParserFunction/SnaksFinder.php b/client/includes/DataAccess/PropertyParserFunction/SnaksFinder.php deleted file mode 100644 index e893a62..0000000 --- a/client/includes/DataAccess/PropertyParserFunction/SnaksFinder.php +++ /dev/null @@ -1,77 +0,0 @@ -<?php - -namespace Wikibase\DataAccess\PropertyParserFunction; - -use Wikibase\DataModel\Entity\EntityDocument; -use Wikibase\DataModel\Entity\EntityId; -use Wikibase\DataModel\Entity\PropertyId; -use Wikibase\DataModel\Snak\Snak; -use Wikibase\DataModel\StatementListProvider; -use Wikibase\Lib\Store\EntityLookup; - -/** - * Find Snaks for claims in an entity, with EntityId, based on property label or property id. - * - * TODO: see what code can be shared with Lua handling code. - * - * @since 0.5 - * - * @licence GNU GPL v2+ - * @author Katie Filbert < [email protected] > - * @author Jeroen De Dauw < [email protected] > - * @author Daniel Kinzler - * @author Liangent < [email protected] > - */ -class SnaksFinder { - - private $entityLookup; - - public function __construct( EntityLookup $entityLookup ) { - $this->entityLookup = $entityLookup; - } - - /** - * @param EntityId $entityId - the item or property that the property is used on - * @param PropertyId $propertyId - the PropertyId for which we want the formatted Snaks - * @param string $languageCode - language to render values - * - * @return Snak[] - */ - public function findSnaks( EntityId $entityId, PropertyId $propertyId, $languageCode ) { - wfProfileIn( __METHOD__ ); - - $entity = $this->entityLookup->getEntity( $entityId ); - - if ( !$entity ) { - wfDebugLog( __METHOD__, 'Entity not found' ); - wfProfileOut( __METHOD__ ); - return array(); - } - - $snaks = $this->getBestMainSnaksForProperty( $entity, $propertyId ); - - if ( empty( $snaks ) ) { - wfDebugLog( __CLASS__, __METHOD__ . ': no claims found.' ); - wfProfileOut( __METHOD__ ); - return array(); - } - - wfProfileOut( __METHOD__ ); - return $snaks; - } - - /** - * @param EntityDocument $entity The Entity from which to get the clams - * @param PropertyId $propertyId - * - * @return Snak[] - */ - private function getBestMainSnaksForProperty( EntityDocument $entity, PropertyId $propertyId ) { - if ( $entity instanceof StatementListProvider ) { - return $entity->getStatements()->getWithPropertyId( $propertyId )->getBestStatements()->getMainSnaks(); - } - - return array(); - } - -} diff --git a/client/includes/DataAccess/SnaksFinder.php b/client/includes/DataAccess/SnaksFinder.php new file mode 100644 index 0000000..96fb16c --- /dev/null +++ b/client/includes/DataAccess/SnaksFinder.php @@ -0,0 +1,92 @@ +<?php + +namespace Wikibase\DataAccess; + +use Wikibase\DataModel\Entity\EntityId; +use Wikibase\DataModel\Entity\PropertyId; +use Wikibase\DataModel\Snak\Snak; +use Wikibase\DataModel\StatementListProvider; +use Wikibase\DataModel\Statement\StatementList; +use Wikibase\Lib\Store\EntityLookup; + +/** + * Find Snaks for claims in an entity, with EntityId, based on PropertyId. + * + * @since 0.5 + * + * @licence GNU GPL v2+ + * @author Katie Filbert < [email protected] > + * @author Jeroen De Dauw < [email protected] > + * @author Daniel Kinzler + * @author Liangent < [email protected] > + * @author Marius Hoch < [email protected] > + */ +class SnaksFinder { + + /** + * @var EntityLookup + */ + private $entityLookup; + + /** + * @param EntityLookup $entityLookup + */ + public function __construct( EntityLookup $entityLookup ) { + $this->entityLookup = $entityLookup; + } + + /** + * @param EntityId $entityId The item or property that the property is used on + * @param PropertyId $propertyId The PropertyId for which we want the formatted Snaks + * @param int[]|null $acceptableRanks + * + * @return Snak[] + */ + public function findSnaks( EntityId $entityId, PropertyId $propertyId, $acceptableRanks = null ) { + $entity = $this->entityLookup->getEntity( $entityId ); + + if ( !$entity instanceof StatementListProvider ) { + return array(); + } + + $statementList = $this->getStatementsWithPropertyId( $entity, $propertyId ); + if ( $acceptableRanks === null ) { + $snaks = $this->getBestMainSnaks( $statementList ); + } else { + $snaks = $this->getMainSnaksByRanks( $statementList, $acceptableRanks ); + } + + return $snaks; + } + + /** + * @param StatementListProvider $statementListProvider + * @param PropertyId $propertyId + * + * @return StatementList + */ + private function getStatementsWithPropertyId( StatementListProvider $statementListProvider, PropertyId $propertyId ) { + return $statementListProvider + ->getStatements() + ->getWithPropertyId( $propertyId ); + } + + /** + * @param StatementList $statementList + * + * @return Snak[] + */ + private function getBestMainSnaks( StatementList $statementList ) { + return $statementList->getBestStatements()->getMainSnaks(); + } + + /** + * @param StatementList $statementList + * @param int[] $acceptableRanks + * + * @return Snak[] + */ + private function getMainSnaksByRanks( StatementList $statementList, array $acceptableRanks ) { + return $statementList->getWithRank( $acceptableRanks )->getMainSnaks(); + } +} diff --git a/client/includes/WikibaseClient.php b/client/includes/WikibaseClient.php index 655975b..067e3bc 100644 --- a/client/includes/WikibaseClient.php +++ b/client/includes/WikibaseClient.php @@ -26,7 +26,7 @@ use Wikibase\DataAccess\PropertyIdResolver; use Wikibase\DataAccess\PropertyParserFunction\PropertyClaimsRendererFactory; use Wikibase\DataAccess\PropertyParserFunction\Runner; -use Wikibase\DataAccess\PropertyParserFunction\SnaksFinder; +use Wikibase\DataAccess\SnaksFinder; use Wikibase\DataModel\Entity\BasicEntityIdParser; use Wikibase\DataModel\Entity\DispatchingEntityIdParser; use Wikibase\DataModel\Entity\EntityIdParser; diff --git a/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/LanguageAwareRendererTest.php b/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/LanguageAwareRendererTest.php index d42954b..ce7cd9d 100644 --- a/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/LanguageAwareRendererTest.php +++ b/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/LanguageAwareRendererTest.php @@ -8,7 +8,7 @@ use Wikibase\Client\Usage\UsageAccumulator; use Wikibase\DataAccess\PropertyIdResolver; use Wikibase\DataAccess\PropertyParserFunction\LanguageAwareRenderer; -use Wikibase\DataAccess\PropertyParserFunction\SnaksFinder; +use Wikibase\DataAccess\SnaksFinder; use Wikibase\DataModel\Entity\EntityId; use Wikibase\DataModel\Entity\EntityIdValue; use Wikibase\DataModel\Entity\ItemId; @@ -151,7 +151,7 @@ */ private function getSnaksFinder( array $snaks ) { $snaksFinder = $this->getMockBuilder( - 'Wikibase\DataAccess\PropertyParserFunction\SnaksFinder' + 'Wikibase\DataAccess\SnaksFinder' ) ->disableOriginalConstructor() ->getMock(); diff --git a/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactoryTest.php b/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactoryTest.php index e787251..dfdc1e9 100644 --- a/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactoryTest.php +++ b/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactoryTest.php @@ -126,7 +126,7 @@ private function getSnaksFinder() { $snakListFinder = $this->getMockBuilder( - 'Wikibase\DataAccess\PropertyParserFunction\SnaksFinder' + 'Wikibase\DataAccess\SnaksFinder' ) ->disableOriginalConstructor() ->getMock(); diff --git a/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/SnaksFinderTest.php b/client/tests/phpunit/includes/DataAccess/SnaksFinderTest.php similarity index 78% rename from client/tests/phpunit/includes/DataAccess/PropertyParserFunction/SnaksFinderTest.php rename to client/tests/phpunit/includes/DataAccess/SnaksFinderTest.php index 5bf4d6b..124032d 100644 --- a/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/SnaksFinderTest.php +++ b/client/tests/phpunit/includes/DataAccess/SnaksFinderTest.php @@ -1,9 +1,9 @@ <?php -namespace Wikibase\Client\Tests\DataAccess\PropertyParserFunction; +namespace Wikibase\Client\Tests\DataAccess; use DataValues\StringValue; -use Wikibase\DataAccess\PropertyParserFunction\SnaksFinder; +use Wikibase\DataAccess\SnaksFinder; use Wikibase\DataModel\Claim\Claim; use Wikibase\DataModel\Entity\Item; use Wikibase\DataModel\Entity\ItemId; @@ -15,7 +15,7 @@ use Wikibase\Test\MockRepository; /** - * @covers Wikibase\DataAccess\PropertyParserFunction\SnaksFinder + * @covers Wikibase\DataAccess\SnaksFinder * * @group Wikibase * @group WikibaseClient @@ -24,6 +24,7 @@ * * @licence GNU GPL v2+ * @author Katie Filbert < [email protected] > + * @author Marius Hoch < [email protected] > */ class SnaksFinderTest extends \PHPUnit_Framework_TestCase { @@ -79,10 +80,10 @@ /** * @dataProvider findSnaksProvider */ - public function testFindSnaks( array $expected, ItemId $itemId, PropertyId $propertyId ) { + public function testFindSnaks( array $expected, ItemId $itemId, PropertyId $propertyId, $acceptableRanks = null ) { $snaksFinder = $this->getSnaksFinder(); - $snakList = $snaksFinder->findSnaks( $itemId, $propertyId, 'en' ); + $snakList = $snaksFinder->findSnaks( $itemId, $propertyId, $acceptableRanks ); $this->assertEquals( $expected, $snakList ); } @@ -91,14 +92,21 @@ $propertyId = new PropertyId( 'P1337' ); - $snaks = array( + $snaksNormal = array( new PropertyValueSnak( $propertyId, new StringValue( 'a kitten!' ) ), new PropertyValueSnak( $propertyId, new StringValue( 'two kittens!!' ) ) ); + $snakDeprecated = array( new PropertyValueSnak( $propertyId, new StringValue( 'three kittens!!!' ) ) ); return array( - array( $snaks, $itemId, new PropertyId( 'P1337' ) ), - array( array(), $itemId, new PropertyId( 'P90001' ) ) + array( $snaksNormal, $itemId, new PropertyId( 'P1337' ) ), + array( array(), $itemId, new PropertyId( 'P90001' ) ), + array( + $snakDeprecated, + $itemId, + new PropertyId( 'P1337' ), + array( Statement::RANK_DEPRECATED ) + ), ); } -- To view, visit https://gerrit.wikimedia.org/r/190809 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I1321a70aa1396319ff7640fd12ffe1ec69dd4ebc Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Hoo man <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
