Hoo man has uploaded a new change for review. https://gerrit.wikimedia.org/r/189415
Change subject: Consolidate duplicate PropertyParserFunction/Lua code ...................................................................... Consolidate duplicate PropertyParserFunction/Lua code As a side effect this fixes T48160 Change-Id: If4d2cd33e3851576f31ac64e42788d00e9badd7e --- A client/includes/DataAccess/EntityStatementsRenderer.php M client/includes/DataAccess/PropertyIdResolver.php 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/includes/scribunto/Scribunto_LuaWikibaseEntityLibrary.php M client/includes/scribunto/WikibaseLuaEntityBindings.php M client/includes/scribunto/mw.wikibase.entity.lua M client/tests/phpunit/MockClientStore.php A client/tests/phpunit/includes/DataAccess/EntityStatementsRendererTest.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 M client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua M client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseEntityLibraryTest.php M client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTestCase.php M client/tests/phpunit/includes/scribunto/WikibaseLuaEntityBindingsTest.php M client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php M docs/lua.wiki 21 files changed, 656 insertions(+), 474 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/15/189415/1 diff --git a/client/includes/DataAccess/EntityStatementsRenderer.php b/client/includes/DataAccess/EntityStatementsRenderer.php new file mode 100644 index 0000000..eb1aaea --- /dev/null +++ b/client/includes/DataAccess/EntityStatementsRenderer.php @@ -0,0 +1,134 @@ +<?php + +namespace Wikibase\DataAccess; + +use InvalidArgumentException; +use Language; +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\Snak\PropertyValueSnak; +use Wikibase\DataModel\Snak\Snak; +use Wikibase\Lib\PropertyLabelNotResolvedException; +use Wikibase\Lib\SnakFormatter; + +/** + * Renders the main Snaks associated with a given Property on an Entity. + * + * @since 0.5 + * + * @licence GNU GPL v2+ + * + * @author Marius Hoch < [email protected] > + */ +class EntityStatementsRenderer { + + /** + * @var Language + */ + private $language; + + /** + * @var PropertyIdResolver + */ + private $propertyIdResolver; + + /** + * @var SnaksFinder + */ + private $snaksFinder; + + /** + * @var SnakFormatter + */ + private $snakFormatter; + + /** + * @var UsageAccumulator + */ + private $usageAccumulator; + + /** + * @param Language $language + * @param PropertyIdResolver $propertyIdResolver + * @param SnaksFinder $snaksFinder + * @param SnakFormatter $snakFormatter + * @param UsageAccumulator $usageAccumulator + */ + public function __construct( + Language $language, + PropertyIdResolver $propertyIdResolver, + SnaksFinder $snaksFinder, + SnakFormatter $snakFormatter, + UsageAccumulator $usageAccumulator + ) { + $this->language = $language; + $this->propertyIdResolver = $propertyIdResolver; + $this->snaksFinder = $snaksFinder; + $this->snakFormatter = $snakFormatter; + $this->usageAccumulator = $usageAccumulator; + } + + /** + * @param EntityId $entityId + * @param string $propertyLabelOrId property label or ID (pXXX) + * @param int[]|null $acceptableRanks + * + * @throws PropertyLabelNotResolvedException + * @return string + */ + public function render( EntityId $entityId, $propertyLabelOrId, $acceptableRanks = null ) { + $propertyId = $this->propertyIdResolver->resolvePropertyId( + $propertyLabelOrId, + $this->language->getCode() + ); + + $snaks = $this->snaksFinder->findSnaks( + $entityId, + $propertyId, + $acceptableRanks + ); + $this->trackUsage( $snaks ); + + return $this->formatSnaks( $snaks ); + } + + /** + * @param Snak[] $snaks + * + * @return string wikitext + */ + private function formatSnaks( array $snaks ) { + $formattedValues = array(); + + foreach ( $snaks as $snak ) { + $formattedValues[] = $this->snakFormatter->formatSnak( $snak ); + } + + return $this->language->commaList( $formattedValues ); + } + + /** + * @param Snak[] $snaks + */ + private function trackUsage( array $snaks ) { + // Note: we track any EntityIdValue as a label usage. + // This is making assumptions about what the respective formatter actually does. + // Ideally, the formatter itself would perform the tracking, but that seems nasty to model. + + foreach ( $snaks as $snak ) { + if ( !( $snak instanceof PropertyValueSnak) ) { + continue; + } + + $value = $snak->getDataValue(); + + if ( $value instanceof EntityIdValue ) { + $this->usageAccumulator->addLabelUsage( $value->getEntityId() ); + } + } + } + +} diff --git a/client/includes/DataAccess/PropertyIdResolver.php b/client/includes/DataAccess/PropertyIdResolver.php index c34f087..2b93995 100644 --- a/client/includes/DataAccess/PropertyIdResolver.php +++ b/client/includes/DataAccess/PropertyIdResolver.php @@ -11,8 +11,6 @@ /** * Resolves the PropertyId for the input, which might be a property label or prefixed id. * - * @fixme see what code can be shared with Lua handling code. - * * @since 0.5 * * @licence GNU GPL v2+ @@ -23,10 +21,20 @@ */ class PropertyIdResolver { + /** + * @var EntityLookup + */ private $entityLookup; + /** + * @var PropertyLabelResolver + */ private $propertyLabelResolver; + /** + * @param EntityLookup $entityLookup + * @param PropertyLabelResolver $propertyLabelResolver + */ public function __construct( EntityLookup $entityLookup, PropertyLabelResolver $propertyLabelResolver diff --git a/client/includes/DataAccess/PropertyParserFunction/LanguageAwareRenderer.php b/client/includes/DataAccess/PropertyParserFunction/LanguageAwareRenderer.php index 8e4b000..98a3aac 100644 --- a/client/includes/DataAccess/PropertyParserFunction/LanguageAwareRenderer.php +++ b/client/includes/DataAccess/PropertyParserFunction/LanguageAwareRenderer.php @@ -5,20 +5,12 @@ use InvalidArgumentException; use Language; use Status; -use Wikibase\Client\Usage\UsageAccumulator; -use Wikibase\DataAccess\PropertyIdResolver; +use Wikibase\DataAccess\EntityStatementsRenderer; use Wikibase\DataModel\Entity\EntityId; -use Wikibase\DataModel\Entity\EntityIdValue; -use Wikibase\DataModel\Entity\PropertyId; -use Wikibase\DataModel\Snak\PropertyValueSnak; -use Wikibase\DataModel\Snak\Snak; use Wikibase\Lib\PropertyLabelNotResolvedException; -use Wikibase\Lib\SnakFormatter; /** * PropertyClaimsRenderer of the {{#property}} parser function. - * - * @fixme see what code can be shared with Lua handling code. * * @since 0.5 * @@ -27,6 +19,7 @@ * @author Jeroen De Dauw < [email protected] > * @author Daniel Kinzler * @author Liangent < [email protected] > + * @author Marius Hoch < [email protected] > */ class LanguageAwareRenderer implements PropertyClaimsRenderer { @@ -35,42 +28,21 @@ */ private $language; - private $propertyIdResolver; - /** - * @var SnaksFinder + * @var EntityStatementsRenderer */ - private $snaksFinder; - - /** - * @var SnakFormatter - */ - private $snakFormatter; - - /** - * @var UsageAccumulator - */ - private $usageAccumulator; + private $entityStatementsRenderer; /** * @param Language $language - * @param PropertyIdResolver $propertyIdResolver - * @param SnaksFinder $snaksFinder - * @param SnakFormatter $snakFormatter - * @param UsageAccumulator $usageAcc + * @param EntityStatementsRenderer $entityStatementsRenderer */ public function __construct( Language $language, - PropertyIdResolver $propertyIdResolver, - SnaksFinder $snaksFinder, - SnakFormatter $snakFormatter, - UsageAccumulator $usageAcc + EntityStatementsRenderer $entityStatementsRenderer ) { $this->language = $language; - $this->propertyIdResolver = $propertyIdResolver; - $this->snaksFinder = $snaksFinder; - $this->snakFormatter = $snakFormatter; - $this->usageAccumulator = $usageAcc; + $this->entityStatementsRenderer = $entityStatementsRenderer; } /** @@ -81,13 +53,9 @@ */ public function render( EntityId $entityId, $propertyLabelOrId ) { try { - // @todo have the $propertyId resolved before passing into here - $propertyId = $this->propertyIdResolver->resolvePropertyId( - $propertyLabelOrId, - $this->language->getCode() + $status = Status::newGood( + $this->entityStatementsRenderer->render( $entityId, $propertyLabelOrId ) ); - - $status = $this->renderWithStatus( $entityId, $propertyId ); } catch ( PropertyLabelNotResolvedException $ex ) { // @fixme use ExceptionLocalizer $status = $this->getStatusForException( $propertyLabelOrId, $ex->getMessage() ); @@ -115,73 +83,6 @@ $propertyLabel, $message ); - } - - /** - * @param Snak[] $snaks - * - * @return string wikitext - */ - private function formatSnaks( array $snaks ) { - $formattedValues = array(); - - foreach ( $snaks as $snak ) { - $formattedValues[] = $this->snakFormatter->formatSnak( $snak ); - } - - return $this->language->commaList( $formattedValues ); - } - - /** - * @todo Share code with WikibaseLuaEntityBindings::trackUsage - * @param Snak[] $snaks - */ - private function trackUsage( array $snaks ) { - // Note: we track any EntityIdValue as a label usage. - // This is making assumptions about what the respective formatter actually does. - // Ideally, the formatter itself would perform the tracking, but that seems nasty to model. - - foreach ( $snaks as $snak ) { - if ( !( $snak instanceof PropertyValueSnak) ) { - continue; - } - - $value = $snak->getDataValue(); - - if ( $value instanceof EntityIdValue ) { - $this->usageAccumulator->addLabelUsage( $value->getEntityId() ); - } - } - } - - /** - * @todo Share code with WikibaseLuaEntityBindings. - * - * @param EntityId $entityId - * @param PropertyId $propertyId - * - * @return Status a status object wrapping a wikitext string - */ - private function renderWithStatus( EntityId $entityId, PropertyId $propertyId ) { - wfProfileIn( __METHOD__ ); - - $snaks = $this->snaksFinder->findSnaks( - $entityId, - $propertyId, - $this->language->getCode() - ); - - if ( !$snaks ) { - return Status::newGood( '' ); - } - - $this->trackUsage( $snaks ); - - $text = $this->formatSnaks( $snaks ); - $status = Status::newGood( $text ); - - wfProfileOut( __METHOD__ ); - return $status; } } diff --git a/client/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactory.php b/client/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactory.php index be10b15..10b75d9 100644 --- a/client/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactory.php +++ b/client/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactory.php @@ -8,6 +8,8 @@ use Wikibase\Client\Usage\ParserOutputUsageAccumulator; use Wikibase\Client\Usage\UsageAccumulator; use Wikibase\DataAccess\PropertyIdResolver; +use Wikibase\DataAccess\EntityStatementsRenderer; +use Wikibase\DataAccess\SnaksFinder; use Wikibase\LanguageFallbackChainFactory; use Wikibase\Lib\OutputFormatSnakFormatterFactory; use Wikibase\Lib\SnakFormatter; @@ -87,13 +89,18 @@ * @return LanguageAwareRenderer */ private function newLanguageAwareRenderer( Language $language, UsageAccumulator $usageAccumulator ) { - return new LanguageAwareRenderer( + $entityStatementsRenderer = new EntityStatementsRenderer( $language, $this->propertyIdResolver, $this->snaksFinder, $this->newSnakFormatterForLanguage( $language ), $usageAccumulator ); + + return new LanguageAwareRenderer( + $language, + $entityStatementsRenderer + ); } /** 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 4c8ce45..a021ea2 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/includes/scribunto/Scribunto_LuaWikibaseEntityLibrary.php b/client/includes/scribunto/Scribunto_LuaWikibaseEntityLibrary.php index ae65d6d..0fc65f7 100644 --- a/client/includes/scribunto/Scribunto_LuaWikibaseEntityLibrary.php +++ b/client/includes/scribunto/Scribunto_LuaWikibaseEntityLibrary.php @@ -1,24 +1,28 @@ <?php use ValueFormatters\FormatterOptions; +use Wikibase\DataAccess\EntityStatementsRenderer; +use Wikibase\DataAccess\PropertyIdResolver; +use Wikibase\DataAccess\SnaksFinder; use Wikibase\Client\Scribunto\WikibaseLuaEntityBindings; use Wikibase\Client\Usage\ParserOutputUsageAccumulator; use Wikibase\Client\WikibaseClient; use Wikibase\Lib\SnakFormatter; +use Wikibase\Lib\PropertyLabelNotResolvedException; /** * Registers and defines functions to access Wikibase through the Scribunto extension * * @since 0.5 * - * @licence GNU GPL v2+ + * @license GNU GPL v2+ * @author Marius Hoch < [email protected] > */ class Scribunto_LuaWikibaseEntityLibrary extends Scribunto_LuaLibraryBase { /** - * @var WikibaseLuaEntityBindings + * @var WikibaseLuaEntityBindings|null */ private $wbLibrary; @@ -43,13 +47,27 @@ SnakFormatter::FORMAT_WIKI, $formatterOptions ); - return new WikibaseLuaEntityBindings( - $snakFormatter, - $wikibaseClient->getStore()->getEntityLookup(), - new ParserOutputUsageAccumulator( $this->getParser()->getOutput() ), - $wikibaseClient->getSettings()->getSetting( 'siteGlobalID' ), + $entityLookup = $wikibaseClient->getStore()->getEntityLookup(); + + $propertyIdResolver = new PropertyIdResolver( + $entityLookup, + $wikibaseClient->getStore()->getPropertyLabelResolver() + ); + + $snaksFinder = new SnaksFinder( $entityLookup ); + + $entityStatementsRenderer = new EntityStatementsRenderer( $wgContLang, - $wikibaseClient->getEntityIdParser() + $propertyIdResolver, + $snaksFinder, + $snakFormatter, + new ParserOutputUsageAccumulator( $this->getParser()->getOutput() ) + ); + + return new WikibaseLuaEntityBindings( + $entityStatementsRenderer, + $wikibaseClient->getEntityIdParser(), + $wikibaseClient->getSettings()->getSetting( 'siteGlobalID' ) ); } @@ -83,28 +101,37 @@ } /** - * Render the main Snaks belonging to a Claim (which is identified by a PropertyId). + * Render the main Snaks belonging to a Statement (which is identified by a PropertyId + * or the label of a Property). * * @since 0.5 * * @param string $entityId - * @param string $propertyId + * @param string $propertyLabelOrId * @param int[]|null $acceptableRanks * * @throws ScribuntoException * @return string[] */ - public function formatPropertyValues( $entityId, $propertyId, array $acceptableRanks = null ) { + public function formatPropertyValues( $entityId, $propertyLabelOrId, array $acceptableRanks = null ) { $this->checkType( 'formatPropertyValues', 0, $entityId, 'string' ); // Use 1 as index for the property id, as the first parameter comes from // internals of mw.wikibase.entity (an index of 2 might confuse users // as they only gave one parameter themselves) - $this->checkType( 'formatPropertyValues', 1, $propertyId, 'string' ); + $this->checkType( 'formatPropertyValues', 1, $propertyLabelOrId, 'string' ); $this->checkTypeOptional( 'formatPropertyValues', 2, $acceptableRanks, 'table', null ); try { - return array( $this->getImplementation()->formatPropertyValues( $entityId, $propertyId, $acceptableRanks ) ); + return array( + $this->getImplementation()->formatPropertyValues( + $entityId, + $propertyLabelOrId, + $acceptableRanks + ) + ); } catch ( InvalidArgumentException $e ) { throw new ScribuntoException( 'wikibase-error-invalid-entity-id' ); + } catch ( PropertyLabelNotResolvedException $e ) { + return array( '' ); } } diff --git a/client/includes/scribunto/WikibaseLuaEntityBindings.php b/client/includes/scribunto/WikibaseLuaEntityBindings.php index 72aecd9..cda119c 100644 --- a/client/includes/scribunto/WikibaseLuaEntityBindings.php +++ b/client/includes/scribunto/WikibaseLuaEntityBindings.php @@ -2,53 +2,23 @@ namespace Wikibase\Client\Scribunto; -use Language; -use Wikibase\Client\Usage\UsageAccumulator; -use Wikibase\DataModel\Entity\EntityDocument; -use Wikibase\DataModel\Entity\EntityId; +use Wikibase\DataAccess\EntityStatementsRenderer; use Wikibase\DataModel\Entity\EntityIdParser; -use Wikibase\DataModel\Entity\EntityIdValue; -use Wikibase\DataModel\Entity\PropertyId; -use Wikibase\DataModel\Snak\PropertyValueSnak; -use Wikibase\DataModel\Snak\Snak; -use Wikibase\DataModel\StatementListProvider; -use Wikibase\Lib\SnakFormatter; -use Wikibase\Lib\Store\EntityLookup; /** * Actual implementations of the functions to access Wikibase through the Scribunto extension * * @since 0.5 * - * @licence GNU GPL v2+ + * @license GNU GPL v2+ * @author Marius Hoch < [email protected] > */ class WikibaseLuaEntityBindings { /** - * @var SnakFormatter + * @var EntityStatementsRenderer */ - private $snakFormatter; - - /** - * @var EntityLookup - */ - private $entityLookup; - - /** - * @var UsageAccumulator - */ - private $usageAccumulator; - - /** - * @var string - */ - private $siteId; - - /** - * @var Language - */ - private $language; + private $entityStatementsRenderer; /** * @var EntityIdParser @@ -56,135 +26,43 @@ private $entityIdParser; /** - * @var EntityDocument[] + * @var string */ - private $entities = array(); + private $siteId; /** - * @param SnakFormatter $snakFormatter - * @param EntityLookup $entityLookup - * @param UsageAccumulator $usageAccumulator - * @param string $siteId - * @param Language $language + * @param EntityStatementsRenderer $entityStatementsRenderer * @param EntityIdParser $entityIdParser + * @param string $siteId */ public function __construct( - SnakFormatter $snakFormatter, - EntityLookup $entityLookup, - UsageAccumulator $usageAccumulator, - $siteId, - Language $language, - EntityIdParser $entityIdParser + EntityStatementsRenderer $entityStatementsRenderer, + EntityIdParser $entityIdParser, + $siteId ) { - $this->snakFormatter = $snakFormatter; - $this->entityLookup = $entityLookup; - $this->usageAccumulator = $usageAccumulator; - $this->siteId = $siteId; - $this->language = $language; + $this->entityStatementsRenderer = $entityStatementsRenderer; $this->entityIdParser = $entityIdParser; + $this->siteId = $siteId; } /** - * Render the main Snaks belonging to a Statement (which is identified by a PropertyId). + * Render the main Snaks belonging to a Statement (which is identified by a PropertyId + * or the label of a Property). * * @since 0.5 - * @todo Share code with LanguageAwareRenderer. * * @param string $entityId - * @param string $propertyId + * @param string $propertyLabelOrId * @param int[]|null $acceptableRanks * - * @return string - */ - public function formatPropertyValues( $entityId, $propertyId, array $acceptableRanks = null ) { - $propertyId = new PropertyId( $propertyId ); - - $entity = $this->getEntity( $this->entityIdParser->parse( $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 = $statements->getBestStatements(); - } else { - // ... unless the user passed in a table of acceptable ranks - $statements = $statements->getWithRank( $acceptableRanks ); - } - - $snakList = $statements->getMainSnaks(); - - $this->trackUsage( $snakList ); - return $this->formatSnakList( $snakList ); - } - - /** - * 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 EntityDocument|null - */ - private function getEntity( EntityId $entityId ) { - if ( !isset( $this->entities[ $entityId->getSerialization() ] ) ) { - $this->entities[ $entityId->getSerialization() ] = - $this->entityLookup->getEntity( $entityId ); - } - - return $this->entities[ $entityId->getSerialization() ]; - } - - /** - * @param Snak[] $snaks + * @throws PropertyLabelNotResolvedException * * @return string */ - private function formatSnakList( array $snaks ) { - $formattedValues = $this->formatSnaks( $snaks ); - return $this->language->commaList( $formattedValues ); - } + public function formatPropertyValues( $entityId, $propertyLabelOrId, array $acceptableRanks = null ) { + $entityId = $this->entityIdParser->parse( $entityId ); - /** - * @param Snak[] $snaks - * - * @return string[] - */ - private function formatSnaks( array $snaks ) { - $formattedValues = array(); - - foreach ( $snaks as $snak ) { - $formattedValues[] = $this->snakFormatter->formatSnak( $snak ); - } - - return $formattedValues; - } - - /** - * @todo Share code with LanguageAwareRenderer::trackUsage - * @param Snak[] $snaks - */ - private function trackUsage( array $snaks ) { - // Note: we track any EntityIdValue as a label usage. - // This is making assumptions about what the respective formatter actually does. - // Ideally, the formatter itself would perform the tracking, but that seems nasty to model. - - foreach ( $snaks as $snak ) { - if ( !( $snak instanceof PropertyValueSnak ) ) { - continue; - } - - $value = $snak->getDataValue(); - - if ( $value instanceof EntityIdValue ) { - $this->usageAccumulator->addLabelUsage( $value->getEntityId() ); - } - } + return $this->entityStatementsRenderer->render( $entityId, $propertyLabelOrId, $acceptableRanks ); } /** diff --git a/client/includes/scribunto/mw.wikibase.entity.lua b/client/includes/scribunto/mw.wikibase.entity.lua index 68609d5..1f464f0 100644 --- a/client/includes/scribunto/mw.wikibase.entity.lua +++ b/client/includes/scribunto/mw.wikibase.entity.lua @@ -116,21 +116,25 @@ -- Get the formatted value of the claims with the given property id -- --- @param propertyId +-- @param propertyLabelOrId -- @param acceptableRanks -methodtable.formatPropertyValues = function( entity, propertyId, acceptableRanks ) +methodtable.formatPropertyValues = function( entity, propertyLabelOrId, acceptableRanks ) acceptableRanks = acceptableRanks or nil local formatted = php.formatPropertyValues( entity.id, - propertyId, + propertyLabelOrId, acceptableRanks ) - local label = mw.wikibase.label( propertyId ) + local label = nil + if propertyLabelOrId:match( '^P%d+$' ) then + label = mw.wikibase.label( propertyLabelOrId ) + end + if label == nil then -- Make the label fallback on the entity id for convenience/ consistency - label = entity.id + label = propertyLabelOrId end return { diff --git a/client/tests/phpunit/MockClientStore.php b/client/tests/phpunit/MockClientStore.php index a125e21..6705537 100644 --- a/client/tests/phpunit/MockClientStore.php +++ b/client/tests/phpunit/MockClientStore.php @@ -27,6 +27,18 @@ class MockClientStore implements ClientStore { /** + * @var string|null + */ + private $languageCode; + + /** + * @param string|null $languageCode + */ + public function __construct( $languageCode = null ) { + $this->languageCode = $languageCode; + } + + /** * @var MockRepository|null */ private static $mockRepository = null; @@ -69,7 +81,10 @@ * @return PropertyLabelResolver */ public function getPropertyLabelResolver() { - // FIXME: Incomplete + return new MockPropertyLabelResolver( + $this->languageCode ?: 'en', + $this->getMockRepository() + ); } /** @@ -111,6 +126,9 @@ public function rebuild() { } + /** + * @return MockRepository + */ private function getMockRepository() { if ( self::$mockRepository === null ) { self::$mockRepository = new MockRepository(); diff --git a/client/tests/phpunit/includes/DataAccess/EntityStatementsRendererTest.php b/client/tests/phpunit/includes/DataAccess/EntityStatementsRendererTest.php new file mode 100644 index 0000000..7e50a8a --- /dev/null +++ b/client/tests/phpunit/includes/DataAccess/EntityStatementsRendererTest.php @@ -0,0 +1,243 @@ +<?php + +namespace Wikibase\Client\Tests\DataAccess\PropertyParserFunction; + +use DataValues\StringValue; +use Language; +use Wikibase\Client\Usage\EntityUsage; +use Wikibase\Client\Usage\UsageAccumulator; +use Wikibase\DataAccess\EntityStatementsRenderer; +use Wikibase\DataAccess\PropertyIdResolver; +use Wikibase\DataAccess\SnaksFinder; +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\Lib\PropertyLabelNotResolvedException; +use Wikibase\Lib\SnakFormatter; + +/** + * @covers Wikibase\DataAccess\EntityStatementsRenderer + * + * @group Wikibase + * @group WikibaseClient + * @group WikibaseDataAccess + * + * @licence GNU GPL v2+ + * @author Katie Filbert < [email protected] > + * @author Daniel Kinzler + * @author Marius Hoch < [email protected] > + */ +class EntityStatementsRendererTest extends \PHPUnit_Framework_TestCase { + + /** + * @param array $usages + * + * @return UsageAccumulator + */ + private function getUsageAccumulator( array &$usages ) { + $mock = $this->getMockBuilder( 'Wikibase\Client\Usage\UsageAccumulator' ) + ->disableOriginalConstructor() + ->getMock(); + + $mock->expects( $this->any() ) + ->method( 'addLabelUsage' ) + ->will( $this->returnCallback( + function ( EntityId $id ) use ( &$usages ) { + $usages[] = new EntityUsage( $id, EntityUsage::LABEL_USAGE ); + } + ) ); + + $mock->expects( $this->never() ) + ->method( 'addAllUsage' ); + + $mock->expects( $this->never() ) + ->method( 'addSiteLinksUsage' ); + + return $mock; + } + + /** + * @param PropertyIdResolver $propertyIdResolver + * @param SnaksFinder $snaksFinder + * @param string $languageCode + * @param array &$usages + * + * @return EntityStatementsRenderer + */ + private function getRenderer( PropertyIdResolver $propertyIdResolver, SnaksFinder $snaksFinder, $languageCode, array &$usages = array() ) { + $targetLanguage = Language::factory( $languageCode ); + + return new EntityStatementsRenderer( + $targetLanguage, + $propertyIdResolver, + $snaksFinder, + $this->getSnakFormatter(), + $this->getUsageAccumulator( $usages ) + ); + } + + public function testRender() { + $propertyId = new PropertyId( 'P1337' ); + $snaks = array( + 'Q42$1' => new PropertyValueSnak( $propertyId, new StringValue( 'a kitten!' ) ), + 'Q42$2' => new PropertyValueSnak( $propertyId, new StringValue( 'two kittens!!' ) ) + ); + + $usages = array(); + $renderer = $this->getRenderer( + $this->getPropertyIdResolver(), + $this->getSnaksFinder( $snaks ), + 'en', + $usages + ); + + $q42 = new ItemId( 'Q42' ); + $result = $renderer->render( $q42, 'p1337' ); + + $expected = 'a kitten!, two kittens!!'; + $this->assertEquals( $expected, $result ); + } + + public function testRender_PropertyLabelNotResolvedException() { + $usages = array(); + $renderer = $this->getRenderer( + $this->getPropertyIdResolverForPropertyNotFound(), + $this->getSnaksFinder( array() ), + 'en', + $usages + ); + + $this->setExpectedException( 'Wikibase\Lib\PropertyLabelNotResolvedException' ); + $renderer->render( new ItemId( 'Q42' ), 'blah' ); + } + + public function testRender_trackUsage() { + $q22 = new ItemId( 'Q22' ); + $q23 = new ItemId( 'Q23' ); + $propertyId = new PropertyId( 'P1337' ); + $snaks = array( + 'Q42$22' => new PropertyValueSnak( $propertyId, new EntityIdValue( $q22 ) ), + 'Q42$23' => new PropertyValueSnak( $propertyId, new EntityIdValue( $q23 ) ) + ); + + $usages = array(); + $renderer = $this->getRenderer( $this->getPropertyIdResolver(), $this->getSnaksFinder( $snaks ), 'en', $usages ); + + $q42 = new ItemId( 'Q42' ); + $renderer->render( $q42, 'p1337' ); + + $expectedUsage = array( + new EntityUsage( $q22, EntityUsage::LABEL_USAGE ), + new EntityUsage( $q23, EntityUsage::LABEL_USAGE ), + ); + + $this->assertSameUsages( $expectedUsage, $usages ); + } + + /** + * @param EntityUsage[] $expected + * @param EntityUsage[] $actual + * @param string $message + */ + private function assertSameUsages( array $expected, array $actual, $message = '' ) { + $expected = $this->getUsageStrings( $expected ); + $actual = $this->getUsageStrings( $actual ); + + $this->assertEquals( $expected, $actual, $message ); + } + + /** + * @param EntityUsage[] $usages + * + * @return string[] + */ + private function getUsageStrings( array $usages ) { + return array_values( + array_map( function( EntityUsage $usage ) { + return $usage->getIdentityString(); + }, $usages ) + ); + } + + /** + * @param Snak[] $snaks + * + * @return SnaksFinder + */ + private function getSnaksFinder( array $snaks ) { + $snaksFinder = $this->getMockBuilder( + 'Wikibase\DataAccess\SnaksFinder' + ) + ->disableOriginalConstructor() + ->getMock(); + + $snaksFinder->expects( $this->any() ) + ->method( 'findSnaks' ) + ->will( $this->returnValue( $snaks ) ); + + return $snaksFinder; + } + + private function getPropertyIdResolver() { + $propertyIdResolver = $this->getMockBuilder( + 'Wikibase\DataAccess\PropertyIdResolver' + ) + ->disableOriginalConstructor() + ->getMock(); + + $propertyIdResolver->expects( $this->any() ) + ->method( 'resolvePropertyId' ) + ->will( $this->returnValue( new PropertyId( 'P1337' ) ) ); + + return $propertyIdResolver; + } + + private function getPropertyIdResolverForPropertyNotFound() { + $propertyIdResolver = $this->getMockBuilder( + 'Wikibase\DataAccess\PropertyIdResolver' + ) + ->disableOriginalConstructor() + ->getMock(); + + $propertyIdResolver->expects( $this->any() ) + ->method( 'resolvePropertyId' ) + ->will( $this->returnCallback( function( $propertyLabelOrId, $languageCode ) { + throw new PropertyLabelNotResolvedException( $propertyLabelOrId, $languageCode ); + } ) + ); + + return $propertyIdResolver; + } + + /*** + * @return SnakFormatter + */ + private function getSnakFormatter() { + $snakFormatter = $this->getMock( 'Wikibase\Lib\SnakFormatter' ); + + $snakFormatter->expects( $this->any() ) + ->method( 'formatSnak' ) + ->will( $this->returnCallback( + function ( Snak $snak ) { + if ( $snak instanceof PropertyValueSnak ) { + $value = $snak->getDataValue(); + if ( $value instanceof StringValue ) { + return $value->getValue(); + } elseif ( $value instanceof EntityIdValue ) { + return $value->getEntityId()->getSerialization(); + } else { + return '(' . $value->getType() . ')'; + } + } else { + return '(' . $snak->getType() . ')'; + } + } + ) ); + + return $snakFormatter; + } + +} diff --git a/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/LanguageAwareRendererTest.php b/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/LanguageAwareRendererTest.php index d42954b..4cbf580 100644 --- a/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/LanguageAwareRendererTest.php +++ b/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/LanguageAwareRendererTest.php @@ -6,9 +6,10 @@ use Language; use Wikibase\Client\Usage\EntityUsage; use Wikibase\Client\Usage\UsageAccumulator; +use Wikibase\DataAccess\EntityStatementsRenderer; 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; @@ -70,12 +71,17 @@ private function getRenderer( PropertyIdResolver $propertyIdResolver, SnaksFinder $snaksFinder, $languageCode, array &$usages = array() ) { $targetLanguage = Language::factory( $languageCode ); - return new LanguageAwareRenderer( + $entityStatementsRenderer = new EntityStatementsRenderer( $targetLanguage, $propertyIdResolver, $snaksFinder, $this->getSnakFormatter(), $this->getUsageAccumulator( $usages ) + ); + + return new LanguageAwareRenderer( + $targetLanguage, + $entityStatementsRenderer ); } @@ -151,7 +157,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 ) + ), ); } diff --git a/client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua b/client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua index 370ce9b..5e52da1 100644 --- a/client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua +++ b/client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua @@ -92,6 +92,12 @@ return entity:formatPropertyValues( propertyId, ranks ) end +local function integrationTestFormatPropertyValuesByLabel() + local entity = mw.wikibase.getEntityObject() + + return entity:formatPropertyValues( 'LuaTestStringProperty' ) +end + local function integrationTestFormatPropertyValuesProperty() local entity = mw.wikibase.getEntityObject( 'P342' ) @@ -188,6 +194,9 @@ args = { { mw.wikibase.entity.claimRanks.RANK_TRUTH } }, expect = { { label = 'LuaTestStringProperty', value = '' } } }, + { name = 'mw.wikibase.entity.formatPropertyValues integration 4', func = integrationTestFormatPropertyValuesByLabel, + expect = { { label = 'LuaTestStringProperty', value = 'Lua :)' } } + }, { name = 'mw.wikibase.entity.formatPropertyValues integration property', func = integrationTestFormatPropertyValuesProperty, expect = { { label = 'LuaTestStringProperty', value = 'Lua :)' } } }, diff --git a/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseEntityLibraryTest.php b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseEntityLibraryTest.php index 7956204..2130012 100644 --- a/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseEntityLibraryTest.php +++ b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseEntityLibraryTest.php @@ -64,10 +64,12 @@ ); } - public function testFormatPropertyValuesInvalidPropertyId() { - $this->setExpectedException( 'ScribuntoException' ); + public function testFormatPropertyValues_noPropertyId() { $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary(); - $luaWikibaseLibrary->formatPropertyValues( 'Q1', '$invalidEntityId€', array() ); + $this->assertSame( + array( '' ), + $luaWikibaseLibrary->formatPropertyValues( 'Q1', 'father', array() ) + ); } private function newScribuntoLuaWikibaseLibrary() { diff --git a/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTestCase.php b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTestCase.php index 865af70..6afe6a2 100644 --- a/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTestCase.php +++ b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTestCase.php @@ -55,7 +55,7 @@ $store = $wikibaseClient->getStore(); if ( ! $store instanceof MockClientStore ) { - $store = new MockClientStore(); + $store = new MockClientStore( 'de' ); $wikibaseClient->overrideStore( $store ); } diff --git a/client/tests/phpunit/includes/scribunto/WikibaseLuaEntityBindingsTest.php b/client/tests/phpunit/includes/scribunto/WikibaseLuaEntityBindingsTest.php index a8b297b..d834f20 100644 --- a/client/tests/phpunit/includes/scribunto/WikibaseLuaEntityBindingsTest.php +++ b/client/tests/phpunit/includes/scribunto/WikibaseLuaEntityBindingsTest.php @@ -2,23 +2,10 @@ namespace Wikibase\Client\Tests\Scribunto; -use Language; use Wikibase\Client\Scribunto\WikibaseLuaEntityBindings; -use Wikibase\Client\Usage\EntityUsage; -use Wikibase\Client\Usage\HashUsageAccumulator; -use Wikibase\Client\Usage\UsageAccumulator; -use Wikibase\DataModel\Claim\Claim; use Wikibase\DataModel\Entity\BasicEntityIdParser; -use Wikibase\DataModel\Entity\EntityDocument; -use Wikibase\DataModel\Entity\EntityIdValue; -use Wikibase\DataModel\Entity\Item; use Wikibase\DataModel\Entity\ItemId; -use Wikibase\DataModel\Entity\PropertyId; -use Wikibase\DataModel\Snak\PropertyValueSnak; use Wikibase\DataModel\Statement\Statement; -use Wikibase\Lib\SnakFormatter; -use Wikibase\Lib\Store\EntityLookup; -use Wikibase\Test\MockRepository; /** * @covers Wikibase\Client\Scribunto\WikibaseLuaEntityBindings @@ -32,105 +19,37 @@ */ class WikibaseLuaEntityBindingsTest extends \PHPUnit_Framework_TestCase { - public function testConstructor() { - $wikibaseLuaEntityBindings = $this->getWikibaseLuaEntityBindings(); + /** + * @return WikibaseLuaEntityBindings + */ + private function getWikibaseLuaEntityBindings() { + $entityStatementsRenderer = $this->getMockBuilder( 'Wikibase\DataAccess\EntityStatementsRenderer' ) + ->disableOriginalConstructor() + ->getMock(); - $this->assertInstanceOf( - 'Wikibase\Client\Scribunto\WikibaseLuaEntityBindings', - $wikibaseLuaEntityBindings - ); - } - - private function getWikibaseLuaEntityBindings( - EntityLookup $entityLookup = null, - UsageAccumulator $usageAccumulator = null - ) { - $language = new Language( 'en' ); + $entityStatementsRenderer->expects( $this->any() ) + ->method( 'render' ) + ->with( new ItemId( 'Q12' ), 'some label', array( Statement::RANK_DEPRECATED ) ) + ->will( $this->returnValue( 'Kittens > Cats' ) ); return new WikibaseLuaEntityBindings( - $this->getSnakFormatter(), - $entityLookup ?: new MockRepository(), - $usageAccumulator ?: new HashUsageAccumulator(), - 'enwiki', - $language, - new BasicEntityIdParser() + $entityStatementsRenderer, + new BasicEntityIdParser(), + 'enwiki' ); } - /** - * @return Item - */ - private function getItem() { - $propertyId = new PropertyId( 'P123456' ); - $snak = new PropertyValueSnak( $propertyId, new EntityIdValue( new ItemId( 'Q11' ) )); - $statement = new Statement( new Claim( $snak ) ); - $statement->setGuid( 'gsdfgsadg' ); - - $item = Item::newEmpty(); - $item->addClaim( $statement ); - - return $item; - } - - /** - * @param EntityDocument|null $entity - * - * @return EntityLookup - */ - private function getEntityLookup( EntityDocument $entity = null ) { - $entityLookup = $this->getMock( 'Wikibase\Lib\Store\EntityLookup' ); - - $entityLookup->expects( $this->any() )->method( 'getEntity' ) - ->will( $this->returnValue( $entity ) ); - - return $entityLookup; - } - - /** - * @return SnakFormatter - */ - private function getSnakFormatter() { - $snakFormatter = $this->getMock( 'Wikibase\Lib\SnakFormatter' ); - - $snakFormatter->expects( $this->any() )->method( 'formatSnak' ) - ->will( $this->returnValue( 'Snak snak snak' ) ); - - return $snakFormatter; - } - - public function testFormatPropertyValues() { - $item = $this->getItem(); + $wikibaseLuaEntityBindings = $this->getWikibaseLuaEntityBindings(); - $entityLookup = $this->getEntityLookup( $item ); - $usageAccumulator = new HashUsageAccumulator(); - $wikibaseLuaEntityBindings = $this->getWikibaseLuaEntityBindings( $entityLookup, $usageAccumulator ); - - $ret = $wikibaseLuaEntityBindings->formatPropertyValues( 'Q1', 'P123456' ); - - $this->assertSame( 'Snak snak snak', $ret ); - - $expectedUsage = new EntityUsage( new ItemId( 'Q11' ), EntityUsage::LABEL_USAGE ); - $usages = $usageAccumulator->getUsages(); - $this->assertArrayHasKey( $expectedUsage->getIdentityString(), $usages ); - } - - public function testFormatPropertyValuesNoProperty() { - $entityLookup = $this->getEntityLookup( Item::newEmpty() ); - - $wikibaseLuaEntityBindings = $this->getWikibaseLuaEntityBindings( $entityLookup ); - $ret = $wikibaseLuaEntityBindings->formatPropertyValues( 'Q2', 'P123456' ); - - $this->assertSame( '', $ret ); - } - - public function testFormatPropertyValuesNoEntity() { - $entityLookup = $this->getEntityLookup(); - - $wikibaseLuaEntityBindings = $this->getWikibaseLuaEntityBindings( $entityLookup ); - $ret = $wikibaseLuaEntityBindings->formatPropertyValues( 'Q3', 'P123456' ); - - $this->assertSame( '', $ret ); + $this->assertEquals( + 'Kittens > Cats', + $wikibaseLuaEntityBindings->formatPropertyValues( + 'Q12', + 'some label', + array( Statement::RANK_DEPRECATED ) + ) + ); } public function testGetGlobalSiteId() { diff --git a/client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php b/client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php index 274e6f1..04c4c23 100644 --- a/client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php +++ b/client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php @@ -31,7 +31,7 @@ protected $mockRepository; public function __construct() { - $clientStore = new MockClientStore(); + $clientStore = new MockClientStore( 'de' ); $this->mockRepository = $clientStore->getEntityLookup(); } diff --git a/docs/lua.wiki b/docs/lua.wiki index 4dd05da..4f225a5 100644 --- a/docs/lua.wiki +++ b/docs/lua.wiki @@ -73,9 +73,9 @@ </source> === mw.wikibase.entity:formatPropertyValues === -<code>entity:formatPropertyValues( propertyId )</code><br> -<code>entity:formatPropertyValues( propertyId, acceptableRanks )</code><br> -Get the formatted value of the claims with the given property id. Per default only the best claims will be returned. +<code>entity:formatPropertyValues( propertyLabelOrId )</code><br> +<code>entity:formatPropertyValues( propertyLabelOrId, acceptableRanks )</code><br> +Get the formatted value of the claims with the given property (which is either identified by a property id, or by the label of the property). Per default only the best claims will be returned. Alternatively a table with acceptable ranks can be given as second parameter (a mapping table with all ranks can be found in [[#mw.wikibase.entity.claimRanks|<code>mw.wikibase.entity.claimRanks</code>]]). An example call might look like this: @@ -83,6 +83,9 @@ -- Return a table like: { value = "Formatted claim value", label = "Label of the Property" } entity:formatPropertyValues( 'P12' ) +-- As above, but uses the label of the Property instead of the id +entity:formatPropertyValues( 'father' ) + -- Return the normal ranked claims with the property Id 42 (same format as above) entity:formatPropertyValues( 'P42', { mw.wikibase.entity.claimRanks.RANK_NORMAL } ) </source> -- To view, visit https://gerrit.wikimedia.org/r/189415 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: If4d2cd33e3851576f31ac64e42788d00e9badd7e 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
