Hoo man has uploaded a new change for review. https://gerrit.wikimedia.org/r/313976
Change subject: Create DataAccessSnakFormatterFactory ...................................................................... Create DataAccessSnakFormatterFactory Factor the code for generating SnakFormatters out of StatementGroupRendererFactory and Scribunto_LuaWikibaseEntityLibrary in order to minimize code duplication. Change-Id: Ic94adc479a9d0b0d1bded1312e6e308e3fea103d --- A client/includes/DataAccess/DataAccessSnakFormatterFactory.php M client/includes/DataAccess/PropertyParserFunction/StatementGroupRendererFactory.php M client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibrary.php M client/includes/WikibaseClient.php A client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterFactoryTest.php M client/tests/phpunit/includes/DataAccess/PropertyParserFunction/StatementGroupRendererFactoryTest.php M client/tests/phpunit/includes/WikibaseClientTest.php 7 files changed, 188 insertions(+), 78 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/76/313976/1 diff --git a/client/includes/DataAccess/DataAccessSnakFormatterFactory.php b/client/includes/DataAccess/DataAccessSnakFormatterFactory.php new file mode 100644 index 0000000..f2c14ac --- /dev/null +++ b/client/includes/DataAccess/DataAccessSnakFormatterFactory.php @@ -0,0 +1,79 @@ +<?php + +namespace Wikibase\Client\DataAccess; + +use Language; +use ValueFormatters\FormatterOptions; +use ValueFormatters\ValueFormatter; +use Wikibase\Client\Usage\UsageAccumulator; +use Wikibase\Client\Usage\UsageTrackingSnakFormatter; +use Wikibase\LanguageFallbackChainFactory; +use Wikibase\Lib\FormatterLabelDescriptionLookupFactory; +use Wikibase\Lib\OutputFormatSnakFormatterFactory; +use Wikibase\Lib\SnakFormatter; + +/** + * @since 0.5 + * + * @license GPL-2.0+ + * @author Katie Filbert < aude.w...@gmail.com > + * @author Marius Hoch + */ +class DataAccessSnakFormatterFactory { + + /** + * @var LanguageFallbackChainFactory + */ + private $languageFallbackChainFactory; + + /** + * @var OutputFormatSnakFormatterFactory + */ + private $snakFormatterFactory; + + /** + * @param LanguageFallbackChainFactory $languageFallbackChainFactory + * @param OutputFormatSnakFormatterFactory $snakFormatterFactory + */ + public function __construct( + LanguageFallbackChainFactory $languageFallbackChainFactory, + OutputFormatSnakFormatterFactory $snakFormatterFactory + ) { + $this->languageFallbackChainFactory = $languageFallbackChainFactory; + $this->snakFormatterFactory = $snakFormatterFactory; + } + + /** + * @param Language $language + * @param UsageAccumulator $usageAccumulator + * + * @return SnakFormatter + */ + public function newSnakFormatterForLanguage( + Language $language, + UsageAccumulator $usageAccumulator + ) { + $languageFallbackChain = $this->languageFallbackChainFactory->newFromLanguage( + $language, + LanguageFallbackChainFactory::FALLBACK_ALL + ); + + $options = new FormatterOptions( array( + FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN => $languageFallbackChain, + ValueFormatter::OPT_LANG => $language->getCode(), + // ...more options... (?) + ) ); + + $snakFormatter = new UsageTrackingSnakFormatter( + $this->snakFormatterFactory->getSnakFormatter( + SnakFormatter::FORMAT_WIKI, + $options + ), + $usageAccumulator, + $languageFallbackChain->getFetchLanguageCodes() + ); + + return $snakFormatter; + } + +} diff --git a/client/includes/DataAccess/PropertyParserFunction/StatementGroupRendererFactory.php b/client/includes/DataAccess/PropertyParserFunction/StatementGroupRendererFactory.php index c9aa472..a1adbb9 100644 --- a/client/includes/DataAccess/PropertyParserFunction/StatementGroupRendererFactory.php +++ b/client/includes/DataAccess/PropertyParserFunction/StatementGroupRendererFactory.php @@ -5,19 +5,13 @@ use Language; use MWException; use Parser; -use ValueFormatters\FormatterOptions; -use ValueFormatters\ValueFormatter; +use Wikibase\Client\DataAccess\DataAccessSnakFormatterFactory; use Wikibase\Client\DataAccess\PropertyIdResolver; use Wikibase\Client\DataAccess\SnaksFinder; use Wikibase\Client\DataAccess\StatementTransclusionInteractor; use Wikibase\Client\Usage\ParserOutputUsageAccumulator; use Wikibase\Client\Usage\UsageAccumulator; -use Wikibase\Client\Usage\UsageTrackingSnakFormatter; use Wikibase\DataModel\Services\Lookup\EntityLookup; -use Wikibase\LanguageFallbackChainFactory; -use Wikibase\Lib\FormatterLabelDescriptionLookupFactory; -use Wikibase\Lib\OutputFormatSnakFormatterFactory; -use Wikibase\Lib\SnakFormatter; /** * @since 0.5 @@ -38,16 +32,6 @@ private $snaksFinder; /** - * @var LanguageFallbackChainFactory - */ - private $languageFallbackChainFactory; - - /** - * @var OutputFormatSnakFormatterFactory - */ - private $snakFormatterFactory; - - /** * @var LanguageAwareRenderer[] */ private $languageAwareRenderers = array(); @@ -58,6 +42,11 @@ private $entityLookup; /** + * @var DataAccessSnakFormatterFactory + */ + private $dataAccessSnakFormatterFactory; + + /** * @var bool */ private $allowDataAccessInUserLanguage; @@ -65,24 +54,21 @@ /** * @param PropertyIdResolver $propertyIdResolver * @param SnaksFinder $snaksFinder - * @param LanguageFallbackChainFactory $languageFallbackChainFactory - * @param OutputFormatSnakFormatterFactory $snakFormatterFactory * @param EntityLookup $entityLookup + * @param DataAccessSnakFormatterFactory $dataAccessSnakFormatterFactory * @param bool $allowDataAccessInUserLanguage */ public function __construct( PropertyIdResolver $propertyIdResolver, SnaksFinder $snaksFinder, - LanguageFallbackChainFactory $languageFallbackChainFactory, - OutputFormatSnakFormatterFactory $snakFormatterFactory, EntityLookup $entityLookup, + DataAccessSnakFormatterFactory $dataAccessSnakFormatterFactory, $allowDataAccessInUserLanguage ) { $this->propertyIdResolver = $propertyIdResolver; $this->snaksFinder = $snaksFinder; - $this->languageFallbackChainFactory = $languageFallbackChainFactory; - $this->snakFormatterFactory = $snakFormatterFactory; $this->entityLookup = $entityLookup; + $this->dataAccessSnakFormatterFactory = $dataAccessSnakFormatterFactory; $this->allowDataAccessInUserLanguage = $allowDataAccessInUserLanguage; } @@ -123,7 +109,7 @@ $language, $this->propertyIdResolver, $this->snaksFinder, - $this->newSnakFormatterForLanguage( $language, $usageAccumulator ), + $this->dataAccessSnakFormatterFactory->newSnakFormatterForLanguage( $language, $usageAccumulator ), $this->entityLookup ); @@ -203,39 +189,6 @@ private function useVariants( Parser $parser ) { $converterLanguageHasVariants = $parser->getConverterLanguage()->hasVariants(); return $this->isParserUsingVariants( $parser ) && $converterLanguageHasVariants; - } - - /** - * @param Language $language - * @param UsageAccumulator $usageAccumulator - * - * @return SnakFormatter - */ - private function newSnakFormatterForLanguage( - Language $language, - UsageAccumulator $usageAccumulator - ) { - $languageFallbackChain = $this->languageFallbackChainFactory->newFromLanguage( - $language, - LanguageFallbackChainFactory::FALLBACK_ALL - ); - - $options = new FormatterOptions( array( - FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN => $languageFallbackChain, - ValueFormatter::OPT_LANG => $language->getCode(), - // ...more options... (?) - ) ); - - $snakFormatter = new UsageTrackingSnakFormatter( - $this->snakFormatterFactory->getSnakFormatter( - SnakFormatter::FORMAT_WIKI, - $options - ), - $usageAccumulator, - $languageFallbackChain->getFetchLanguageCodes() - ); - - return $snakFormatter; } } diff --git a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibrary.php b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibrary.php index 80379da..3f555a3 100644 --- a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibrary.php +++ b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibrary.php @@ -44,21 +44,10 @@ $lang = $this->getLanguage(); $wikibaseClient = WikibaseClient::getDefaultInstance(); - - $languageFallbackChain = $wikibaseClient->getDataAccessLanguageFallbackChain( $lang ); - - $formatterOptions = new FormatterOptions( array( - SnakFormatter::OPT_LANG => $lang->getCode(), - FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN => $languageFallbackChain - ) ); - - $snakFormatter = new UsageTrackingSnakFormatter( - $wikibaseClient->getSnakFormatterFactory()->getSnakFormatter( - SnakFormatter::FORMAT_WIKI, - $formatterOptions - ), - $this->getUsageAccumulator(), - $languageFallbackChain->getFetchLanguageCodes() + $snakFormatterFactory = $wikibaseClient->getDataAccessSnakFormatterFactory(); + $snakFormatter = $snakFormatterFactory->newSnakFormatterForLanguage( + $lang, + $this->getUsageAccumulator() ); $entityLookup = $wikibaseClient->getRestrictedEntityLookup(); diff --git a/client/includes/WikibaseClient.php b/client/includes/WikibaseClient.php index 26d6a2c..9810ae8 100644 --- a/client/includes/WikibaseClient.php +++ b/client/includes/WikibaseClient.php @@ -33,6 +33,7 @@ use Wikibase\Client\Changes\ChangeHandler; use Wikibase\Client\Changes\ChangeRunCoalescer; use Wikibase\Client\Changes\WikiPageUpdater; +use Wikibase\Client\DataAccess\DataAccessSnakFormatterFactory; use Wikibase\Client\DataAccess\PropertyIdResolver; use Wikibase\Client\DataAccess\PropertyParserFunction\StatementGroupRendererFactory; use Wikibase\Client\DataAccess\PropertyParserFunction\Runner; @@ -1055,14 +1056,23 @@ return new StatementGroupRendererFactory( $propertyIdResolver, new SnaksFinder(), - $this->getLanguageFallbackChainFactory(), - $this->getSnakFormatterFactory(), $entityLookup, + $this->getDataAccessSnakFormatterFactory(), $this->getSettings()->getSetting( 'allowDataAccessInUserLanguage' ) ); } /** + * @return DataAccessSnakFormatterFactory + */ + public function getDataAccessSnakFormatterFactory() { + return new DataAccessSnakFormatterFactory( + $this->getLanguageFallbackChainFactory(), + $this->getSnakFormatterFactory() + ); + } + + /** * @return Runner */ public function getPropertyParserFunctionRunner() { diff --git a/client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterFactoryTest.php b/client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterFactoryTest.php new file mode 100644 index 0000000..30e7451 --- /dev/null +++ b/client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterFactoryTest.php @@ -0,0 +1,68 @@ +<?php + +namespace Wikibase\Client\Tests\DataAccess; + +use Language; +use PHPUnit_Framework_TestCase; +use ValueFormatters\FormatterOptions; +use Wikibase\Client\DataAccess\DataAccessSnakFormatterFactory; +use Wikibase\Client\Usage\UsageAccumulator; +use Wikibase\LanguageFallbackChainFactory; +use Wikibase\Lib\OutputFormatSnakFormatterFactory; +use Wikibase\Lib\SnakFormatter; + +/** + * @covers Wikibase\Client\DataAccess\DataAccessSnakFormatterFactory + * + * @group Wikibase + * @group WikibaseClient + * @group WikibaseDataAccess + * + * @license GPL-2.0+ + * @author Marius Hoch + */ +class DataAccessSnakFormatterFactoryTest extends PHPUnit_Framework_TestCase { + + private function getDataAccessSnakFormatterFactory() { + return new DataAccessSnakFormatterFactory( + $this->getLanguageFallbackChainFactory(), + $this->getOutputFormatSnakFormatterFactory() + ); + } + + private function getLanguageFallbackChainFactory() { + $realFactory = new LanguageFallbackChainFactory(); + + $factory = $this->getMock( LanguageFallbackChainFactory::class ); + $factory->expects( $this->once() ) + ->method( 'newFromLanguage' ) + ->with( $this->isInstanceOf( Language::class ), LanguageFallbackChainFactory::FALLBACK_ALL ) + ->will( $this->returnCallback( [ $realFactory, 'newFromLanguage' ] ) ); + + return $factory; + } + + private function getOutputFormatSnakFormatterFactory() { + $factory = $this->getMockBuilder( OutputFormatSnakFormatterFactory::class ) + ->disableOriginalConstructor() + ->getMock(); + + $factory->expects( $this->once() ) + ->method( 'getSnakFormatter' ) + ->with( SnakFormatter::FORMAT_WIKI, $this->isInstanceOf( FormatterOptions::class ) ) + ->will( $this->returnValue( $this->getMock( SnakFormatter::class ) ) ); + + return $factory; + } + + public function testNewSnakFormatterForLanguage() { + $factory = $this->getDataAccessSnakFormatterFactory(); + $snakFormatter = $factory->newSnakFormatterForLanguage( + Language::factory( 'fr' ), + $this->getMock( UsageAccumulator::class ) + ); + + $this->assertInstanceOf( SnakFormatter::class, $snakFormatter ); + } + +} diff --git a/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/StatementGroupRendererFactoryTest.php b/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/StatementGroupRendererFactoryTest.php index 789cfdc..49789c6 100644 --- a/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/StatementGroupRendererFactoryTest.php +++ b/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/StatementGroupRendererFactoryTest.php @@ -9,6 +9,7 @@ use User; use ValueFormatters\FormatterOptions; use ValueFormatters\ValueFormatter; +use Wikibase\Client\DataAccess\DataAccessSnakFormatterFactory; use Wikibase\Client\DataAccess\PropertyIdResolver; use Wikibase\Client\DataAccess\PropertyParserFunction\LanguageAwareRenderer; use Wikibase\Client\DataAccess\PropertyParserFunction\StatementGroupRendererFactory; @@ -151,9 +152,11 @@ $factory = new StatementGroupRendererFactory( $idResolver, new SnaksFinder(), - new LanguageFallbackChainFactory(), - $formatterFactory, $this->getMock( EntityLookup::class ), + new DataAccessSnakFormatterFactory( + $this->getLanguageFallbackChainFactory(), + $formatterFactory + ), $allowDataAccessInUserLanguage ); $factory->newRendererFromParser( $this->getParser( 'de', 'es' ) ); @@ -170,9 +173,11 @@ return new StatementGroupRendererFactory( $this->getPropertyIdResolver(), $this->getSnaksFinder(), - $this->getLanguageFallbackChainFactory(), - $this->getSnakFormatterFactory(), $this->getEntityLookup(), + new DataAccessSnakFormatterFactory( + $this->getLanguageFallbackChainFactory(), + $this->getSnakFormatterFactory() + ), $allowDataAccessInUserLanguage ); } diff --git a/client/tests/phpunit/includes/WikibaseClientTest.php b/client/tests/phpunit/includes/WikibaseClientTest.php index af9a132..75068a9 100644 --- a/client/tests/phpunit/includes/WikibaseClientTest.php +++ b/client/tests/phpunit/includes/WikibaseClientTest.php @@ -10,6 +10,7 @@ use Site; use SiteStore; use Wikibase\Client\Changes\ChangeHandler; +use Wikibase\Client\DataAccess\DataAccessSnakFormatterFactory; use Wikibase\Client\DataAccess\PropertyParserFunction\Runner; use Wikibase\Client\Hooks\LanguageLinkBadgeDisplay; use Wikibase\Client\Hooks\OtherProjectsSidebarGeneratorFactory; @@ -380,6 +381,11 @@ $this->assertCount( 2, $langCodes ); } + public function testGetDataAccessSnakFormatterFactory() { + $dataAccessSnakFormatterFactory = $this->getWikibaseClient()->getDataAccessSnakFormatterFactory(); + $this->assertInstanceOf( DataAccessSnakFormatterFactory::class, $dataAccessSnakFormatterFactory ); + } + /** * @return WikibaseClient */ -- To view, visit https://gerrit.wikimedia.org/r/313976 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ic94adc479a9d0b0d1bded1312e6e308e3fea103d Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Hoo man <h...@online.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits