Daniel Kinzler has uploaded a new change for review.

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


Change subject: (bug #56685) Parsing of localized decimal values.
......................................................................

(bug #56685) Parsing of localized decimal values.

This introduces teh Unlocalizer interface for converting localized
strings to a canonical form.

Change-Id: I9acb56d4f7f472d5c612bcfcee092dec303cf726
---
M DataValuesCommon/DataValuesCommon.classes.php
M DataValuesCommon/DataValuesCommon.mw.php
M DataValuesCommon/src/ValueParsers/ApiParseValue.php
M DataValuesCommon/src/ValueParsers/DecimalParser.php
M DataValuesCommon/src/ValueParsers/QuantityParser.php
A DataValuesCommon/src/ValueParsers/Unlocalizer.php
M DataValuesCommon/tests/ValueParsers/DecimalParserTest.php
7 files changed, 100 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DataValues 
refs/changes/00/98100/1

diff --git a/DataValuesCommon/DataValuesCommon.classes.php 
b/DataValuesCommon/DataValuesCommon.classes.php
index d0f17ea..b4ef886 100644
--- a/DataValuesCommon/DataValuesCommon.classes.php
+++ b/DataValuesCommon/DataValuesCommon.classes.php
@@ -33,7 +33,7 @@
        'ValueParsers\QuantityParser' => 'src/ValueParsers/QuantityParser.php',
        'ValueParsers\StringValueParser' => 
'src/ValueParsers/StringValueParser.php',
        'ValueParsers\DecimalParser' => 'src/ValueParsers/DecimalParser.php',
-       'ValueParsers\QuantityParser' => 'src/ValueParsers/QuantityParser.php',
+       'ValueParsers\Unlocalizer' => 'src/ValueParsers/Unlocalizer.php',
 
        'ValueParsers\Test\StringValueParserTest' => 
'tests/ValueParsers/StringValueParserTest.php',
        'ValueParsers\Test\ValueParserTestBase' => 
'tests/ValueParsers/ValueParserTestBase.php',
diff --git a/DataValuesCommon/DataValuesCommon.mw.php 
b/DataValuesCommon/DataValuesCommon.mw.php
index 98e6ece..83dcdcc 100644
--- a/DataValuesCommon/DataValuesCommon.mw.php
+++ b/DataValuesCommon/DataValuesCommon.mw.php
@@ -24,6 +24,10 @@
        'globecoordinate' => 'ValueFormatters\GlobeCoordinateFormatter',
        'time' => 'ValueFormatters\TimeFormatter',
        'string' => 'ValueFormatters\StringFormatter',
+       'decimal' => 'ValueFormatters\DecimalFormatter',
+       'quantity' => function( ValueFormatters\FormatterOptions $options ) {
+               return new \ValueFormatters\QuantityFormatter( new 
\ValueFormatters\DecimalFormatter( $options ), $options );
+       },
 );
 
 global $wgValueParsers;
@@ -38,7 +42,10 @@
 $wgValueParsers['globecoordinate'] = 'ValueParsers\GlobeCoordinateParser';
 $wgValueParsers['int'] = 'ValueParsers\IntParser';
 $wgValueParsers['null'] = 'ValueParsers\NullParser';
-$wgValueParsers['quantity'] = 'ValueParsers\QuantityParser';
+$wgValueParsers['decimal'] = 'ValueParsers\DecimalParser';
+$wgValueParsers['quantity'] = function( ValueParsers\ParserOptions $options ) {
+       return new \ValueParsers\QuantityParser( new 
\ValueParsers\DecimalParser( $options ), $options );
+};
 
 global $wgValueValidators;
 
diff --git a/DataValuesCommon/src/ValueParsers/ApiParseValue.php 
b/DataValuesCommon/src/ValueParsers/ApiParseValue.php
index 0d324eb..84e67e9 100644
--- a/DataValuesCommon/src/ValueParsers/ApiParseValue.php
+++ b/DataValuesCommon/src/ValueParsers/ApiParseValue.php
@@ -121,9 +121,13 @@
                $parserOptions = new ParserOptions();
                $parserOptions->setOption( ValueParser::OPT_LANG, 
$this->getLanguage()->getCode() );
 
-               $options = \FormatJson::decode( $optionsParam, true );
+               if ( $optionsParam !== null && $optionsParam !== '' ) {
+                       $options = \FormatJson::decode( $optionsParam, true );
 
-               if ( is_array( $options ) ) {
+                       if ( !is_array( $options ) ) {
+                               $this->dieUsage( 'Malformed options parameter', 
'malformed-options' );
+                       }
+
                        foreach ( $options as $name => $value ) {
                                $parserOptions->setOption( $name, $value );
                        }
diff --git a/DataValuesCommon/src/ValueParsers/DecimalParser.php 
b/DataValuesCommon/src/ValueParsers/DecimalParser.php
index 5d8e548..e50470d 100644
--- a/DataValuesCommon/src/ValueParsers/DecimalParser.php
+++ b/DataValuesCommon/src/ValueParsers/DecimalParser.php
@@ -16,6 +16,23 @@
 class DecimalParser extends StringValueParser {
 
        /**
+        * @var null|Unlocalizer
+        */
+       protected $unlocalizer;
+
+       /**
+        * @since 0.1
+        *
+        * @param ParserOptions|null $options
+        * @param Unlocalizer|null $unlocalizer
+        */
+       public function __construct( ParserOptions $options = null, Unlocalizer 
$unlocalizer = null) {
+               parent::__construct( $options );
+
+               $this->unlocalizer = $unlocalizer;
+       }
+
+       /**
         * Creates a DecimalValue from a given string.
         *
         * The decimal notation for the value is based on ISO 31-0, with some 
modifications:
@@ -38,6 +55,11 @@
         * @throws ParseException
         */
        protected function stringParse( $value ) {
+               if ( $this->unlocalizer !== null ) {
+                       $lang = $this->options->getOption( 
ValueParser::OPT_LANG );
+                       $value = $this->unlocalizer->unlocalize( $value, $lang, 
$this->options );
+               }
+
                $value = $this->normalizeDecimal( $value );
 
                if ( $value === '' ) {
diff --git a/DataValuesCommon/src/ValueParsers/QuantityParser.php 
b/DataValuesCommon/src/ValueParsers/QuantityParser.php
index 3d1d1b5..4365db1 100644
--- a/DataValuesCommon/src/ValueParsers/QuantityParser.php
+++ b/DataValuesCommon/src/ValueParsers/QuantityParser.php
@@ -22,6 +22,23 @@
        const UNIT_PATTERN = '[a-zA-ZµåÅöÖ°%][-.a-zA-Z0-9åÅöÖ°%²³^]*';
 
        /**
+        * @var DecimalParser
+        */
+       protected $decimalParser;
+
+       /**
+        * @since 0.1
+        *
+        * @param DecimalParser $decimalParser
+        * @param ParserOptions|null $options
+        */
+       public function __construct( DecimalParser $decimalParser, 
ParserOptions $options = null ) {
+               parent::__construct( $options );
+
+               $this->decimalParser = $decimalParser;
+       }
+
+       /**
         * @see StringValueParser::stringParse
         *
         * @since 0.1
@@ -62,15 +79,14 @@
         * @return QuantityValue
         */
        private function newQuantityFromParts( $amount, $exactness, $margin, 
$unit ) {
-               $decimalParser = new DecimalParser( $this->options );
-               $amountValue = $decimalParser->parse( $amount );
+               $amountValue = $this->decimalParser->parse( $amount );
 
                if ( $exactness === '!' ) {
                        // the amount is an exact number
                        $quantity = $this->newExactQuantity( $amountValue, 
$unit );
                } elseif ( $margin !== null ) {
                        // uncertainty margin given
-                       $marginValue = $decimalParser->parse( $margin );
+                       $marginValue = $this->decimalParser->parse( $margin );
                        $quantity = $this->newUncertainQuantityFromMargin( 
$amountValue, $unit, $marginValue );
                } else {
                        // derive uncertainty from given decimals
diff --git a/DataValuesCommon/src/ValueParsers/Unlocalizer.php 
b/DataValuesCommon/src/ValueParsers/Unlocalizer.php
new file mode 100644
index 0000000..fade52a
--- /dev/null
+++ b/DataValuesCommon/src/ValueParsers/Unlocalizer.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace ValueParsers;
+
+/**
+ * Interface for services that convert a string to canonical form.
+ *
+ * @license GPL 2+
+ * @author Daniel Kinzler
+ */
+interface Unlocalizer {
+
+       /**
+        * Converts a localized string to canonical/internal representation.
+        *
+        * @param string $string string to process
+        * @param string $language language code
+        * @param ParserOptions $options
+        *
+        * @return string unlocalized string
+        */
+       public function unlocalize( $string, $language, ParserOptions $options 
);
+
+}
+ 
\ No newline at end of file
diff --git a/DataValuesCommon/tests/ValueParsers/DecimalParserTest.php 
b/DataValuesCommon/tests/ValueParsers/DecimalParserTest.php
index bcca87d..5a7db86 100644
--- a/DataValuesCommon/tests/ValueParsers/DecimalParserTest.php
+++ b/DataValuesCommon/tests/ValueParsers/DecimalParserTest.php
@@ -4,6 +4,8 @@
 
 use DataValues\DecimalValue;
 use ValueParsers\DecimalParser;
+use ValueParsers\ParserOptions;
+use ValueParsers\ValueParser;
 
 /**
  * @covers ValueParsers\DecimalParser
@@ -93,4 +95,21 @@
                return 'ValueParsers\DecimalParser';
        }
 
+       public function testUnlocalization() {
+               $unlocalizer = $this->getMock( 'ValueParsers\Unlocalizer' );
+               $unlocalizer->expects( $this->once() )
+                       ->method( 'unlocalize' )
+                       ->will( $this->returnCallback( function( $number, 
$language, ParserOptions $options ) {
+                               return str_replace( '#', '', $number );
+                       } ) );
+
+               $options = new ParserOptions();
+               $parser = new DecimalParser( $options, $unlocalizer );
+
+               $input = '###20#000#000###';
+               $value = $parser->parse( $input );
+
+               $this->assertEquals( '20000000', $value->getValue() );
+       }
+
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9acb56d4f7f472d5c612bcfcee092dec303cf726
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/DataValues
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