Thiemo Mättig (WMDE) has uploaded a new change for review.
https://gerrit.wikimedia.org/r/235495
Change subject: Use QuantityHtmlFormatter
......................................................................
Use QuantityHtmlFormatter
This requires the new QuantityHtmlFormatter from DataValues Number to
be merged and released.
Change-Id: I5b8ba0fd8e24e99e7bbed82849101cbb383d7f29
Fixes: T110675
---
M lib/includes/formatters/EntityLabelUnitFormatter.php
M lib/includes/formatters/QuantityDetailsFormatter.php
A lib/includes/formatters/VocabularyUriFormatter.php
M lib/includes/formatters/WikibaseValueFormatterBuilders.php
M lib/tests/phpunit/formatters/EntityLabelUnitFormatterTest.php
M lib/tests/phpunit/formatters/QuantityDetailsFormatterTest.php
A lib/tests/phpunit/formatters/VocabularyUriFormatterTest.php
7 files changed, 216 insertions(+), 80 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/95/235495/1
diff --git a/lib/includes/formatters/EntityLabelUnitFormatter.php
b/lib/includes/formatters/EntityLabelUnitFormatter.php
index 11aa8d2..4cde813 100644
--- a/lib/includes/formatters/EntityLabelUnitFormatter.php
+++ b/lib/includes/formatters/EntityLabelUnitFormatter.php
@@ -2,11 +2,7 @@
namespace Wikibase\Lib;
-use OutOfBoundsException;
use ValueFormatters\QuantityUnitFormatter;
-use Wikibase\DataModel\Services\EntityId\EntityIdParser;
-use Wikibase\DataModel\Services\EntityId\EntityIdParsingException;
-use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
/**
* QuantityUnitFormatter for representing units by their respective entity
label.
@@ -19,76 +15,39 @@
class EntityLabelUnitFormatter implements QuantityUnitFormatter {
/**
- * @var EntityIdParser
+ * @var VocabularyUriFormatter
*/
- private $externalEntityIdParser;
+ private $vocabularyUriFormatter;
/**
- * @var LabelDescriptionLookup
+ * @param VocabularyUriFormatter $vocabularyUriFormatter
*/
- private $labelLookup;
-
- /**
- * @var array A set of unitless IDs, represented as string keys.
- */
- private $unitlessUnitIds;
-
- /**
- * @param EntityIdParser $externalEntityIdParser
- * @param LabelDescriptionLookup $labelLookup
- * @param string[] $unitlessUnitIds A list of IDs that represent the
"unitless" unit (one),
- * e.g. "http://www.wikidata.org/entity/Q199". The strings ""
and "1" are always
- * treated as "non-units".
- */
- public function __construct(
- EntityIdParser $externalEntityIdParser,
- LabelDescriptionLookup $labelLookup,
- array $unitlessUnitIds = array()
- ) {
- $this->unitlessUnitIds = array_flip( $unitlessUnitIds );
- $this->unitlessUnitIds[''] = true;
- $this->unitlessUnitIds['1'] = true;
-
- $this->externalEntityIdParser = $externalEntityIdParser;
- $this->labelLookup = $labelLookup;
+ public function __construct( VocabularyUriFormatter
$vocabularyUriFormatter ) {
+ $this->vocabularyUriFormatter = $vocabularyUriFormatter;
}
/**
- * @see QuantityUnitFormatter::applyUnit()
+ * @see QuantityUnitFormatter::applyUnit
*
* This implementation will interpret $unit as an external entity ID
(typically a URI), which
* can be parsed using the EntityIdParser supplied to the constructor.
If $unit is successfully
* parsed, the label of the entity is looked up, and appended to
$numberText with a single
* space as a separator.
*
- * @param string $unit
+ * @param string $conceptUri
* @param string $numberText
*
* @return string Text
*/
- public function applyUnit( $unit, $numberText ) {
- if ( array_key_exists( $unit, $this->unitlessUnitIds ) ) {
+ public function applyUnit( $conceptUri, $numberText ) {
+ $label = $this->vocabularyUriFormatter->format( $conceptUri );
+
+ if ( $label === null ) {
return $numberText;
}
- try {
- $entityId = $this->externalEntityIdParser->parse( $unit
);
-
- try {
- // TODO: Ideally we would show unit *symbols*,
taking from a config file,
- // a system message, or a statement on the
unit's item. Then the
- // name "EntityLabelUnitFormatter" doesn't
apply any more, though.
- $label = $this->labelLookup->getLabel(
$entityId )->getText();
- } catch ( OutOfBoundsException $ex ) {
- $label = $entityId->getSerialization();
- }
-
- // TODO: localizable pattern for placement
(before/after, separator)
- return "$numberText $label";
- } catch ( EntityIdParsingException $ex ) {
- // Use raw ID (URI)
- return "$numberText $unit";
- }
+ // TODO: localizable pattern for placement (before/after,
separator)
+ return $numberText . ' ' . $label;
}
}
diff --git a/lib/includes/formatters/QuantityDetailsFormatter.php
b/lib/includes/formatters/QuantityDetailsFormatter.php
index cf93bf1..088125e 100644
--- a/lib/includes/formatters/QuantityDetailsFormatter.php
+++ b/lib/includes/formatters/QuantityDetailsFormatter.php
@@ -11,7 +11,6 @@
use ValueFormatters\FormatterOptions;
use ValueFormatters\NumberLocalizer;
use ValueFormatters\QuantityFormatter;
-use ValueFormatters\QuantityUnitFormatter;
use ValueFormatters\ValueFormatter;
use ValueFormatters\ValueFormatterBase;
@@ -36,21 +35,25 @@
protected $quantityFormatter;
/**
- * @var QuantityUnitFormatter
+ * @var ValueFormatter|null
*/
- protected $unitFormatter;
+ protected $vocabularyUriFormatter;
/**
* @param NumberLocalizer $numberLocalizer
- * @param QuantityUnitFormatter $unitFormatter
+ * @param ValueFormatter|null $vocabularyUriFormatter
* @param FormatterOptions|null $options
*/
- public function __construct( NumberLocalizer $numberLocalizer,
QuantityUnitFormatter $unitFormatter, FormatterOptions $options = null ) {
+ public function __construct(
+ NumberLocalizer $numberLocalizer,
+ ValueFormatter $vocabularyUriFormatter = null,
+ FormatterOptions $options = null
+ ) {
parent::__construct( $options );
- $this->unitFormatter = $unitFormatter;
+ $this->vocabularyUriFormatter = $vocabularyUriFormatter;
$this->decimalFormatter = new DecimalFormatter( $this->options,
$numberLocalizer );
- $this->quantityFormatter = new QuantityFormatter(
$this->decimalFormatter, $unitFormatter, $this->options );
+ $this->quantityFormatter = new QuantityFormatter(
$this->decimalFormatter, $vocabularyUriFormatter, $this->options );
}
/**
@@ -92,9 +95,17 @@
}
private function formatNumber( DecimalValue $number, $unit ) {
- $text = $this->decimalFormatter->format( $number );
- $text = $this->unitFormatter->applyUnit( $unit, $text );
- return htmlspecialchars( $text );
+ $html = htmlspecialchars( $this->decimalFormatter->format(
$number ) );
+
+ if ( $this->vocabularyUriFormatter !== null ) {
+ $label = $this->vocabularyUriFormatter->format( $unit );
+
+ if ( $label !== null ) {
+ $html .= ' ' . htmlspecialchars( $label );
+ }
+ }
+
+ return $html;
}
/**
diff --git a/lib/includes/formatters/VocabularyUriFormatter.php
b/lib/includes/formatters/VocabularyUriFormatter.php
new file mode 100644
index 0000000..30a2eb7
--- /dev/null
+++ b/lib/includes/formatters/VocabularyUriFormatter.php
@@ -0,0 +1,90 @@
+<?php
+
+namespace Wikibase\Lib;
+
+use InvalidArgumentException;
+use OutOfBoundsException;
+use ValueFormatters\ValueFormatter;
+use Wikibase\DataModel\Services\EntityId\EntityIdParser;
+use Wikibase\DataModel\Services\EntityId\EntityIdParsingException;
+use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
+
+/**
+ * A dedicated formatter for concept URIs refering to entities on a vocabulary
repository.
+ *
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Thiemo Mättig
+ */
+class VocabularyUriFormatter implements ValueFormatter {
+
+ /**
+ * @var EntityIdParser
+ */
+ private $externalEntityIdParser;
+
+ /**
+ * @var LabelDescriptionLookup
+ */
+ private $labelLookup;
+
+ /**
+ * @var array A set of unitless IDs, represented as string keys.
+ */
+ private $unitlessUnitIds;
+
+ /**
+ * @param EntityIdParser $externalEntityIdParser
+ * @param LabelDescriptionLookup $labelLookup
+ * @param string[] $unitlessUnitIds A list of IDs that represent the
"unitless" unit (one),
+ * e.g. "http://www.wikidata.org/entity/Q199". The strings ""
and "1" are always
+ * treated as "non-units".
+ */
+ function __construct(
+ EntityIdParser $externalEntityIdParser,
+ LabelDescriptionLookup $labelLookup,
+ array $unitlessUnitIds = array()
+ ) {
+ $this->externalEntityIdParser = $externalEntityIdParser;
+ $this->labelLookup = $labelLookup;
+
+ $this->unitlessUnitIds = array_flip( $unitlessUnitIds );
+ $this->unitlessUnitIds[''] = true;
+ $this->unitlessUnitIds['1'] = true;
+ }
+
+ /**
+ * @param string $conceptUri
+ *
+ * @throws InvalidArgumentException
+ * @return string|null Null if the concept URI refers to a unitless
unit. Otherwise a label or
+ * an entity ID or the original concept URI.
+ */
+ public function format( $conceptUri ) {
+ if ( !is_string( $conceptUri ) ) {
+ throw new InvalidArgumentException( '$conceptUri must
be a string' );
+ }
+
+ if ( array_key_exists( $conceptUri, $this->unitlessUnitIds ) ) {
+ return null;
+ }
+
+ try {
+ $entityId = $this->externalEntityIdParser->parse(
$conceptUri );
+
+ try {
+ // TODO: Ideally we would show unit *symbols*,
taking from a config file,
+ // a system message, or a statement on the
unit's item.
+ return $this->labelLookup->getLabel( $entityId
)->getText();
+ } catch ( OutOfBoundsException $ex ) {
+ // Fall back to the entity ID.
+ return $entityId->getSerialization();
+ }
+ } catch ( EntityIdParsingException $ex ) {
+ // Fall back to the raw concept URI.
+ return $conceptUri;
+ }
+ }
+
+}
diff --git a/lib/includes/formatters/WikibaseValueFormatterBuilders.php
b/lib/includes/formatters/WikibaseValueFormatterBuilders.php
index 60c5f84..faed972 100644
--- a/lib/includes/formatters/WikibaseValueFormatterBuilders.php
+++ b/lib/includes/formatters/WikibaseValueFormatterBuilders.php
@@ -11,6 +11,7 @@
use ValueFormatters\DecimalFormatter;
use ValueFormatters\FormatterOptions;
use ValueFormatters\QuantityFormatter;
+use ValueFormatters\QuantityHtmlFormatter;
use ValueFormatters\ValueFormatter;
use Wikibase\DataModel\Services\EntityId\EntityIdLabelFormatter;
use Wikibase\DataModel\Services\EntityId\EntityIdParser;
@@ -94,7 +95,7 @@
SnakFormatter::FORMAT_PLAIN => array(
'VT:string' => 'ValueFormatters\StringFormatter',
'VT:globecoordinate' => array( 'this',
'newGlobeCoordinateFormatter' ),
- 'VT:quantity' => array( 'this', 'newQuantityFormatter'
),
+ 'VT:quantity' => array( 'this',
'newPlainQuantityFormatter' ),
'VT:time' => 'Wikibase\Lib\MwTimeIsoFormatter',
'VT:wikibase-entityid' => array( 'this',
'newEntityIdFormatter' ),
'VT:bad' =>
'Wikibase\Lib\UnDeserializableValueFormatter',
@@ -116,6 +117,7 @@
'PT:commonsMedia' =>
'Wikibase\Lib\CommonsLinkFormatter',
'PT:wikibase-item' => array( 'this',
'newEntityIdHtmlFormatter' ),
'PT:wikibase-property' => array( 'this',
'newEntityIdHtmlFormatter' ),
+ 'VT:quantity' => array( 'this',
'newHtmlQuantityFormatter' ),
'VT:time' => array( 'this', 'newHtmlTimeFormatter' ),
'VT:monolingualtext' => array( 'this',
'newMonolingualHtmlFormatter' ),
),
@@ -602,10 +604,14 @@
return new MediaWikiNumberLocalizer( $language );
}
- private function getQuantityUnitFormatter( FormatterOptions $options ) {
- $labelDescriptionLookup =
$this->labelDescriptionLookupFactory->getLabelDescriptionLookup( $options );
-
- return new EntityLabelUnitFormatter( $this->repoUriParser,
$labelDescriptionLookup, $this->unitOneUris );
+ /**
+ * @param FormatterOptions $options
+ *
+ * @return VocabularyUriFormatter
+ */
+ private function getVocabularyUriFormatter( FormatterOptions $options )
{
+ $labelLookup =
$this->labelDescriptionLookupFactory->getLabelDescriptionLookup( $options );
+ return new VocabularyUriFormatter( $this->repoUriParser,
$labelLookup, $this->unitOneUris );
}
/**
@@ -613,11 +619,21 @@
*
* @return QuantityFormatter
*/
- private function newQuantityFormatter( FormatterOptions $options ) {
+ private function newPlainQuantityFormatter( FormatterOptions $options )
{
$decimalFormatter = new DecimalFormatter( $options,
$this->getNumberLocalizer( $options ) );
- $labelDescriptionLookup =
$this->labelDescriptionLookupFactory->getLabelDescriptionLookup( $options );
- $unitFormatter = new EntityLabelUnitFormatter(
$this->repoUriParser, $labelDescriptionLookup );
- return new QuantityFormatter( $decimalFormatter,
$unitFormatter, $options );
+ $vocabularyUriFormatter = $this->getVocabularyUriFormatter(
$options );
+ return new QuantityFormatter( $decimalFormatter,
$vocabularyUriFormatter, $options );
+ }
+
+ /**
+ * @param FormatterOptions $options
+ *
+ * @return QuantityHtmlFormatter
+ */
+ private function newHtmlQuantityFormatter( FormatterOptions $options ) {
+ $decimalFormatter = new DecimalFormatter( $options,
$this->getNumberLocalizer( $options ) );
+ $vocabularyUriFormatter = $this->getVocabularyUriFormatter(
$options );
+ return new QuantityHtmlFormatter( $decimalFormatter,
$vocabularyUriFormatter, $options );
}
/**
@@ -630,8 +646,8 @@
*/
private function newQuantityDetailsFormatter( FormatterOptions $options
) {
$localizer = $this->getNumberLocalizer( $options );
- $unitFormatter = $this->getQuantityUnitFormatter( $options );
- return new QuantityDetailsFormatter( $localizer,
$unitFormatter, $options );
+ $vocabularyUriFormatter = $this->getVocabularyUriFormatter(
$options );
+ return new QuantityDetailsFormatter( $localizer,
$vocabularyUriFormatter, $options );
}
/**
diff --git a/lib/tests/phpunit/formatters/EntityLabelUnitFormatterTest.php
b/lib/tests/phpunit/formatters/EntityLabelUnitFormatterTest.php
index e62d71e..2722486 100644
--- a/lib/tests/phpunit/formatters/EntityLabelUnitFormatterTest.php
+++ b/lib/tests/phpunit/formatters/EntityLabelUnitFormatterTest.php
@@ -8,6 +8,7 @@
use Wikibase\DataModel\Services\EntityId\BasicEntityIdParser;
use Wikibase\DataModel\Term\Term;
use Wikibase\Lib\EntityLabelUnitFormatter;
+use Wikibase\Lib\VocabularyUriFormatter;
/**
* @covers Wikibase\Lib\EntityLabelUnitFormatter
@@ -52,7 +53,8 @@
$idParser = new BasicEntityIdParser();
- $formatter = new EntityLabelUnitFormatter( $idParser,
$labelLookup, array( 'NotAUnit' ) );
+ $vocabularyUriFormatter = new VocabularyUriFormatter(
$idParser, $labelLookup, array( 'NotAUnit' ) );
+ $formatter = new EntityLabelUnitFormatter(
$vocabularyUriFormatter );
$this->assertEquals( $expected, $formatter->applyUnit( $unit,
$number ) );
}
diff --git a/lib/tests/phpunit/formatters/QuantityDetailsFormatterTest.php
b/lib/tests/phpunit/formatters/QuantityDetailsFormatterTest.php
index 93b6050..5f3ac73 100644
--- a/lib/tests/phpunit/formatters/QuantityDetailsFormatterTest.php
+++ b/lib/tests/phpunit/formatters/QuantityDetailsFormatterTest.php
@@ -5,7 +5,6 @@
use DataValues\NumberValue;
use DataValues\QuantityValue;
use ValueFormatters\BasicNumberLocalizer;
-use ValueFormatters\BasicQuantityUnitFormatter;
use ValueFormatters\FormatterOptions;
use ValueFormatters\ValueFormatter;
use Wikibase\Lib\QuantityDetailsFormatter;
@@ -24,10 +23,13 @@
private function newFormatter( FormatterOptions $options = null ) {
$numberLocalizer = new BasicNumberLocalizer();
- $unitFormatter = new BasicQuantityUnitFormatter();
- $formatter = new QuantityDetailsFormatter( $numberLocalizer,
$unitFormatter, $options );
- return $formatter;
+ $vocabularyUriFormatter = $this->getMock(
'ValueFormatters\ValueFormatter' );
+ $vocabularyUriFormatter->expects( $this->any() )
+ ->method( 'format' )
+ ->will( $this->returnValue( null ) );
+
+ return new QuantityDetailsFormatter( $numberLocalizer,
$vocabularyUriFormatter, $options );
}
/**
diff --git a/lib/tests/phpunit/formatters/VocabularyUriFormatterTest.php
b/lib/tests/phpunit/formatters/VocabularyUriFormatterTest.php
new file mode 100644
index 0000000..2e0ed2c
--- /dev/null
+++ b/lib/tests/phpunit/formatters/VocabularyUriFormatterTest.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace Wikibase\Lib\Test;
+
+use OutOfBoundsException;
+use PHPUnit_Framework_TestCase;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Services\EntityId\BasicEntityIdParser;
+use Wikibase\DataModel\Term\Term;
+use Wikibase\Lib\VocabularyUriFormatter;
+
+/**
+ * @covers Wikibase\Lib\VocabularyUriFormatter
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ * @author Thiemo Mättig
+ */
+class VocabularyUriFormatterTest extends PHPUnit_Framework_TestCase {
+
+ public function unitProvider() {
+ return array(
+ 'empty unit' => array( '', null ),
+ 'unit is 1' => array( '1', null ),
+ 'unit is "NotAUnit"' => array( 'NotAUnit', null ),
+ 'unit is bad id' => array( 'kittens', 'kittens' ),
+ 'unit has label' => array( 'Q7', 'LABEL:Q7' ),
+ 'unit has no label' => array( 'Q112233', 'Q112233' ),
+ 'unknown int' => array( '2', '2' ),
+ 'unknown URI' => array(
'http://www.wikidata.org/entity/Q200', 'http://www.wikidata.org/entity/Q200' ),
+ 'property id' => array( 'P7', 'LABEL:P7' ),
+ );
+ }
+
+ /**
+ * @dataProvider unitProvider
+ */
+ public function testFormat( $unit, $expected ) {
+ $labelLookup = $this->getMock(
'Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup' );
+ $labelLookup->expects( $this->any() )
+ ->method( 'getLabel' )
+ ->will( $this->returnCallback( function( EntityId $id )
{
+ if ( $id->getNumericId() > 1000 ) {
+ throw new OutOfBoundsException( 'No
such label!' );
+ }
+ return new Term( 'en', 'LABEL:' .
$id->getSerialization() );
+ } ) );
+
+ $idParser = new BasicEntityIdParser();
+
+ $formatter = new VocabularyUriFormatter( $idParser,
$labelLookup, array( 'NotAUnit' ) );
+
+ $this->assertEquals( $expected, $formatter->format( $unit ) );
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/235495
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I5b8ba0fd8e24e99e7bbed82849101cbb383d7f29
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits