jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/369442 )
Change subject: Only record relevant label usages
......................................................................
Only record relevant label usages
Bug: T138409
Change-Id: I75b4c519c4622126c52f6f6eddea9ca3613d2084
---
M client/includes/DataAccess/DataAccessSnakFormatterFactory.php
M client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
M client/includes/DataAccess/Scribunto/WikibaseLanguageDependentLuaBindings.php
A client/includes/Usage/UsageTrackingLanguageFallbackLabelDescriptionLookup.php
M client/includes/Usage/UsageTrackingSnakFormatter.php
D client/includes/Usage/UsageTrackingTermLookup.php
M client/includes/WikibaseClient.php
M
client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterFactoryTest.php
M
client/tests/phpunit/includes/DataAccess/PropertyParserFunction/StatementGroupRendererFactoryTest.php
M
client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibraryTest.php
A
client/tests/phpunit/includes/Usage/UsageTrackingLanguageFallbackLabelDescriptionLookupTest.php
M client/tests/phpunit/includes/Usage/UsageTrackingSnakFormatterTest.php
D client/tests/phpunit/includes/Usage/UsageTrackingTermLookupTest.php
13 files changed, 366 insertions(+), 222 deletions(-)
Approvals:
Daniel Kinzler: Looks good to me, approved
jenkins-bot: Verified
diff --git a/client/includes/DataAccess/DataAccessSnakFormatterFactory.php
b/client/includes/DataAccess/DataAccessSnakFormatterFactory.php
index 971dea8..c0c3bab 100644
--- a/client/includes/DataAccess/DataAccessSnakFormatterFactory.php
+++ b/client/includes/DataAccess/DataAccessSnakFormatterFactory.php
@@ -5,6 +5,7 @@
use Language;
use ValueFormatters\FormatterOptions;
use Wikibase\Client\Usage\UsageAccumulator;
+use Wikibase\Client\Usage\UsageTrackingLanguageFallbackLabelDescriptionLookup;
use Wikibase\Client\Usage\UsageTrackingSnakFormatter;
use Wikibase\DataModel\Entity\EntityIdParser;
use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup;
@@ -14,6 +15,7 @@
use Wikibase\Lib\FormatterLabelDescriptionLookupFactory;
use Wikibase\Lib\OutputFormatSnakFormatterFactory;
use Wikibase\Lib\SnakFormatter;
+use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookupFactory;
/**
* A factory for SnakFormatters in a client context, to be reused in different
methods that "access
@@ -45,16 +47,23 @@
*/
private $repoItemUriParser;
+ /**
+ * @var LanguageFallbackLabelDescriptionLookupFactory
+ */
+ private $languageFallbackLabelDescriptionLookupFactory;
+
public function __construct(
LanguageFallbackChainFactory $languageFallbackChainFactory,
OutputFormatSnakFormatterFactory $snakFormatterFactory,
PropertyDataTypeLookup $propertyDataTypeLookup,
- EntityIdParser $repoItemUriParser
+ EntityIdParser $repoItemUriParser,
+ LanguageFallbackLabelDescriptionLookupFactory
$languageFallbackLabelDescriptionLookupFactory
) {
$this->languageFallbackChainFactory =
$languageFallbackChainFactory;
$this->snakFormatterFactory = $snakFormatterFactory;
$this->propertyDataTypeLookup = $propertyDataTypeLookup;
$this->repoItemUriParser = $repoItemUriParser;
+ $this->languageFallbackLabelDescriptionLookupFactory =
$languageFallbackLabelDescriptionLookupFactory;
}
/**
@@ -91,8 +100,12 @@
return new UsageTrackingSnakFormatter(
$snakFormatter,
$usageAccumulator,
- $fallbackChain->getFetchLanguageCodes(),
- $this->repoItemUriParser
+ $this->repoItemUriParser,
+ new UsageTrackingLanguageFallbackLabelDescriptionLookup(
+
$this->languageFallbackLabelDescriptionLookupFactory->newLabelDescriptionLookup(
$language ),
+ $usageAccumulator,
+ $fallbackChain
+ )
);
}
diff --git
a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
index cbf7970..29e7189 100644
--- a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
+++ b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
@@ -11,7 +11,7 @@
use Wikibase\Client\PropertyLabelNotResolvedException;
use Wikibase\Client\RepoLinker;
use Wikibase\Client\Usage\ParserOutputUsageAccumulator;
-use Wikibase\Client\Usage\UsageTrackingTermLookup;
+use Wikibase\Client\Usage\UsageTrackingLanguageFallbackLabelDescriptionLookup;
use Wikibase\Client\WikibaseClient;
use Wikibase\DataModel\Entity\EntityIdParser;
use Wikibase\DataModel\Entity\EntityIdParsingException;
@@ -248,16 +248,19 @@
$usageAccumulator = $this->getUsageAccumulator();
$labelDescriptionLookup = new
LanguageFallbackLabelDescriptionLookup(
- new UsageTrackingTermLookup(
- $wikibaseClient->getTermLookup(),
- $usageAccumulator
- ),
+ $wikibaseClient->getTermLookup(),
+ $this->getLanguageFallbackChain()
+ );
+
+ $usageTrackingLabelDescriptionLookup = new
UsageTrackingLanguageFallbackLabelDescriptionLookup(
+ $labelDescriptionLookup,
+ $usageAccumulator,
$this->getLanguageFallbackChain()
);
return new WikibaseLanguageDependentLuaBindings(
$this->getEntityIdParser(),
- $labelDescriptionLookup,
+ $usageTrackingLabelDescriptionLookup,
$usageAccumulator
);
}
diff --git
a/client/includes/DataAccess/Scribunto/WikibaseLanguageDependentLuaBindings.php
b/client/includes/DataAccess/Scribunto/WikibaseLanguageDependentLuaBindings.php
index 0a4fda6..984814a 100644
---
a/client/includes/DataAccess/Scribunto/WikibaseLanguageDependentLuaBindings.php
+++
b/client/includes/DataAccess/Scribunto/WikibaseLanguageDependentLuaBindings.php
@@ -5,7 +5,7 @@
use Wikibase\Client\Usage\UsageAccumulator;
use Wikibase\DataModel\Entity\EntityIdParser;
use Wikibase\DataModel\Entity\EntityIdParsingException;
-use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookup;
+use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookupException;
use Wikibase\Lib\Store\StorageException;
@@ -25,7 +25,7 @@
private $entityIdParser;
/**
- * @var LanguageFallbackLabelDescriptionLookup
+ * @var LabelDescriptionLookup
*/
private $labelDescriptionLookup;
@@ -36,7 +36,7 @@
/**
* @param EntityIdParser $entityIdParser
- * @param LanguageFallbackLabelDescriptionLookup $labelDescriptionLookup
+ * @param LabelDescriptionLookup $labelDescriptionLookup
* @param UsageAccumulator $usageAccumulator for tracking title usage
via getEntityId.
*
* @note: label usage is not tracked in $usageAccumulator. This should
be done inside
@@ -44,7 +44,7 @@
*/
public function __construct(
EntityIdParser $entityIdParser,
- LanguageFallbackLabelDescriptionLookup $labelDescriptionLookup,
+ LabelDescriptionLookup $labelDescriptionLookup,
UsageAccumulator $usageAccumulator
) {
$this->entityIdParser = $entityIdParser;
diff --git
a/client/includes/Usage/UsageTrackingLanguageFallbackLabelDescriptionLookup.php
b/client/includes/Usage/UsageTrackingLanguageFallbackLabelDescriptionLookup.php
new file mode 100644
index 0000000..571473d
--- /dev/null
+++
b/client/includes/Usage/UsageTrackingLanguageFallbackLabelDescriptionLookup.php
@@ -0,0 +1,108 @@
+<?php
+
+namespace Wikibase\Client\Usage;
+
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
+use Wikibase\DataModel\Term\TermFallback;
+use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookup;
+use Wikibase\LanguageFallbackChain;
+
+/**
+ * LanguageFallbackLabelDescriptionLookup decorator that records label usage
in an UsageAccumulator.
+ *
+ * @see UsageAccumulator
+ *
+ * @license GPL-2.0+
+ * @author Marius Hoch
+ */
+class UsageTrackingLanguageFallbackLabelDescriptionLookup implements
LabelDescriptionLookup {
+
+ /**
+ * @var LanguageFallbackLabelDescriptionLookup
+ */
+ private $labelDescriptionLookup;
+
+ /**
+ * @var UsageAccumulator
+ */
+ private $usageAccumulator;
+
+ /**
+ * @var LanguageFallbackChain
+ */
+ private $languageFallbackChain;
+
+ /**
+ * @param LanguageFallbackLabelDescriptionLookup $labelDescriptionLookup
+ * @param UsageAccumulator $usageAccumulator
+ * @param LanguageFallbackChain $languageFallbackChain
+ */
+ public function __construct(
+ LanguageFallbackLabelDescriptionLookup $labelDescriptionLookup,
+ UsageAccumulator $usageAccumulator,
+ LanguageFallbackChain $languageFallbackChain
+ ) {
+ $this->labelDescriptionLookup = $labelDescriptionLookup;
+ $this->usageAccumulator = $usageAccumulator;
+ $this->languageFallbackChain = $languageFallbackChain;
+ }
+
+ /**
+ * @param EntityId $entityId
+ *
+ * @throws LabelDescriptionLookupException
+ * @return TermFallback|null
+ */
+ public function getLabel( EntityId $entityId ) {
+ $termFallback = $this->labelDescriptionLookup->getLabel(
$entityId );
+
+ $this->recordLabelUsages( $entityId, $termFallback );
+
+ return $termFallback;
+ }
+
+ /**
+ * Record the appropriate label usages for a given TermFallback.
+ *
+ * @param EntityId $entityId
+ * @param TermFallback|null $termFallback
+ */
+ private function recordLabelUsages( EntityId $entityId, TermFallback
$termFallback = null ) {
+ $fetchLanguages =
$this->languageFallbackChain->getFetchLanguageCodes();
+
+ if ( $termFallback === null ) {
+ // Nothing found: Record the full fallback chains as
used.
+ $languagesTouched = $fetchLanguages;
+ } else {
+ // Found something: Find out which language it was
originally in
+ $languageUsed = $termFallback->getSourceLanguageCode();
+ if ( !$languageUsed ) {
+ $languageUsed =
$termFallback->getActualLanguageCode();
+ }
+
+ // Record the relevant part of the fallback chain as
used.
+ $languagesTouched = array_slice(
+ $fetchLanguages,
+ 0,
+ array_search( $languageUsed, $fetchLanguages )
+ 1
+ );
+ }
+
+ foreach ( $languagesTouched as $lang ) {
+ $this->usageAccumulator->addLabelUsage( $entityId,
$lang );
+ }
+ }
+
+ /**
+ * @param EntityId $entityId
+ *
+ * @throws LabelDescriptionLookupException
+ * @return TermFallback|null
+ */
+ public function getDescription( EntityId $entityId ) {
+ // TODO: track description usage, see T106287
+ return $this->labelDescriptionLookup->getDescription( $entityId
);
+ }
+
+}
diff --git a/client/includes/Usage/UsageTrackingSnakFormatter.php
b/client/includes/Usage/UsageTrackingSnakFormatter.php
index 88d2625..9dfe594 100644
--- a/client/includes/Usage/UsageTrackingSnakFormatter.php
+++ b/client/includes/Usage/UsageTrackingSnakFormatter.php
@@ -13,6 +13,8 @@
/**
* SnakFormatter decorator that records entity usage.
+ * This uses an UsageTrackingLanguageFallbackLabelDescriptionLookup internally
in order
+ * to track label usage.
*
* @see UsageAccumulator
*
@@ -32,31 +34,31 @@
private $usageAccumulator;
/**
- * @var string[]
- */
- private $languages;
-
- /**
* @var EntityIdParser
*/
private $repoItemUriParser;
/**
+ * @var UsageTrackingLanguageFallbackLabelDescriptionLookup
+ */
+ private $usageTrackingLabelDescriptionLookup;
+
+ /**
* @param SnakFormatter $snakFormatter
* @param UsageAccumulator $usageAccumulator
- * @param string[] $languages language codes to consider used for
formatting
* @param EntityIdParser $repoItemUriParser
+ * @param UsageTrackingLanguageFallbackLabelDescriptionLookup
$usageTrackingLabelDescriptionLookup
*/
public function __construct(
SnakFormatter $snakFormatter,
UsageAccumulator $usageAccumulator,
- array $languages,
- EntityIdParser $repoItemUriParser
+ EntityIdParser $repoItemUriParser,
+ UsageTrackingLanguageFallbackLabelDescriptionLookup
$usageTrackingLabelDescriptionLookup
) {
$this->snakFormatter = $snakFormatter;
$this->usageAccumulator = $usageAccumulator;
- $this->languages = $languages;
$this->repoItemUriParser = $repoItemUriParser;
+ $this->usageTrackingLabelDescriptionLookup =
$usageTrackingLabelDescriptionLookup;
}
/**
@@ -90,10 +92,13 @@
return $this->snakFormatter->formatSnak( $snak );
}
+ /**
+ * @param EntityId $id
+ */
private function addLabelUsage( EntityId $id ) {
- foreach ( $this->languages as $lang ) {
- $this->usageAccumulator->addLabelUsage( $id, $lang );
- }
+ // Just get the label from
UsageTrackingLanguageFallbackLabelDescriptionLookup::getLabel,
+ // which will record the appropriate usage(s) for us.
+ $this->usageTrackingLabelDescriptionLookup->getLabel( $id );
}
/**
diff --git a/client/includes/Usage/UsageTrackingTermLookup.php
b/client/includes/Usage/UsageTrackingTermLookup.php
deleted file mode 100644
index 412a4be..0000000
--- a/client/includes/Usage/UsageTrackingTermLookup.php
+++ /dev/null
@@ -1,91 +0,0 @@
-<?php
-
-namespace Wikibase\Client\Usage;
-
-use Wikibase\DataModel\Entity\EntityId;
-use Wikibase\DataModel\Services\Lookup\TermLookup;
-use Wikibase\DataModel\Services\Lookup\TermLookupException;
-
-/**
- * TermLookup decorator that records label usage in an TermLookup.
- *
- * @see UsageAccumulator
- *
- * @license GPL-2.0+
- * @author Daniel Kinzler
- */
-class UsageTrackingTermLookup implements TermLookup {
-
- /**
- * @var TermLookup
- */
- private $termLookup;
-
- /**
- * @var UsageAccumulator
- */
- private $usageAccumulator;
-
- public function __construct( TermLookup $termLookup, UsageAccumulator
$usageAccumulator ) {
- $this->termLookup = $termLookup;
- $this->usageAccumulator = $usageAccumulator;
- }
-
- /**
- * Gets the label of an Entity with the specified EntityId and language
code.
- *
- * @param EntityId $entityId
- * @param string $languageCode
- *
- * @throws TermLookupException
- * @return string|null
- */
- public function getLabel( EntityId $entityId, $languageCode ) {
- $this->usageAccumulator->addLabelUsage( $entityId,
$languageCode );
- return $this->termLookup->getLabel( $entityId, $languageCode );
- }
-
- /**
- * Gets all labels of an Entity with the specified EntityId.
- *
- * @param EntityId $entityId
- * @param string[] $languages
- *
- * @throws TermLookupException
- * @return string[]
- */
- public function getLabels( EntityId $entityId, array $languages ) {
- foreach ( $languages as $lang ) {
- $this->usageAccumulator->addLabelUsage( $entityId,
$lang );
- }
-
- return $this->termLookup->getLabels( $entityId, $languages );
- }
-
- /**
- * Gets the description of an Entity with the specified EntityId and
language code.
- *
- * @param EntityId $entityId
- * @param string $languageCode
- *
- * @throws TermLookupException
- * @return string|null
- */
- public function getDescription( EntityId $entityId, $languageCode ) {
- return $this->termLookup->getDescription( $entityId,
$languageCode );
- }
-
- /**
- * Gets all descriptions of an Entity with the specified EntityId.
- *
- * @param EntityId $entityId
- * @param string[] $languages
- *
- * @throws TermLookupException
- * @return string[]
- */
- public function getDescriptions( EntityId $entityId, array $languages )
{
- return $this->termLookup->getDescriptions( $entityId,
$languages );
- }
-
-}
diff --git a/client/includes/WikibaseClient.php
b/client/includes/WikibaseClient.php
index 9a0ba9d..083f143 100644
--- a/client/includes/WikibaseClient.php
+++ b/client/includes/WikibaseClient.php
@@ -1127,7 +1127,8 @@
$this->getLanguageFallbackChainFactory(),
$this->getSnakFormatterFactory(),
$this->getPropertyDataTypeLookup(),
- $this->getRepoItemUriParser()
+ $this->getRepoItemUriParser(),
+
$this->getLanguageFallbackLabelDescriptionLookupFactory()
);
}
diff --git
a/client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterFactoryTest.php
b/client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterFactoryTest.php
index 62937f3..d8cb2ea 100644
---
a/client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterFactoryTest.php
+++
b/client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterFactoryTest.php
@@ -16,6 +16,8 @@
use Wikibase\LanguageFallbackChainFactory;
use Wikibase\Lib\OutputFormatSnakFormatterFactory;
use Wikibase\Lib\SnakFormatter;
+use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookup;
+use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookupFactory;
/**
* @covers Wikibase\Client\DataAccess\DataAccessSnakFormatterFactory
@@ -32,11 +34,24 @@
class DataAccessSnakFormatterFactoryTest extends PHPUnit_Framework_TestCase {
private function getDataAccessSnakFormatterFactory( $expectedFormat ) {
+ $languageFallbackLabelDescriptionLookup =
$this->getMockBuilder( LanguageFallbackLabelDescriptionLookup::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $languageFallbackLabelDescriptionLookupFactory =
$this->getMockBuilder( LanguageFallbackLabelDescriptionLookupFactory::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $languageFallbackLabelDescriptionLookupFactory->expects(
$this->once() )
+ ->method( 'newLabelDescriptionLookup' )
+ ->will( $this->returnValue(
$languageFallbackLabelDescriptionLookup ) );
+
return new DataAccessSnakFormatterFactory(
$this->getLanguageFallbackChainFactory(),
$this->getOutputFormatSnakFormatterFactory(
$expectedFormat ),
$this->getMock( PropertyDataTypeLookup::class ),
- $this->getMock( EntityIdParser::class )
+ $this->getMock( EntityIdParser::class ),
+ $languageFallbackLabelDescriptionLookupFactory
);
}
diff --git
a/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/StatementGroupRendererFactoryTest.php
b/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/StatementGroupRendererFactoryTest.php
index 98b34d5..8fb84fb 100644
---
a/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/StatementGroupRendererFactoryTest.php
+++
b/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/StatementGroupRendererFactoryTest.php
@@ -29,6 +29,8 @@
use Wikibase\LanguageFallbackChainFactory;
use Wikibase\Lib\OutputFormatSnakFormatterFactory;
use Wikibase\Lib\SnakFormatter;
+use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookup;
+use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookupFactory;
/**
* @covers
Wikibase\Client\DataAccess\PropertyParserFunction\StatementGroupRendererFactory
@@ -169,7 +171,8 @@
$this->getLanguageFallbackChainFactory(),
$formatterFactory,
$this->getMock( PropertyDataTypeLookup::class ),
- $this->getMock( EntityIdParser::class )
+ $this->getMock( EntityIdParser::class ),
+
$this->getLanguageFallbackLabelDescriptionLookupFactory()
),
$allowDataAccessInUserLanguage
);
@@ -196,7 +199,8 @@
$this->getLanguageFallbackChainFactory(),
$this->getSnakFormatterFactory(),
$this->getMock( PropertyDataTypeLookup::class ),
- $this->getMock( EntityIdParser::class )
+ $this->getMock( EntityIdParser::class ),
+
$this->getLanguageFallbackLabelDescriptionLookupFactory()
),
$allowDataAccessInUserLanguage
);
@@ -306,4 +310,20 @@
return $parserOptions;
}
+ private function getLanguageFallbackLabelDescriptionLookupFactory() {
+ $languageFallbackLabelDescriptionLookup =
$this->getMockBuilder( LanguageFallbackLabelDescriptionLookup::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $languageFallbackLabelDescriptionLookupFactory =
$this->getMockBuilder( LanguageFallbackLabelDescriptionLookupFactory::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $languageFallbackLabelDescriptionLookupFactory->expects(
$this->any() )
+ ->method( 'newLabelDescriptionLookup' )
+ ->will( $this->returnValue(
$languageFallbackLabelDescriptionLookup ) );
+
+ return $languageFallbackLabelDescriptionLookupFactory;
+ }
+
}
diff --git
a/client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibraryTest.php
b/client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibraryTest.php
index 769a19b..284f277 100644
---
a/client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibraryTest.php
+++
b/client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibraryTest.php
@@ -146,11 +146,8 @@
$label = $luaWikibaseLibrary->getLabel( 'Q885588' );
$this->assertEquals( [ 'پسیک', 'ku-arab' ], $label, 'getLabel'
);
- // All languages in the fallback chain for 'ku-arab' count as
"used".
$usage =
$luaWikibaseLibrary->getUsageAccumulator()->getUsages();
- $this->assertArrayHasKey( 'Q885588#L.ku', $usage );
$this->assertArrayHasKey( 'Q885588#L.ku-arab', $usage );
- $this->assertArrayHasKey( 'Q885588#L.ku-latn', $usage );
}
public function testGetEntityInvalidIdType() {
@@ -295,11 +292,13 @@
$luaWikibaseLibrary->renderSnak( $snak )
);
- // All languages in the fallback chain for 'ku-arab' count as
"used".
+ // All languages in the fallback chain from 'ku' to 'ku-latn'
count as "used".
$usage =
$luaWikibaseLibrary->getUsageAccumulator()->getUsages();
$this->assertArrayHasKey( 'Q885588#L.ku', $usage );
$this->assertArrayHasKey( 'Q885588#L.ku-arab', $usage );
$this->assertArrayHasKey( 'Q885588#L.ku-latn', $usage );
+ $this->assertArrayNotHasKey( 'Q885588#L.en', $usage );
+
$this->assertArrayHasKey( 'Q885588#T', $usage );
$this->assertSame( true, $cacheSplit );
diff --git
a/client/tests/phpunit/includes/Usage/UsageTrackingLanguageFallbackLabelDescriptionLookupTest.php
b/client/tests/phpunit/includes/Usage/UsageTrackingLanguageFallbackLabelDescriptionLookupTest.php
new file mode 100644
index 0000000..5138e66
--- /dev/null
+++
b/client/tests/phpunit/includes/Usage/UsageTrackingLanguageFallbackLabelDescriptionLookupTest.php
@@ -0,0 +1,116 @@
+<?php
+
+namespace Wikibase\Client\Tests\Usage;
+
+use MediaWikiTestCase;
+use Wikibase\Client\Usage\UsageTrackingLanguageFallbackLabelDescriptionLookup;
+use Wikibase\Client\Usage\HashUsageAccumulator;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Term\TermFallback;
+use Wikibase\LanguageFallbackChain;
+use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookup;
+
+/**
+ * @covers
Wikibase\Client\Usage\UsageTrackingLanguageFallbackLabelDescriptionLookup
+ *
+ * @group Wikibase
+ * @group WikibaseClient
+ * @group WikibaseUsageTracking
+ *
+ * @license GPL-2.0+
+ * @author Marius Hoch
+ */
+class UsageTrackingLanguageFallbackLabelDescriptionLookupTest extends
MediaWikiTestCase {
+
+ public function provideGetLabel() {
+ return [
+ 'No term found -> all languages tracked' => [
+ [ 'Q2#L.a', 'Q2#L.b', 'Q2#L.c' ],
+ [ 'a', 'b', 'c' ],
+ null
+ ],
+ 'Only language in chain used' => [
+ [ 'Q2#L.en' ],
+ [ 'en' ],
+ new TermFallback( 'en', 'blah blah blah', 'en',
null )
+ ],
+ 'One language in chain used' => [
+ [ 'Q2#L.de', 'Q2#L.es' ],
+ [ 'de', 'es', 'en' ],
+ new TermFallback( 'de', 'blah blah blah', 'es',
null )
+ ],
+ 'Last language in chain used' => [
+ [ 'Q2#L.de', 'Q2#L.es', 'Q2#L.ru' ],
+ [ 'de', 'es', 'ru' ],
+ new TermFallback( 'de', 'blah blah blah', 'ru',
null )
+ ],
+ 'Transliteration' => [
+ [ 'Q2#L.foo', 'Q2#L.ku', 'Q2#L.ku-arab',
'Q2#L.ku-latn' ],
+ [ 'foo', 'ku', 'ku-arab', 'ku-latn', 'en' ],
+ new TermFallback( 'ku', 'blah blah blah', 'ku',
'ku-latn' )
+ ]
+ ];
+ }
+
+ /**
+ * @dataProvider provideGetLabel
+ */
+ public function testGetLabel( array $expectedUsages, array
$fetchLanguageCodes, TermFallback $term = null ) {
+ $q2 = new ItemId( 'Q2' );
+
+ $usageAccumulator = new HashUsageAccumulator();
+
+ $languageFallbackChain = $this->getMockBuilder(
LanguageFallbackChain::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+ $languageFallbackChain->expects( $this->once() )
+ ->method( 'getFetchLanguageCodes' )
+ ->will( $this->returnValue( $fetchLanguageCodes ) );
+
+ $usageTrackingLanguageFallbackLabelDescriptionLookup = new
UsageTrackingLanguageFallbackLabelDescriptionLookup(
+ $this->getLanguageFallbackLabelDescriptionLookup(
'getLabel', $q2, $term ),
+ $usageAccumulator,
+ $languageFallbackChain
+ );
+
+ $this->assertSame( $term,
$usageTrackingLanguageFallbackLabelDescriptionLookup->getLabel( $q2 ) );
+ $this->assertSame( $expectedUsages, array_keys(
$usageAccumulator->getUsages() ) );
+ }
+
+ public function testGetDescription() {
+ $q2 = new ItemId( 'Q2' );
+ $description = new TermFallback( 'de', 'blah', 'df', 'sd' );
+
+ $usageAccumulator = new HashUsageAccumulator();
+
+ $usageTrackingLanguageFallbackLabelDescriptionLookup = new
UsageTrackingLanguageFallbackLabelDescriptionLookup(
+ $this->getLanguageFallbackLabelDescriptionLookup(
'getDescription', $q2, $description ),
+ $usageAccumulator,
+ new LanguageFallbackChain( [] )
+ );
+
+ $this->assertSame( $description,
$usageTrackingLanguageFallbackLabelDescriptionLookup->getDescription( $q2 ) );
+ $this->assertSame( [], $usageAccumulator->getUsages() );
+ }
+
+ /**
+ * @param string $method
+ * @param ItemId $item
+ * @param TermFallback|null $value
+ *
+ * @return LanguageFallbackLabelDescriptionLookup
+ */
+ private function getLanguageFallbackLabelDescriptionLookup( $method,
ItemId $item, TermFallback $value = null ) {
+ $languageFallbackLabelDescriptionLookup =
$this->getMockBuilder( LanguageFallbackLabelDescriptionLookup::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $languageFallbackLabelDescriptionLookup->expects( $this->once()
)
+ ->method( $method )
+ ->with( $item )
+ ->will( $this->returnValue( $value ) );
+
+ return $languageFallbackLabelDescriptionLookup;
+ }
+
+}
diff --git
a/client/tests/phpunit/includes/Usage/UsageTrackingSnakFormatterTest.php
b/client/tests/phpunit/includes/Usage/UsageTrackingSnakFormatterTest.php
index 5410437..19edb02 100644
--- a/client/tests/phpunit/includes/Usage/UsageTrackingSnakFormatterTest.php
+++ b/client/tests/phpunit/includes/Usage/UsageTrackingSnakFormatterTest.php
@@ -5,9 +5,11 @@
use DataValues\StringValue;
use DataValues\UnboundedQuantityValue;
use Wikibase\Client\Usage\HashUsageAccumulator;
+use Wikibase\Client\Usage\UsageTrackingLanguageFallbackLabelDescriptionLookup;
use Wikibase\Client\Usage\UsageTrackingSnakFormatter;
use Wikibase\DataModel\Entity\EntityIdParser;
use Wikibase\DataModel\Entity\EntityIdValue;
+use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
@@ -23,6 +25,7 @@
*
* @license GPL-2.0+
* @author Daniel Kinzler
+ * @author Marius Hoch
*/
class UsageTrackingSnakFormatterTest extends \MediaWikiTestCase {
@@ -42,6 +45,27 @@
return $mockFormatter;
}
+ /**
+ * Gets a UsageTrackingLanguageFallbackLabelDescriptionLookup that
makes sure that
+ * it has been called/ not called with the correct EntityId.
+ *
+ * @param EntityId|null $formatEntity
+ *
+ * @return UsageTrackingLanguageFallbackLabelDescriptionLookup
+ */
+ private function getUsageTrackingLabelDescriptionLookup( EntityId
$formatEntity = null ) {
+ $usageTrackingLabelDescriptionLookup = $this->getMockBuilder(
UsageTrackingLanguageFallbackLabelDescriptionLookup::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $getLabelInvocationMocker =
$usageTrackingLabelDescriptionLookup->expects( $this->exactly( $formatEntity ?
1 : 0 ) )
+ ->method( 'getLabel' )
+ ->with( $formatEntity )
+ ->will( $this->returnValue( 'foobar' ) );
+
+ return $usageTrackingLabelDescriptionLookup;
+ }
+
public function testFormatSnak_item() {
$p1 = new PropertyId( 'P1' );
$q1 = new ItemId( 'Q1' );
@@ -50,13 +74,15 @@
$mockFormatter = $this->getMockSnakFormatter( 'formatSnak',
'test' );
$acc = new HashUsageAccumulator();
$formatter = new UsageTrackingSnakFormatter(
- $mockFormatter, $acc, [ 'ru', 'en' ], $this->getMock(
EntityIdParser::class ) );
+ $mockFormatter,
+ $acc,
+ $this->getMock( EntityIdParser::class ),
+ $this->getUsageTrackingLabelDescriptionLookup( $q1 )
+ );
$formatter->formatSnak( $itemSnak );
$usages = $acc->getUsages();
- $this->assertArrayHasKey( 'Q1#L.ru', $usages );
- $this->assertArrayHasKey( 'Q1#L.en', $usages );
$this->assertArrayHasKey( 'Q1#T', $usages );
}
@@ -76,15 +102,15 @@
public function formatSnakQuantityProvider() {
return [
- [ '1', [] ],
- [ 'Q1', [ 'Q1#L.ru', 'Q1#L.en' ] ],
+ [ '1', null ],
+ [ 'Q1', new ItemId( 'Q1' ) ],
];
}
/**
* @dataProvider formatSnakQuantityProvider
*/
- public function testFormatSnak_quantity( $unit, array $usages ) {
+ public function testFormatSnak_quantity( $unit, EntityId
$formattedEntityId = null ) {
$p1 = new PropertyId( 'P1' );
$itemSnak = new PropertyValueSnak(
$p1, UnboundedQuantityValue::newFromNumber( '1', $unit
) );
@@ -92,11 +118,15 @@
$mockFormatter = $this->getMockSnakFormatter( 'formatSnak',
'test' );
$acc = new HashUsageAccumulator();
$formatter = new UsageTrackingSnakFormatter(
- $mockFormatter, $acc, [ 'ru', 'en' ],
$this->getRepoItemUriParser() );
+ $mockFormatter,
+ $acc,
+ $this->getRepoItemUriParser(),
+ $this->getUsageTrackingLabelDescriptionLookup(
$formattedEntityId )
+ );
$formatter->formatSnak( $itemSnak );
- $this->assertEquals( $usages, array_keys( $acc->getUsages() ) );
+ $this->assertSame( [], $acc->getUsages() );
}
public function testFormatSnak_novalue() {
@@ -106,7 +136,11 @@
$mockFormatter = $this->getMockSnakFormatter( 'formatSnak',
'test' );
$acc = new HashUsageAccumulator();
$formatter = new UsageTrackingSnakFormatter(
- $mockFormatter, $acc, [ 'ru', 'en' ], $this->getMock(
EntityIdParser::class ) );
+ $mockFormatter,
+ $acc,
+ $this->getMock( EntityIdParser::class ),
+ $this->getUsageTrackingLabelDescriptionLookup()
+ );
$formatter->formatSnak( $novalueSnak );
$this->assertEmpty( $acc->getUsages(), 'novalue' );
@@ -119,7 +153,11 @@
$mockFormatter = $this->getMockSnakFormatter( 'formatSnak',
'test' );
$acc = new HashUsageAccumulator();
$formatter = new UsageTrackingSnakFormatter(
- $mockFormatter, $acc, [ 'ru', 'en' ], $this->getMock(
EntityIdParser::class ) );
+ $mockFormatter,
+ $acc,
+ $this->getMock( EntityIdParser::class ),
+ $this->getUsageTrackingLabelDescriptionLookup()
+ );
$formatter->formatSnak( $stringSnak );
$this->assertEmpty( $acc->getUsages(), 'string value' );
@@ -131,7 +169,11 @@
$acc = new HashUsageAccumulator();
$formatter = new UsageTrackingSnakFormatter(
- $mockFormatter, $acc, [ 'ru', 'en' ], $this->getMock(
EntityIdParser::class ) );
+ $mockFormatter,
+ $acc,
+ $this->getMock( EntityIdParser::class ),
+ $this->getUsageTrackingLabelDescriptionLookup()
+ );
$this->assertEquals( 'TEST', $formatter->getFormat(),
'getFormat' );
$this->assertCount( 0, $acc->getUsages() );
diff --git
a/client/tests/phpunit/includes/Usage/UsageTrackingTermLookupTest.php
b/client/tests/phpunit/includes/Usage/UsageTrackingTermLookupTest.php
deleted file mode 100644
index c3951c6..0000000
--- a/client/tests/phpunit/includes/Usage/UsageTrackingTermLookupTest.php
+++ /dev/null
@@ -1,87 +0,0 @@
-<?php
-
-namespace Wikibase\Client\Tests\Usage;
-
-use Wikibase\Client\Usage\HashUsageAccumulator;
-use Wikibase\Client\Usage\UsageTrackingTermLookup;
-use Wikibase\DataModel\Entity\ItemId;
-use Wikibase\DataModel\Services\Lookup\TermLookup;
-
-/**
- * @covers Wikibase\Client\Usage\UsageTrackingTermLookup
- *
- * @group Wikibase
- * @group WikibaseClient
- * @group WikibaseUsageTracking
- *
- * @license GPL-2.0+
- * @author Daniel Kinzler
- */
-class UsageTrackingTermLookupTest extends \MediaWikiTestCase {
-
- /**
- * @param string $method Method name expected to be called once.
- * @param ItemId $entityId Expected entity id.
- * @param string|string[] $languageCode Expected language code or array
of language codes.
- *
- * @return TermLookup
- */
- private function getMockTermLookup( $method, ItemId $entityId,
$languageCode ) {
- $mockLookup = $this->getMock( TermLookup::class );
- $mockLookup->expects( $this->once() )
- ->method( $method )
- ->with( $entityId, $languageCode )
- ->will( $this->returnValue( 'TEST' ) );
-
- return $mockLookup;
- }
-
- public function testGetLabel() {
- $q1 = new ItemId( 'Q1' );
- $mockLookup = $this->getMockTermLookup( 'getLabel', $q1, 'en' );
-
- $acc = new HashUsageAccumulator();
-
- $lookup = new UsageTrackingTermLookup( $mockLookup, $acc );
- $lookup->getLabel( $q1, 'en' );
-
- $this->assertCount( 1, $acc->getUsages() );
- }
-
- public function testGetDescription() {
- $q1 = new ItemId( 'Q1' );
- $mockLookup = $this->getMockTermLookup( 'getDescription', $q1,
'en' );
-
- $acc = new HashUsageAccumulator();
-
- $lookup = new UsageTrackingTermLookup( $mockLookup, $acc );
- $lookup->getDescription( $q1, 'en' );
-
- $this->assertCount( 0, $acc->getUsages() );
- }
-
- public function testGetLabels() {
- $q1 = new ItemId( 'Q1' );
- $mockLookup = $this->getMockTermLookup( 'getLabels', $q1, [
'en', 'de' ] );
-
- $acc = new HashUsageAccumulator();
-
- $lookup = new UsageTrackingTermLookup( $mockLookup, $acc );
- $lookup->getLabels( $q1, [ 'en', 'de' ] );
-
- $this->assertCount( 2, $acc->getUsages() );
- }
-
- public function testGetDescriptions() {
- $q1 = new ItemId( 'Q1' );
- $mockLookup = $this->getMockTermLookup( 'getDescriptions', $q1,
[ 'en', 'de' ] );
-
- $acc = new HashUsageAccumulator();
-
- $lookup = new UsageTrackingTermLookup( $mockLookup, $acc );
- $lookup->getDescriptions( $q1, [ 'en', 'de' ] );
-
- $this->assertCount( 0, $acc->getUsages() );
- }
-
-}
--
To view, visit https://gerrit.wikimedia.org/r/369442
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I75b4c519c4622126c52f6f6eddea9ca3613d2084
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Hoo man <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Jackmcbarn <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits