jenkins-bot has submitted this change and it was merged.
Change subject: Use the user's language in DataAccess if
allowDataAccessInUserLanguage
......................................................................
Use the user's language in DataAccess if allowDataAccessInUserLanguage
Bug: T75460
Change-Id: I2c162d8f14956abe834400967ac81cd83ff0a718
---
M client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
M client/includes/DataAccess/Scribunto/WikibaseLuaBindings.php
M
client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibraryTest.php
M client/tests/phpunit/includes/DataAccess/Scribunto/WikibaseLuaBindingsTest.php
4 files changed, 165 insertions(+), 101 deletions(-)
Approvals:
Hoo man: Looks good to me, approved
jenkins-bot: Verified
diff --git
a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
index d650989..a32bd28 100644
--- a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
+++ b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibrary.php
@@ -140,13 +140,46 @@
}
/**
+ * Returns the language to use. If we are on a multilingual wiki
+ * (allowDataAccessInUserLanguage is true) this will be the user's
interface
+ * language, otherwise it will be the content language.
+ * In a perfect world, this would equal Parser::getTargetLanguage.
+ *
+ * This doesn't split the ParserCache by language yet, please see
+ * self::splitParserCacheIfMultilingual for that.
+ *
* @return Language
*/
private function getLanguage() {
- // For the language we need $wgContLang, not parser target
language or anything else.
- // See Scribunto_LuaLanguageLibrary::getContLangCode().
global $wgContLang;
+
+ if ( $this->allowDataAccessInUserLanguage() ) {
+ // Can't use ParserOptions::getUserLang as that already
splits the ParserCache
+ $userLang =
$this->getParserOptions()->getUser()->getOption( 'language' );
+
+ return Language::factory( $userLang );
+ }
+
return $wgContLang;
+ }
+
+ /**
+ * Splits the page's ParserCache in case we're on a multilingual wiki
+ */
+ private function splitParserCacheIfMultilingual() {
+ if ( $this->allowDataAccessInUserLanguage() ) {
+ // ParserOptions::getUserLang splits the ParserCache
+ $this->getParserOptions()->getUserLang();
+ }
+ }
+
+ /**
+ * @return bool
+ */
+ private function allowDataAccessInUserLanguage() {
+ $settings = WikibaseClient::getDefaultInstance()->getSettings();
+
+ return $settings->getSetting( 'allowDataAccessInUserLanguage' );
}
private function newEntityAccessor() {
@@ -209,7 +242,6 @@
$wikibaseClient->getSettings(),
$labelDescriptionLookup,
$usageAccumulator,
- $this->getParserOptions(),
$wikibaseClient->getSettings()->getSetting(
'siteGlobalID' )
);
}
@@ -256,6 +288,7 @@
*/
public function getEntity( $prefixedEntityId ) {
$this->checkType( 'getEntity', 1, $prefixedEntityId, 'string' );
+ $this->splitParserCacheIfMultilingual();
try {
$entityArr = $this->getEntityAccessor()->getEntity(
$prefixedEntityId );
@@ -308,6 +341,8 @@
*/
public function getLabel( $prefixedEntityId ) {
$this->checkType( 'getLabel', 1, $prefixedEntityId, 'string' );
+ $this->splitParserCacheIfMultilingual();
+
return array( $this->getLuaBindings()->getLabel(
$prefixedEntityId ) );
}
@@ -322,6 +357,8 @@
*/
public function getDescription( $prefixedEntityId ) {
$this->checkType( 'getDescription', 1, $prefixedEntityId,
'string' );
+ $this->splitParserCacheIfMultilingual();
+
return array( $this->getLuaBindings()->getDescription(
$prefixedEntityId ) );
}
@@ -351,6 +388,8 @@
*/
public function renderSnak( $snakSerialization ) {
$this->checkType( 'renderSnak', 1, $snakSerialization, 'table'
);
+ $this->splitParserCacheIfMultilingual();
+
try {
$ret = array(
$this->getSnakSerializationRenderer()->renderSnak( $snakSerialization ) );
return $ret;
@@ -371,6 +410,8 @@
*/
public function renderSnaks( $snaksSerialization ) {
$this->checkType( 'renderSnaks', 1, $snaksSerialization,
'table' );
+ $this->splitParserCacheIfMultilingual();
+
try {
$ret = array(
$this->getSnakSerializationRenderer()->renderSnaks( $snaksSerialization ) );
return $ret;
@@ -389,27 +430,16 @@
* @return string[]|null[]
*/
public function resolvePropertyId( $propertyLabelOrId ) {
+ global $wgContLang;
+
$this->checkType( 'resolvePropertyId', 1, $propertyLabelOrId,
'string' );
try {
- $languageCode = $this->getLanguage()->getCode();
- $propertyId =
$this->getPropertyIdResolver()->resolvePropertyId( $propertyLabelOrId,
$languageCode );
+ $propertyId =
$this->getPropertyIdResolver()->resolvePropertyId( $propertyLabelOrId,
$wgContLang->getCode() );
$ret = array( $propertyId->getSerialization() );
return $ret;
} catch ( PropertyLabelNotResolvedException $e ) {
return array( null );
}
- }
-
- /**
- * Wrapper for getUserLang in WikibaseLuaBindings
- * Side effect: Splits the parser cache by user language!
- *
- * @since 0.5
- *
- * @return string[]
- */
- public function getUserLang() {
- return array( $this->getLuaBindings()->getUserLang() );
}
}
diff --git a/client/includes/DataAccess/Scribunto/WikibaseLuaBindings.php
b/client/includes/DataAccess/Scribunto/WikibaseLuaBindings.php
index dbaf5c5..de084a3 100644
--- a/client/includes/DataAccess/Scribunto/WikibaseLuaBindings.php
+++ b/client/includes/DataAccess/Scribunto/WikibaseLuaBindings.php
@@ -3,7 +3,6 @@
namespace Wikibase\Client\DataAccess\Scribunto;
use InvalidArgumentException;
-use ParserOptions;
use Wikibase\Client\Usage\UsageAccumulator;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\Entity\EntityIdParser;
@@ -59,11 +58,6 @@
private $usageAccumulator;
/**
- * @var ParserOptions
- */
- private $parserOptions;
-
- /**
* @var string
*/
private $siteId;
@@ -75,7 +69,6 @@
* @param SettingsArray $settings
* @param LabelDescriptionLookup $labelDescriptionLookup
* @param UsageAccumulator $usageAccumulator for tracking title usage
via getEntityId.
- * @param ParserOptions $parserOptions
* @param string $siteId
*
* @note: label usage is not tracked in $usageAccumulator. This should
be done inside
@@ -88,7 +81,6 @@
SettingsArray $settings,
LabelDescriptionLookup $labelDescriptionLookup,
UsageAccumulator $usageAccumulator,
- ParserOptions $parserOptions,
$siteId
) {
$this->entityIdParser = $entityIdParser;
@@ -97,7 +89,6 @@
$this->settings = $settings;
$this->labelDescriptionLookup = $labelDescriptionLookup;
$this->usageAccumulator = $usageAccumulator;
- $this->parserOptions = $parserOptions;
$this->siteId = $siteId;
}
@@ -219,20 +210,6 @@
}
return null;
- }
-
- /**
- * Get the user's language.
- * Side effect: Splits the parser cache by user language!
- *
- * @since 0.5
- *
- * @return string Language code
- */
- public function getUserLang() {
- // Note: We can't just inject the user language, as getting it
from ParserOptions
- // already splits the cache (which also is an intended side
effect here).
- return $this->parserOptions->getUserLang();
}
}
diff --git
a/client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibraryTest.php
b/client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibraryTest.php
index d8eda28..9d3912f 100644
---
a/client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibraryTest.php
+++
b/client/tests/phpunit/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseLibraryTest.php
@@ -7,7 +7,9 @@
use ParserOptions;
use Scribunto;
use Title;
+use User;
use Wikibase\Client\DataAccess\Scribunto\Scribunto_LuaWikibaseLibrary;
+use Wikibase\Client\WikibaseClient;
/**
* @covers Wikibase\Client\DataAccess\Scribunto\Scribunto_LuaWikibaseLibrary
@@ -25,6 +27,11 @@
protected static $moduleName = 'LuaWikibaseLibraryTests';
+ /**
+ * @var bool
+ */
+ private $oldAllowDataAccessInUserLanguage;
+
protected function getTestModules() {
return parent::getTestModules() + array(
'LuaWikibaseLibraryTests' => __DIR__ .
'/LuaWikibaseLibraryTests.lua',
@@ -37,6 +44,26 @@
protected static function getEntityAccessLimit() {
// testGetEntity_entityAccessLimitExceeded needs this to be 2
return 2;
+ }
+
+ protected function setUp() {
+ parent::setUp();
+
+ $settings = WikibaseClient::getDefaultInstance()->getSettings();
+ $this->oldAllowDataAccessInUserLanguage =
$settings->getSetting( 'allowDataAccessInUserLanguage' );
+ }
+
+ protected function tearDown() {
+ parent::tearDown();
+
+ $this->setAllowDataAccessInUserLanguage(
$this->oldAllowDataAccessInUserLanguage );
+ }
+
+ public function allowDataAccessInUserLanguageProvider() {
+ return array(
+ array( true ),
+ array( false ),
+ );
}
public function testConstructor() {
@@ -60,10 +87,18 @@
);
}
- public function testGetEntity() {
- $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary();
+ /**
+ * @dataProvider allowDataAccessInUserLanguageProvider
+ */
+ public function testGetEntity( $allowDataAccessInUserLanguage ) {
+ $this->setAllowDataAccessInUserLanguage(
$allowDataAccessInUserLanguage );
+ $cacheSplit = false;
+
+ $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary(
$cacheSplit );
$entity = $luaWikibaseLibrary->getEntity( 'Q888' );
$this->assertEquals( array( null ), $entity );
+
+ $this->assertSame( $allowDataAccessInUserLanguage, $cacheSplit
);
}
public function testGetEntity_hasLanguageFallback() {
@@ -139,8 +174,41 @@
$this->assertEquals( array( null ), $entityId );
}
- public function testRenderSnak() {
- $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary();
+ /**
+ * @dataProvider allowDataAccessInUserLanguageProvider
+ */
+ public function testGetLabel( $allowDataAccessInUserLanguage ) {
+ $user = new User();
+ $user->setOption( 'language', 'de' );
+
+ $this->setMwGlobals( array(
+ 'wgContLang' => Language::factory( 'en' ),
+ 'wgUser' => $user
+ ) );
+
+ $this->setAllowDataAccessInUserLanguage(
$allowDataAccessInUserLanguage );
+ $cacheSplit = false;
+
+ $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary(
$cacheSplit );
+ $label = $luaWikibaseLibrary->getLabel( 'Q32487' );
+
+ if ( $allowDataAccessInUserLanguage ) {
+ $this->assertSame( 'Lua Test Item', $label[0] );
+ } else {
+ $this->assertSame( 'Test all the code paths', $label[0]
);
+ }
+
+ $this->assertSame( $allowDataAccessInUserLanguage, $cacheSplit
);
+ }
+
+ /**
+ * @dataProvider allowDataAccessInUserLanguageProvider
+ */
+ public function testRenderSnak( $allowDataAccessInUserLanguage ) {
+ $this->setAllowDataAccessInUserLanguage(
$allowDataAccessInUserLanguage );
+ $cacheSplit = false;
+
+ $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary(
$cacheSplit );
$entityArr = $luaWikibaseLibrary->getEntity( 'Q32488' );
$snak = $entityArr[0]['claims']['P456'][1]['mainsnak'];
@@ -152,8 +220,20 @@
// When rendering the item reference in the snak,
// track table and title usage.
$usage =
$luaWikibaseLibrary->getUsageAccumulator()->getUsages();
- $this->assertArrayHasKey( 'Q885588#L.de', $usage );
+
+ if ( $allowDataAccessInUserLanguage ) {
+ global $wgUser;
+
+ $userLang = $wgUser->getOption( 'language' );
+
+ $this->assertArrayHasKey( 'Q885588#L.' . $userLang,
$usage );
+ } else {
+ $this->assertArrayHasKey( 'Q885588#L.de', $usage );
+ }
+
$this->assertArrayHasKey( 'Q885588#T', $usage );
+
+ $this->assertSame( $allowDataAccessInUserLanguage, $cacheSplit
);
}
public function testRenderSnak_invalidSerialization() {
@@ -163,8 +243,14 @@
$luaWikibaseLibrary->renderSnak( array( 'a' => 'b' ) );
}
- public function testRenderSnaks() {
- $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary();
+ /**
+ * @dataProvider allowDataAccessInUserLanguageProvider
+ */
+ public function testRenderSnaks( $allowDataAccessInUserLanguage ) {
+ $this->setAllowDataAccessInUserLanguage(
$allowDataAccessInUserLanguage );
+ $cacheSplit = false;
+
+ $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary(
$cacheSplit );
$entityArr = $luaWikibaseLibrary->getEntity( 'Q32487' );
$snaks = $entityArr[0]['claims']['P342'][1]['qualifiers'];
@@ -172,6 +258,8 @@
array( 'A qualifier Snak, Moar qualifiers' ),
$luaWikibaseLibrary->renderSnaks( $snaks )
);
+
+ $this->assertSame( $allowDataAccessInUserLanguage, $cacheSplit
);
}
public function testRenderSnaks_invalidSerialization() {
@@ -208,38 +296,28 @@
);
}
- public function testGetUserLang() {
+ /**
+ * @param bool &$cacheSplit Will become true when the ParserCache has
been split
+ *
+ * @return Scribunto_LuaWikibaseLibrary
+ */
+ private function newScribuntoLuaWikibaseLibrary( &$cacheSplit = false )
{
+ $title = Title::newFromText( 'Whatever' );
$parserOptions = new ParserOptions();
- $parserOptions->setUserLang( Language::factory( 'ru' ) );
- $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary(
$parserOptions );
+ $parser = new Parser();
+ $parser->startExternalParse(
+ $title,
+ $parserOptions,
+ Parser::OT_HTML
+ );
- $self = $this; // PHP 5.3 ...
- $cacheSplit = false;
+ $self = $this; // PHP 5.3 ...
$parserOptions->registerWatcher(
function( $optionName ) use ( $self, &$cacheSplit ) {
$self->assertSame( 'userlang', $optionName );
$cacheSplit = true;
}
- );
-
- $userLang = $luaWikibaseLibrary->getUserLang();
- $this->assertSame( array( 'ru' ), $userLang );
- $this->assertTrue( $cacheSplit );
- }
-
- /**
- * @param ParserOptions|null $parserOptions
- * @return Scribunto_LuaWikibaseLibrary
- */
- private function newScribuntoLuaWikibaseLibrary( ParserOptions
$parserOptions = null ) {
- $title = Title::newFromText( 'Whatever' );
-
- $parser = new Parser();
- $parser->startExternalParse(
- $title,
- $parserOptions ?: new ParserOptions(),
- Parser::OT_HTML
);
$engine = Scribunto::newDefaultEngine( array(
@@ -251,4 +329,12 @@
return new Scribunto_LuaWikibaseLibrary( $engine );
}
+ /**
+ * @param bool $value
+ */
+ private function setAllowDataAccessInUserLanguage( $value ) {
+ $settings = WikibaseClient::getDefaultInstance()->getSettings();
+ $settings->setSetting( 'allowDataAccessInUserLanguage', $value
);
+ }
+
}
diff --git
a/client/tests/phpunit/includes/DataAccess/Scribunto/WikibaseLuaBindingsTest.php
b/client/tests/phpunit/includes/DataAccess/Scribunto/WikibaseLuaBindingsTest.php
index e6f9393..1424733 100644
---
a/client/tests/phpunit/includes/DataAccess/Scribunto/WikibaseLuaBindingsTest.php
+++
b/client/tests/phpunit/includes/DataAccess/Scribunto/WikibaseLuaBindingsTest.php
@@ -49,15 +49,12 @@
* @param EntityLookup $entityLookup
* @param SiteLinkLookup $siteLinkLookup
* @param UsageAccumulator|null $usageAccumulator
- * @param ParserOptions|null $parserOptions
- *
* @return WikibaseLuaBindings
*/
private function getWikibaseLuaBindings(
EntityLookup $entityLookup,
SiteLinkLookup $siteLinkLookup,
- UsageAccumulator $usageAccumulator = null,
- ParserOptions $parserOptions = null
+ UsageAccumulator $usageAccumulator = null
) {
$labelDescriptionLookup = $this->getMock(
'Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup' );
$labelDescriptionLookup->expects( $this->any() )
@@ -75,7 +72,6 @@
new SettingsArray(),
$labelDescriptionLookup,
$usageAccumulator ?: new HashUsageAccumulator(),
- $parserOptions ?: new ParserOptions(),
"enwiki" // siteId
);
}
@@ -242,31 +238,6 @@
$this->assertTrue( $this->hasUsage( $usages->getUsages(),
$itemId, EntityUsage::TITLE_USAGE ), 'title usage' );
$this->assertFalse( $this->hasUsage( $usages->getUsages(),
$itemId, EntityUsage::LABEL_USAGE ), 'label usage' );
$this->assertFalse( $this->hasUsage( $usages->getUsages(),
$itemId, EntityUsage::ALL_USAGE ), 'all usage' );
- }
-
- public function testGetUserLang() {
- $parserOptions = new ParserOptions();
- $parserOptions->setUserLang( Language::factory( 'ru' ) );
-
- $self = $this; // PHP 5.3 ...
- $cacheSplit = false;
- $parserOptions->registerWatcher(
- function( $optionName ) use ( $self, &$cacheSplit ) {
- $self->assertSame( 'userlang', $optionName );
- $cacheSplit = true;
- }
- );
-
- $wikibaseLuaBindings = $this->getWikibaseLuaBindings(
- new MockRepository(),
- new HashSiteLinkStore(),
- new HashUsageAccumulator(),
- $parserOptions
- );
-
- $userLang = $wikibaseLuaBindings->getUserLang();
- $this->assertSame( 'ru', $userLang );
- $this->assertTrue( $cacheSplit );
}
protected function getItem() {
--
To view, visit https://gerrit.wikimedia.org/r/243645
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I2c162d8f14956abe834400967ac81cd83ff0a718
Gerrit-PatchSet: 3
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: Hoo man <[email protected]>
Gerrit-Reviewer: Jackmcbarn <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits