Hoo man has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/381028 )

Change subject: Lua: Add mw.wikibase.getStatements
......................................................................

Lua: Add mw.wikibase.getStatements

Note: This will also include "deprecated" statements.

Bug: T176124
Change-Id: I02d8f7dcc3cacb02ef33f96e0530697ae4b65f97
---
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/tests/phpunit/includes/DataAccess/Scribunto/EntityAccessorTest.php
M client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua
M docs/lua.wiki
6 files changed, 142 insertions(+), 23 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/28/381028/1

diff --git a/client/includes/DataAccess/Scribunto/EntityAccessor.php 
b/client/includes/DataAccess/Scribunto/EntityAccessor.php
index 6a48c09..4ddde71 100644
--- a/client/includes/DataAccess/Scribunto/EntityAccessor.php
+++ b/client/includes/DataAccess/Scribunto/EntityAccessor.php
@@ -10,11 +10,11 @@
 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;
+use Wikimedia\Assert\Assert;
 
 /**
  * Functionality needed to expose Entities to Lua.
@@ -151,10 +151,17 @@
         *
         * @param string $prefixedEntityId
         * @param string $propertyIdSerialization
+        * @param string $bestStatementsOnly Either 'best' or 'all'
         *
         * @return array|null
         */
-       public function getEntityStatement( $prefixedEntityId, 
$propertyIdSerialization ) {
+       public function getEntityStatements( $prefixedEntityId, 
$propertyIdSerialization, $bestStatementsOnly ) {
+               Assert::parameter(
+                       in_array( $bestStatementsOnly, [ 'best', 'all' ] ),
+                       '$bestStatementsOnly',
+                       'must be either "best" or "all", "' . 
$bestStatementsOnly . '" given.'
+               );
+
                $prefixedEntityId = trim( $prefixedEntityId );
                $entityId = $this->entityIdParser->parse( $prefixedEntityId );
 
@@ -180,8 +187,12 @@
                $statements = $entityObject->getStatements();
 
                $statementsProp = $statements->getByPropertyId( $propertyId );
-               $statementsRanked = $statementsProp->getBestStatements();
-               $statementArr = 
$this->newClientStatementListSerializer()->serialize( $statementsRanked );
+
+               if ( $bestStatementsOnly === 'best' ) {
+                       $statementsProp = $statementsProp->getBestStatements();
+               }
+
+               $statementArr = 
$this->newClientStatementListSerializer()->serialize( $statementsProp );
                $this->renumber( $statementArr );
 
                return $statementArr;
diff --git 
a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php 
b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
index 5b56291..e12789e 100644
--- a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
+++ b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
@@ -297,7 +297,7 @@
                $lib = [
                        'getLabel' => [ $this, 'getLabel' ],
                        'getEntity' => [ $this, 'getEntity' ],
-                       'getEntityStatement' => [ $this, 'getEntityStatement' ],
+                       'getEntityStatements' => [ $this, 'getEntityStatements' 
],
                        'getSetting' => [ $this, 'getSetting' ],
                        'getEntityUrl' => [ $this, 'getEntityUrl' ],
                        'renderSnak' => [ $this, 'renderSnak' ],
@@ -350,16 +350,18 @@
         *
         * @param string $prefixedEntityId
         * @param string $propertyId
+        * @param string $bestStatementsOnly Either 'best' or 'all'
         *
         * @throws ScribuntoException
         * @return array
         */
-       public function getEntityStatement( $prefixedEntityId, $propertyId ) {
+       public function getEntityStatements( $prefixedEntityId, $propertyId, 
$bestStatementsOnly ) {
                $this->checkType( 'getEntityStatement', 1, $prefixedEntityId, 
'string' );
                $this->checkType( 'getEntityStatement', 2, $propertyId, 
'string' );
+               $this->checkType( 'getEntityStatement', 3, $bestStatementsOnly, 
'string' );
 
                try {
-                       $statements = 
$this->getEntityAccessor()->getEntityStatement( $prefixedEntityId, $propertyId 
);
+                       $statements = 
$this->getEntityAccessor()->getEntityStatements( $prefixedEntityId, 
$propertyId, $bestStatementsOnly );
                } catch ( EntityAccessLimitException $ex ) {
                        throw new ScribuntoException( 
'wikibase-error-exceeded-entity-access-limit' );
                } catch ( EntityIdParsingException $ex ) {
diff --git a/client/includes/DataAccess/Scribunto/mw.wikibase.lua 
b/client/includes/DataAccess/Scribunto/mw.wikibase.lua
index 85ff9ea..6a70867 100644
--- a/client/includes/DataAccess/Scribunto/mw.wikibase.lua
+++ b/client/includes/DataAccess/Scribunto/mw.wikibase.lua
@@ -135,19 +135,20 @@
        -- 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.
+       -- Get statements for the specified entityId and propertyId.
        --
        -- @param {string} [entityId]
        -- @param {string} [propertyId]
-       wikibase.getBestStatements = function( entityId, propertyId )
+       -- @param {string} [bestStatementsOnly] Which Statements to include: 
Either "best" or "all"
+       local getEntityStatements = function( entityId, propertyId, funcName, 
bestStatementsOnly )
                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' )
+               checkType( funcName, 1, entityId, 'string' )
+               checkType( funcName, 2, propertyId, 'string' )
 
-               statements = php.getEntityStatement( entityId, propertyId )
+               statements = php.getEntityStatements( entityId, propertyId, 
bestStatementsOnly )
                if statements == nil or statements[propertyId] == nil then
                        return {}
                else
@@ -155,6 +156,22 @@
                end
        end
 
+       -- Get all statements for the specified entityId and propertyId.
+       --
+       -- @param {string} [entityId]
+       -- @param {string} [propertyId]
+       wikibase.getStatements = function( entityId, propertyId )
+               return getEntityStatements( entityId, propertyId, 
'getStatements', 'all' )
+       end
+
+       -- Get the best statements for the specified entityId and propertyId.
+       --
+       -- @param {string} [entityId]
+       -- @param {string} [propertyId]
+       wikibase.getBestStatements = function( entityId, propertyId )
+               return getEntityStatements( entityId, propertyId, 
'getBestStatements', 'best' )
+       end
+
        -- Get the URL for the given entity id, if specified, or of the
        -- connected entity, if exists.
        --
diff --git 
a/client/tests/phpunit/includes/DataAccess/Scribunto/EntityAccessorTest.php 
b/client/tests/phpunit/includes/DataAccess/Scribunto/EntityAccessorTest.php
index b3454f8..fc8db6a 100644
--- a/client/tests/phpunit/includes/DataAccess/Scribunto/EntityAccessorTest.php
+++ b/client/tests/phpunit/includes/DataAccess/Scribunto/EntityAccessorTest.php
@@ -195,17 +195,44 @@
                );
        }
 
-       public function testGetEntityStatement() {
-               $item = $this->getItemWithStatements();
+       public function getEntityStatementsProvider() {
+               return [
+                       'Normal Statement, get best Statements' => [
+                               
$this->getItemWithStatementsClaimClientSerialization( false ),
+                               false,
+                               'best'
+                       ],
+                       'Normal Statement, get all Statements' => [
+                               
$this->getItemWithStatementsClaimClientSerialization( false ),
+                               false,
+                               'all'
+                       ],
+                       'Deprecated Statement, get best Statements' => [
+                               [],
+                               true,
+                               'best'
+                       ],
+                       'Deprecated Statement, get all Statements' => [
+                               
$this->getItemWithStatementsClaimClientSerialization( true ),
+                               true,
+                               'all'
+                       ]
+               ];
+       }
+
+       /**
+        * @dataProvider getEntityStatementsProvider
+        */
+       public function testGetEntityStatements( $expected, 
$statementDeprecated, $bestStatementsOnly ) {
+               $item = $this->getItemWithStatements( $statementDeprecated );
 
                $entityLookup = new MockRepository();
                $entityLookup->putEntity( $item );
 
                $usages = new HashUsageAccumulator();
                $entityAccessor = $this->getEntityAccessor( $entityLookup, 
$usages );
-               $actual = $entityAccessor->getEntityStatement( 'Q123099', 'P65' 
);
+               $actual = $entityAccessor->getEntityStatements( 'Q123099', 
'P65', $bestStatementsOnly );
 
-               $expected = 
$this->getItemWithStatementsClaimClientSerialization();
                $this->assertSameSize( $expected, $actual );
                $this->assertEquals( $expected, $actual );
 
@@ -218,7 +245,7 @@
        /**
         * @return Item
         */
-       private function getItemWithStatements() {
+       private function getItemWithStatements( $statementDeprecated = false ) {
                $p65 = new PropertyId( 'P65' );
                $p68 = new PropertyId( 'P68' );
 
@@ -243,6 +270,11 @@
                ] );
 
                $statement->setGuid( 'imaguid' );
+
+               if ( $statementDeprecated ) {
+                       $statement->setRank( Statement::RANK_DEPRECATED );
+               }
+
                $item->getStatements()->addStatement( $statement );
 
                return $item;
@@ -293,7 +325,7 @@
                ];
        }
 
-       private function getItemWithStatementsClaimClientSerialization() {
+       private function getItemWithStatementsClaimClientSerialization( 
$statementDeprecated = false ) {
                return [
                        'P65' => [
                                1 => [
@@ -328,7 +360,7 @@
                                                        ],
                                                ],
                                        ],
-                                       'rank' => 'normal',
+                                       'rank' => $statementDeprecated ? 
'deprecated' : 'normal',
                                        'qualifiers-order' => [
                                                1 => 'P65'
                                        ],
@@ -362,21 +394,34 @@
                ];
        }
 
-       public function testGetEntityStatementBadProperty() {
+       public function bestStatementsOnlyProvider() {
+               return [
+                       [ 'best' ],
+                       [ 'all' ]
+               ];
+       }
+
+       /**
+        * @dataProvider bestStatementsOnlyProvider
+        */
+       public function testGetEntityStatementsBadProperty( $bestStatementsOnly 
) {
                $entityLookup = new MockRepository();
                $entityAccessor = $this->getEntityAccessor( $entityLookup );
 
                $this->setExpectedException( InvalidArgumentException::class );
-               $entityAccessor->getEntityStatement( 'Q123099', 'ffsdfs' );
+               $entityAccessor->getEntityStatements( 'Q123099', 'ffsdfs', 
$bestStatementsOnly );
        }
 
-       public function testGetEntityStatementMissingStatement() {
+       /**
+        * @dataProvider bestStatementsOnlyProvider
+        */
+       public function testGetEntityStatementsMissingStatement( 
$bestStatementsOnly ) {
                $item = new Item( new ItemId( 'Q123099' ) );
                $entityLookup = new MockRepository();
                $entityLookup->putEntity( $item );
                $entityAccessor = $this->getEntityAccessor( $entityLookup );
 
-               $actual = $entityAccessor->getEntityStatement( 'Q123099', 'P13' 
);
+               $actual = $entityAccessor->getEntityStatements( 'Q123099', 
'P13', $bestStatementsOnly );
 
                $this->assertSame( [], $actual );
        }
diff --git 
a/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua
 
b/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua
index 481443c..c865934 100644
--- 
a/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua
+++ 
b/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua
@@ -32,6 +32,18 @@
        return directAccess == entityAccess
 end
 
+local function testGetStatementsType()
+       return type( mw.wikibase.getStatements( 'Q199024', 'P342' ) )
+end
+
+local function testGetStatementsFormat()
+       local directAccess = mw.dumpObject( mw.wikibase.getStatements( 
'Q32487', 'P342' ) )
+       local directBestAccess = mw.dumpObject( mw.wikibase.getBestStatements( 
'Q32487', 'P342' ) )
+       local entityAccess = mw.dumpObject( mw.wikibase.getEntity( 'Q32487' 
).claims.P342 )
+
+       return directBestAccess ~= directAccess and directAccess == entityAccess
+end
+
 local function testGetEntityObjectIsCloned()
        mw.wikibase.getEntityObject( 'Q199024' ).id = 'a'
 
@@ -102,12 +114,34 @@
        { name = 'mw.wikibase.getEntityObject (type)', func = 
testGetEntityObjectType, type='ToString',
          expect = { 'table' }
        },
+       { name = 'mw.wikibase.getBestStatements (entityId must be string)', 
func = mw.wikibase.getBestStatements, type='ToString',
+         args = { 0, 'P12' },
+         expect = "bad argument #1 to 'getBestStatements' (string expected, 
got number)"
+       },
+       { name = 'mw.wikibase.getBestStatements (entityId must be string)', 
func = mw.wikibase.getBestStatements, type='ToString',
+         args = { 'Q2', 12 },
+         expect = "bad argument #2 to 'getBestStatements' (string expected, 
got number)"
+       },
        { name = 'mw.wikibase.getBestStatements (type)', func = 
testGetBestStatementsType, type='ToString',
          expect = { 'table' }
        },
        { name = 'mw.wikibase.getBestStatements (format)', func = 
testGetBestStatementsFormat,
          expect = { true }
        },
+       { name = 'mw.wikibase.getStatements (entityId must be string)', func = 
mw.wikibase.getStatements, type='ToString',
+         args = { 0, 'P12' },
+         expect = "bad argument #1 to 'getStatements' (string expected, got 
number)"
+       },
+       { name = 'mw.wikibase.getStatements (entityId must be string)', func = 
mw.wikibase.getStatements, type='ToString',
+         args = { 'Q2', 12 },
+         expect = "bad argument #2 to 'getStatements' (string expected, got 
number)"
+       },
+       { name = 'mw.wikibase.getStatements (type)', func = 
testGetStatementsType, type='ToString',
+         expect = { 'table' }
+       },
+       { name = 'mw.wikibase.getStatements (format)', func = 
testGetStatementsFormat,
+         expect = { true }
+       },
        { name = 'mw.wikibase.getEntityObject (is cloned)', func = 
testGetEntityObjectIsCloned, type='ToString',
          expect = { 'Q199024' }
        },
diff --git a/docs/lua.wiki b/docs/lua.wiki
index c450221..628a395 100644
--- a/docs/lua.wiki
+++ b/docs/lua.wiki
@@ -188,6 +188,16 @@
 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.getStatements ===
+<code>wikibase.getStatements( entityId, propertyId )</code><br>
+Returns a table with all statements (even "deprecated" ones) for the given 
entity ID and property ID.
+
+
+An example call might look like this:
+<source lang="lua">
+mw.wikibase.getStatements( 'Q1', 'P12' ) -- Returns a table containing the 
serialization of the 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/381028
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I02d8f7dcc3cacb02ef33f96e0530697ae4b65f97
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

Reply via email to