Hoo man has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/383043 )
Change subject: Introduce mw.wikibase.getLabelByLang
......................................................................
Introduce mw.wikibase.getLabelByLang
Implementation is in WikibaseLanguageIndependentLuaBindings because
the function does not depend on the content/ user language, but the
language is explicitely passed in.
Bug: T173262
Change-Id: I573afbb6fd5384af4dc7e9b921d26cd379e87a0b
---
M client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
M
client/includes/DataAccess/Scribunto/WikibaseLanguageIndependentLuaBindings.php
M client/includes/DataAccess/Scribunto/mw.wikibase.lua
M client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua
M
client/tests/phpunit/includes/DataAccess/Scribunto/WikibaseLanguageIndependentLuaBindingsTest.php
M docs/lua.wiki
6 files changed, 215 insertions(+), 1 deletion(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/43/383043/1
diff --git
a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
index ffbe1fd..8dbb779 100644
--- a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
+++ b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
@@ -270,6 +270,9 @@
$wikibaseClient->getStore()->getSiteLinkLookup(),
$wikibaseClient->getSettings(),
$this->getUsageAccumulator(),
+ $this->getEntityIdParser(),
+ $wikibaseClient->getTermLookup(),
+ $wikibaseClient->getTermsLanguages(),
$wikibaseClient->getSettings()->getSetting(
'siteGlobalID' )
);
}
@@ -296,6 +299,7 @@
// these can't be called from user code, unless explicitly
exposed in Lua.
$lib = [
'getLabel' => [ $this, 'getLabel' ],
+ 'getLabelByLanguage' => [ $this, 'getLabelByLanguage' ],
'getEntity' => [ $this, 'getEntity' ],
'getEntityStatements' => [ $this, 'getEntityStatements'
],
'getSetting' => [ $this, 'getSetting' ],
@@ -447,6 +451,21 @@
}
/**
+ * Wrapper for getLabel in WikibaseLanguageIndependentLuaBindings
+ *
+ * @param string $prefixedEntityId
+ * @param string $languageCode
+ *
+ * @return string[]|null[]
+ */
+ public function getLabelByLanguage( $prefixedEntityId, $languageCode ) {
+ $this->checkType( 'getLabelByLanguage', 1, $prefixedEntityId,
'string' );
+ $this->checkType( 'getLabelByLanguage', 2, $languageCode,
'string' );
+
+ return [
$this->getLanguageIndependentLuaBindings()->getLabelByLanguage(
$prefixedEntityId, $languageCode ) ];
+ }
+
+ /**
* Wrapper for getDescription in
Scribunto_LuaWikibaseLibraryImplementation
*
* @param string $prefixedEntityId
diff --git
a/client/includes/DataAccess/Scribunto/WikibaseLanguageIndependentLuaBindings.php
b/client/includes/DataAccess/Scribunto/WikibaseLanguageIndependentLuaBindings.php
index 78295b7..9f999c3 100644
---
a/client/includes/DataAccess/Scribunto/WikibaseLanguageIndependentLuaBindings.php
+++
b/client/includes/DataAccess/Scribunto/WikibaseLanguageIndependentLuaBindings.php
@@ -4,8 +4,13 @@
use InvalidArgumentException;
use Wikibase\Client\Usage\UsageAccumulator;
+use Wikibase\DataModel\Entity\EntityIdParser;
+use Wikibase\DataModel\Entity\EntityIdParsingException;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\SiteLink;
+use Wikibase\DataModel\Services\Lookup\TermLookup;
+use Wikibase\DataModel\Services\Lookup\TermLookupException;
+use Wikibase\Lib\ContentLanguages;
use Wikibase\Lib\Store\SiteLinkLookup;
use Wikibase\SettingsArray;
@@ -37,6 +42,21 @@
private $usageAccumulator;
/**
+ * @var EntityIdParser
+ */
+ private $entityIdParser;
+
+ /**
+ * @var TermLookup
+ */
+ private $termLookup;
+
+ /**
+ * @var ContentLanguages
+ */
+ private $termsLanguages;
+
+ /**
* @var string
*/
private $siteId;
@@ -44,18 +64,27 @@
/**
* @param SiteLinkLookup $siteLinkLookup
* @param SettingsArray $settings
- * @param UsageAccumulator $usageAccumulator for tracking title usage
via getEntityId.
+ * @param UsageAccumulator $usageAccumulator
+ * @param EntityIdParser $entityIdParser
+ * @param TermLookup $termLookup
+ * @param ContentLanguages $termsLanguages
* @param string $siteId
*/
public function __construct(
SiteLinkLookup $siteLinkLookup,
SettingsArray $settings,
UsageAccumulator $usageAccumulator,
+ EntityIdParser $entityIdParser,
+ TermLookup $termLookup,
+ ContentLanguages $termsLanguages,
$siteId
) {
$this->siteLinkLookup = $siteLinkLookup;
$this->settings = $settings;
$this->usageAccumulator = $usageAccumulator;
+ $this->entityIdParser = $entityIdParser;
+ $this->termLookup = $termLookup;
+ $this->termsLanguages = $termsLanguages;
$this->siteId = $siteId;
}
@@ -78,6 +107,34 @@
}
/**
+ * @param string $prefixedEntityId
+ * @param string $languageCode
+ *
+ * @return string|null Null if language code invalid or entity couldn't
be found/ no label present.
+ */
+ public function getLabelByLanguage( $prefixedEntityId, $languageCode ) {
+ if ( !$this->termsLanguages->hasLanguage( $languageCode ) ) {
+ // Directly abort: Only track label usages for valid
languages
+ return null;
+ }
+
+ try {
+ $entityId = $this->entityIdParser->parse(
$prefixedEntityId );
+ } catch ( EntityIdParsingException $e ) {
+ return null;
+ }
+
+ $this->usageAccumulator->addLabelUsage( $entityId,
$languageCode );
+ try {
+ $label = $this->termLookup->getLabel( $entityId,
$languageCode );
+ } catch ( TermLookupException $ex ) {
+ return null;
+ }
+
+ return $label;
+ }
+
+ /**
* @param string $setting
*
* @return mixed
diff --git a/client/includes/DataAccess/Scribunto/mw.wikibase.lua
b/client/includes/DataAccess/Scribunto/mw.wikibase.lua
index c3553c8..93024ea 100644
--- a/client/includes/DataAccess/Scribunto/mw.wikibase.lua
+++ b/client/includes/DataAccess/Scribunto/mw.wikibase.lua
@@ -216,6 +216,17 @@
return label
end
+ -- Get the label in languageCode for the given entity id.
+ --
+ -- @param {string} id
+ -- @param {string} languageCode
+ wikibase.getLabelByLang = function( id, languageCode )
+ checkType( 'getLabelByLang', 1, id, 'string' )
+ checkType( 'getLabelByLang', 2, languageCode, 'string' )
+
+ return php.getLabelByLanguage( id, languageCode )
+ end
+
-- Get the description, description language for the given entity id,
if specified,
-- or of the connected entity, if exists.
--
diff --git
a/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua
b/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua
index f1bebd2..73de15f 100644
---
a/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua
+++
b/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua
@@ -191,6 +191,38 @@
args = { 'Q32488' },
expect = { nil, nil }
},
+ { name = 'mw.wikibase.getLabelByLang (invalid id type)', func =
mw.wikibase.getLabelByLang, type='ToString',
+ args = { 1, 'de' },
+ expect = "bad argument #1 to 'getLabelByLang' (string expected, got
number)"
+ },
+ { name = 'mw.wikibase.getLabelByLang (invalid languageCode type)', func
= mw.wikibase.getLabelByLang, type='ToString',
+ args = { "Q42", 1.2 },
+ expect = "bad argument #2 to 'getLabelByLang' (string expected, got
number)"
+ },
+ { name = 'mw.wikibase.getLabelByLang (invalid id)', func =
mw.wikibase.getLabelByLang, type='ToString',
+ args = { '-1', 'de' },
+ expect = { nil }
+ },
+ { name = 'mw.wikibase.getLabelByLang 1', func =
mw.wikibase.getLabelByLang, type='ToString',
+ args = { 'Q32487', 'de' },
+ expect = { 'Lua Test Item' }
+ },
+ { name = 'mw.wikibase.getLabelByLang 2', func =
mw.wikibase.getLabelByLang, type='ToString',
+ args = { 'Q32487', 'en' },
+ expect = { 'Test all the code paths' }
+ },
+ { name = 'mw.wikibase.getLabelByLang (no such item)', func =
mw.wikibase.getLabelByLang, type='ToString',
+ args = { 'Q1224342342', 'de' },
+ expect = { nil }
+ },
+ { name = 'mw.wikibase.getLabelByLang (no such lang)', func =
mw.wikibase.getLabelByLang, type='ToString',
+ args = { 'Q32487', 'blahblahblah' },
+ expect = { nil }
+ },
+ { name = 'mw.wikibase.getLabelByLang (no label)', func =
mw.wikibase.getLabelByLang, type='ToString',
+ args = { 'Q32488', 'de' },
+ expect = { nil }
+ },
{ name = 'mw.wikibase.description', func = mw.wikibase.description,
type='ToString',
args = { 'Q32487' },
expect = { 'Description of Q32487' }
diff --git
a/client/tests/phpunit/includes/DataAccess/Scribunto/WikibaseLanguageIndependentLuaBindingsTest.php
b/client/tests/phpunit/includes/DataAccess/Scribunto/WikibaseLanguageIndependentLuaBindingsTest.php
index 6f12dbe..c35d6fb 100644
---
a/client/tests/phpunit/includes/DataAccess/Scribunto/WikibaseLanguageIndependentLuaBindingsTest.php
+++
b/client/tests/phpunit/includes/DataAccess/Scribunto/WikibaseLanguageIndependentLuaBindingsTest.php
@@ -7,9 +7,12 @@
use Wikibase\Client\Usage\EntityUsage;
use Wikibase\Client\Usage\HashUsageAccumulator;
use Wikibase\Client\Usage\UsageAccumulator;
+use Wikibase\DataModel\Entity\BasicEntityIdParser;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\Entity\Item;
use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Services\Lookup\TermLookup;
+use Wikibase\Lib\StaticContentLanguages;
use Wikibase\Lib\Store\HashSiteLinkStore;
use Wikibase\Lib\Store\SiteLinkLookup;
use Wikibase\SettingsArray;
@@ -50,6 +53,9 @@
$siteLinkLookup,
new SettingsArray(),
$usageAccumulator ?: new HashUsageAccumulator(),
+ new BasicEntityIdParser,
+ $this->getMock( TermLookup::class ),
+ new StaticContentLanguages( [] ),
'enwiki'
);
}
@@ -68,6 +74,9 @@
$this->getMock( SiteLinkLookup::class ),
$settings,
new HashUsageAccumulator(),
+ new BasicEntityIdParser,
+ $this->getMock( TermLookup::class ),
+ new StaticContentLanguages( [] ),
'enwiki'
);
@@ -108,6 +117,83 @@
$this->assertNull( $id );
}
+ public function getLabelByLanguageProvider() {
+ $q2 = new ItemId( 'Q2' );
+
+ return [
+ 'Item and label exist' => [
+ [ 'Q2#L.de' ],
+ 'Q2-de',
+ 'Q2',
+ 'de',
+ $q2,
+ true,
+ true
+ ],
+ 'Item id valid, but label does not exist' => [
+ [ 'Q2#L.de' ],
+ null,
+ 'Q2',
+ 'de',
+ $q2,
+ false,
+ true
+ ],
+ 'Invalid Item Id' => [
+ [],
+ null,
+ 'dsfa',
+ 'de',
+ null,
+ true,
+ true
+ ],
+ 'Invalid lang' => [
+ [],
+ null,
+ 'Q2',
+ 'de',
+ $q2,
+ true,
+ false
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider getLabelByLanguageProvider
+ */
+ public function testGetLabelByLanguage(
+ array $expectedUsages,
+ $expected,
+ $prefixedEntityId,
+ $languageCode,
+ EntityId $entityId = null,
+ $hasLabel,
+ $hasLang
+ ) {
+ $usages = new HashUsageAccumulator();
+
+ $termLookup = $this->getMock( TermLookup::class );
+ $termLookup->expects( $this->exactly( $hasLang && $entityId ? 1
: 0 ) )
+ ->method( 'getLabel' )
+ ->with( $entityId )
+ ->will( $this->returnValue( $hasLabel ?
"$prefixedEntityId-$languageCode" : null ) );
+
+ $bindings = new WikibaseLanguageIndependentLuaBindings(
+ $this->getMock( SiteLinkLookup::class ),
+ new SettingsArray(),
+ $usages,
+ new BasicEntityIdParser,
+ $termLookup,
+ new StaticContentLanguages( $hasLang ? [ $languageCode
] : [] ),
+ 'enwiki'
+ );
+
+ $this->assertSame( $expected, $bindings->getLabelByLanguage(
$prefixedEntityId, $languageCode ) );
+ $this->assertSame( $expectedUsages, array_keys(
$usages->getUsages() ) );
+ }
+
public function getSiteLinkPageNameProvider() {
return [
[ 'Beer', 'Q666', null ],
diff --git a/docs/lua.wiki b/docs/lua.wiki
index 02ac146..2a504c0 100644
--- a/docs/lua.wiki
+++ b/docs/lua.wiki
@@ -71,6 +71,15 @@
local label, lang = mw.wikibase.getLabelWithLang( 'Q42' ) -- label contains
the text of the label. lang is the language the returned label is in, like "de".
</source>
+=== mw.wikibase.getLabelByLang ===
+<code>wikibase.getLabelByLang( id, languageCode )</code><br>
+Get the label from an entity for a specific language.
+
+An example call might look like this:
+<source lang="lua">
+mw.wikibase.getLabelByLang( 'Q42', 'es' ) -- Returns the Spanish label of the
item as a string, like "BerlĂn".
+</source>
+
=== mw.wikibase.sitelink ===
<code>wikibase.sitelink( itemId )</code><br>
<code>wikibase.sitelink( itemId, globalSiteId )</code><br>
--
To view, visit https://gerrit.wikimedia.org/r/383043
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I573afbb6fd5384af4dc7e9b921d26cd379e87a0b
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