Hoo man has submitted this change and it was merged. Change subject: Add mw.wikibase.renderSnak(s) for rendering arbitrary Snaks ......................................................................
Add mw.wikibase.renderSnak(s) for rendering arbitrary Snaks This is useful for displaying References or Qualifiers. Bug: T76213 Change-Id: Ie1f006927f717acf11cd5a72a1dfa00f7be9d66e --- M client/i18n/en.json M client/i18n/qqq.json M client/includes/WikibaseClient.php M client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php A client/includes/scribunto/SnakSerializationRenderer.php M client/includes/scribunto/mw.wikibase.lua M client/tests/phpunit/includes/WikibaseClientTest.php M client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua M client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTest.php A client/tests/phpunit/includes/scribunto/SnakSerializationRendererTest.php M client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php M docs/lua.wiki 12 files changed, 555 insertions(+), 20 deletions(-) Approvals: Hoo man: Looks good to me, approved jenkins-bot: Verified diff --git a/client/i18n/en.json b/client/i18n/en.json index 66a5502..94ce595 100644 --- a/client/i18n/en.json +++ b/client/i18n/en.json @@ -56,6 +56,7 @@ "wikibase-rc-wikibase-edit-title": "{{WBREPONAME}} edit", "wikibase-replicationnote": "Please notice that it can take several minutes until the changes are visible on all wikis.", "wikibase-watchlist-show-changes-pref": "Show {{WBREPONAME}} edits in your watchlist", + "wikibase-error-deserialize-error": "Failed to deserialize data.", "wikibase-error-serialize-error": "Failed to serialize data.", "wikibase-error-invalid-entity-id": "The ID entered is unknown to the system. Please use a valid entity ID.", "special-unconnectedpages": "Pages not connected to items", diff --git a/client/i18n/qqq.json b/client/i18n/qqq.json index 9774dfa..0dd7c9a 100644 --- a/client/i18n/qqq.json +++ b/client/i18n/qqq.json @@ -66,6 +66,7 @@ "wikibase-rc-wikibase-edit-title": "Tooltip for {{msg-mw|wikibase-rc-wikibase-edit-letter}}.\n\nSee also:\n* {{msg-mw|Recentchanges-label-newpage}}\n* {{msg-mw|Recentchanges-label-minor}}\n* {{msg-mw|Recentchanges-label-bot}}\n* {{msg-mw|Recentchanges-label-unpatrolled}}\n{{Identical|Edit}}", "wikibase-replicationnote": "Note telling the user that it can take a few minutes until the made changes are visible on all wikis.\nPreceded by message {{msg-mw|Wikibase-linkitem-success-link}}", "wikibase-watchlist-show-changes-pref": "Option in the watchlist section of preferences to always show wikibase edits by default in the watchlist. Appears as a checkbox in the same list with options such as:\n* {{msg-mw|tog-watchdefault}}\n* {{msg-mw|tog-watchmoves}}\nAnd so on.", + "wikibase-error-deserialize-error": "Generic error when invalid (undeserializable) data has been given.", "wikibase-error-serialize-error": "Generic error for when entity data failed to serialize or cannot be handled.", "wikibase-error-invalid-entity-id": "Generic error message when an invalid entity ID was entered.", "special-unconnectedpages": "{{doc-special|UnconnectedPages}}", diff --git a/client/includes/WikibaseClient.php b/client/includes/WikibaseClient.php index 4c8ce45..dca79be 100644 --- a/client/includes/WikibaseClient.php +++ b/client/includes/WikibaseClient.php @@ -27,6 +27,7 @@ use Wikibase\DataAccess\PropertyParserFunction\PropertyClaimsRendererFactory; use Wikibase\DataAccess\PropertyParserFunction\Runner; use Wikibase\DataAccess\PropertyParserFunction\SnaksFinder; +use Wikibase\DataModel\DeserializerFactory; use Wikibase\DataModel\Entity\BasicEntityIdParser; use Wikibase\DataModel\Entity\DispatchingEntityIdParser; use Wikibase\DataModel\Entity\EntityIdParser; @@ -35,7 +36,7 @@ use Wikibase\DataModel\Entity\PropertyDataTypeLookup; use Wikibase\DirectSqlStore; use Wikibase\EntityFactory; -use Wikibase\InternalSerialization\DeserializerFactory; +use Wikibase\InternalSerialization\DeserializerFactory as InternalDeserializerFactory; use Wikibase\LangLinkHandler; use Wikibase\LanguageFallbackChainFactory; use Wikibase\Lib\Changes\EntityChangeFactory; @@ -620,24 +621,31 @@ } /** - * @return DeserializerFactory + * @return InternalDeserializerFactory */ - protected function getInternalDeserializerFactory() { - return new DeserializerFactory( - new DataValueDeserializer( array( - 'boolean' => 'DataValues\BooleanValue', - 'number' => 'DataValues\NumberValue', - 'string' => 'DataValues\StringValue', - 'unknown' => 'DataValues\UnknownValue', - 'globecoordinate' => 'DataValues\Geo\Values\GlobeCoordinateValue', - 'monolingualtext' => 'DataValues\MonolingualTextValue', - 'multilingualtext' => 'DataValues\MultilingualTextValue', - 'quantity' => 'DataValues\QuantityValue', - 'time' => 'DataValues\TimeValue', - 'wikibase-entityid' => 'Wikibase\DataModel\Entity\EntityIdValue', - ) ), + private function getInternalDeserializerFactory() { + return new InternalDeserializerFactory( + $this->getDataValueDeserializer(), $this->getEntityIdParser() ); + } + + /** + * @return DataValueDeserializer + */ + private function getDataValueDeserializer() { + return new DataValueDeserializer( array( + 'boolean' => 'DataValues\BooleanValue', + 'number' => 'DataValues\NumberValue', + 'string' => 'DataValues\StringValue', + 'unknown' => 'DataValues\UnknownValue', + 'globecoordinate' => 'DataValues\Geo\Values\GlobeCoordinateValue', + 'monolingualtext' => 'DataValues\MonolingualTextValue', + 'multilingualtext' => 'DataValues\MultilingualTextValue', + 'quantity' => 'DataValues\QuantityValue', + 'time' => 'DataValues\TimeValue', + 'wikibase-entityid' => 'Wikibase\DataModel\Entity\EntityIdValue', + ) ); } /** @@ -763,4 +771,13 @@ ); } + /** + * @return DeserializerFactory + */ + public function getDeserializerFactory() { + return new DeserializerFactory( + $this->getDataValueDeserializer(), + $this->getEntityIdParser() + ); + } } diff --git a/client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php b/client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php index 42329d1..c4fbc6c 100644 --- a/client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php +++ b/client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php @@ -1,7 +1,11 @@ <?php +use Deserializers\Exceptions\DeserializationException; +use ValueFormatters\FormatterOptions; +use Wikibase\Lib\SnakFormatter; use Wikibase\Client\Scribunto\EntityAccessor; use Wikibase\Client\Scribunto\WikibaseLuaBindings; +use Wikibase\Client\Scribunto\SnakSerializationRenderer; use Wikibase\Client\Usage\ParserOutputUsageAccumulator; use Wikibase\Client\WikibaseClient; use Wikibase\DataModel\Entity\EntityIdParsingException; @@ -28,7 +32,7 @@ private $luaBindings; /** - * @var EntityAccessor + * @var EntityAccessor|null */ private $entityAccessor; @@ -41,6 +45,11 @@ * @var LanguageFallbackChain|null */ private $fallbackChain = null; + + /** + * @var SnakSerializationRenderer|null + */ + private $snakSerializationRenderer; private function getLuaBindings() { if ( !$this->luaBindings ) { @@ -58,6 +67,17 @@ return $this->entityAccessor; } + private function getSnakSerializationRenderer() { + if ( !$this->snakSerializationRenderer ) { + $this->snakSerializationRenderer = $this->newSnakSerializationRenderer(); + } + + return $this->snakSerializationRenderer; + } + + /** + * @return Language + */ private function getLanguage() { // For the language we need $wgContLang, not parser target language or anything else. // See Scribunto_LuaLanguageLibrary::getContLangCode(). @@ -76,6 +96,27 @@ $this->getLanguageFallbackChain(), $this->getLanguage(), Utils::getLanguageCodes() + ); + } + + private function newSnakSerializationRenderer() { + $wikibaseClient = WikibaseClient::getDefaultInstance(); + + $formatterOptions = new FormatterOptions( array( 'language' => $this->getLanguage() ) ); + + $snakFormatter = $wikibaseClient->getSnakFormatterFactory()->getSnakFormatter( + SnakFormatter::FORMAT_WIKI, $formatterOptions + ); + + $snakDeserializer = $wikibaseClient->getDeserializerFactory()->newSnakDeserializer(); + $snaksDeserializer = $wikibaseClient->getDeserializerFactory()->newSnaksDeserializer(); + + return new SnakSerializationRenderer( + $snakFormatter, + $snakDeserializer, + $this->getLanguage(), + $snaksDeserializer, + $this->getUsageAccumulator() ); } @@ -139,6 +180,8 @@ 'getLabel' => array( $this, 'getLabel' ), 'getEntity' => array( $this, 'getEntity' ), 'getSetting' => array( $this, 'getSetting' ), + 'renderSnak' => array( $this, 'renderSnak' ), + 'renderSnaks' => array( $this, 'renderSnaks' ), 'getEntityId' => array( $this, 'getEntityId' ), 'getSiteLinkPageName' => array( $this, 'getSiteLinkPageName' ), ); @@ -149,7 +192,7 @@ } /** - * Wrapper for getEntity in Scribunto_LuaWikibaseLibraryImplementation + * Wrapper for getEntity in EntityAccessor * * @since 0.5 * @@ -229,4 +272,44 @@ $this->checkType( 'getSiteLinkPageName', 1, $prefixedEntityId, 'string' ); return array( $this->getLuaBindings()->getSiteLinkPageName( $prefixedEntityId ) ); } + + /** + * Wrapper for renderSnak in SnakRenderer + * + * @since 0.5 + * + * @param array $snakSerialization + * + * @throws ScribuntoException + * @return string[] + */ + public function renderSnak( $snakSerialization ) { + $this->checkType( 'renderSnak', 1, $snakSerialization, 'table' ); + try { + $ret = array( $this->getSnakSerializationRenderer()->renderSnak( $snakSerialization ) ); + return $ret; + } catch ( DeserializationException $e ) { + throw new ScribuntoException( 'wikibase-error-deserialize-error' ); + } + } + + /** + * Wrapper for renderSnaks in SnakRenderer + * + * @since 0.5 + * + * @param array $snaksSerialization + * + * @throws ScribuntoException + * @return string[] + */ + public function renderSnaks( $snaksSerialization ) { + $this->checkType( 'renderSnaks', 1, $snaksSerialization, 'table' ); + try { + $ret = array( $this->getSnakSerializationRenderer()->renderSnaks( $snaksSerialization ) ); + return $ret; + } catch ( DeserializationException $e ) { + throw new ScribuntoException( 'wikibase-error-deserialize-error' ); + } + } } diff --git a/client/includes/scribunto/SnakSerializationRenderer.php b/client/includes/scribunto/SnakSerializationRenderer.php new file mode 100644 index 0000000..9e30dac --- /dev/null +++ b/client/includes/scribunto/SnakSerializationRenderer.php @@ -0,0 +1,142 @@ +<?php + +namespace Wikibase\Client\Scribunto; + +use Language; +use Wikibase\Client\Usage\UsageAccumulator; +use Wikibase\DataModel\Deserializers\SnakListDeserializer; +use Wikibase\DataModel\Deserializers\SnakDeserializer; +use Wikibase\DataModel\Snak\PropertyValueSnak; +use Wikibase\DataModel\Entity\EntityIdValue; +use Wikibase\Lib\SnakFormatter; + +/** + * Functionality needed to render snaks as provided through Lua. + * + * @since 0.5 + * + * @license GNU GPL v2+ + * @author Marius Hoch < h...@online.de > + */ +class SnakSerializationRenderer { + + /** + * @var SnakFormatter + */ + private $snakFormatter; + + /** + * @var SnakDeserializer + */ + private $snakDeserializer; + + /** + * @var Language + */ + private $language; + + /** + * @var SnakListDeserializer + */ + private $snakListDeserializer; + + /** + * @var UsageAccumulator + */ + private $usageAccumulator; + + /** + * @param SnakFormatter $snakFormatter + * @param SnakDeserializer $snakDeserializer + * @param Language $language + * @param SnakListDeserializer $snakListDeserializer + * @param UsageAccumulator $usageAccumulator + */ + public function __construct( + SnakFormatter $snakFormatter, + SnakDeserializer $snakDeserializer, + Language $language, + SnakListDeserializer $snakListDeserializer, + UsageAccumulator $usageAccumulator + ) { + $this->snakFormatter = $snakFormatter; + $this->snakDeserializer = $snakDeserializer; + $this->language = $language; + $this->snakListDeserializer = $snakListDeserializer; + $this->usageAccumulator = $usageAccumulator; + } + + /** + * Render a snak from its serialization as provided from Lua. + * + * @since 0.5 + * + * @param array $snakSerialization As obtained from ItemSerializer + * + * @return string wikitext + */ + public function renderSnak( array $snakSerialization ) { + $snak = $this->snakDeserializer->deserialize( $snakSerialization ); + + $this->trackUsage( array( $snak ) ); + + return $this->snakFormatter->formatSnak( $snak ); + } + + /** + * Render a list of snaks from their serialization as provided from Lua. + * + * @since 0.5 + * + * @param array $snaksSerialization Nested array structure, as obtained from ItemSerializer + * + * @return string wikitext, snaks are comma separated + */ + public function renderSnaks( array $snaksSerialization ) { + $snaks = $this->snakListDeserializer->deserialize( $snaksSerialization ); + + if ( $snaks->isEmpty() ) { + return ''; + } + + $snaks = iterator_to_array( $snaks ); + $this->trackUsage( $snaks ); + return $this->formatSnakList( $snaks ); + } + + /** + * @param Snak[] $snaks + * + * @return string + */ + private function formatSnakList( array $snaks ) { + $formattedValues = array_map( + array( $this->snakFormatter, 'formatSnak' ), + $snaks + ); + + return $this->language->commaList( $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() ); + } + } + } +} \ No newline at end of file diff --git a/client/includes/scribunto/mw.wikibase.lua b/client/includes/scribunto/mw.wikibase.lua index 1ac0ff2..58a7893 100644 --- a/client/includes/scribunto/mw.wikibase.lua +++ b/client/includes/scribunto/mw.wikibase.lua @@ -105,6 +105,29 @@ return php.getSiteLinkPageName( id ) end + + -- Render a Snak from its serialization + -- + -- @param snakSerialization + wikibase.renderSnak = function( snakSerialization ) + if type( snakSerialization ) ~= 'table' then + error( 'snakSerialization must be a table, ' .. type( snakSerialization ) .. ' given', 2 ) + end + + return php.renderSnak( snakSerialization ) + end + + -- Render a list of Snaks from their serialization + -- + -- @param snaksSerialization + wikibase.renderSnaks = function( snaksSerialization ) + if type( snaksSerialization ) ~= 'table' then + error( 'snaksSerialization must be a table, ' .. type( snaksSerialization ) .. ' given', 2 ) + end + + return php.renderSnaks( snaksSerialization ) + end + mw = mw or {} mw.wikibase = wikibase package.loaded['mw.wikibase'] = wikibase diff --git a/client/tests/phpunit/includes/WikibaseClientTest.php b/client/tests/phpunit/includes/WikibaseClientTest.php index e992372..7cdfb60 100644 --- a/client/tests/phpunit/includes/WikibaseClientTest.php +++ b/client/tests/phpunit/includes/WikibaseClientTest.php @@ -151,6 +151,11 @@ $this->assertInstanceOf( 'Wikibase\Lib\OutputFormatValueFormatterFactory', $returnValue ); } + public function testGetDeserializerFactoryReturnType() { + $returnValue = $this->getWikibaseClient()->getDeserializerFactory(); + $this->assertInstanceOf( 'Wikibase\DataModel\DeserializerFactory', $returnValue ); + } + public function testGetLanguageLinkBadgeDisplay() { $returnValue = $this->getWikibaseClient()->getLanguageLinkBadgeDisplay(); $this->assertInstanceOf( 'Wikibase\Client\Hooks\LanguageLinkBadgeDisplay', $returnValue ); diff --git a/client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua b/client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua index aafba0b..c44a5b2 100644 --- a/client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua +++ b/client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua @@ -36,6 +36,20 @@ return mw.wikibase.getEntityObject( 'Q199024' ):getLabel( 'de' ) end +local function testRenderSnak() + local entity = mw.wikibase.getEntityObject( 'Q32487' ) + local snak = entity['claims']['P342'][1]['qualifiers']['P342'][1] + + return mw.wikibase.renderSnak( snak ) +end + +local function testRenderSnaks() + local entity = mw.wikibase.getEntityObject( 'Q32487' ) + local snaks = entity['claims']['P342'][1]['qualifiers'] + + return mw.wikibase.renderSnaks( snaks ) +end + local tests = { -- Integration tests @@ -80,7 +94,21 @@ { name = 'mw.wikibase.sitelink', func = mw.wikibase.sitelink, type='ToString', args = { 'Q32488' }, expect = { nil } - } + }, + { name = 'mw.wikibase.renderSnak', func = testRenderSnak, type='ToString', + expect = { 'A qualifier Snak' } + }, + { name = 'mw.wikibase.renderSnak - (must be table)', func = mw.wikibase.renderSnak, + args = { 'meep' }, + expect = 'snakSerialization must be a table, string given' + }, + { name = 'mw.wikibase.renderSnaks', func = testRenderSnaks, type='ToString', + expect = { 'A qualifier Snak, Moar qualifiers' } + }, + { name = 'mw.wikibase.renderSnaks - (must be table)', func = mw.wikibase.renderSnaks, + args = { 'meep' }, + expect = 'snaksSerialization must be a table, string given' + }, } return testframework.getTestProvider( tests ) diff --git a/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTest.php b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTest.php index d2990d1..02d9dc9 100644 --- a/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTest.php +++ b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTest.php @@ -8,7 +8,6 @@ use Scribunto; use Scribunto_LuaWikibaseLibrary; use Title; -use Wikibase\Client\WikibaseClient; /** * @covers Scribunto_LuaWikibaseLibrary @@ -107,6 +106,42 @@ $this->assertEquals( array( null ), $entityId ); } + public function testRenderSnak() { + $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary(); + $entityArr = $luaWikibaseLibrary->getEntity( 'Q32487', false ); + + $snak = $entityArr[0]['claims']['P342'][1]['qualifiers']['P342'][1]; + $this->assertSame( + array( 'A qualifier Snak' ), + $luaWikibaseLibrary->renderSnak( $snak ) + ); + } + + public function testRenderSnak_invalidSerialization() { + $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary(); + + $this->setExpectedException( 'ScribuntoException' ); + $luaWikibaseLibrary->renderSnak( array( 'a' => 'b' ) ); + } + + public function testRenderSnaks() { + $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary(); + $entityArr = $luaWikibaseLibrary->getEntity( 'Q32487', false ); + + $snaks = $entityArr[0]['claims']['P342'][1]['qualifiers']; + $this->assertSame( + array( 'A qualifier Snak, Moar qualifiers' ), + $luaWikibaseLibrary->renderSnaks( $snaks ) + ); + } + + public function testRenderSnaks_invalidSerialization() { + $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary(); + + $this->setExpectedException( 'ScribuntoException' ); + $luaWikibaseLibrary->renderSnaks( array( 'a' => 'b' ) ); + } + private function newScribuntoLuaWikibaseLibrary() { $title = Title::newFromText( 'Whatever' ); $parser = new Parser(); diff --git a/client/tests/phpunit/includes/scribunto/SnakSerializationRendererTest.php b/client/tests/phpunit/includes/scribunto/SnakSerializationRendererTest.php new file mode 100644 index 0000000..a7cdd10 --- /dev/null +++ b/client/tests/phpunit/includes/scribunto/SnakSerializationRendererTest.php @@ -0,0 +1,155 @@ +<?php + +namespace Wikibase\Client\Tests\Scribunto; + +use Language; +use PHPUnit_Framework_TestCase; +use DataValues\StringValue; +use DataValues\DataValue; +use Wikibase\DataModel\Entity\EntityIdValue; +use Wikibase\Client\Scribunto\SnakSerializationRenderer; +use Wikibase\Client\Usage\EntityUsage; +use Wikibase\Client\Usage\HashUsageAccumulator; +use Wikibase\Client\Usage\UsageAccumulator; +use Wikibase\Client\WikibaseClient; +use Wikibase\DataModel\Entity\ItemId; +use Wikibase\DataModel\Entity\PropertyId; +use Wikibase\DataModel\Snak\PropertyValueSnak; +use Wikibase\Lib\Serializers\SnakSerializer; + +/** + * @covers Wikibase\Client\Scribunto\SnakSerializationRenderer + * + * @group Wikibase + * @group WikibaseClient + * @group WikibaseScribunto + * + * @license GNU GPL v2+ + * @author Marius Hoch < h...@online.de > + */ +class SnakSerializationRendererTest extends PHPUnit_Framework_TestCase { + + /** + * @return array + */ + private function getSnakSerialization( DataValue $value ) { + $snak = new PropertyValueSnak( + new PropertyId( 'P42' ), + $value + ); + + $snakSerializer = new SnakSerializer(); + $serialized = $snakSerializer->getSerialized( $snak ); + + return $serialized; + } + + /** + * @return SnakSerializationRenderer + */ + private function getSnakRenderer( UsageAccumulator $usageAccumulator ) { + $wikibaseClient = WikibaseClient::getDefaultInstance(); + + $snakFormatter = $this->getMock( 'Wikibase\Lib\SnakFormatter' ); + $snakFormatter->expects( $this->any() ) + ->method( 'formatSnak' ) + ->will( $this->returnCallback( function ( PropertyValueSnak $snak ) { + $value = $snak->getDataValue(); + if ( $value instanceof EntityIdValue ) { + return $value->getEntityId()->getSerialization(); + } else { + return $value->getValue(); + } + } ) ); + + $snakDeserializer = $wikibaseClient->getDeserializerFactory()->newSnakDeserializer(); + $snaksDeserializer = $wikibaseClient->getDeserializerFactory()->newSnaksDeserializer(); + + return new SnakSerializationRenderer( + $snakFormatter, + $snakDeserializer, + Language::factory( 'en' ), + $snaksDeserializer, + $usageAccumulator + ); + } + + public function testRenderSnak() { + $snakSerialization = $this->getSnakSerialization( new StringValue( 'foo bar foo' ) ); + $usageAccumulator = new HashUsageAccumulator(); + $snakRenderer = $this->getSnakRenderer( $usageAccumulator ); + + $this->assertSame( 'foo bar foo', $snakRenderer->renderSnak( $snakSerialization ) ); + + $this->assertCount( 0, $usageAccumulator->getUsages() ); + } + + public function testRenderSnak_usage() { + $Q42 = new ItemId( 'Q42' ); + $snakSerialization = $this->getSnakSerialization( new EntityIdValue( $Q42 ) ); + $usageAccumulator = new HashUsageAccumulator(); + $snakRenderer = $this->getSnakRenderer( $usageAccumulator ); + + $this->assertSame( 'Q42', $snakRenderer->renderSnak( $snakSerialization ) ); + $usages = $usageAccumulator->getUsages(); + + $this->assertCount( 1, $usages ); + $this->assertEquals( + new EntityUsage( $Q42, EntityUsage::LABEL_USAGE ), + array_shift( $usages ) + ); + } + + public function provideRenderSnaks() { + return array( + 'Single Snak' => array( + 'foo bar foo', + array( 'P42' => array( $this->getSnakSerialization( new StringValue( 'foo bar foo' ) ) ) ) + ), + 'Multiple Snaks' => array( + 'foo, bar, Berlin', + array( array( + $this->getSnakSerialization( new StringValue( 'foo' ) ), + $this->getSnakSerialization( new StringValue( 'bar' ) ), + $this->getSnakSerialization( new StringValue( 'Berlin' ) ) + ) ) + ) + ); + } + + /** + * @dataProvider provideRenderSnaks + */ + public function testRenderSnaks( $expected, array $snaksSerialization ) { + $usageAccumulator = new HashUsageAccumulator(); + $snakRenderer = $this->getSnakRenderer( $usageAccumulator ); + + $this->assertSame( $expected, $snakRenderer->renderSnaks( $snaksSerialization ) ); + + $this->assertCount( 0, $usageAccumulator->getUsages() ); + } + + public function testRenderSnaks_usage() { + $Q42 = new ItemId( 'Q42' ); + $Q43 = new ItemId( 'Q43' ); + $snaksSerialization = array( array() ); + $snaksSerialization[0][] = $this->getSnakSerialization( new EntityIdValue( $Q42 ) ); + $snaksSerialization[0][] = $this->getSnakSerialization( new EntityIdValue( $Q43 ) ); + + $usageAccumulator = new HashUsageAccumulator(); + $snakRenderer = $this->getSnakRenderer( $usageAccumulator ); + + $this->assertSame( 'Q42, Q43', $snakRenderer->renderSnaks( $snaksSerialization ) ); + $usages = $usageAccumulator->getUsages(); + + $this->assertCount( 2, $usages ); + $this->assertEquals( + new EntityUsage( $Q42, EntityUsage::LABEL_USAGE ), + array_shift( $usages ) + ); + $this->assertEquals( + new EntityUsage( $Q43, EntityUsage::LABEL_USAGE ), + array_shift( $usages ) + ); + } +} diff --git a/client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php b/client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php index d08bfc3..6c1fa84 100644 --- a/client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php +++ b/client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php @@ -10,8 +10,10 @@ use Wikibase\DataModel\Entity\ItemId; use Wikibase\DataModel\Entity\Property; use Wikibase\DataModel\Entity\PropertyId; +use Wikibase\DataModel\ReferenceList; use Wikibase\DataModel\SiteLink; use Wikibase\DataModel\Snak\Snak; +use Wikibase\DataModel\Snak\SnakList; use Wikibase\DataModel\Statement\Statement; use Wikibase\SnakFactory; use Wikibase\Test\MockClientStore; @@ -59,6 +61,25 @@ $statement1 = $this->getTestStatement( $stringSnak ); $statement1->setRank( Statement::RANK_PREFERRED ); + $qualifierSnak1 = $this->getTestSnak( + new PropertyId( 'P342' ), + new StringValue( 'A qualifier Snak') + ); + $qualifierSnak2 = $this->getTestSnak( + new PropertyId( 'P342' ), + new StringValue( 'Moar qualifiers') + ); + $referenceSnak = $this->getTestSnak( + new PropertyId( 'P342' ), + new StringValue( 'A reference') + ); + + $statement1->setQualifiers( + new SnakList( array( $qualifierSnak1, $qualifierSnak2 ) ) + ); + + $statement1->addNewReference( $referenceSnak ); + $stringProperty->getStatements()->addStatement( $statement1 ); $this->mockRepository->putEntity( $stringProperty ); diff --git a/docs/lua.wiki b/docs/lua.wiki index 4dd05da..3a37a7a 100644 --- a/docs/lua.wiki +++ b/docs/lua.wiki @@ -31,6 +31,30 @@ mw.wikibase.sitelink( 'Q42' ) -- Returns the given items page title in the current Wiki as a string, like "Berlin". </source> +=== mw.wikibase.renderSnak === +<code>wikibase.renderSnak( snakSerialization )</code><br> +Renders a serialized Snak to text. This is useful for displaying References or Qualifiers. + +An example call might look like this: +<source lang="lua"> +local entity = mw.wikibase.getEntityObject( 'Q32487' ) +local snak = entity['claims']['P342'][1]['qualifiers']['P342'][1] + +mw.wikibase.renderSnak( snak ) -- Returns the given Snak formatted as wiki text. +</source> + +=== mw.wikibase.renderSnaks === +<code>wikibase.renderSnaks( snaksSerialization )</code><br> +Renders a list of serialized Snaks to text. This is useful for displaying References or Qualifiers. + +An example call might look like this: +<source lang="lua"> +local entity = mw.wikibase.getEntityObject( 'Q32487' ) +local snaks = entity['claims']['P342'][1]['qualifiers'] + +mw.wikibase.renderSnaks( snaks ) -- Returns the given Snaks formatted as wiki text. +</source> + === mw.wikibase.getEntity === <code>wikibase.getEntity()</code><br> Gets entity data of the Wikidata item connected with the current page. The returned object will have a legacy format, where tables are 0-based rather than 1-based. Also they will hold claims with both upper an lower case property ids (lower key property ids are deprecated). -- To view, visit https://gerrit.wikimedia.org/r/192594 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ie1f006927f717acf11cd5a72a1dfa00f7be9d66e Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: wmf/1.25wmf19 Gerrit-Owner: Hoo man <h...@online.de> Gerrit-Reviewer: Hoo man <h...@online.de> Gerrit-Reviewer: Jackmcbarn <jackmcb...@gmail.com> Gerrit-Reviewer: Siebrand <siebr...@kitano.nl> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits