Tobias Gritschacher has uploaded a new change for review. https://gerrit.wikimedia.org/r/60805
Change subject: Pass language from property parser function to entity id label formatter ...................................................................... Pass language from property parser function to entity id label formatter This is a temporary solution that should be revised when we figure out how to set options for data type dependent objects in a generic but sane way Change-Id: I15582e1baad50901482805d328a800cd71bbe566 --- M client/includes/parserhooks/PropertyParserFunction.php M lib/includes/SnakFormatter.php M lib/includes/TypedValueFormatter.php M lib/tests/phpunit/SnakFormatterTest.php M lib/tests/phpunit/TypedValueFormatterTest.php 5 files changed, 27 insertions(+), 21 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/05/60805/1 diff --git a/client/includes/parserhooks/PropertyParserFunction.php b/client/includes/parserhooks/PropertyParserFunction.php index 0e09de6..6cf6eae 100644 --- a/client/includes/parserhooks/PropertyParserFunction.php +++ b/client/includes/parserhooks/PropertyParserFunction.php @@ -101,7 +101,7 @@ * @return string - wikitext format */ private function formatSnakList( $snaks ) { - $formattedValues = $this->snaksFormatter->formatSnaks( $snaks ); + $formattedValues = $this->snaksFormatter->formatSnaks( $snaks, $this->language->getCode() ); return $this->language->commaList( $formattedValues ); } diff --git a/lib/includes/SnakFormatter.php b/lib/includes/SnakFormatter.php index 4345f2f..ba47249 100644 --- a/lib/includes/SnakFormatter.php +++ b/lib/includes/SnakFormatter.php @@ -66,33 +66,34 @@ * @since 0.4 * * @param Snak[] $snaks + * @param string $languageCode * * @return string[] */ - public function formatSnaks( array $snaks ) { + public function formatSnaks( array $snaks, $languageCode ) { $formattedValues = array(); foreach ( $snaks as $snak ) { - $formattedValues[] = $this->formatSnak( $snak ); + $formattedValues[] = $this->formatSnak( $snak, $languageCode ); } return $formattedValues; } - private function formatSnak( Snak $snak ) { + private function formatSnak( Snak $snak, $languageCode ) { if ( $snak instanceof PropertyValueSnak ) { - return $this->formatPropertyValueSnak( $snak ); + return $this->formatPropertyValueSnak( $snak, $languageCode ); } // TODO: we might want to allow customization here (this happens for NoValue and SomeValue snaks) return ''; } - private function formatPropertyValueSnak( PropertyValueSnak $snak ) { + private function formatPropertyValueSnak( PropertyValueSnak $snak, $languageCode ) { $dataValue = $snak->getDataValue(); $dataTypeId = $this->getDataTypeForProperty( $snak->getPropertyId() ); - return $this->typedValueFormatter->formatToString( $dataValue, $dataTypeId ); + return $this->typedValueFormatter->formatToString( $dataValue, $dataTypeId, $languageCode ); } private function getDataTypeForProperty( EntityId $propertyId ) { diff --git a/lib/includes/TypedValueFormatter.php b/lib/includes/TypedValueFormatter.php index 831af3b..4cedb67 100644 --- a/lib/includes/TypedValueFormatter.php +++ b/lib/includes/TypedValueFormatter.php @@ -4,7 +4,11 @@ use DataTypes\DataType; use DataValues\DataValue; +use ValueFormatters\FormatterOptions; use ValueFormatters\ValueFormatter; +use Wikibase\CachingEntityLoader; +use Wikibase\Settings; +use Wikibase\WikiPageEntityLookup; /** * Provides a string representation for a DataValue given its associated DataType. @@ -34,7 +38,7 @@ */ class TypedValueFormatter { - public function formatToString( DataValue $dataValue, DataType $dataType ) { + public function formatToString( DataValue $dataValue, DataType $dataType, $languageCode ) { // TODO: update this code to obtain the string formatter as soon as corresponding changes // in the DataTypes library have been made. @@ -44,7 +48,7 @@ // 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(); + $valueFormatter = $this->evilGetEntityIdFormatter( $languageCode ); } if ( $valueFormatter === false ) { @@ -64,24 +68,25 @@ return $valueFormatter->format( $dataValue ); } - private function evilGetEntityIdFormatter() { - $entityLookup = new \Wikibase\CachingEntityLoader( new \Wikibase\WikiPageEntityLookup( \Wikibase\Settings::get( 'repoDatabase' ) ) ); + private function evilGetEntityIdFormatter( $languageCode ) { + $entityLookup = new CachingEntityLoader( new WikiPageEntityLookup( Settings::get( 'repoDatabase' ) ) ); $prefixMap = array(); - foreach ( \Wikibase\Settings::get( 'entityPrefixes' ) as $prefix => $entityType ) { + foreach ( Settings::get( 'entityPrefixes' ) as $prefix => $entityType ) { $prefixMap[$entityType] = $prefix; } - $options = new \ValueFormatters\FormatterOptions( array( - \Wikibase\Lib\EntityIdFormatter::OPT_PREFIX_MAP => $prefixMap + $options = new FormatterOptions( array( + EntityIdFormatter::OPT_PREFIX_MAP => $prefixMap ) ); - $idFormatter = new \Wikibase\Lib\EntityIdFormatter( $options ); + $idFormatter = new EntityIdFormatter( $options ); - $options = new \ValueFormatters\FormatterOptions(); + $options = new FormatterOptions(); + $options->setOption( EntityIdLabelFormatter::OPT_LANG, $languageCode ); - $labelFormatter = new \Wikibase\Lib\EntityIdLabelFormatter( $options, $entityLookup ); + $labelFormatter = new EntityIdLabelFormatter( $options, $entityLookup ); $labelFormatter->setIdFormatter( $idFormatter ); return $labelFormatter; diff --git a/lib/tests/phpunit/SnakFormatterTest.php b/lib/tests/phpunit/SnakFormatterTest.php index 49e451e..33183b4 100644 --- a/lib/tests/phpunit/SnakFormatterTest.php +++ b/lib/tests/phpunit/SnakFormatterTest.php @@ -113,7 +113,7 @@ } public function testFormatNoSnaks() { - $formatted = $this->newFormatter()->formatSnaks( array() ); + $formatted = $this->newFormatter()->formatSnaks( array(), 'en' ); $this->assertInternalType( 'array', $formatted ); $this->assertEmpty( $formatted ); @@ -146,7 +146,7 @@ new StringValue( $expected ) ); - $formatted = $this->newFormatter()->formatSnaks( array( $propertyValueSnak ) ); + $formatted = $this->newFormatter()->formatSnaks( array( $propertyValueSnak ), 'en' ); $this->assertFormatSnaksReturnType( $formatted ); $this->assertCount( 1, $formatted ); @@ -174,7 +174,7 @@ new EntityId( Item::ENTITY_TYPE, 1337 ) ); - $formatted = $this->newFormatter()->formatSnaks( $propertyValueSnaks ); + $formatted = $this->newFormatter()->formatSnaks( $propertyValueSnaks, 'en' ); $this->assertFormatSnaksReturnType( $formatted ); $expected = array_merge( diff --git a/lib/tests/phpunit/TypedValueFormatterTest.php b/lib/tests/phpunit/TypedValueFormatterTest.php index 69538fb..82b8b11 100644 --- a/lib/tests/phpunit/TypedValueFormatterTest.php +++ b/lib/tests/phpunit/TypedValueFormatterTest.php @@ -79,7 +79,7 @@ public function testFormatToString( DataValue $input, DataType $type, $expected ) { $formatter = new TypedValueFormatter(); - $actual = $formatter->formatToString( $input, $type ); + $actual = $formatter->formatToString( $input, $type, 'en' ); $this->assertInternalType( 'string', $actual ); $this->assertEquals( $expected, $actual ); -- To view, visit https://gerrit.wikimedia.org/r/60805 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I15582e1baad50901482805d328a800cd71bbe566 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: mw1.22-wmf3 Gerrit-Owner: Tobias Gritschacher <tobias.gritschac...@wikimedia.de> Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits