Daniel Kinzler has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/83090


Change subject: (bug 53745) Remove old SnakFormatter and TypedValueFormatter.
......................................................................

(bug 53745) Remove old SnakFormatter and TypedValueFormatter.

This replaces the old "evil" TypedValueFormatter logic with the
new SnakFormatterFactory facility.

Change-Id: Ided9fe7f93f37c5faf4a5a9a742646b38fbca5b5
---
M client/includes/WikibaseClient.php
M client/includes/parserhooks/PropertyParserFunction.php
M client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php
M lib/includes/LanguageFallbackChain.php
D lib/includes/OldSnakFormatter.php
D lib/includes/TypedValueFormatter.php
M lib/includes/formatters/EntityIdLabelFormatter.php
M lib/includes/formatters/SnakFormatterFactory.php
M lib/includes/formatters/WikibaseSnakFormatterBuilders.php
D lib/tests/phpunit/OldSnakFormatterTest.php
D lib/tests/phpunit/TypedValueFormatterTest.php
M lib/tests/phpunit/formatters/SnakFormatterFactoryTest.php
M lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php
M repo/includes/WikibaseRepo.php
M repo/tests/phpunit/includes/store/sql/EntityPerPageBuilderTest.php
15 files changed, 190 insertions(+), 515 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/90/83090/1

diff --git a/client/includes/WikibaseClient.php 
b/client/includes/WikibaseClient.php
index ae820b8..3d8d565 100644
--- a/client/includes/WikibaseClient.php
+++ b/client/includes/WikibaseClient.php
@@ -17,9 +17,10 @@
 use Wikibase\Lib\EntityRetrievingDataTypeLookup;
 use Wikibase\Lib\PropertyDataTypeLookup;
 use Wikibase\Lib\PropertyInfoDataTypeLookup;
-use Wikibase\Lib\OldSnakFormatter;
-use Wikibase\Lib\TypedValueFormatter;
+use Wikibase\Lib\SnakFormatter;
+use Wikibase\Lib\SnakFormatterFactory;
 use Wikibase\Lib\WikibaseDataTypeBuilders;
+use Wikibase\Lib\WikibaseSnakFormatterBuilders;
 use Wikibase\RepoLinker;
 use Wikibase\Settings;
 use Wikibase\SettingsArray;
@@ -74,6 +75,11 @@
         * @var \Site
         */
        private $site = null;
+
+       /**
+        * @var SnakFormatterFactory
+        */
+       private $snakFormatterFactory;
 
        /**
         * @since 0.4
@@ -175,14 +181,13 @@
        /**
         * @since 0.4
         *
-        * @return OldSnakFormatter
+        * @param string $format The desired format, use 
SnakFormatterFactory::FORMAT_XXX
+        * @param FormatterOptions $options
+        *
+        * @return SnakFormatter
         */
-       public function newSnakFormatter() {
-               return new OldSnakFormatter(
-                       $this->getPropertyDataTypeLookup(),
-                       new TypedValueFormatter(),
-                       $this->getDataTypeFactory()
-               );
+       public function newSnakFormatter( $format = 
SnakFormatterFactory::FORMAT_PLAIN, FormatterOptions $options = null )  {
+               return $this->getSnakFormatterFactory()->getFormatter( $format, 
$options );
        }
 
        /**
@@ -386,4 +391,32 @@
 
                return $group;
        }
+
+       /**
+        * Returns a SnakFormatterFactory the provides SnakFormatters
+        * for different output formats.
+        *
+        * @return SnakFormatterFactory
+        */
+       public function getSnakFormatterFactory() {
+               if ( !$this->snakFormatterFactory ) {
+                       $this->snakFormatterFactory = 
$this->newSnakFormatterFactory();
+               }
+
+               return $this->snakFormatterFactory;
+       }
+
+       /**
+        * @return SnakFormatterFactory
+        */
+       protected function newSnakFormatterFactory() {
+               $builders = new WikibaseSnakFormatterBuilders(
+                       $this->getEntityLookup(),
+                       $this->getPropertyDataTypeLookup(),
+                       $this->contentLanguage
+               );
+
+               $factory = new SnakFormatterFactory( 
$builders->getSnakFormatterBuildersForFormats() );
+               return $factory;
+       }
 }
diff --git a/client/includes/parserhooks/PropertyParserFunction.php 
b/client/includes/parserhooks/PropertyParserFunction.php
index 71f71b6..55bc65f 100644
--- a/client/includes/parserhooks/PropertyParserFunction.php
+++ b/client/includes/parserhooks/PropertyParserFunction.php
@@ -2,9 +2,11 @@
 
 namespace Wikibase;
 
+use ValueFormatters\FormatterOptions;
 use Wikibase\Client\WikibaseClient;
 use Wikibase\DataModel\SimpleSiteLink;
-use Wikibase\Lib\OldSnakFormatter;
+use Wikibase\Lib\SnakFormatter;
+use Wikibase\Lib\SnakFormatterFactory;
 
 /**
  * Handler of the {{#property}} parser function.
@@ -34,6 +36,7 @@
  * @licence GNU GPL v2+
  * @author Katie Filbert < [email protected] >
  * @author Jeroen De Dauw < [email protected] >
+ * @author Daniel Kinzler
  */
 class PropertyParserFunction {
 
@@ -49,7 +52,7 @@
        /* @var ParserErrorMessageFormatter */
        protected $errorFormatter;
 
-       /* @var OldSnakFormatter */
+       /* @var SnakFormatter */
        protected $snaksFormatter;
 
        /**
@@ -59,11 +62,11 @@
         * @param EntityLookup                $entityLookup
         * @param PropertyLabelResolver       $propertyLabelResolver
         * @param ParserErrorMessageFormatter $errorFormatter
-        * @param Lib\OldSnakFormatter           $snaksFormatter
+        * @param SnakFormatter               $snaksFormatter
         */
        public function __construct( \Language $language,
                EntityLookup $entityLookup, PropertyLabelResolver 
$propertyLabelResolver,
-               ParserErrorMessageFormatter $errorFormatter, OldSnakFormatter 
$snaksFormatter ) {
+               ParserErrorMessageFormatter $errorFormatter, SnakFormatter 
$snaksFormatter ) {
                $this->language = $language;
                $this->entityLookup = $entityLookup;
                $this->propertyLabelResolver = $propertyLabelResolver;
@@ -114,12 +117,18 @@
         * @return string - wikitext format
         */
        private function formatSnakList( $snaks ) {
-               $languageFallbackChainFactory = 
WikibaseClient::getDefaultInstance()->getLanguageFallbackChainFactory();
-               $languageFallbackChain = 
$languageFallbackChainFactory->newFromLanguage( $this->language,
-                       LanguageFallbackChainFactory::FALLBACK_SELF | 
LanguageFallbackChainFactory::FALLBACK_VARIANTS
-               );
-               $formattedValues = $this->snaksFormatter->formatSnaks( $snaks, 
$languageFallbackChain );
+               $formattedValues = $this->formatSnaks( $snaks );
                return $this->language->commaList( $formattedValues );
+       }
+
+       private function formatSnaks( $snaks ) {
+               $strings = array();
+
+               foreach ( $snaks as $snak ) {
+                       $strings[] = $this->snaksFormatter->formatSnak( $snak );
+               }
+
+               return $strings;
        }
 
        /**
@@ -191,7 +200,18 @@
 
                $entityLookup = $wikibaseClient->getStore()->getEntityLookup();
                $propertyLabelResolver = 
$wikibaseClient->getStore()->getPropertyLabelResolver();
-               $formatter = $wikibaseClient->newSnakFormatter();
+
+               $languageFallbackChainFactory = 
WikibaseClient::getDefaultInstance()->getLanguageFallbackChainFactory();
+               $languageFallbackChain = 
$languageFallbackChainFactory->newFromLanguage( $targetLanguage,
+                       LanguageFallbackChainFactory::FALLBACK_SELF | 
LanguageFallbackChainFactory::FALLBACK_VARIANTS
+               );
+
+               $options = new FormatterOptions( array(
+                       'languages' => $languageFallbackChain,
+                       // ...more options...
+               ) );
+
+               $formatter = $wikibaseClient->newSnakFormatter( 
SnakFormatterFactory::FORMAT_WIKI, $options );
 
                $instance = new self( $targetLanguage,
                        $entityLookup, $propertyLabelResolver,
diff --git 
a/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php 
b/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php
index d6f2c4c..4b24dbe 100644
--- a/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php
+++ b/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php
@@ -8,9 +8,6 @@
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\Entity\PropertyId;
 use Wikibase\Item;
-use Wikibase\Lib\EntityRetrievingDataTypeLookup;
-use Wikibase\Lib\OldSnakFormatter;
-use Wikibase\Lib\TypedValueFormatter;
 use Wikibase\ParserErrorMessageFormatter;
 use Wikibase\Property;
 use Wikibase\PropertyParserFunction;
@@ -43,11 +40,10 @@
                $mockRepo = $this->newMockRepository();
                $mockResolver = new MockPropertyLabelResolver( 
$targetLanguage->getCode(), $mockRepo );
 
-               $formatter = new OldSnakFormatter(
-                       new EntityRetrievingDataTypeLookup( $mockRepo ),
-                       new TypedValueFormatter(),
-                       $dataTypeFactory
-               );
+               $formatter = $this->getMock( 'Wikibase\Lib\SnakFormatter' );
+               $formatter->expects( $this->any() )
+                       ->method( 'formatSnak' )
+                       ->will( $this->returnValue( '(a kitten)' ) );
 
                return new PropertyParserFunction(
                        $targetLanguage,
@@ -90,12 +86,12 @@
                return array(
                        array(
                                'p1337',
-                               'Please write tests before merging your code, 
or kittens will die',
+                               '(a kitten), (a kitten)',
                                'Congratulations, you just killed a kitten'
                        ),
                        array(
                                'kitten',
-                               'Please write tests before merging your code, 
or kittens will die',
+                               '(a kitten), (a kitten)',
                                'Congratulations, you just killed a kitten'
                        ),
                );
diff --git a/lib/includes/LanguageFallbackChain.php 
b/lib/includes/LanguageFallbackChain.php
index 3d64cfa..060abe4 100644
--- a/lib/includes/LanguageFallbackChain.php
+++ b/lib/includes/LanguageFallbackChain.php
@@ -25,7 +25,7 @@
        /**
         * Constructor
         *
-        * @param LanguageWithConversion[] $chain
+        * hm
         */
        public function __construct( array $chain ) {
                $this->chain = $chain;
diff --git a/lib/includes/OldSnakFormatter.php 
b/lib/includes/OldSnakFormatter.php
deleted file mode 100644
index f13ba7b..0000000
--- a/lib/includes/OldSnakFormatter.php
+++ /dev/null
@@ -1,98 +0,0 @@
-<?php
-
-namespace Wikibase\Lib;
-
-use DataTypes\DataType;
-use DataTypes\DataTypeFactory;
-use RuntimeException;
-use Wikibase\EntityId;
-use Wikibase\LanguageFallbackChain;
-use Wikibase\PropertyValueSnak;
-use Wikibase\Snak;
-
-/**
- * Turns a list of Snak objects into a list of corresponding string 
representations.
- *
- * @since 0.4
- *
- * @file
- * @ingroup WikibaseLib
- *
- * @deprecated use a SnakFormatter from a SnakFormatterFactory instead.
- *
- * @licence GNU GPL v2+
- * @author Jeroen De Dauw < [email protected] >
- */
-class OldSnakFormatter {
-
-       /**
-        * @var TypedValueFormatter
-        */
-       private $typedValueFormatter;
-
-       /**
-        * @var PropertyDataTypeLookup
-        */
-       private $dataTypeLookup;
-
-       /**
-        * @var DataTypeFactory
-        */
-       private $dataTypeFactory;
-
-       public function __construct( PropertyDataTypeLookup $dataTypeLookup,
-               TypedValueFormatter $formatter, DataTypeFactory 
$dataTypeFactory ) {
-
-               $this->dataTypeLookup = $dataTypeLookup;
-               $this->typedValueFormatter = $formatter;
-               $this->dataTypeFactory = $dataTypeFactory;
-       }
-
-       /**
-        * Turns an array of snaks into an array of strings.
-        *
-        * @since 0.4
-        *
-        * @param Snak[] $snaks
-        * @param LanguageFallbackChain|string $language language code string 
or LanguageFallbackChain object
-        *
-        * @return string[]
-        */
-       public function formatSnaks( array $snaks, $language ) {
-               $formattedValues = array();
-
-               foreach ( $snaks as $snak ) {
-                       $formattedValues[] = $this->formatSnak( $snak, 
$language );
-               }
-
-               return $formattedValues;
-       }
-
-       private function formatSnak( Snak $snak, $language ) {
-               if ( $snak instanceof PropertyValueSnak ) {
-                       return $this->formatPropertyValueSnak( $snak, $language 
);
-               }
-
-               // TODO: throw NotSupportedException
-               return '';
-       }
-
-       private function formatPropertyValueSnak( PropertyValueSnak $snak, 
$language ) {
-               $dataValue = $snak->getDataValue();
-               $dataTypeId = $this->getDataTypeForProperty( 
$snak->getPropertyId() );
-
-               return $this->typedValueFormatter->formatToString( $dataValue, 
$dataTypeId, $language );
-       }
-
-       private function getDataTypeForProperty( EntityId $propertyId ) {
-               $dataTypeId = $this->dataTypeLookup->getDataTypeIdForProperty( 
$propertyId );
-               $dataType = $this->dataTypeFactory->getType( $dataTypeId );
-
-               if ( $dataType === null ) {
-                       throw new RuntimeException( "Could not construct 
DataType with unknown id '$dataTypeId'" );
-               }
-
-               return $dataType;
-       }
-
-}
diff --git a/lib/includes/TypedValueFormatter.php 
b/lib/includes/TypedValueFormatter.php
deleted file mode 100644
index 26f9a51..0000000
--- a/lib/includes/TypedValueFormatter.php
+++ /dev/null
@@ -1,86 +0,0 @@
-<?php
-
-namespace Wikibase\Lib;
-
-use DataTypes\DataType;
-use DataValues\DataValue;
-use DataValues\IllegalValueException;
-use ValueFormatters\FormatterOptions;
-use ValueFormatters\ValueFormatter;
-use Wikibase\CachingEntityLoader;
-use Wikibase\LanguageFallbackChain;
-use Wikibase\Settings;
-use Wikibase\WikiPageEntityLookup;
-
-/**
- * Provides a string representation for a DataValue given its associated 
DataType.
- *
- * @since 0.4
- *
- * @file
- * @ingroup WikibaseLib
- *
- * @deprecated use PropertyValueSnakFormatter instead.
- *
- * @licence GNU GPL v2+
- * @author Jeroen De Dauw < [email protected] >
- */
-class TypedValueFormatter {
-
-       /**
-        * @param DataValue $dataValue
-        * @param DataType $dataType
-        * @param LanguageFallbackChain|string $language language code string 
or LanguageFallbackChain object
-        *
-        * @return string
-        */
-       public function formatToString( DataValue $dataValue, DataType 
$dataType, $language ) {
-               // TODO: update this code to obtain the string formatter as 
soon as corresponding changes
-               // in the DataTypes library have been made.
-
-               if ( $dataValue->getType() === 'bad' ) {
-                       throw new IllegalValueException( 
$dataValue->getReason() );
-               }
-
-               $valueFormatters = $dataType->getFormatters();
-               $valueFormatter = reset( $valueFormatters );
-
-               // FIXME: before we can properly use the DataType system some 
issues to its implementation need
-               // to be solved. Once this is done, this evil if block and 
function it calls should go.
-               if ( $valueFormatter === false && $dataType->getId() === 
'wikibase-item' ) {
-                       $valueFormatter = $this->evilGetEntityIdFormatter( 
$language );
-               }
-
-               if ( $valueFormatter === false ) {
-                       $value = $dataValue->getValue();
-
-                       if ( is_string( $value ) ) {
-                               return $value;
-                       }
-
-                       // @todo: implement: error message or other error 
handling
-                       // @todo: implement value formatter for time!
-                       return '';
-               }
-
-               /**
-                * @var ValueFormatter $valueFormatter
-                */
-               return $valueFormatter->format( $dataValue );
-       }
-
-       private function evilGetEntityIdFormatter( $language ) {
-               $entityLookup = new CachingEntityLoader( new 
WikiPageEntityLookup( Settings::get( 'repoDatabase' ) ) );
-
-               $idFormatter = new EntityIdFormatter( new FormatterOptions() );
-
-               $options = new FormatterOptions();
-               $options->setOption( EntityIdLabelFormatter::OPT_LANG, 
$language );
-
-               $labelFormatter = new EntityIdLabelFormatter( $options, 
$entityLookup );
-               $labelFormatter->setIdFormatter( $idFormatter );
-
-               return $labelFormatter;
-       }
-
-}
diff --git a/lib/includes/formatters/EntityIdLabelFormatter.php 
b/lib/includes/formatters/EntityIdLabelFormatter.php
index b80f484..56960cf 100644
--- a/lib/includes/formatters/EntityIdLabelFormatter.php
+++ b/lib/includes/formatters/EntityIdLabelFormatter.php
@@ -153,21 +153,17 @@
                /* @var LanguageFallbackChain $languageFallbackChain */
                if ( $this->options->hasOption( 'languages' ) ) {
                        $languageFallbackChain = $this->getOption( 'languages' 
);
+
+                       $extractedData = 
$languageFallbackChain->extractPreferredValue( $entity->getLabels() );
+
+                       if ( $extractedData === null ) {
+                               return false;
+                       } else {
+                               return $extractedData['value'];
+                       }
                } else {
-                       $languageFallbackChain = $this->getOption( 
self::OPT_LANG );
-               }
-
-               // back-compat for usages where self::OPT_LANG is a string as a 
language code
-               if ( is_string( $languageFallbackChain ) ) {
-                       return $entity->getLabel( $languageFallbackChain );
-               }
-
-               $extractedData = $languageFallbackChain->extractPreferredValue( 
$entity->getLabels() );
-
-               if ( $extractedData === null ) {
-                       return false;
-               } else {
-                       return $extractedData['value'];
+                       $lang = $this->getOption( self::OPT_LANG );
+                       return $entity->getLabel( $lang );
                }
        }
 
diff --git a/lib/includes/formatters/SnakFormatterFactory.php 
b/lib/includes/formatters/SnakFormatterFactory.php
index 022083e..e7f2602 100644
--- a/lib/includes/formatters/SnakFormatterFactory.php
+++ b/lib/includes/formatters/SnakFormatterFactory.php
@@ -57,7 +57,7 @@
         * @throws \InvalidArgumentException
         * @return SnakFormatter
         */
-       public function getFormatter( $format, FormatterOptions $options = null 
) {
+       public function getFormatter( $format, FormatterOptions $options ) {
                if ( !array_key_exists( $format, $this->builders ) ) {
                        throw new \InvalidArgumentException( "Unsupported 
format: $format" );
                }
diff --git a/lib/includes/formatters/WikibaseSnakFormatterBuilders.php 
b/lib/includes/formatters/WikibaseSnakFormatterBuilders.php
index 3a93802..5ebe3d7 100644
--- a/lib/includes/formatters/WikibaseSnakFormatterBuilders.php
+++ b/lib/includes/formatters/WikibaseSnakFormatterBuilders.php
@@ -9,6 +9,9 @@
 use Wikibase\Client\WikibaseClient;
 use Wikibase\EntityLookup;
 use Wikibase\Item;
+use Wikibase\LanguageFallbackChain;
+use Wikibase\LanguageFallbackChainFactory;
+use Wikibase\LanguageWithConversion;
 use Wikibase\Repo\WikibaseRepo;
 
 /**
@@ -123,17 +126,6 @@
                return $types;
        }
 
-       private function getMessage( $key, $languages ) {
-               $msg = wfMessage( $key );
-
-               if ( !empty( $languages ) ) {
-                       $lang = reset( $languages );
-                       $msg = $msg->inLanguage( $lang );
-               }
-
-               return $msg;
-       }
-
        /**
         * Returns a DispatchingSnakFormatter for the given format, that will 
dispatch based on
         * the snak type. The instance returned by this method will cover all 
standard snak types.
@@ -145,20 +137,11 @@
         * @return DispatchingSnakFormatter
         */
        public function buildDispatchingSnakFormatter( SnakFormatterFactory 
$factory, $format, FormatterOptions $options ) {
-               //TODO: Sort out how the desired language is specified. We have 
two language options,
-               //      each accepting different ways of specifying the 
language. That's horrible.
-               if ( !$options->hasOption( 'languages' ) ) {
-                       $options->setOption( 'languages', array( 
$this->defaultLanguage ) );
-               }
+               $this->initLanguageDefaults( $options );
+               $lang = $options->getOption( ValueFormatter::OPT_LANG );
 
-               if ( !$options->hasOption( ValueFormatter::OPT_LANG ) ) {
-                       $options->setOption( ValueFormatter::OPT_LANG, 
$this->defaultLanguage->getCode() );
-               }
-
-               $languages = $options->getOption( 'languages' );
-
-               $noValueSnakFormatter = new MessageSnakFormatter( 
$this->getMessage( 'wikibase-snakview-snaktypeselector-novalue', $languages ), 
$format );
-               $someValueSnakFormatter = new MessageSnakFormatter( 
$this->getMessage( 'wikibase-snakview-snaktypeselector-somevalue', $languages 
), $format );
+               $noValueSnakFormatter = new MessageSnakFormatter( wfMessage( 
'wikibase-snakview-snaktypeselector-novalue')->inLanguage( $lang ), $format );
+               $someValueSnakFormatter = new MessageSnakFormatter( wfMessage( 
'wikibase-snakview-snaktypeselector-somevalue' )->inLanguage( $lang ), $format 
);
                $valueSnakFormatter = $this->buildValueSnakFormatter( $factory, 
$format, $options );
 
                $formatters = array(
@@ -170,6 +153,42 @@
                return new DispatchingSnakFormatter( $format, $formatters );
        }
 
+       /**
+        * Initializes the options keys ValueFormatter::OPT_LANG and 
'languages' if
+        * they are not yet set.
+        *
+        * @param FormatterOptions $options
+        *
+        * @throws \InvalidArgumentException
+        * @todo  : Sort out how the desired language is specified. We have two 
language options,
+        *        each accepting different ways of specifying the language. 
That's horrible.
+        */
+       private function initLanguageDefaults( $options ) {
+               $languageFallbackChainFactory = new 
LanguageFallbackChainFactory();
+
+               if ( !$options->hasOption( ValueFormatter::OPT_LANG ) ) {
+                       $options->setOption( ValueFormatter::OPT_LANG, 
$this->defaultLanguage->getCode() );
+               }
+
+               $lang = $options->getOption( ValueFormatter::OPT_LANG );
+               if ( !is_string( $lang ) ) {
+                       throw new \InvalidArgumentException( 'The value of 
OPT_LANG must be a language code. For a fallback chain, use the `languages` 
option.' );
+               }
+
+               if ( !$options->hasOption( 'languages' ) ) {
+                       $fallbackMode = (
+                               LanguageFallbackChainFactory::FALLBACK_VARIANTS
+                               | LanguageFallbackChainFactory::FALLBACK_OTHERS
+                               | LanguageFallbackChainFactory::FALLBACK_SELF );
+
+                       $options->setOption( 'languages', 
$languageFallbackChainFactory->newFromLanguageCode( $lang, $fallbackMode ) );
+               }
+
+               if ( !( $options->getOption( 'languages' ) instanceof 
LanguageFallbackChain ) ) {
+                       throw new \InvalidArgumentException( 'The value of the 
`languages` option must be an instance of LanguageFallbackChain.' );
+               }
+       }
+
        public function buildValueSnakFormatter( SnakFormatterFactory $factory, 
$format, FormatterOptions $options ) {
                switch ( $format ) {
                        case SnakFormatterFactory::FORMAT_PLAIN:
diff --git a/lib/tests/phpunit/OldSnakFormatterTest.php 
b/lib/tests/phpunit/OldSnakFormatterTest.php
deleted file mode 100644
index 6b73e94..0000000
--- a/lib/tests/phpunit/OldSnakFormatterTest.php
+++ /dev/null
@@ -1,167 +0,0 @@
-<?php
-
-namespace Wikibase\Lib\Test;
-
-use DataTypes\DataType;
-use DataTypes\DataTypeFactory;
-use DataValues\StringValue;
-use ValueFormatters\FormatterOptions;
-use ValueParsers\ParserOptions;
-use Wikibase\DataModel\Entity\EntityIdValue;
-use Wikibase\DataModel\Entity\ItemId;
-use Wikibase\DataModel\Entity\PropertyId;
-use Wikibase\Item;
-use Wikibase\Lib\EntityIdFormatter;
-use Wikibase\Lib\EntityIdParser;
-use Wikibase\Lib\InMemoryDataTypeLookup;
-use Wikibase\Lib\OldSnakFormatter;
-use Wikibase\Lib\TypedValueFormatter;
-use Wikibase\Property;
-use Wikibase\PropertyValueSnak;
-
-/**
- * @covers Wikibase\Lib\SnakFormatter
- *
- * @since 0.4
- *
- * @file
- * @ingroup WikibaseLib
- * @ingroup Test
- *
- * @group WikibaseLib
- * @group OldSnakFormatterTest
- *
- * @licence GNU GPL v2+
- * @author Jeroen De Dauw < [email protected] >
- */
-class OldSnakFormatterTest extends \PHPUnit_Framework_TestCase {
-
-       private $stringPropertyId = 'P572106';
-
-       private $itemPropertyId = 'P1730';
-
-       private function newPropertyDataTypeLookup() {
-               $lookup = new InMemoryDataTypeLookup();
-
-               $lookup->setDataTypeForProperty(
-                       new PropertyId( $this->stringPropertyId ),
-                       'string-datatype'
-               );
-
-               $lookup->setDataTypeForProperty(
-                       new PropertyId( $this->itemPropertyId ),
-                       'wikibase-item-datatype'
-               );
-
-               return $lookup;
-       }
-
-       private function newFormatter() {
-               return new OldSnakFormatter(
-                       $this->newPropertyDataTypeLookup(),
-                       new TypedValueFormatter(),
-                       DataTypeFactory::newFromTypes( array(
-                               $this->newStringDataType(),
-                               $this->newItemDataType()
-                       ) )
-               );
-       }
-
-       private function newStringDataType() {
-               return new DataType(
-                       'string-datatype',
-                       'string',
-                       array(),
-                       array(),
-                       array()
-               );
-       }
-
-       private function newItemDataType() {
-               $formatterOptions = new FormatterOptions();
-               $parserOptions = new ParserOptions();
-
-               return new DataType(
-                       'wikibase-item-datatype',
-                       'wikibase-entityid',
-                       array( new EntityIdParser( $parserOptions ) ),
-                       array( new EntityIdFormatter( $formatterOptions ) ),
-                       array()
-               );
-       }
-
-       public function testFormatNoSnaks() {
-               $formatted = $this->newFormatter()->formatSnaks( array(), 'en' 
);
-
-               $this->assertInternalType( 'array', $formatted );
-               $this->assertEmpty( $formatted );
-       }
-
-       private function getStrings() {
-               return array(
-                       '',
-                       'foo BAR baz!  ',
-                       '~=[,,_,,]:3'
-               );
-       }
-
-       public function stringProvider() {
-               $argLists = array();
-
-               foreach ( $this->getStrings() as $string ) {
-                       $argLists[] = array( $string );
-               }
-
-               return $argLists;
-       }
-
-       /**
-        * @dataProvider stringProvider
-        */
-       public function testFormatOnePropertyValueSnakWithString( $expected ) {
-               $propertyValueSnak = new PropertyValueSnak(
-                       new PropertyId( $this->stringPropertyId ),
-                       new StringValue( $expected )
-               );
-
-               $formatted = $this->newFormatter()->formatSnaks( array( 
$propertyValueSnak ), 'en' );
-               $this->assertFormatSnaksReturnType( $formatted );
-               $this->assertCount( 1, $formatted );
-
-               $actual = $formatted[0];
-               $this->assertEquals( $expected, $actual );
-       }
-
-       private function assertFormatSnaksReturnType( $returnValue ) {
-               $this->assertInternalType( 'array', $returnValue );
-               $this->assertContainsOnly( 'string', $returnValue );
-       }
-
-       public function testFormatMultipleValues() {
-               $propertyValueSnaks = array();
-
-               foreach ( $this->getStrings() as $string ) {
-                       $propertyValueSnaks[] = new PropertyValueSnak(
-                               new PropertyId( $this->stringPropertyId ),
-                               new StringValue( $string )
-                       );
-               }
-
-               $propertyValueSnaks[] = new PropertyValueSnak(
-                       new PropertyId( $this->itemPropertyId ),
-                       new EntityIdValue( new ItemId( 'Q1337' ) )
-               );
-
-               $formatted = $this->newFormatter()->formatSnaks( 
$propertyValueSnaks, 'en' );
-               $this->assertFormatSnaksReturnType( $formatted );
-
-               $expected = array_merge(
-                       $this->getStrings(),
-                       array( 'Q1337' )
-               );
-
-               $this->assertSameSize( $expected, $formatted );
-               $this->assertEquals( $expected, $formatted );
-       }
-
-}
diff --git a/lib/tests/phpunit/TypedValueFormatterTest.php 
b/lib/tests/phpunit/TypedValueFormatterTest.php
deleted file mode 100644
index 82b8b11..0000000
--- a/lib/tests/phpunit/TypedValueFormatterTest.php
+++ /dev/null
@@ -1,88 +0,0 @@
-<?php
-
-namespace Wikibase\Lib\Test;
-
-use DataTypes\DataType;
-use DataValues\DataValue;
-use DataValues\StringValue;
-use ValueFormatters\ValueFormatterBase;
-use Wikibase\Lib\TypedValueFormatter;
-
-/**
- * Tests for the Wikibase\Lib\TypedValueFormatter class.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @since 0.4
- *
- * @file
- * @ingroup WikibaseLib
- * @ingroup Test
- *
- * @group WikibaseLib
- * @group TypedValueFormatterTest
- *
- * @licence GNU GPL v2+
- * @author Jeroen De Dauw < [email protected] >
- */
-class TypedValueFormatterTest extends \PHPUnit_Framework_TestCase {
-
-       public function formatToStringProvider() {
-               $stringType = new DataType(
-                       'string-of-doom',
-                       'string',
-                       array(),
-                       array(),
-                       array()
-               );
-
-               $stringValues = array(
-                       '',
-                       'foo',
-                       ' foo ',
-                       'FoO',
-                       'foo bar baz!',
-                       '~=[,,_,,]:3 NyanData all the way across the sky! 
~=[,,_,,]:3'
-               );
-
-               $argLists = array();
-
-               foreach ( $stringValues as $stringValue ) {
-                       $argLists[] = array(
-                               new StringValue( $stringValue ),
-                               $stringType,
-                               $stringValue
-                       );
-               }
-
-               // TODO: test other types, esp ones with a formatter
-
-               return $argLists;
-       }
-
-       /**
-        * @dataProvider formatToStringProvider
-        */
-       public function testFormatToString( DataValue $input, DataType $type, 
$expected ) {
-               $formatter = new TypedValueFormatter();
-
-               $actual = $formatter->formatToString( $input, $type, 'en' );
-
-               $this->assertInternalType( 'string', $actual );
-               $this->assertEquals( $expected, $actual );
-       }
-
-}
diff --git a/lib/tests/phpunit/formatters/SnakFormatterFactoryTest.php 
b/lib/tests/phpunit/formatters/SnakFormatterFactoryTest.php
index eaa249b..d1351e7 100644
--- a/lib/tests/phpunit/formatters/SnakFormatterFactoryTest.php
+++ b/lib/tests/phpunit/formatters/SnakFormatterFactoryTest.php
@@ -73,7 +73,7 @@
         */
        public function testGetFormatter( $builders, $format ) {
                $factory = new SnakFormatterFactory( $builders );
-               $formatter = $factory->getFormatter( $format );
+               $formatter = $factory->getFormatter( $format, new 
FormatterOptions() );
 
                $this->assertInstanceOf( 'Wikibase\Lib\SnakFormatter', 
$formatter );
                $this->assertEquals( $format, $formatter->getFormat() );
diff --git a/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php 
b/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php
index a2df625..f3243d2 100644
--- a/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php
+++ b/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php
@@ -2,6 +2,7 @@
 namespace Wikibase\Lib\Test;
 
 use DataValues\StringValue;
+use Language;
 use ValueFormatters\FormatterOptions;
 use ValueFormatters\StringFormatter;
 use Wikibase\DataModel\Entity\EntityId;
@@ -46,7 +47,9 @@
                        ->method( 'getEntity' )
                        ->will( $this->returnValue( $entity ) );
 
-               return new WikibaseSnakFormatterBuilders( $entityLookup, 
$typeLookup );
+               $lang = Language::factory( 'en' );
+
+               return new WikibaseSnakFormatterBuilders( $entityLookup, 
$typeLookup, $lang );
        }
 
        /**
diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php
index a7a4ef6..fba0813 100644
--- a/repo/includes/WikibaseRepo.php
+++ b/repo/includes/WikibaseRepo.php
@@ -4,6 +4,7 @@
 
 use DataTypes\DataTypeFactory;
 use DataValues\DataValueFactory;
+use Language;
 use ValueFormatters\FormatterOptions;
 use ValueParsers\ParserOptions;
 use Wikibase\EntityContentFactory;
@@ -16,8 +17,10 @@
 use Wikibase\Lib\PropertyDataTypeLookup;
 use Wikibase\Lib\PropertyInfoDataTypeLookup;
 use Wikibase\Lib\SnakConstructionService;
+use Wikibase\Lib\SnakFormatterFactory;
 use Wikibase\Lib\WikibaseDataTypeBuilders;
 use Wikibase\Lib\ClaimGuidValidator;
+use Wikibase\Lib\WikibaseSnakFormatterBuilders;
 use Wikibase\Settings;
 use Wikibase\SettingsArray;
 use Wikibase\Store;
@@ -98,16 +101,27 @@
         */
        private $stringNormalizer;
 
+       /**
+        * @var Language
+        */
+       private $contentLanguage;
+
+       /**
+        * @var SnakFormatterFactory
+        */
+       private $snakFormatterFactory;
 
        /**
         * @since 0.4
         *
-        * @param SettingsArray   $settings
-        * @param Store           $store
+        * @param SettingsArray $settings
+        * @param Store         $store
+        * @param Language      $contentLanguage
         */
-       public function __construct( SettingsArray $settings, Store $store ) {
+       public function __construct( SettingsArray $settings, Store $store, 
Language $contentLanguage ) {
                $this->settings = $settings;
                $this->store = $store;
+               $this->contentLanguage = $contentLanguage;
        }
 
        /**
@@ -310,9 +324,11 @@
         * @return WikibaseRepo
         */
        protected static function newInstance() {
+               global $wgContLang;
                return new self(
                        Settings::singleton(),
-                       StoreFactory::getStore()
+                       StoreFactory::getStore(),
+                       $wgContLang
                );
        }
 
@@ -344,4 +360,32 @@
                return StoreFactory::getStore();
        }
 
+
+       /**
+        * Returns a SnakFormatterFactory the provides SnakFormatters
+        * for different output formats.
+        *
+        * @return SnakFormatterFactory
+        */
+       public function getSnakFormatterFactory() {
+               if ( !$this->snakFormatterFactory ) {
+                       $this->snakFormatterFactory = 
$this->newSnakFormatterFactory();
+               }
+
+               return $this->snakFormatterFactory;
+       }
+
+       /**
+        * @return SnakFormatterFactory
+        */
+       protected function newSnakFormatterFactory() {
+               $builders = new WikibaseSnakFormatterBuilders(
+                       $this->getEntityLookup(),
+                       $this->getPropertyDataTypeLookup(),
+                       $this->contentLanguage
+               );
+
+               $factory = new SnakFormatterFactory( 
$builders->getSnakFormatterBuildersForFormats() );
+               return $factory;
+       }
 }
diff --git a/repo/tests/phpunit/includes/store/sql/EntityPerPageBuilderTest.php 
b/repo/tests/phpunit/includes/store/sql/EntityPerPageBuilderTest.php
index 3f911af..ca73640 100644
--- a/repo/tests/phpunit/includes/store/sql/EntityPerPageBuilderTest.php
+++ b/repo/tests/phpunit/includes/store/sql/EntityPerPageBuilderTest.php
@@ -2,6 +2,7 @@
 
 namespace Wikibase\Test;
 
+use Language;
 use Wikibase\SettingsArray;
 use Wikibase\StoreFactory;
 use Wikibase\EntityPerPageBuilder;
@@ -42,10 +43,12 @@
 
                $settings = $this->getTestSettings();
                $store = StoreFactory::getStore( 'sqlstore' );
+               $lang = Language::factory( 'en' );
 
                $this->wikibaseRepo = new WikibaseRepo(
                        $settings,
-                       $store
+                       $store,
+                       $lang
                );
 
                $this->entityPerPageTable = $store->newEntityPerPage();

-- 
To view, visit https://gerrit.wikimedia.org/r/83090
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ided9fe7f93f37c5faf4a5a9a742646b38fbca5b5
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to