Thiemo Mättig (WMDE) has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/376838 )
Change subject: [WIP] Add a mw.wikibase.getAllStatements Lua function ...................................................................... [WIP] Add a mw.wikibase.getAllStatements Lua function Currently there is only a getBestStatement. There is no alternative to get the other statements, except fetching the entire entity. This is bad because this is usage tracked as "all" usage. WIP because: * Missing tests. * I suggest to swap the parameter order, but this is in conflict with the existing function. Change-Id: Iddfc271df83501eda8a44b0031f3937533b4d878 --- M client/includes/DataAccess/Scribunto/EntityAccessor.php M client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php M client/includes/DataAccess/Scribunto/mw.wikibase.lua M docs/lua.wiki 4 files changed, 66 insertions(+), 13 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/38/376838/1 diff --git a/client/includes/DataAccess/Scribunto/EntityAccessor.php b/client/includes/DataAccess/Scribunto/EntityAccessor.php index 6a48c09..13b0fef 100644 --- a/client/includes/DataAccess/Scribunto/EntityAccessor.php +++ b/client/includes/DataAccess/Scribunto/EntityAccessor.php @@ -2,6 +2,7 @@ namespace Wikibase\Client\DataAccess\Scribunto; +use InvalidArgumentException; use Language; use Serializers\Serializer; use Wikibase\Client\Serializer\ClientEntitySerializer; @@ -151,10 +152,11 @@ * * @param string $prefixedEntityId * @param string $propertyIdSerialization + * @param string $rank Either "best" (default) or "all". * * @return array|null */ - public function getEntityStatement( $prefixedEntityId, $propertyIdSerialization ) { + public function getEntityStatement( $prefixedEntityId, $propertyIdSerialization, $rank ) { $prefixedEntityId = trim( $prefixedEntityId ); $entityId = $this->entityIdParser->parse( $prefixedEntityId ); @@ -173,18 +175,21 @@ return null; } - if ( $entityObject === null ) { + if ( !( $entityObject instanceof StatementListProvider ) ) { return null; } - $statements = $entityObject->getStatements(); + $statements = $entityObject->getStatements()->getByPropertyId( $propertyId ); - $statementsProp = $statements->getByPropertyId( $propertyId ); - $statementsRanked = $statementsProp->getBestStatements(); - $statementArr = $this->newClientStatementListSerializer()->serialize( $statementsRanked ); - $this->renumber( $statementArr ); + if ( $rank === 'best' ) { + $statements = $statements->getBestStatements(); + } elseif ( $rank !== 'all' ) { + throw new InvalidArgumentException( "Invalid rank \"$rank\"" ); + } - return $statementArr; + $serialization = $this->newClientStatementListSerializer()->serialize( $statements ); + $this->renumber( $serialization ); + return $serialization; } private function newClientEntitySerializer() { diff --git a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php index 5b56291..daa765a 100644 --- a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php +++ b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php @@ -298,6 +298,7 @@ 'getLabel' => [ $this, 'getLabel' ], 'getEntity' => [ $this, 'getEntity' ], 'getEntityStatement' => [ $this, 'getEntityStatement' ], + 'getAllStatements' => [ $this, 'getAllStatements' ], 'getSetting' => [ $this, 'getSetting' ], 'getEntityUrl' => [ $this, 'getEntityUrl' ], 'renderSnak' => [ $this, 'renderSnak' ], @@ -350,16 +351,17 @@ * * @param string $prefixedEntityId * @param string $propertyId + * @param string $rank Either "best" (default) or "all". * * @throws ScribuntoException * @return array */ - public function getEntityStatement( $prefixedEntityId, $propertyId ) { + public function getEntityStatement( $prefixedEntityId, $propertyId, $rank = 'best' ) { $this->checkType( 'getEntityStatement', 1, $prefixedEntityId, 'string' ); $this->checkType( 'getEntityStatement', 2, $propertyId, 'string' ); try { - $statements = $this->getEntityAccessor()->getEntityStatement( $prefixedEntityId, $propertyId ); + $statements = $this->getEntityAccessor()->getEntityStatement( $prefixedEntityId, $propertyId, $rank ); } catch ( EntityAccessLimitException $ex ) { throw new ScribuntoException( 'wikibase-error-exceeded-entity-access-limit' ); } catch ( EntityIdParsingException $ex ) { @@ -374,6 +376,17 @@ } /** + * @param string $prefixedEntityId + * @param string $propertyId + * + * @throws ScribuntoException + * @return array + */ + public function getAllStatements( $prefixedEntityId, $propertyId ) { + return $this->getEntityStatement( $prefixedEntityId, $propertyId, 'all' ); + } + + /** * 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 85ff9ea..f99f5d2 100644 --- a/client/includes/DataAccess/Scribunto/mw.wikibase.lua +++ b/client/includes/DataAccess/Scribunto/mw.wikibase.lua @@ -137,8 +137,8 @@ -- Get the statement list array for the specified entityId and propertyId. -- - -- @param {string} [entityId] - -- @param {string} [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 ) @@ -155,6 +155,32 @@ end end + -- Returns a table with all statements (including all ranks, even deprecated) matching the given + -- property ID on the given entity ID. If no entity ID is given, the entity connected to the + -- current page will be used. + -- + -- @param {string} propertyId + -- @param {string} [entityId] + wikibase.getAllStatements = function( propertyId, entityId ) + checkType( 'getAllStatements', 1, propertyId, 'string' ) + checkTypeMulti( 'getAllStatements', 2, entityId, { 'string', 'nil' } ) + + if entityId + and entityId ~= wikibase.getEntityIdForCurrentPage() + and not php.getSetting( 'allowArbitraryDataAccess' ) + then + error( 'Access to arbitrary items has been disabled.', 2 ) + end + + entityId = getIdOfConnectedItemIfNil( entityId ) + statements = php.getAllStatements( entityId, propertyId ) + if statements and statements[propertyId] then + return statements[propertyId] + end + + return {} + end + -- Get the URL for the given entity id, if specified, or of the -- connected entity, if exists. -- diff --git a/docs/lua.wiki b/docs/lua.wiki index 4a01a25..d70fa8a 100644 --- a/docs/lua.wiki +++ b/docs/lua.wiki @@ -182,12 +182,21 @@ <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.getAllStatements === +<code>wikibase.getAllStatements( propertyId )</code><br> +<code>wikibase.getAllStatements( propertyId, entityId )</code><br> +Returns a table with all statements (including all ranks, even deprecated) matching the given property ID on the given entity ID. If no entity ID is given, the entity connected to the current page will be used. + +An example call might look like this: +<source lang="lua"> +mw.wikibase.getAllStatements( 'P12', '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/376838 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Iddfc271df83501eda8a44b0031f3937533b4d878 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits