Jens Ohlig has uploaded a new change for review. https://gerrit.wikimedia.org/r/101201
Change subject: Implementation of snakformatted property table in Lua ...................................................................... Implementation of snakformatted property table in Lua Change-Id: I6db8dfcc999f05630ee9a82a67dfabc6f1693b06 --- M client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php M client/includes/scribunto/Scribunto_LuaWikibaseLibraryImplementation.php M client/includes/scribunto/mw.wikibase.lua M client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryImplementationTest.php 4 files changed, 100 insertions(+), 7 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/01/101201/1 diff --git a/client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php b/client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php index b95df46..37d6105 100644 --- a/client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php +++ b/client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php @@ -1,7 +1,10 @@ <?php +use ValueFormatters\FormatterOptions; use ValueParsers\ParseException; use Wikibase\Client\WikibaseClient; +use Wikibase\DataModel\Entity\EntityId; +use Wikibase\Lib\SnakFormatter; /** * Registers and defines functions to access Wikibase through the Scribunto extension @@ -28,10 +31,12 @@ // See Scribunto_LuaLanguageLibrary::getContLangCode(). global $wgContLang; $language = $wgContLang; + $formatterOptions = new FormatterOptions( array( "language" => $language ) ); $this->wbLibrary = new Scribunto_LuaWikibaseLibraryImplementation( WikibaseClient::getDefaultInstance()->getEntityIdParser(), // EntityIdParser WikibaseClient::getDefaultInstance()->getStore()->getEntityLookup(), // EntityLookup WikibaseClient::getDefaultInstance()->getEntityIdFormatter(), // EntityIdFormatter + WikibaseClient::getDefaultInstance()->getSnakFormatterFactory()->getSnakFormatter( SnakFormatter::FORMAT_WIKI, $formatterOptions ), // SnakFormatter WikibaseClient::getDefaultInstance()->getStore()->getSiteLinkTable(), // SiteLinkLookup $language, // language WikibaseClient::getDefaultInstance()->getSettings()->getSetting( 'siteGlobalID' ) // siteId @@ -48,7 +53,8 @@ $lib = array( 'getEntity' => array( $this, 'getEntity' ), 'getEntityId' => array( $this, 'getEntityId' ), - 'getGlobalSiteId' => array( $this, 'getGlobalSiteId' ) + 'getGlobalSiteId' => array( $this, 'getGlobalSiteId' ), + 'renderForEntityId' => array( $this, 'renderForEntityId' ) ); $this->getEngine()->registerInterface( dirname( __FILE__ ) . '/mw.wikibase.lua', $lib, array() ); } @@ -92,6 +98,15 @@ } /** + * Wrapper for renderForEntityId in Scribunto_LuaWikibaseLibraryImplementation + * + * @since 0.5 + * + */ + public function renderForEntityId( $entityId, $propertyId ) { + return $this->wbLibrary->renderForEntityId( $entityId, $propertyId ); + } + /** * Wrapper for getGlobalSiteId in Scribunto_LuaWikibaseLibraryImplementation * * @since 0.5 diff --git a/client/includes/scribunto/Scribunto_LuaWikibaseLibraryImplementation.php b/client/includes/scribunto/Scribunto_LuaWikibaseLibraryImplementation.php index 302619e..cf87a2b 100644 --- a/client/includes/scribunto/Scribunto_LuaWikibaseLibraryImplementation.php +++ b/client/includes/scribunto/Scribunto_LuaWikibaseLibraryImplementation.php @@ -2,6 +2,9 @@ use Wikibase\Item; use Wikibase\DataModel\Entity\EntityId; +use Wikibase\Claims; +use Wikibase\Entity; +use Wikibase\Snak; use Wikibase\EntityLookup; use Wikibase\SiteLinkLookup; use Wikibase\Lib\Serializers\SerializationOptions; @@ -10,7 +13,8 @@ use Wikibase\Lib\EntityIdParser; use Wikibase\LanguageFallbackChainFactory; use Wikibase\Utils; -use \Language; +use Wikibase\Lib\SnakFormatter; + /** * Actual implementations of the functions to access Wikibase through the Scribunto extension @@ -40,6 +44,8 @@ /* @var mixed */ protected $siteId; + /* @var SnakFormatter */ + protected $snaksFormatter; /** * @param EntityIdParser $entityIdParser @@ -47,6 +53,7 @@ public function __construct( EntityIdParser $entityIdParser, EntityLookup $entityLookup, EntityIdFormatter $entityIdFormatter, + SnakFormatter $snakFormatter, SiteLinkLookup $siteLinkTable, Language $language, $siteId ) { @@ -56,6 +63,7 @@ $this->siteLinkTable = $siteLinkTable; $this->language = $language; $this->siteId = $siteId; + $this->snaksFormatter = $snakFormatter; } @@ -136,6 +144,73 @@ } /** + * Returns such Claims from $entity that have a main Snak for the property that + * is specified by $propertyLabel. + * + * @param Entity $entity The Entity from which to get the clams + * @param string $propertyId A prefixed property ID. + * + * @return Claims The claims for the given property. + */ + private function getClaimsForProperty( Entity $entity, $propertyId ) { + $allClaims = new Claims( $entity->getClaims() ); + + $claims = $allClaims->getClaimsForProperty( $propertyId ); + + return $claims; + } + + /** + * @param Snak[] $snaks + * + * @return string - wikitext format + */ + private function formatSnakList( $snaks ) { + $formattedValues = $this->formatSnaks( $snaks ); + return $this->language->commaList( $formattedValues ); + } + + private function formatSnaks( $snaks ) { + $strings = array(); + + foreach ( $snaks as $snak ) { + $strings[] = $this->snaksFormatter->formatSnak( $snak ); + } + + return $strings; + } + + /** + * @param EntityId $entityId + * @param string $propertyLabel + * + * @return Status a status object wrapping a wikitext string + */ + public function renderForEntityId( $entityId, $propertyId ) { + $prefixedEntityId = trim( $entityId ); + $entityId = $this->entityIdParser->parse( $prefixedEntityId ); + $prefixedPropertyId = trim( $propertyId ); + $propertyId = $this->entityIdParser->parse( $prefixedPropertyId ); + $entity = $this->entityLookup->getEntity( $entityId ); + + if ( !$entity ) { + wfProfileOut( __METHOD__ ); + return Status::newGood( '' ); + } + + $claims = $this->getClaimsForProperty( $entity, $propertyId ); + + if ( $claims->isEmpty() ) { + wfProfileOut( __METHOD__ ); + return Status::newGood( '' ); + } + + $snakList = $claims->getMainSnaks(); + $text = $this->formatSnakList( $snakList, $propertyId ); + return array( $text ); + } + + /** * Get global site ID (e.g. "enwiki") * This is basically a helper function. * I can see this becoming part of mw.site in the Scribunto extension. diff --git a/client/includes/scribunto/mw.wikibase.lua b/client/includes/scribunto/mw.wikibase.lua index 1bdc845..0496341 100644 --- a/client/includes/scribunto/mw.wikibase.lua +++ b/client/includes/scribunto/mw.wikibase.lua @@ -91,7 +91,9 @@ properties = p p = {} for i,v in pairs( properties ) do - p[v] = { ["label"] = wikibase.label( v ) } + formattedProperty = php.renderForEntityId( php.getEntityId( tostring( mw.title.getCurrentTitle().prefixedText ) ), v ) + p[v] = { ["value"] = tostring( formattedProperty ), ["label"] = wikibase.label( v ) } + -- ["label"] = wikibase.label( v ), end return p end diff --git a/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryImplementationTest.php b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryImplementationTest.php index 996e260..2127fe6 100644 --- a/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryImplementationTest.php +++ b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryImplementationTest.php @@ -3,11 +3,10 @@ namespace Wikibase\Test; use Scribunto_LuaWikibaseLibraryImplementation; +use ValueFormatters\FormatterOptions; use Wikibase\Client\WikibaseClient; use \Language; -use \Site; -use \Wikibase\DirectSqlStore; -use \MediaWikiSite; +use Wikibase\Lib\SnakFormatter; /** * @covers Wikibase\Scribunto_LuaWikibaseLibraryImplementation @@ -26,13 +25,15 @@ public function getWikibaseLibraryImplementation() { $entityLookup = new MockRepository(); $language = new Language( "en" ); - $siteLinkLookup = $siteLinkLookup = $this->getMockBuilder( '\Wikibase\SiteLinkTable' ) + $siteLinkLookup = $this->getMockBuilder( '\Wikibase\SiteLinkTable' ) ->disableOriginalConstructor() ->getMock(); + $formatterOptions = new FormatterOptions(); return new Scribunto_LuaWikibaseLibraryImplementation( WikibaseClient::getDefaultInstance()->getEntityIdParser(), // EntityIdParser $entityLookup, WikibaseClient::getDefaultInstance()->getEntityIdFormatter(), // EntityIdFormatter + WikibaseClient::getDefaultInstance()->getSnakFormatterFactory()->getSnakFormatter( SnakFormatter::FORMAT_WIKI, $formatterOptions ), $siteLinkLookup, // SiteLinkLookup $language, // language "enwiki" // siteId -- To view, visit https://gerrit.wikimedia.org/r/101201 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I6db8dfcc999f05630ee9a82a67dfabc6f1693b06 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Jens Ohlig <jens.oh...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits