jenkins-bot has submitted this change and it was merged.

Change subject: (bug 56684) Show Quantity details in diff.
......................................................................


(bug 56684) Show Quantity details in diff.

This introduces QuantityDetailsFormatter and hooks it up
to be used in diffs.

Change-Id: I8ef2d0f0c96e404afe28e18bd6af839732df719d
---
M lib/WikibaseLib.classes.php
M lib/WikibaseLib.i18n.php
A lib/includes/formatters/QuantityDetailsFormatter.php
M lib/includes/formatters/WikibaseValueFormatterBuilders.php
A lib/tests/phpunit/formatters/QuantityDetailsFormatterTest.php
5 files changed, 172 insertions(+), 0 deletions(-)

Approvals:
  Addshore: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/WikibaseLib.classes.php b/lib/WikibaseLib.classes.php
index 66cfa33..eb577d6 100644
--- a/lib/WikibaseLib.classes.php
+++ b/lib/WikibaseLib.classes.php
@@ -120,6 +120,7 @@
                'Wikibase\Lib\SnakFormatter' => 
'includes/formatters/SnakFormatter.php',
                'Wikibase\Lib\OutputFormatSnakFormatterFactory' => 
'includes/formatters/OutputFormatSnakFormatterFactory.php',
                'Wikibase\Lib\OutputFormatValueFormatterFactory' => 
'includes/formatters/OutputFormatValueFormatterFactory.php',
+               'Wikibase\Lib\QuantityDetailsFormatter' => 
'includes/formatters/QuantityDetailsFormatter.php',
                'Wikibase\Lib\UnDeserializableValueFormatter' => 
'includes/formatters/UnDeserializableValueFormatter.php',
                'Wikibase\Lib\WikibaseSnakFormatterBuilders' => 
'includes/formatters/WikibaseSnakFormatterBuilders.php',
                'Wikibase\Lib\WikibaseValueFormatterBuilders' => 
'includes/formatters/WikibaseValueFormatterBuilders.php',
diff --git a/lib/WikibaseLib.i18n.php b/lib/WikibaseLib.i18n.php
index 95ac27e..3a48fd3 100644
--- a/lib/WikibaseLib.i18n.php
+++ b/lib/WikibaseLib.i18n.php
@@ -45,6 +45,10 @@
        'wikibase-error-ui-link-exists' => 'You cannot link to this page 
because another item already links to it.',
        'wikibase-error-ui-session-failure' => 'Your session has expired. 
Please log in again.',
        'wikibase-error-ui-edit-conflict' => 'There is an edit conflict. Please 
reload and save again.',
+       'wikibase-quantitydetails-amount' => 'Amount',
+       'wikibase-quantitydetails-upperbound' => 'Upper Bound',
+       'wikibase-quantitydetails-lowerbound' => 'Lower Bound',
+       'wikibase-quantitydetails-unit' => 'Unit',
        'wikibase-replicationnote' => 'Please notice that it can take several 
minutes until the changes are visible on all wikis.',
        'wikibase-sitelinks-wikipedia' => 'Wikipedia pages linked to this item',
        'wikibase-sitelinks-sitename-columnheading' => 'Language',
@@ -143,6 +147,10 @@
        'wikibase-error-ui-session-failure' => 'This is a human readable 
version of the API error "wikibase-api-session-failure" which is shown in the 
UI.',
        'wikibase-error-ui-edit-conflict' => 'This is a human readable version 
of the API error "edit-conflict" which is shown in the UI.
 Note that the default message says the user shall "reload and save", but after 
a reload the content that should be saved will be lost.',
+       'wikibase-quantitydetails-amount' => 'Label used for the "amount" field 
of a quantity value when showing a detailed representation of the quantity, 
e.g. in a diff.',
+       'wikibase-quantitydetails-upperbound' => 'Label used for the "upper 
bound" field of a quantity value when showing a detailed representation of the 
quantity, e.g. in a diff.',
+       'wikibase-quantitydetails-lowerbound' => 'Label used for the "lower 
bound" field of a quantity value when showing a detailed representation of the 
quantity, e.g. in a diff.',
+       'wikibase-quantitydetails-unit' => 'Label used for the "unit" field of 
a quantity value when showing a detailed representation of the quantity, e.g. 
in a diff.',
        'wikibase-replicationnote' => 'Note telling the user that it can take a 
few minutes until the made changes are visible on all wikis.
 
 Preceded by any one of the following messages:
diff --git a/lib/includes/formatters/QuantityDetailsFormatter.php 
b/lib/includes/formatters/QuantityDetailsFormatter.php
new file mode 100644
index 0000000..3a5c63d
--- /dev/null
+++ b/lib/includes/formatters/QuantityDetailsFormatter.php
@@ -0,0 +1,94 @@
+<?php
+
+namespace Wikibase\Lib;
+
+use DataValues\QuantityValue;
+use Html;
+use InvalidArgumentException;
+use Message;
+use ValueFormatters\DecimalFormatter;
+use ValueFormatters\FormatterOptions;
+use ValueFormatters\ValueFormatter;
+use ValueFormatters\ValueFormatterBase;
+
+/**
+ * Formatter for rendering the details of a QuantityValue (most useful for 
diffs) in HTML.
+ *
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+class QuantityDetailsFormatter extends ValueFormatterBase {
+
+       /**
+        * @var DecimalFormatter
+        */
+       protected $decimalFormatter;
+
+       /**
+        * @param FormatterOptions $options
+        */
+       public function __construct( FormatterOptions $options ) {
+               parent::__construct( $options );
+
+               $this->decimalFormatter = new EscapingValueFormatter( new 
DecimalFormatter( $options ), 'htmlspecialchars' );
+       }
+
+       /**
+        * Generates HTML representing the details of a QuantityValue,
+        * as an itemized list.
+        *
+        * @since 0.5
+        *
+        * @param QuantityValue $value The ID to format
+        *
+        * @return string
+        * @throws InvalidArgumentException
+        */
+       public function format( $value ) {
+               if ( !( $value instanceof QuantityValue ) ) {
+                       throw new InvalidArgumentException( 'Data value type 
mismatch. Expected an QuantityValue.' );
+               }
+
+               $html = '';
+               $html .= Html::openElement( 'dl', array( 'class' => 
'wikibase-details wikibase-quantity-details' ) );
+
+               $html .= $this->renderLabelValuePair( 'amount', 
$this->decimalFormatter->format( $value->getAmount() ) );
+               $html .= $this->renderLabelValuePair( 'upperBound', 
$this->decimalFormatter->format( $value->getUpperBound() ) );
+               $html .= $this->renderLabelValuePair( 'lowerBound', 
$this->decimalFormatter->format( $value->getLowerBound() ) );
+               $html .= $this->renderLabelValuePair( 'unit', htmlspecialchars( 
$value->getUnit() ) );
+
+               $html .= Html::closeElement( 'dl' );
+
+               return $html;
+       }
+
+       /**
+        * @param string $fieldName
+        * @param string $valueHtml
+        *
+        * @return string HTML for the label/value pair
+        */
+       public function renderLabelValuePair( $fieldName, $valueHtml ) {
+               $html = '';
+               $html .= Html::element( 'dt', array( 'class' => 
'wikibase-quantity-' . $fieldName ), $this->getFieldLabel( $fieldName )->text() 
);
+               $html .= Html::element( 'dd', array( 'class' => 
'wikibase-quantity-' . $fieldName ), $valueHtml );
+
+               return $html;
+       }
+
+       /**
+        * @param string $fieldName
+        *
+        * @return Message
+        */
+       protected function getFieldLabel( $fieldName ) {
+               $lang = $this->getOption( ValueFormatter::OPT_LANG );
+
+               $key = 'wikibase-quantitydetails-' . strtolower( $fieldName );
+               $msg = wfMessage( $key )->inLanguage( $lang );
+
+               return $msg;
+       }
+}
diff --git a/lib/includes/formatters/WikibaseValueFormatterBuilders.php 
b/lib/includes/formatters/WikibaseValueFormatterBuilders.php
index b5b4ff7..b3c2908 100644
--- a/lib/includes/formatters/WikibaseValueFormatterBuilders.php
+++ b/lib/includes/formatters/WikibaseValueFormatterBuilders.php
@@ -93,6 +93,7 @@
                // Formatters to use for HTML in diffs.
                // Falls back to HTML display formatters.
                SnakFormatter::FORMAT_HTML_DIFF => array(
+                       'PT:quantity' => 
'Wikibase\Lib\QuantityDetailsFormatter',
                ),
        );
 
diff --git a/lib/tests/phpunit/formatters/QuantityDetailsFormatterTest.php 
b/lib/tests/phpunit/formatters/QuantityDetailsFormatterTest.php
new file mode 100644
index 0000000..d2be0e0
--- /dev/null
+++ b/lib/tests/phpunit/formatters/QuantityDetailsFormatterTest.php
@@ -0,0 +1,68 @@
+<?php
+
+namespace Wikibase\Lib\Test;
+
+use DataValues\NumberValue;
+use DataValues\QuantityValue;
+use ValueFormatters\FormatterOptions;
+use ValueFormatters\ValueFormatter;
+use Wikibase\Lib\QuantityDetailsFormatter;
+
+/**
+ * @covers Wikibase\Lib\QuantityDetailsFormatter
+ *
+ * @since 0.5
+ *
+ * @group ValueFormatters
+ * @group WikibaseLib
+ * @group Wikibase
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+class QuantityDetailsFormatterTest extends \PHPUnit_Framework_TestCase {
+
+       /**
+        * @dataProvider quantityFormatProvider
+        *
+        * @covers QuantityDetailsFormatterTest::format()
+        */
+       public function testFormat( $value, $options, $pattern ) {
+               $formatter = new QuantityDetailsFormatter( $options );
+
+               $html = $formatter->format( $value );
+               $this->assertRegExp( $pattern, $html );
+       }
+
+       public function quantityFormatProvider() {
+               $options = new FormatterOptions( array(
+                       ValueFormatter::OPT_LANG => 'en'
+               ) );
+
+               return array(
+                       array(
+                               QuantityValue::newFromNumber( '+5', '1', '+6', 
'+4' ),
+                               $options,
+                               '@' . implode( '.*',
+                                       array(
+                                               '<dd[^<>]*>[^<>]*5[^<>]*</dd>',
+                                               '<dd[^<>]*>[^<>]*6[^<>]*</dd>',
+                                               '<dd[^<>]*>[^<>]*4[^<>]*</dd>',
+                                               '<dd[^<>]*>[^<>]*1[^<>]*</dd>',
+                                       )
+                               ) . '@s'
+                       ),
+               );
+       }
+
+       /**
+        * @covers QuantityDetailsFormatterTest::format()
+        */
+       public function testFormatError() {
+               $formatter = new QuantityDetailsFormatter( new 
FormatterOptions() );
+               $value = new NumberValue( 23 );
+
+               $this->setExpectedException( 'InvalidArgumentException' );
+               $formatter->format( $value );
+       }
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I8ef2d0f0c96e404afe28e18bd6af839732df719d
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Addshore <addshorew...@gmail.com>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Henning Snater <henning.sna...@wikimedia.de>
Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com>
Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to