Lucie Kaffee has uploaded a new change for review. https://gerrit.wikimedia.org/r/293519
Change subject: Add a function to get the entity URL ...................................................................... Add a function to get the entity URL Bug: T120378 Change-Id: I3b1157ce9efbd8c205ccfff1787676a0590a3804 --- M client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php M client/includes/DataAccess/Scribunto/mw.wikibase.lua M client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibraryTest.php 3 files changed, 107 insertions(+), 1 deletion(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/19/293519/1 diff --git a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php index f59e040..917db9f 100644 --- a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php +++ b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php @@ -8,12 +8,14 @@ use Scribunto_LuaLibraryBase; use ScribuntoException; use ValueFormatters\FormatterOptions; +use Wikibase\Client\RepoLinker; +use Wikibase\Client\WikibaseClient; use Wikibase\Client\DataAccess\PropertyIdResolver; use Wikibase\Client\PropertyLabelNotResolvedException; use Wikibase\Client\Usage\ParserOutputUsageAccumulator; use Wikibase\Client\Usage\UsageTrackingSnakFormatter; use Wikibase\Client\Usage\UsageTrackingTermLookup; -use Wikibase\Client\WikibaseClient; +use Wikibase\DataModel\Entity\EntityIdParser; use Wikibase\DataModel\Entity\EntityIdParsingException; use Wikibase\DataModel\SerializerFactory; use Wikibase\DataModel\Services\Lookup\EntityAccessLimitException; @@ -273,6 +275,7 @@ 'getLabel' => array( $this, 'getLabel' ), 'getEntity' => array( $this, 'getEntity' ), 'getSetting' => array( $this, 'getSetting' ), + 'getEntityUrl' => array( $this, 'getEntityUrl' ), 'renderSnak' => array( $this, 'renderSnak' ), 'renderSnaks' => array( $this, 'renderSnaks' ), 'getEntityId' => array( $this, 'getEntityId' ), @@ -345,6 +348,59 @@ } /** + * @param string $entityIdSerialization entity ID serialization + * + * @return string[]|null[] + */ + public function getEntityUrl( $entityIdSerialization ) { + try{ + $url = $this->getRepoLinker()->getEntityUrl( + $this->getEntityIdParser()->parse( $entityIdSerialization ) + ); + } catch (EntityIdParsingException $ex) { + $url = null; + } + + return $url; + } + + /** + * @return RepoLinker + */ + private function getRepoLinker() { + if ( !$this->repoLinker ) { + $wikibaseClient = WikibaseClient::getDefaultInstance(); + $this->repoLinker = $wikibaseClient->newRepoLinker(); + } + return $this->repoLinker; + } + + /** + * @param RepoLinker $repoLinker + */ + public function setRepoLinker( RepoLinker $repoLinker ) { + $this->repoLinker = $repoLinker; + } + + /** + * @return EntityIdParser + */ + private function getEntityIdParser() { + if ( !$this->entityIdParser ) { + $wikibaseClient = WikibaseClient::getDefaultInstance(); + $this->entityIdParser = $wikibaseClient->getEntityIdParser(); + } + return $this->entityIdParser; + } + + /** + * @param EntityIdParser $entityIdParser + */ + public function setEntityIdParser( EntityIdParser $entityIdParser ) { + $this->entityIdParser = $entityIdParser; + } + + /** * Wrapper for getLabel in Scribunto_LuaWikibaseLibraryImplementation * * @since 0.5 diff --git a/client/includes/DataAccess/Scribunto/mw.wikibase.lua b/client/includes/DataAccess/Scribunto/mw.wikibase.lua index e8334cf..9f58e00 100644 --- a/client/includes/DataAccess/Scribunto/mw.wikibase.lua +++ b/client/includes/DataAccess/Scribunto/mw.wikibase.lua @@ -126,6 +126,22 @@ -- getEntityObject is an alias for getEntity as these used to be different. wikibase.getEntityObject = wikibase.getEntity + -- Get the URL for the given entity id, if specified, or of the + -- connected entity, if exists. + -- + -- @param {string} [id] + wikibase.getEntityUrl = function( id ) + checkTypeMulti( 'getEntityUrl', 1, id, { 'string', 'nil' } ) + + id = getIdOfConnectedItemIfNil( id ) + + if id == nil then + return nil + end + + return php.getEntityUrl( id ) + end + -- Get the label for the given entity id, if specified, or of the -- connected entity, if exists. (in content language) -- diff --git a/client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibraryTest.php b/client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibraryTest.php index e1fc58e..09c9e5f 100644 --- a/client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibraryTest.php +++ b/client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibraryTest.php @@ -9,7 +9,9 @@ use ScribuntoException; use User; use Wikibase\Client\DataAccess\Scribunto\Scribunto_LuaWikibaseLibrary; +use Wikibase\Client\RepoLinker; use Wikibase\Client\WikibaseClient; +use Wikibase\DataModel\Entity\EntityIdParser; use Wikibase\Lib\Store\PropertyOrderProvider; /** @@ -180,6 +182,38 @@ $this->assertEquals( array( null ), $entityId ); } + public function testGetEntityUrl() { + $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary(); + $luaWikibaseLibrary->setRepoLinker( $this->getRepoLinker() ); + $luaWikibaseLibrary->setEntityIdParser( $this->getEntityIdParser() ); + $luaWikibaseLibrary->getEntityUrl( 'Q1' ); + } + + private function getRepoLinker() { + $repoLinker = $this->getMockBuilder( RepoLinker::class ) + ->disableOriginalConstructor() + ->getMock(); + + $repoLinker->expects( $this->once() ) + ->method( 'getEntityUrl' ); + + return $repoLinker; + } + + private function getEntityIdParser() { + $entityIdParser = $this->getMock( EntityIdParser::class ); + + $entityIdParser->expects( $this->once() ) + ->method( 'parse' ) + ->with( 'Q1' ) + ->will( $this->returnValue( + WikibaseClient::getDefaultInstance()->getEntityIdParser()->parse( 'Q1' ) + ) ); + + return $entityIdParser; + } + + /** * @dataProvider allowDataAccessInUserLanguageProvider */ -- To view, visit https://gerrit.wikimedia.org/r/293519 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I3b1157ce9efbd8c205ccfff1787676a0590a3804 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Lucie Kaffee <lucie.kaf...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits