jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/370956 )

Change subject: Adding a new mw.wikibase.getBestStatements
......................................................................


Adding a new mw.wikibase.getBestStatements

A new Lua interface to directly access entity statements,
with equivalent output to existing:
* mw.wikibase.getEntity( 'Q3'):getBestStatements('P2') - existing
* mw.wikibase.getBestStatements( 'Q3', 'P2') - new one

the php code filters the claims hence allowing less serialization
(other claims aren't serialized), and may later on provide better
tracking on the actually used claims.

Bug: T172905
Change-Id: I38a160d4acd6b2e1ab61d3e25b229e2cd0bd7f96
---
M client/includes/DataAccess/Scribunto/EntityAccessor.php
M client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
M client/includes/DataAccess/Scribunto/mw.wikibase.lua
M client/includes/Serializer/ClientEntitySerializer.php
A client/includes/Serializer/ClientSerializer.php
A client/includes/Serializer/ClientStatementListSerializer.php
M client/includes/WikibaseClient.php
M client/tests/phpunit/includes/DataAccess/Scribunto/EntityAccessorTest.php
M client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua
A client/tests/phpunit/includes/Serializer/ClientStatementListSerializerTest.php
M client/tests/phpunit/includes/WikibaseClientTest.php
M docs/lua.wiki
12 files changed, 501 insertions(+), 78 deletions(-)

Approvals:
  Hoo man: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/client/includes/DataAccess/Scribunto/EntityAccessor.php 
b/client/includes/DataAccess/Scribunto/EntityAccessor.php
index b192598..6a48c09 100644
--- a/client/includes/DataAccess/Scribunto/EntityAccessor.php
+++ b/client/includes/DataAccess/Scribunto/EntityAccessor.php
@@ -5,10 +5,13 @@
 use Language;
 use Serializers\Serializer;
 use Wikibase\Client\Serializer\ClientEntitySerializer;
+use Wikibase\Client\Serializer\ClientStatementListSerializer;
 use Wikibase\Client\Usage\UsageAccumulator;
 use Wikibase\DataModel\Entity\EntityIdParser;
 use Wikibase\DataModel\Services\Lookup\EntityLookup;
 use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup;
+use Wikibase\DataModel\Statement\StatementListProvider;
+use Wikibase\DataModel\Entity\PropertyId;
 use Wikibase\LanguageFallbackChain;
 use Wikibase\Lib\ContentLanguages;
 use Wikibase\Lib\Store\RevisionedUnresolvedRedirectException;
@@ -41,6 +44,11 @@
        private $entitySerializer;
 
        /**
+        * @var Serializer
+        */
+       private $statementSerializer;
+
+       /**
         * @var PropertyDataTypeLookup
         */
        private $dataTypeLookup;
@@ -65,6 +73,7 @@
                EntityLookup $entityLookup,
                UsageAccumulator $usageAccumulator,
                Serializer $entitySerializer,
+               Serializer $statementSerializer,
                PropertyDataTypeLookup $dataTypeLookup,
                LanguageFallbackChain $fallbackChain,
                Language $language,
@@ -74,6 +83,7 @@
                $this->entityLookup = $entityLookup;
                $this->usageAccumulator = $usageAccumulator;
                $this->entitySerializer = $entitySerializer;
+               $this->statementSerializer = $statementSerializer;
                $this->dataTypeLookup = $dataTypeLookup;
                $this->fallbackChain = $fallbackChain;
                $this->language = $language;
@@ -136,6 +146,47 @@
                return $entityArr;
        }
 
+       /**
+        * Get statement list from prefixed ID (e.g. "Q23") and property (e.g 
"P123") and return it as serialized array.
+        *
+        * @param string $prefixedEntityId
+        * @param string $propertyIdSerialization
+        *
+        * @return array|null
+        */
+       public function getEntityStatement( $prefixedEntityId, 
$propertyIdSerialization ) {
+               $prefixedEntityId = trim( $prefixedEntityId );
+               $entityId = $this->entityIdParser->parse( $prefixedEntityId );
+
+               $propertyId = new PropertyId( $propertyIdSerialization );
+               $this->usageAccumulator->addStatementUsage( $entityId, 
$propertyId );
+               $this->usageAccumulator->addOtherUsage( $entityId );
+
+               try {
+                       $entityObject = $this->entityLookup->getEntity( 
$entityId );
+               } catch ( RevisionedUnresolvedRedirectException $ex ) {
+                       // We probably hit a double redirect
+                       wfLogWarning(
+                               'Encountered a UnresolvedRedirectException when 
trying to load ' . $prefixedEntityId
+                       );
+
+                       return null;
+               }
+
+               if ( $entityObject === null ) {
+                       return null;
+               }
+
+               $statements = $entityObject->getStatements();
+
+               $statementsProp = $statements->getByPropertyId( $propertyId );
+               $statementsRanked = $statementsProp->getBestStatements();
+               $statementArr = 
$this->newClientStatementListSerializer()->serialize( $statementsRanked );
+               $this->renumber( $statementArr );
+
+               return $statementArr;
+       }
+
        private function newClientEntitySerializer() {
                return new ClientEntitySerializer(
                        $this->entitySerializer,
@@ -149,4 +200,11 @@
                );
        }
 
+       private function newClientStatementListSerializer() {
+               return new ClientStatementListSerializer(
+                       $this->statementSerializer,
+                       $this->dataTypeLookup
+               );
+       }
+
 }
diff --git 
a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php 
b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
index f74fbaa..2b0a153 100644
--- a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
+++ b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
@@ -210,6 +210,10 @@
                                
SerializerFactory::OPTION_SERIALIZE_MAIN_SNAKS_WITHOUT_HASH +
                                
SerializerFactory::OPTION_SERIALIZE_REFERENCE_SNAKS_WITHOUT_HASH
                        ),
+                       $wikibaseClient->getSerializerFactory(
+                               
SerializerFactory::OPTION_SERIALIZE_MAIN_SNAKS_WITHOUT_HASH +
+                               
SerializerFactory::OPTION_SERIALIZE_REFERENCE_SNAKS_WITHOUT_HASH
+                       )->newStatementListSerializer(),
                        $wikibaseClient->getPropertyDataTypeLookup(),
                        $this->getLanguageFallbackChain(),
                        $this->getLanguage(),
@@ -299,6 +303,7 @@
                $lib = [
                        'getLabel' => [ $this, 'getLabel' ],
                        'getEntity' => [ $this, 'getEntity' ],
+                       'getEntityStatement' => [ $this, 'getEntityStatement' ],
                        'getSetting' => [ $this, 'getSetting' ],
                        'getEntityUrl' => [ $this, 'getEntityUrl' ],
                        'renderSnak' => [ $this, 'renderSnak' ],
@@ -347,6 +352,34 @@
        }
 
        /**
+        * Wrapper for getEntityStatement in EntityAccessor
+        *
+        * @param string $prefixedEntityId
+        * @param string $propertyId
+        *
+        * @throws ScribuntoException
+        * @return array
+        */
+       public function getEntityStatement( $prefixedEntityId, $propertyId ) {
+               $this->checkType( 'getEntityStatement', 1, $prefixedEntityId, 
'string' );
+               $this->checkType( 'getEntityStatement', 2, $propertyId, 
'string' );
+
+               try {
+                       $statements = 
$this->getEntityAccessor()->getEntityStatement( $prefixedEntityId, $propertyId 
);
+               } catch ( EntityAccessLimitException $ex ) {
+                       throw new ScribuntoException( 
'wikibase-error-exceeded-entity-access-limit' );
+               } catch ( EntityIdParsingException $ex ) {
+                       throw new ScribuntoException(
+                               'wikibase-error-invalid-entity-id',
+                               [ 'args' => [ $prefixedEntityId ] ]
+                       );
+               } catch ( Exception $ex ) {
+                       throw new ScribuntoException( 
'wikibase-error-serialize-error' );
+               }
+               return [ $statements ];
+       }
+
+       /**
         * Wrapper for getEntityId in WikibaseLuaBindings
         *
         * @param string|null $pageTitle
diff --git a/client/includes/DataAccess/Scribunto/mw.wikibase.lua 
b/client/includes/DataAccess/Scribunto/mw.wikibase.lua
index 8fb16ee..85ff9ea 100644
--- a/client/includes/DataAccess/Scribunto/mw.wikibase.lua
+++ b/client/includes/DataAccess/Scribunto/mw.wikibase.lua
@@ -135,6 +135,26 @@
        -- getEntityObject is an alias for getEntity as these used to be 
different.
        wikibase.getEntityObject = wikibase.getEntity
 
+       -- Get the statement list array for the specified entityId and 
propertyId.
+       --
+       -- @param {string} [entityId]
+       -- @param {string} [propertyId]
+       wikibase.getBestStatements = function( entityId, propertyId )
+               if not php.getSetting( 'allowArbitraryDataAccess' ) and 
entityId ~= wikibase.getEntityIdForCurrentPage() then
+                       error( 'Access to arbitrary items has been disabled.', 
2 )
+               end
+
+               checkType( 'getBestStatements', 1, entityId, 'string' )
+               checkType( 'getBestStatements', 2, propertyId, 'string' )
+
+               statements = php.getEntityStatement( entityId, propertyId )
+               if statements == nil or statements[propertyId] == nil then
+                       return {}
+               else
+                       return statements[propertyId]
+               end
+       end
+
        -- Get the URL for the given entity id, if specified, or of the
        -- connected entity, if exists.
        --
diff --git a/client/includes/Serializer/ClientEntitySerializer.php 
b/client/includes/Serializer/ClientEntitySerializer.php
index e539e1c..ef9908b 100644
--- a/client/includes/Serializer/ClientEntitySerializer.php
+++ b/client/includes/Serializer/ClientEntitySerializer.php
@@ -7,14 +7,13 @@
 use Wikibase\DataModel\Entity\EntityDocument;
 use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup;
 use Wikibase\LanguageFallbackChain;
-use Wikibase\Lib\Serialization\CallbackFactory;
 use Wikibase\Lib\Serialization\SerializationModifier;
 
 /**
  * @license GPL-2.0+
  * @author Addshore
  */
-class ClientEntitySerializer implements Serializer {
+class ClientEntitySerializer extends ClientSerializer {
 
        /**
         * @var Serializer
@@ -22,29 +21,14 @@
        private $entitySerializer;
 
        /**
-        * @var PropertyDataTypeLookup
+        * @var LanguageFallbackChain[]
         */
-       private $dataTypeLookup;
-
-       /**
-        * @var SerializationModifier
-        */
-       private $modifier;
-
-       /**
-        * @var CallbackFactory
-        */
-       private $callbackFactory;
+       private $fallbackChains;
 
        /**
         * @var string[]
         */
        private $filterLangCodes;
-
-       /**
-        * @var LanguageFallbackChain[]
-        */
-       private $fallbackChains;
 
        /**
         * @param Serializer $entitySerializer
@@ -58,13 +42,10 @@
                array $filterLangCodes,
                array $fallbackChains
        ) {
-               $this->entitySerializer = $entitySerializer;
-               $this->dataTypeLookup = $dataTypeLookup;
+               parent::__construct( $dataTypeLookup );
                $this->filterLangCodes = $filterLangCodes;
+               $this->entitySerializer = $entitySerializer;
                $this->fallbackChains = $fallbackChains;
-
-               $this->modifier = new SerializationModifier();
-               $this->callbackFactory = new CallbackFactory();
        }
 
        /**
@@ -82,19 +63,10 @@
                        $serialization = 
$this->addEntitySerializationFallbackInfo( $serialization );
                }
 
-               $serialization = $this->injectEntitySerializationWithDataTypes( 
$serialization );
+               $serialization = $this->injectSerializationWithDataTypes( 
$serialization, 'claims/' );
                $serialization = 
$this->filterEntitySerializationUsingLangCodes( $serialization );
 
                return $this->omitEmptyArrays( $serialization );
-       }
-
-       private function omitEmptyArrays( array $serialization ) {
-               return array_filter(
-                       $serialization,
-                       function( $value ) {
-                               return $value !== [];
-                       }
-               );
        }
 
        /**
@@ -112,46 +84,6 @@
                        $serialization['descriptions']
                );
                return $serialization;
-       }
-
-       /**
-        * @param array $serialization
-        *
-        * @TODO FIXME duplicated / similar code in Repo ResultBuilder
-        *
-        * @return array
-        */
-       private function injectEntitySerializationWithDataTypes( array 
$serialization ) {
-               $serialization = $this->modifier->modifyUsingCallback(
-                       $serialization,
-                       'claims/*/*/mainsnak',
-                       $this->callbackFactory->getCallbackToAddDataTypeToSnak( 
$this->dataTypeLookup )
-               );
-               $serialization = 
$this->getArrayWithDataTypesInGroupedSnakListAtPath(
-                       $serialization,
-                       'claims/*/*/qualifiers'
-               );
-               $serialization = 
$this->getArrayWithDataTypesInGroupedSnakListAtPath(
-                       $serialization,
-                       'claims/*/*/references/*/snaks'
-               );
-               return $serialization;
-       }
-
-       /**
-        * @param array $array
-        * @param string $path
-        *
-        * @TODO FIXME duplicated / similar code in Repo ResultBuilder
-        *
-        * @return array
-        */
-       private function getArrayWithDataTypesInGroupedSnakListAtPath( array 
$array, $path ) {
-               return $this->modifier->modifyUsingCallback(
-                       $array,
-                       $path,
-                       
$this->callbackFactory->getCallbackToAddDataTypeToSnaksGroupedByProperty( 
$this->dataTypeLookup )
-               );
        }
 
        /**
diff --git a/client/includes/Serializer/ClientSerializer.php 
b/client/includes/Serializer/ClientSerializer.php
new file mode 100644
index 0000000..8db85ea
--- /dev/null
+++ b/client/includes/Serializer/ClientSerializer.php
@@ -0,0 +1,93 @@
+<?php
+
+namespace Wikibase\Client\Serializer;
+
+use Serializers\Serializer;
+use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup;
+use Wikibase\Lib\Serialization\CallbackFactory;
+use Wikibase\Lib\Serialization\SerializationModifier;
+
+/**
+ * @license GPL-2.0+
+ * @author Addshore
+ */
+abstract class ClientSerializer implements Serializer {
+
+       /**
+        * @var PropertyDataTypeLookup
+        */
+       private $dataTypeLookup;
+
+       /**
+        * @var SerializationModifier
+        */
+       private $modifier;
+
+       /**
+        * @var CallbackFactory
+        */
+       private $callbackFactory;
+
+       /**
+        * @param PropertyDataTypeLookup $dataTypeLookup
+        */
+       public function __construct(
+               PropertyDataTypeLookup $dataTypeLookup
+       ) {
+               $this->dataTypeLookup = $dataTypeLookup;
+
+               $this->modifier = new SerializationModifier();
+               $this->callbackFactory = new CallbackFactory();
+       }
+
+       protected function omitEmptyArrays( array $serialization ) {
+               return array_filter(
+                       $serialization,
+                       function( $value ) {
+                               return $value !== [];
+                       }
+               );
+       }
+
+       /**
+        * @param array $serialization
+        * @param string $pathPrefix
+        *
+        * @TODO FIXME duplicated / similar code in Repo ResultBuilder
+        *
+        * @return array
+        */
+       protected function injectSerializationWithDataTypes( array 
$serialization, $pathPrefix ) {
+               $serialization = $this->modifier->modifyUsingCallback(
+                       $serialization,
+                       "$pathPrefix*/*/mainsnak",
+                       $this->callbackFactory->getCallbackToAddDataTypeToSnak( 
$this->dataTypeLookup )
+               );
+               $serialization = 
$this->getArrayWithDataTypesInGroupedSnakListAtPath(
+                       $serialization,
+                       "$pathPrefix*/*/qualifiers"
+               );
+               $serialization = 
$this->getArrayWithDataTypesInGroupedSnakListAtPath(
+                       $serialization,
+                       "$pathPrefix*/*/references/*/snaks"
+               );
+               return $serialization;
+       }
+
+       /**
+        * @param array $array
+        * @param string $path
+        *
+        * @TODO FIXME duplicated / similar code in Repo ResultBuilder
+        *
+        * @return array
+        */
+       private function getArrayWithDataTypesInGroupedSnakListAtPath( array 
$array, $path ) {
+               return $this->modifier->modifyUsingCallback(
+                       $array,
+                       $path,
+                       
$this->callbackFactory->getCallbackToAddDataTypeToSnaksGroupedByProperty( 
$this->dataTypeLookup )
+               );
+       }
+
+}
diff --git a/client/includes/Serializer/ClientStatementListSerializer.php 
b/client/includes/Serializer/ClientStatementListSerializer.php
new file mode 100644
index 0000000..7109e3b
--- /dev/null
+++ b/client/includes/Serializer/ClientStatementListSerializer.php
@@ -0,0 +1,49 @@
+<?php
+
+namespace Wikibase\Client\Serializer;
+
+use Serializers\Exceptions\SerializationException;
+use Serializers\Serializer;
+use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup;
+use Wikibase\Lib\Serialization\SerializationModifier;
+
+/**
+ * @license GPL-2.0+
+ * @author eranroz
+ */
+class ClientStatementListSerializer extends ClientSerializer {
+
+       /**
+        * @var Serializer
+        */
+       private $statementSerializer;
+
+       /**
+        * @param Serializer $entitySerializer
+        * @param PropertyDataTypeLookup $dataTypeLookup
+        */
+       public function __construct(
+               Serializer $statementSerializer,
+               PropertyDataTypeLookup $dataTypeLookup
+       ) {
+               parent::__construct( $dataTypeLookup );
+               $this->statementSerializer = $statementSerializer;
+       }
+
+       /**
+        * Adds data types to serialization
+        *
+        * @param StatementList $statementList
+        *
+        * @throws SerializationException
+        * @return array
+        */
+       public function serialize( $statementList ) {
+               $serialization = $this->statementSerializer->serialize( 
$statementList );
+
+               $serialization = $this->injectSerializationWithDataTypes( 
$serialization, '' );
+
+               return $this->omitEmptyArrays( $serialization );
+       }
+
+}
diff --git a/client/includes/WikibaseClient.php 
b/client/includes/WikibaseClient.php
index 164f09f..a5c3e68 100644
--- a/client/includes/WikibaseClient.php
+++ b/client/includes/WikibaseClient.php
@@ -1019,12 +1019,22 @@
        /**
         * @param int $options bitwise combination of the 
SerializerFactory::OPTION_ flags
         *
+        * @return SerializerFactory
+        */
+       public function getSerializerFactory( $options = 
SerializerFactory::OPTION_DEFAULT ) {
+               $baseSerializerFactory = new SerializerFactory( new 
DataValueSerializer(), $options );
+               return $baseSerializerFactory;
+       }
+
+       /**
+        * @param int $options bitwise combination of the 
SerializerFactory::OPTION_ flags
+        *
         * @return Serializer
         */
        public function getAllTypesEntitySerializer( $options = 
SerializerFactory::OPTION_DEFAULT ) {
                if ( !isset( $this->entitySerializers[$options] ) ) {
                        $serializerFactoryCallbacks = 
$this->entityTypeDefinitions->getSerializerFactoryCallbacks();
-                       $baseSerializerFactory = new SerializerFactory( new 
DataValueSerializer(), $options );
+                       $baseSerializerFactory = $this->getSerializerFactory( 
$options );
                        $serializers = [];
 
                        foreach ( $serializerFactoryCallbacks as $callback ) {
diff --git 
a/client/tests/phpunit/includes/DataAccess/Scribunto/EntityAccessorTest.php 
b/client/tests/phpunit/includes/DataAccess/Scribunto/EntityAccessorTest.php
index a0d3a6d..9f30d05 100644
--- a/client/tests/phpunit/includes/DataAccess/Scribunto/EntityAccessorTest.php
+++ b/client/tests/phpunit/includes/DataAccess/Scribunto/EntityAccessorTest.php
@@ -2,6 +2,7 @@
 
 namespace Wikibase\Client\Tests\DataAccess\Scribunto;
 
+use InvalidArgumentException;
 use DataValues\Serializers\DataValueSerializer;
 use DataValues\StringValue;
 use Language;
@@ -14,6 +15,7 @@
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\Entity\ItemIdParser;
+use Wikibase\DataModel\Entity\PropertyId;
 use Wikibase\DataModel\ReferenceList;
 use Wikibase\DataModel\SerializerFactory;
 use Wikibase\DataModel\Services\Lookup\EntityLookup;
@@ -49,13 +51,13 @@
                $langCode = 'en'
        ) {
                $language = new Language( $langCode );
-
                $serializerFactory = new SerializerFactory(
                        new DataValueSerializer(),
                        
SerializerFactory::OPTION_SERIALIZE_MAIN_SNAKS_WITHOUT_HASH +
                        
SerializerFactory::OPTION_SERIALIZE_REFERENCE_SNAKS_WITHOUT_HASH
                );
                $entitySerializer = $serializerFactory->newItemSerializer();
+               $statementSerializer = 
$serializerFactory->newStatementListSerializer();
 
                $propertyDataTypeLookup = $this->getMock( 
PropertyDataTypeLookup::class );
                $propertyDataTypeLookup->expects( $this->any() )
@@ -72,6 +74,7 @@
                        $entityLookup ?: new MockRepository(),
                        $usageAccumulator ?: new HashUsageAccumulator(),
                        $entitySerializer,
+                       $statementSerializer,
                        $propertyDataTypeLookup,
                        $fallbackChain,
                        $language,
@@ -79,8 +82,8 @@
                );
        }
 
-       private function hasUsage( $actualUsages, EntityId $entityId, $aspect ) 
{
-               $usage = new EntityUsage( $entityId, $aspect );
+       private function hasUsage( $actualUsages, EntityId $entityId, $aspect, 
$modifier = null ) {
+               $usage = new EntityUsage( $entityId, $aspect, $modifier );
                $key = $usage->getIdentityString();
                return isset( $actualUsages[$key] );
        }
@@ -323,4 +326,132 @@
                $this->assertEquals( $expected, $entityAccessor->getEntity( 
'Q123098' ) );
        }
 
+       /**
+        * @dataProvider getEntityStatementProvider
+        */
+       public function testGetEntityStatement( array $expected ) {
+               $qid = 'Q123099';
+               $pid = 'P65';
+               $item = new Item( new ItemId( $qid ) );
+               $snak = new PropertyValueSnak( new PropertyId( 'P65' ), new 
StringValue( 'snakStringValue' ) );
+
+               $qualifiers = new SnakList();
+               $qualifiers->addSnak( new PropertyValueSnak( new PropertyId( 
$pid ), new StringValue( 'string!' ) ) );
+               $qualifiers->addSnak( new PropertySomeValueSnak( new 
PropertyId( $pid ) ) );
+
+               $references = new ReferenceList();
+               $references->addNewReference( [
+                       new PropertySomeValueSnak( new PropertyId( 'P65' ) ),
+                       new PropertySomeValueSnak( new PropertyId( 'P68' ) )
+               ] );
+
+               $guid = 'imaguid';
+               $item->getStatements()->addNewStatement( $snak, $qualifiers, 
$references, $guid );
+
+               $entityLookup = new MockRepository();
+               $entityLookup->putEntity( $item );
+
+               $usages = new HashUsageAccumulator();
+               $entityAccessor = $this->getEntityAccessor( $entityLookup, 
$usages );
+               $actual = $entityAccessor->getEntityStatement( 'Q123099', $pid 
);
+
+               $this->assertSameSize( $expected, $actual );
+               $this->assertEquals( $expected, $actual );
+
+               $this->assertEquals( [ 'Q123099#C.P65', 'Q123099#O' ], 
array_keys( $usages->getUsages() ) );
+               $this->assertFalse(
+                       $this->hasUsage( $usages->getUsages(), $item->getId(), 
EntityUsage::ALL_USAGE ), 'all usage'
+               );
+       }
+
+       public function getEntityStatementProvider() {
+               return [
+                       [
+                               [
+                                       'P65' => [
+                                               1 => [
+                                                       'id' => 'imaguid',
+                                                       'type' => 'statement',
+                                                       'mainsnak' => [
+                                                               'snaktype' => 
'value',
+                                                               'property' => 
'P65',
+                                                               'datatype' => 
'structured-cat',
+                                                               'datavalue' => [
+                                                                       'value' 
=> 'snakStringValue',
+                                                                       'type' 
=> 'string',
+                                                               ],
+                                                       ],
+                                                       'qualifiers' => [
+                                                               'P65' => [
+                                                                       1 => [
+                                                                               
'hash' => '3ea0f5404dd4e631780b3386d17a15a583e499a6',
+                                                                               
'snaktype' => 'value',
+                                                                               
'property' => 'P65',
+                                                                               
'datavalue' => [
+                                                                               
        'value' => 'string!',
+                                                                               
        'type' => 'string',
+                                                                               
],
+                                                                               
'datatype' => 'structured-cat',
+                                                                       ],
+                                                                       2 => [
+                                                                               
'hash' => 'aa9a5f05e20d7fa5cda7d98371e44c0bdd5de35e',
+                                                                               
'snaktype' => 'somevalue',
+                                                                               
'property' => 'P65',
+                                                                               
'datatype' => 'structured-cat',
+                                                                       ],
+                                                               ],
+                                                       ],
+                                                       'rank' => 'normal',
+                                                       'qualifiers-order' => [
+                                                               1 => 'P65'
+                                                       ],
+                                                       'references' => [
+                                                               1 => [
+                                                                       'hash' 
=> '8445204eb74e636cb53687e2f947c268d5186075',
+                                                                       'snaks' 
=> [
+                                                                               
'P65' => [
+                                                                               
        1 => [
+                                                                               
                'snaktype' => 'somevalue',
+                                                                               
                'property' => 'P65',
+                                                                               
                'datatype' => 'structured-cat',
+                                                                               
        ]
+                                                                               
],
+                                                                               
'P68' => [
+                                                                               
        1 => [
+                                                                               
                'snaktype' => 'somevalue',
+                                                                               
                'property' => 'P68',
+                                                                               
                'datatype' => 'structured-cat',
+                                                                               
        ]
+                                                                               
],
+                                                                       ],
+                                                                       
'snaks-order' => [
+                                                                               
1 => 'P65',
+                                                                               
2 => 'P68'
+                                                                       ],
+                                                               ],
+                                                       ],
+                                               ],
+                                       ]
+                               ]
+                       ]
+               ];
+       }
+
+       public function testGetEntityStatementBadProperty() {
+               $entityLookup = new MockRepository();
+               $entityAccessor = $this->getEntityAccessor( $entityLookup );
+               $this->setExpectedException( InvalidArgumentException::class );
+               $actual = $entityAccessor->getEntityStatement( 'Q123099', 
'ffsdfs' );
+       }
+
+       public function testGetEntityStatementMissingStatement() {
+               $entityLookup = new MockRepository();
+               $item = new Item( new ItemId( 'Q123099' ) );
+               $entityLookup = new MockRepository();
+               $entityLookup->putEntity( $item );
+               $entityAccessor = $this->getEntityAccessor( $entityLookup );
+               $actual = $entityAccessor->getEntityStatement( 'Q123099', 'P13' 
);
+               $this->assertEquals( [], $actual );
+       }
+
 }
diff --git 
a/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua
 
b/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua
index 4fbb1a7..481443c 100644
--- 
a/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua
+++ 
b/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua
@@ -22,6 +22,16 @@
        return type( mw.wikibase.getEntityObject() )
 end
 
+local function testGetBestStatementsType()
+       return type( mw.wikibase.getBestStatements( 'Q199024', 'P342' ) )
+end
+
+local function testGetBestStatementsFormat()
+       local directAccess = mw.dumpObject( mw.wikibase.getBestStatements( 
'Q199024', 'P342' ) )
+       local entityAccess = mw.dumpObject( mw.wikibase.getEntity( 'Q199024' 
):getBestStatements( 'P342' ) )
+       return directAccess == entityAccess
+end
+
 local function testGetEntityObjectIsCloned()
        mw.wikibase.getEntityObject( 'Q199024' ).id = 'a'
 
@@ -92,6 +102,12 @@
        { name = 'mw.wikibase.getEntityObject (type)', func = 
testGetEntityObjectType, type='ToString',
          expect = { 'table' }
        },
+       { name = 'mw.wikibase.getBestStatements (type)', func = 
testGetBestStatementsType, type='ToString',
+         expect = { 'table' }
+       },
+       { name = 'mw.wikibase.getBestStatements (format)', func = 
testGetBestStatementsFormat,
+         expect = { true }
+       },
        { name = 'mw.wikibase.getEntityObject (is cloned)', func = 
testGetEntityObjectIsCloned, type='ToString',
          expect = { 'Q199024' }
        },
diff --git 
a/client/tests/phpunit/includes/Serializer/ClientStatementListSerializerTest.php
 
b/client/tests/phpunit/includes/Serializer/ClientStatementListSerializerTest.php
new file mode 100644
index 0000000..6127dc7
--- /dev/null
+++ 
b/client/tests/phpunit/includes/Serializer/ClientStatementListSerializerTest.php
@@ -0,0 +1,65 @@
+<?php
+
+namespace Wikibase\Client\Tests\Serializer;
+
+use DataValues\Serializers\DataValueSerializer;
+use PHPUnit_Framework_TestCase;
+use Wikibase\Client\Serializer\ClientStatementListSerializer;
+use Wikibase\DataModel\Statement\StatementList;
+use Wikibase\DataModel\SerializerFactory;
+use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup;
+use Wikibase\DataModel\Snak\PropertyNoValueSnak;
+use Wikibase\DataModel\Entity\PropertyId;
+
+/**
+ * @covers Wikibase\Client\Serializer\ClientStatementListSerializer
+ *
+ * @group Wikibase
+ * @group WikibaseClient
+ *
+ * @license GPL-2.0+
+ * @author eranroz
+ */
+class ClientStatementListSerializerTest extends PHPUnit_Framework_TestCase {
+
+       private function newInstance() {
+               $serializerFactory = new SerializerFactory(
+                       new DataValueSerializer(),
+                       
SerializerFactory::OPTION_SERIALIZE_MAIN_SNAKS_WITHOUT_HASH +
+                       
SerializerFactory::OPTION_SERIALIZE_REFERENCE_SNAKS_WITHOUT_HASH
+               );
+
+               $dataTypeLookup = $this->getMock( PropertyDataTypeLookup::class 
);
+               $dataTypeLookup->expects( $this->any() )
+                       ->method( 'getDataTypeIdForProperty' )
+                       ->will( $this->returnValue( '<DATATYPE>' ) );
+
+               return new ClientStatementListSerializer(
+                       $serializerFactory->newStatementListSerializer(),
+                       $dataTypeLookup
+               );
+       }
+
+       public function testSerialize() {
+               $statementList = new StatementList();
+               $statementList->addNewStatement( new PropertyNoValueSnak( 1 ) );
+               $statements = $statementList->getByPropertyId( new PropertyId( 
'P1' ) );
+
+               $instance = $this->newInstance();
+               $serialization = $instance->serialize( $statements );
+
+               $expected = [
+                       'P1' => [ [
+                               'mainsnak' => [
+                                       'snaktype' => 'novalue',
+                                       'property' => 'P1',
+                                       'datatype' => '<DATATYPE>',
+                               ],
+                               'type' => 'statement',
+                               'rank' => 'normal'
+                       ] ],
+               ];
+               $this->assertSame( $expected, $serialization );
+       }
+
+}
diff --git a/client/tests/phpunit/includes/WikibaseClientTest.php 
b/client/tests/phpunit/includes/WikibaseClientTest.php
index 5465207..5c7b37e 100644
--- a/client/tests/phpunit/includes/WikibaseClientTest.php
+++ b/client/tests/phpunit/includes/WikibaseClientTest.php
@@ -21,6 +21,7 @@
 use Wikibase\Client\RepoLinker;
 use Wikibase\Client\WikibaseClient;
 use Wikibase\Client\Store\ClientStore;
+use Wikibase\DataModel\SerializerFactory;
 use Wikibase\DataModel\DeserializerFactory;
 use Wikibase\DataModel\Entity\EntityIdParser;
 use Wikibase\DataModel\Entity\Item;
@@ -275,6 +276,11 @@
                $this->assertInstanceOf( Deserializer::class, $deserializer );
        }
 
+       public function testGetSerializerFactory() {
+               $serializerFactory = 
$this->getWikibaseClient()->getSerializerFactory();
+               $this->assertInstanceOf( SerializerFactory::class, 
$serializerFactory );
+       }
+
        public function testGetChangeHandler() {
                $handler = $this->getWikibaseClient()->getChangeHandler();
                $this->assertInstanceOf( ChangeHandler::class, $handler );
diff --git a/docs/lua.wiki b/docs/lua.wiki
index 4751212..760912e 100644
--- a/docs/lua.wiki
+++ b/docs/lua.wiki
@@ -174,6 +174,16 @@
 mw.wikibase.orderProperties( propertyIds ) -- Returns a table with ordered 
property IDs such as { 'P5', 'P1', 'P31' }
 </source>
 
+=== mw.wikibase.getBestStatements ===
+<code>wikibase.getBestStatements( entityId, propertyId )</code><br>
+Returns a table with the best statements for the given entity ID and property 
ID.
+
+
+An example call might look like this:
+<source lang="lua">
+mw.wikibase.getBestStatements( 'Q1', 'P12' ) -- Returns a table containing the 
serialization of the best statements with the property id P12 of Q1
+</source>
+
 == mw.wikibase.entity ==
 <code>mw.wikibase.entity</code> represents a Wikibase entity in Lua. A 
<code>mw.wikibase.entity</code> table for the item which is linked with the 
current page can be obtained with 
[[#mw.wikibase.getEntity|<code>mw.wikibase.getEntity</code>]].
 

-- 
To view, visit https://gerrit.wikimedia.org/r/370956
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I38a160d4acd6b2e1ab61d3e25b229e2cd0bd7f96
Gerrit-PatchSet: 12
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Eranroz <eranro...@gmail.com>
Gerrit-Reviewer: Eranroz <eranro...@gmail.com>
Gerrit-Reviewer: Hoo man <h...@online.de>
Gerrit-Reviewer: Jackmcbarn <jackmcb...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to