Jeroen De Dauw has uploaded a new change for review. https://gerrit.wikimedia.org/r/174299
Change subject: Stop using Claims and Entity in WikibaseLuaEntityBindings ...................................................................... Stop using Claims and Entity in WikibaseLuaEntityBindings The two private methods introduced, getBestStatements and getStatementsWithRanks contain things that are better put into StatementList itself. Once they are these, we can further simplify this code Change-Id: Id223877bc2e740a8af366c1568cc24499479b340 --- M client/includes/scribunto/WikibaseLuaEntityBindings.php 1 file changed, 68 insertions(+), 63 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/99/174299/1 diff --git a/client/includes/scribunto/WikibaseLuaEntityBindings.php b/client/includes/scribunto/WikibaseLuaEntityBindings.php index cf68e20..e813b86 100644 --- a/client/includes/scribunto/WikibaseLuaEntityBindings.php +++ b/client/includes/scribunto/WikibaseLuaEntityBindings.php @@ -4,14 +4,16 @@ use Language; use Wikibase\Client\Usage\UsageAccumulator; -use Wikibase\DataModel\Claim\Claims; -use Wikibase\DataModel\Entity\Entity; +use Wikibase\DataModel\Entity\EntityDocument; use Wikibase\DataModel\Entity\EntityId; use Wikibase\DataModel\Entity\EntityIdValue; use Wikibase\DataModel\Entity\ItemId; use Wikibase\DataModel\Entity\PropertyId; use Wikibase\DataModel\Snak\PropertyValueSnak; use Wikibase\DataModel\Snak\Snak; +use Wikibase\DataModel\Statement\Statement; +use Wikibase\DataModel\Statement\StatementList; +use Wikibase\DataModel\StatementListProvider; use Wikibase\Lib\SnakFormatter; use Wikibase\Lib\Store\EntityLookup; @@ -23,7 +25,6 @@ * @licence GNU GPL v2+ * @author Marius Hoch < [email protected] > */ - class WikibaseLuaEntityBindings { /** @@ -52,7 +53,7 @@ private $language; /** - * @var Entity[] + * @var EntityDocument[] */ private $entities = array(); @@ -78,13 +79,75 @@ } /** + * Render the main Snaks belonging to a Statement (which is identified by a PropertyId). + * + * @since 0.5 + * @todo Share code with LanguageAwareRenderer. + * + * @param string $entityId + * @param string $propertyId + * @param int[] $acceptableRanks + * + * @return string + */ + public function formatPropertyValues( $entityId, $propertyId, array $acceptableRanks = null ) { + $propertyId = new PropertyId( $propertyId ); + + // FIXME: this only works for item ids + $entity = $this->getEntity( new ItemId( $entityId ) ); + + if ( !( $entity instanceof StatementListProvider ) ) { + return ''; + } + + $statements = $entity->getStatements()->getWithPropertyId( $propertyId ); + + if ( $acceptableRanks === null ) { + // We only want the best claims over here, so that we only show the most + // relevant information. + $statements = $this->getBestStatements( $statements ); + } else { + // ... unless the user passed in a table of acceptable ranks + $statements = $this->getStatementsWithRanks( $statements, $acceptableRanks ); + } + + $snakList = $statements->getMainSnaks(); + + $this->trackUsage( $snakList ); + return $this->formatSnakList( $snakList ); + } + + private function getBestStatements( StatementList $statements ) { + $rank = Statement::RANK_PREFERRED; + + do { + $filteredList = $this->getStatementsWithRanks( $statements, array( $rank ) ); + $rank--; + } while ( $filteredList->isEmpty() && $rank > Statement::RANK_DEPRECATED ); + + return $filteredList; + } + + private function getStatementsWithRanks( StatementList $statements, array $acceptableRanks ) { + $filteredList = array(); + + foreach ( $statements->toArray() as $statement ) { + if ( in_array( $statement->getRank(), $acceptableRanks ) ) { + $filteredList[] = $statement; + } + } + + return new StatementList( $filteredList ); + } + + /** * Get the entity for the given EntityId (cached within the class). * This *might* be redundant with caching in EntityLookup, but we * don't want to rely on that (per Daniel). * * @param EntityId $entityId * - * @return Entity|null + * @return EntityDocument|null */ private function getEntity( EntityId $entityId ) { if ( !isset( $this->entities[ $entityId->getSerialization() ] ) ) { @@ -93,21 +156,6 @@ } return $this->entities[ $entityId->getSerialization() ]; - } - - /** - * Returns such Claims from $entity that have a main Snak for the property that - * is specified by $propertyLabel. - * - * @param Entity $entity - * @param PropertyId $propertyId - * - * @return Claims - */ - private function getClaimsForProperty( Entity $entity, PropertyId $propertyId ) { - $allClaims = new Claims( $entity->getClaims() ); - - return $allClaims->getClaimsForProperty( $propertyId ); } /** @@ -155,49 +203,6 @@ $this->usageAccumulator->addLabelUsage( $value->getEntityId() ); } } - } - - /** - * Render the main Snaks belonging to a Claim (which is identified by a PropertyId). - * - * @since 0.5 - * @todo Share code with LanguageAwareRenderer. - * - * @param string $entityId - * @param string $propertyId - * @param int[] $acceptableRanks - * - * @return string - */ - public function formatPropertyValues( $entityId, $propertyId, array $acceptableRanks = null ) { - $entityId = new ItemId( $entityId ); - $propertyId = new PropertyId( $propertyId ); - - $entity = $this->getEntity( $entityId ); - - if ( !$entity ) { - return ''; - } - - $claims = $this->getClaimsForProperty( $entity, $propertyId ); - - if ( !$acceptableRanks ) { - // We only want the best claims over here, so that we only show the most - // relevant information. - $claims = $claims->getBestClaims(); - } else { - // ... unless the user passed in a table of acceptable ranks - $claims = $claims->getByRanks( $acceptableRanks ); - } - - if ( $claims->isEmpty() ) { - return ''; - } - - $snakList = $claims->getMainSnaks(); - - $this->trackUsage( $snakList ); - return $this->formatSnakList( $snakList ); } /** -- To view, visit https://gerrit.wikimedia.org/r/174299 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Id223877bc2e740a8af366c1568cc24499479b340 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Jeroen De Dauw <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
