jenkins-bot has submitted this change and it was merged. Change subject: Introduce mw.wikibase.entity:formatStatements() ......................................................................
Introduce mw.wikibase.entity:formatStatements() Similar to the {{#statments:…}} parser function. Bug: T142942 Change-Id: I5eabdf0961bab3c4f2e7fdcb5b8cec3fffce0781 --- M client/config/WikibaseClient.default.php M client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibrary.php M client/includes/DataAccess/Scribunto/WikibaseLuaEntityBindings.php M client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua M client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseEntityLibraryTests.lua M client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibraryTest.php M client/tests/phpunit/includes/DataAccess/Scribunto/WikibaseLuaEntityBindingsTest.php M docs/lua.wiki M docs/options.wiki 9 files changed, 352 insertions(+), 49 deletions(-) Approvals: Jonas Kress (WMDE): Looks good to me, but someone else must approve Thiemo Mättig (WMDE): Looks good to me, approved jenkins-bot: Verified diff --git a/client/config/WikibaseClient.default.php b/client/config/WikibaseClient.default.php index b609d7d..da14b0d 100644 --- a/client/config/WikibaseClient.default.php +++ b/client/config/WikibaseClient.default.php @@ -51,6 +51,7 @@ // Allows users to split the ParserCache by user language. 'allowDataAccessInUserLanguage' => false, 'enableStatementsParserFunction' => false, + 'enableLuaEntityFormatStatements' => false, /** * Prefix to use for cache keys that should be shared among a Wikibase Repo instance and all diff --git a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibrary.php b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibrary.php index 235f958..c0c2bde 100644 --- a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibrary.php +++ b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibrary.php @@ -41,7 +41,11 @@ $wikibaseClient = WikibaseClient::getDefaultInstance(); $snakFormatterFactory = $wikibaseClient->getDataAccessSnakFormatterFactory(); - $snakFormatter = $snakFormatterFactory->newEscapedPlainTextSnakFormatter( + $plainTextSnakFormatter = $snakFormatterFactory->newEscapedPlainTextSnakFormatter( + $lang, + $this->getUsageAccumulator() + ); + $richWikitextSnakFormatter = $snakFormatterFactory->newRichWikitextSnakFormatter( $lang, $this->getUsageAccumulator() ); @@ -53,16 +57,24 @@ $wikibaseClient->getStore()->getPropertyLabelResolver() ); - $entityStatementsRenderer = new StatementTransclusionInteractor( + $plainTextTransclusionInteractor = new StatementTransclusionInteractor( $lang, $propertyIdResolver, new SnaksFinder(), - $snakFormatter, + $plainTextSnakFormatter, + $entityLookup + ); + $richWikitextTransclusionInteractor = new StatementTransclusionInteractor( + $lang, + $propertyIdResolver, + new SnaksFinder(), + $richWikitextSnakFormatter, $entityLookup ); return new WikibaseLuaEntityBindings( - $entityStatementsRenderer, + $plainTextTransclusionInteractor, + $richWikitextTransclusionInteractor, $wikibaseClient->getEntityIdParser(), $wikibaseClient->getSettings()->getSetting( 'siteGlobalID' ) ); @@ -108,7 +120,7 @@ } /** - * Register mw.wikibase.lua library + * Register mw.wikibase.entity.lua library * * @since 0.5 * @@ -118,13 +130,15 @@ // These functions will be exposed to the Lua module. // They are member functions on a Lua table which is private to the module, thus // these can't be called from user code, unless explicitly exposed in Lua. - $lib = array( - 'getGlobalSiteId' => array( $this, 'getGlobalSiteId' ), - 'formatPropertyValues' => array( $this, 'formatPropertyValues' ), - ); + $lib = [ + 'getGlobalSiteId' => [ $this, 'getGlobalSiteId' ], + 'formatStatements' => [ $this, 'formatStatements' ], + 'formatPropertyValues' => [ $this, 'formatPropertyValues' ], + 'isFormatStatementsEnabled' => [ $this, 'isFormatStatementsEnabled' ], + ]; return $this->getEngine()->registerInterface( - __DIR__ . '/mw.wikibase.entity.lua', $lib, array() + __DIR__ . '/mw.wikibase.entity.lua', $lib, [] ); } @@ -136,12 +150,28 @@ * @return string[] */ public function getGlobalSiteId() { - return array( $this->getImplementation()->getGlobalSiteId() ); + return [ $this->getImplementation()->getGlobalSiteId() ]; } /** - * Render the main Snaks belonging to a Statement (which is identified by a PropertyId - * or the label of a Property). + * Returns the value of the "enableLuaEntityFormatStatements" setting. + * + * @since 0.5 + * + * @return bool[] + */ + public function isFormatStatementsEnabled() { + // TODO: Remove this once the feature flag is not needed anymore! + $value = WikibaseClient::getDefaultInstance()->getSettings()->getSetting( + 'enableLuaEntityFormatStatements' + ); + + return [ $value ]; + } + + /** + * Format the main Snaks belonging to a Statement (which is identified by a PropertyId + * or the label of a Property) as escaped plain text. * * @since 0.5 * @@ -160,17 +190,52 @@ $this->checkType( 'formatPropertyValues', 1, $propertyLabelOrId, 'string' ); $this->checkTypeOptional( 'formatPropertyValues', 2, $acceptableRanks, 'table', null ); try { - return array( + return [ $this->getImplementation()->formatPropertyValues( $entityId, $propertyLabelOrId, $acceptableRanks ) - ); + ]; } catch ( InvalidArgumentException $e ) { throw new ScribuntoException( 'wikibase-error-invalid-entity-id' ); } catch ( PropertyLabelNotResolvedException $e ) { - return array( null ); + return [ null ]; + } + } + + /** + * Format the main Snaks belonging to a Statement (which is identified by a PropertyId + * or the label of a Property) as rich wikitext. + * + * @since 0.5 + * + * @param string $entityId + * @param string $propertyLabelOrId + * @param int[]|null $acceptableRanks + * + * @throws ScribuntoException + * @return string[]|null[] + */ + public function formatStatements( $entityId, $propertyLabelOrId, array $acceptableRanks = null ) { + $this->checkType( 'formatStatements', 0, $entityId, 'string' ); + // Use 1 as index for the property id, as the first parameter comes from + // internals of mw.wikibase.entity (an index of 2 might confuse users + // as they only gave one parameter themselves) + $this->checkType( 'formatStatements', 1, $propertyLabelOrId, 'string' ); + $this->checkTypeOptional( 'formatStatements', 2, $acceptableRanks, 'table', null ); + try { + return [ + $this->getImplementation()->formatStatements( + $entityId, + $propertyLabelOrId, + $acceptableRanks + ) + ]; + } catch ( InvalidArgumentException $e ) { + throw new ScribuntoException( 'wikibase-error-invalid-entity-id' ); + } catch ( PropertyLabelNotResolvedException $e ) { + return [ null ]; } } diff --git a/client/includes/DataAccess/Scribunto/WikibaseLuaEntityBindings.php b/client/includes/DataAccess/Scribunto/WikibaseLuaEntityBindings.php index 8c4652c..ee08478 100644 --- a/client/includes/DataAccess/Scribunto/WikibaseLuaEntityBindings.php +++ b/client/includes/DataAccess/Scribunto/WikibaseLuaEntityBindings.php @@ -18,7 +18,12 @@ /** * @var StatementTransclusionInteractor */ - private $statementTransclusionInteractor; + private $plainTextTransclusionInteractor; + + /** + * @var StatementTransclusionInteractor + */ + private $richWikitextTransclusionInteractor; /** * @var EntityIdParser @@ -31,23 +36,26 @@ private $siteId; /** - * @param StatementTransclusionInteractor $statementTransclusionInteractor + * @param StatementTransclusionInteractor $plainTextTransclusionInteractor + * @param StatementTransclusionInteractor $richWikitextTransclusionInteractor * @param EntityIdParser $entityIdParser * @param string $siteId */ public function __construct( - StatementTransclusionInteractor $statementTransclusionInteractor, + StatementTransclusionInteractor $plainTextTransclusionInteractor, + StatementTransclusionInteractor $richWikitextTransclusionInteractor, EntityIdParser $entityIdParser, $siteId ) { - $this->statementTransclusionInteractor = $statementTransclusionInteractor; + $this->plainTextTransclusionInteractor = $plainTextTransclusionInteractor; + $this->richWikitextTransclusionInteractor = $richWikitextTransclusionInteractor; $this->entityIdParser = $entityIdParser; $this->siteId = $siteId; } /** - * Render the main Snaks belonging to a Statement (which is identified by a PropertyId - * or the label of a Property). + * Format the main Snaks belonging to a Statement (which is identified by a PropertyId + * or the label of a Property) as escaped plain text. * * @since 0.5 * @@ -55,12 +63,34 @@ * @param string $propertyLabelOrId * @param int[]|null $acceptableRanks * - * @return string + * @return string Wikitext */ public function formatPropertyValues( $entityId, $propertyLabelOrId, array $acceptableRanks = null ) { $entityId = $this->entityIdParser->parse( $entityId ); - return $this->statementTransclusionInteractor->render( + return $this->plainTextTransclusionInteractor->render( + $entityId, + $propertyLabelOrId, + $acceptableRanks + ); + } + + /** + * Format the main Snaks belonging to a Statement (which is identified by a PropertyId + * or the label of a Property) as rich wikitext. + * + * @since 0.5 + * + * @param string $entityId + * @param string $propertyLabelOrId + * @param int[]|null $acceptableRanks + * + * @return string Wikitext + */ + public function formatStatements( $entityId, $propertyLabelOrId, array $acceptableRanks = null ) { + $entityId = $this->entityIdParser->parse( $entityId ); + + return $this->richWikitextTransclusionInteractor->render( $entityId, $propertyLabelOrId, $acceptableRanks diff --git a/client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua b/client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua index 1fb0623..a13ed5d 100644 --- a/client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua +++ b/client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua @@ -177,15 +177,14 @@ -- Get the formatted value of the claims with the given property id -- +-- @param {table} entity +-- @param {string} phpFormatterFunction -- @param {string} propertyLabelOrId -- @param {table} [acceptableRanks] -methodtable.formatPropertyValues = function( entity, propertyLabelOrId, acceptableRanks ) - checkType( 'formatPropertyValues', 1, propertyLabelOrId, 'string' ) - checkTypeMulti( 'formatPropertyValues', 2, acceptableRanks, { 'table', 'nil' } ) - +local formatValuesByPropertyId = function( entity, phpFormatterFunction, propertyLabelOrId, acceptableRanks ) acceptableRanks = acceptableRanks or nil - local formatted = php.formatPropertyValues( + local formatted = php[phpFormatterFunction]( entity.id, propertyLabelOrId, acceptableRanks @@ -207,6 +206,45 @@ } end +-- Format the main Snaks belonging to a Statement (which is identified by a PropertyId +-- or the label of a Property) as escaped plain text. +-- +-- @param {string} propertyLabelOrId +-- @param {table} [acceptableRanks] +methodtable.formatPropertyValues = function( entity, propertyLabelOrId, acceptableRanks ) + checkType( 'formatPropertyValues', 1, propertyLabelOrId, 'string' ) + checkTypeMulti( 'formatPropertyValues', 2, acceptableRanks, { 'table', 'nil' } ) + + return formatValuesByPropertyId( + entity, + 'formatPropertyValues', + propertyLabelOrId, + acceptableRanks + ); +end + +-- Format the main Snaks belonging to a Statement (which is identified by a PropertyId +-- or the label of a Property) as rich wikitext. +-- +-- @param {string} propertyLabelOrId +-- @param {table} [acceptableRanks] +methodtable.formatStatements = function( entity, propertyLabelOrId, acceptableRanks ) + checkType( 'formatStatements', 1, propertyLabelOrId, 'string' ) + checkTypeMulti( 'formatStatements', 2, acceptableRanks, { 'table', 'nil' } ) + + -- TODO: Remove the feature flag when not needed anymore! + if php.isFormatStatementsEnabled() ~= true then + error( 'mw.wikibase.entity:formatStatements() is not enabled on this wiki.', 2 ) + end + + return formatValuesByPropertyId( + entity, + 'formatStatements', + propertyLabelOrId, + acceptableRanks + ); +end + mw.wikibase.entity = entity package.loaded['mw.wikibase.entity'] = entity mw_interface = nil diff --git a/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseEntityLibraryTests.lua b/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseEntityLibraryTests.lua index 4256816..1078aee 100644 --- a/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseEntityLibraryTests.lua +++ b/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseEntityLibraryTests.lua @@ -94,6 +94,10 @@ return getNewTestItem():formatPropertyValues( propertyId, acceptableRanks ) end +local function testFormatStatements( propertyId, acceptableRanks ) + return getNewTestItem():formatStatements( propertyId, acceptableRanks ) +end + local function getClaimRank() return mw.wikibase.entity.claimRanks.RANK_PREFERRED end @@ -159,6 +163,31 @@ local entity = mw.wikibase.getEntityObject( 'P342' ) return entity:formatPropertyValues( 'P342', mw.wikibase.entity.claimRanks ) +end + +local function integrationTestFormatStatements( ranks ) + local entity = mw.wikibase.getEntityObject() + local propertyId = entity:getProperties()[1] + + return entity:formatStatements( propertyId, ranks ) +end + +local function integrationTestFormatStatementsByLabel( label ) + local entity = mw.wikibase.getEntityObject() + + return entity:formatStatements( label ) +end + +local function integrationTestFormatStatementsNoSuchProperty( propertyIdOrLabel ) + local entity = mw.wikibase.getEntityObject( 'Q199024' ) + + return entity:formatStatements( propertyIdOrLabel ) +end + +local function integrationTestFormatStatementsProperty() + local entity = mw.wikibase.getEntityObject( 'P342' ) + + return entity:formatStatements( 'P342', mw.wikibase.entity.claimRanks ) end local tests = { @@ -284,6 +313,14 @@ args = { 'Q123', function() end }, expect = "bad argument #2 to 'formatPropertyValues' (table or nil expected, got function)" }, + { name = 'mw.wikibase.entity.formatStatements bad param 1', func = testFormatStatements, + args = { function() end }, + expect = "bad argument #1 to 'formatStatements' (string expected, got function)" + }, + { name = 'mw.wikibase.entity.formatStatements bad param 2', func = testFormatStatements, + args = { 'Q123', function() end }, + expect = "bad argument #2 to 'formatStatements' (table or nil expected, got function)" + }, { name = 'mw.wikibase.entity.claimRanks', func = getClaimRank, expect = { 2 } }, @@ -362,6 +399,40 @@ { name = 'mw.wikibase.entity.formatPropertyValues integration property', func = integrationTestFormatPropertyValuesProperty, expect = { { label = 'LuaTestStringProperty', value = 'Lua :)' } } }, + { name = 'mw.wikibase.entity.formatStatements integration 1', func = integrationTestFormatStatements, + expect = { { label = 'LuaTestStringProperty', value = '<span><span>Lua :)</span></span>' } } + }, + { name = 'mw.wikibase.entity.formatStatements integration 2', func = integrationTestFormatStatements, + args = { { mw.wikibase.entity.claimRanks.RANK_PREFERRED, mw.wikibase.entity.claimRanks.RANK_NORMAL } }, + expect = { { label = 'LuaTestStringProperty', value = '<span><span>Lua :)</span>, <span>Lua is clearly superior to the parser function</span></span>' } } + }, + { name = 'mw.wikibase.entity.formatStatements integration 3', func = integrationTestFormatStatements, + args = { { mw.wikibase.entity.claimRanks.RANK_TRUTH } }, + expect = { { label = 'LuaTestStringProperty', value = '' } } + }, + { name = 'mw.wikibase.entity.formatStatements integration (by label)', func = integrationTestFormatStatementsByLabel, + args = { 'LuaTestStringProperty' }, + expect = { { label = 'LuaTestStringProperty', value = '<span><span>Lua :)</span></span>' } } + }, + { name = 'mw.wikibase.entity.formatStatements by non-existing label', func = integrationTestFormatStatementsByLabel, + args = { 'A label that doesn\'t exist' }, + expect = { { label = 'A label that doesn\'t exist', value = nil } } + }, + { name = 'mw.wikibase.entity.formatStatements by non-existing property', func = integrationTestFormatStatementsByLabel, + args = { 'P123456789' }, + expect = { { label = 'P123456789', value = nil } } + }, + { name = 'mw.wikibase.entity.formatStatements no such property', func = integrationTestFormatStatementsNoSuchProperty, + args = { 'P342' }, + expect = { { label = 'LuaTestStringProperty', value = '' } } + }, + { name = 'mw.wikibase.entity.formatStatements no such property (by label)', func = integrationTestFormatStatementsNoSuchProperty, + args = { 'LuaTestStringProperty' }, + expect = { { label = 'LuaTestStringProperty', value = '' } } + }, + { name = 'mw.wikibase.entity.formatStatements integration property', func = integrationTestFormatStatementsProperty, + expect = { { label = 'LuaTestStringProperty', value = '<span><span>Lua :)</span></span>' } } + }, } return testframework.getTestProvider( tests ) diff --git a/client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibraryTest.php b/client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibraryTest.php index 1ebd7da..878b092 100644 --- a/client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibraryTest.php +++ b/client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibraryTest.php @@ -27,18 +27,27 @@ */ private $oldAllowDataAccessInUserLanguage; + /** + * @var bool + */ + private $oldEnableLuaEntityFormatStatements; + protected static $moduleName = 'LuaWikibaseEntityLibraryTests'; protected function getTestModules() { - return parent::getTestModules() + array( + return parent::getTestModules() + [ 'LuaWikibaseEntityLibraryTests' => __DIR__ . '/LuaWikibaseEntityLibraryTests.lua', - ); + ]; } protected function setUp() { parent::setUp(); $settings = WikibaseClient::getDefaultInstance()->getSettings(); + + $this->oldEnableLuaEntityFormatStatements = $settings->getSetting( 'enableLuaEntityFormatStatements' ); + $settings->setSetting( 'enableLuaEntityFormatStatements', true ); + $this->oldAllowDataAccessInUserLanguage = $settings->getSetting( 'allowDataAccessInUserLanguage' ); $this->setAllowDataAccessInUserLanguage( false ); } @@ -47,13 +56,19 @@ parent::tearDown(); $this->setAllowDataAccessInUserLanguage( $this->oldAllowDataAccessInUserLanguage ); + + $settings = WikibaseClient::getDefaultInstance()->getSettings(); + $settings->setSetting( + 'enableLuaEntityFormatStatements', + $this->oldEnableLuaEntityFormatStatements + ); } public function allowDataAccessInUserLanguageProvider() { - return array( - array( true ), - array( false ), - ); + return [ + [ true ], + [ false ], + ]; } public function testConstructor() { @@ -100,8 +115,8 @@ $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary( $cacheSplit ); $this->assertSame( - array( '' ), - $luaWikibaseLibrary->formatPropertyValues( 'Q1', 'P65536', array() ) + [ '' ], + $luaWikibaseLibrary->formatPropertyValues( 'Q1', 'P65536', [] ) ); $this->assertSame( $allowDataAccessInUserLanguage, $cacheSplit ); @@ -111,8 +126,8 @@ $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary(); $this->assertSame( - array( '' ), - $luaWikibaseLibrary->formatPropertyValues( 'Q1', 'father', array() ) + [ '' ], + $luaWikibaseLibrary->formatPropertyValues( 'Q1', 'father', [] ) ); } @@ -127,7 +142,7 @@ $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary( $cacheSplit, $lang ); $this->assertSame( - array( 'Q885588' ), + [ 'Q885588' ], $luaWikibaseLibrary->formatPropertyValues( 'Q32488', 'P456', null ) ); @@ -144,6 +159,59 @@ } /** + * @dataProvider allowDataAccessInUserLanguageProvider + */ + public function testFormatStatements( $allowDataAccessInUserLanguage ) { + $cacheSplit = false; + $this->setAllowDataAccessInUserLanguage( $allowDataAccessInUserLanguage ); + + $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary( $cacheSplit ); + + $this->assertSame( + [ '' ], + $luaWikibaseLibrary->formatStatements( 'Q1', 'P65536', [] ) + ); + + $this->assertSame( $allowDataAccessInUserLanguage, $cacheSplit ); + } + + public function testFormatStatements_noPropertyId() { + $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary(); + + $this->assertSame( + [ '' ], + $luaWikibaseLibrary->formatStatements( 'Q1', 'father', [] ) + ); + } + + /** + * @dataProvider allowDataAccessInUserLanguageProvider + */ + public function testFormatStatements_usage( $allowDataAccessInUserLanguage ) { + $cacheSplit = false; + $this->setAllowDataAccessInUserLanguage( $allowDataAccessInUserLanguage ); + + $lang = Language::factory( 'es' ); + $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary( $cacheSplit, $lang ); + + $this->assertSame( + [ '<span><span>Q885588</span></span>' ], + $luaWikibaseLibrary->formatStatements( 'Q32488', 'P456', null ) + ); + + $usages = $luaWikibaseLibrary->getUsageAccumulator()->getUsages(); + $this->assertArrayHasKey( 'Q885588#T', $usages ); + + if ( $allowDataAccessInUserLanguage ) { + $this->assertArrayHasKey( 'Q885588#L.' . $lang->getCode(), $usages ); + } else { + $this->assertArrayHasKey( 'Q885588#L.de', $usages ); + } + + $this->assertSame( $allowDataAccessInUserLanguage, $cacheSplit ); + } + + /** * @param bool &$cacheSplit Will become true when the ParserCache has been split * @param Language|null $userLang The user's language * diff --git a/client/tests/phpunit/includes/DataAccess/Scribunto/WikibaseLuaEntityBindingsTest.php b/client/tests/phpunit/includes/DataAccess/Scribunto/WikibaseLuaEntityBindingsTest.php index 3c43d19..5f8042b 100644 --- a/client/tests/phpunit/includes/DataAccess/Scribunto/WikibaseLuaEntityBindingsTest.php +++ b/client/tests/phpunit/includes/DataAccess/Scribunto/WikibaseLuaEntityBindingsTest.php @@ -24,17 +24,27 @@ * @return WikibaseLuaEntityBindings */ private function getWikibaseLuaEntityBindings() { - $entityStatementsRenderer = $this->getMockBuilder( StatementTransclusionInteractor::class ) + $plainTextTransclusionInteractor = $this->getMockBuilder( StatementTransclusionInteractor::class ) ->disableOriginalConstructor() ->getMock(); - $entityStatementsRenderer->expects( $this->any() ) + $plainTextTransclusionInteractor->expects( $this->any() ) ->method( 'render' ) - ->with( new ItemId( 'Q12' ), 'some label', array( Statement::RANK_DEPRECATED ) ) + ->with( new ItemId( 'Q12' ), 'some label', [ Statement::RANK_DEPRECATED ] ) ->will( $this->returnValue( 'Kittens > Cats' ) ); + $richWikitextTransclusionInteractor = $this->getMockBuilder( StatementTransclusionInteractor::class ) + ->disableOriginalConstructor() + ->getMock(); + + $richWikitextTransclusionInteractor->expects( $this->any() ) + ->method( 'render' ) + ->with( new ItemId( 'Q12' ), 'some label', [ Statement::RANK_DEPRECATED ] ) + ->will( $this->returnValue( '<span>Kittens > Cats</span>' ) ); + return new WikibaseLuaEntityBindings( - $entityStatementsRenderer, + $plainTextTransclusionInteractor, + $richWikitextTransclusionInteractor, new BasicEntityIdParser(), 'enwiki' ); @@ -43,12 +53,25 @@ public function testFormatPropertyValues() { $wikibaseLuaEntityBindings = $this->getWikibaseLuaEntityBindings(); - $this->assertEquals( + $this->assertSame( 'Kittens > Cats', $wikibaseLuaEntityBindings->formatPropertyValues( 'Q12', 'some label', - array( Statement::RANK_DEPRECATED ) + [ Statement::RANK_DEPRECATED ] + ) + ); + } + + public function testFormatStatements() { + $wikibaseLuaEntityBindings = $this->getWikibaseLuaEntityBindings(); + + $this->assertSame( + '<span>Kittens > Cats</span>', + $wikibaseLuaEntityBindings->formatStatements( + 'Q12', + 'some label', + [ Statement::RANK_DEPRECATED ] ) ); } diff --git a/docs/lua.wiki b/docs/lua.wiki index 0eadfa0..7cdb875 100644 --- a/docs/lua.wiki +++ b/docs/lua.wiki @@ -198,8 +198,8 @@ An example call might look like this: <source lang="lua"> -entity:getSitelink() -- Returns the items page title in the current Wiki as a string, like "Moskow" -entity:getSitelink( 'ruwiki' ) -- Returns the items page title in the Russian Wikipedia as a string, like "Москва" +entity:getSitelink() -- Returns the item's page title in the current Wiki as a string, like "Moskow" +entity:getSitelink( 'ruwiki' ) -- Returns the item's page title in the Russian Wikipedia as a string, like "Москва" </source> === mw.wikibase.entity:getProperties === @@ -225,7 +225,7 @@ === mw.wikibase.entity:formatPropertyValues === <code>entity:formatPropertyValues( propertyLabelOrId )</code><br> <code>entity:formatPropertyValues( propertyLabelOrId, acceptableRanks )</code><br> -Get the formatted value of the claims with the given property (which is either identified by a property id, or by the label of the property). Per default only the best claims will be returned. +Get the values of the Statements with the given property (which is either identified by a property id, or by the label of the property), formatted as wikitext escaped plain text. Per default only the best claims will be returned. Alternatively a table with acceptable ranks can be given as second parameter (a mapping table with all ranks can be found in [[#mw.wikibase.entity.claimRanks|<code>mw.wikibase.entity.claimRanks</code>]]). An example call might look like this: @@ -242,6 +242,12 @@ <code>value</code> is an empty string (<nowiki>''</nowiki>) if there's no statement with the given property on the entity. <code>value</code> will be nil if the given property doesn't exist. +=== mw.wikibase.entity:formatStatements === +<code>entity:formatStatements( propertyLabelOrId )</code><br> +<code>entity:formatStatements( propertyLabelOrId, acceptableRanks )</code><br> + +Like [[#mw.wikibase.entity:formatPropertyValues|<code>mw.wikibase.entity:formatPropertyValues</code>]], but the returned values will be formatted as rich wikitext, rather than just escaped plain text. + === mw.wikibase.entity.claimRanks === The <code>mw.wikibase.entity.claimRanks</code> table contains a map of all available claim ranks. diff --git a/docs/options.wiki b/docs/options.wiki index 39d8ec3..325d8c6 100644 --- a/docs/options.wiki +++ b/docs/options.wiki @@ -107,3 +107,4 @@ ;sendEchoNotification: If true, allows users on the client wiki to get a notification when a page they created is connected to a repo item. This requires the Echo extension. ;repoIcon: If <code>sendEchoNotification</code> is set to <code>true</code>, you can also provide what icon the user will see. The correct syntax is <code>array( 'url' => '...' )</code> or <code>array( 'path' => '...' )</code> where <code>path</code> is relative to <code>$wgExtensionAssetsPath</code>. Defaults to <code>false</code> which means that there will be the default Echo icon. ;enableStatementsParserFunction: Feature flag for the <code><nowiki>{{#statements|…}}</nowiki></code> parser function. +;enableLuaEntityFormatStatements: Feature flag for the <code>mw.wikibase.entity:formatStatements()</code> Lua function. -- To view, visit https://gerrit.wikimedia.org/r/318316 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I5eabdf0961bab3c4f2e7fdcb5b8cec3fffce0781 Gerrit-PatchSet: 2 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Hoo man <h...@online.de> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Hoo man <h...@online.de> Gerrit-Reviewer: Jackmcbarn <jackmcb...@gmail.com> Gerrit-Reviewer: Jonas Kress (WMDE) <jonas.kr...@wikimedia.de> Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits