Aude has uploaded a new change for review.

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

Change subject: Set precision in MWTimeIsoParser
......................................................................

Set precision in MWTimeIsoParser

instead of automagically handling in ValueParsers\TimeParser

Fixes handling of input like '10. century BC'

includes partial revert of changes to test in I927209e2
(the tests were doing the right thing before that)

Change-Id: I74f88c0f629614e93bc33d4b1d893da594a0276e
---
M lib/includes/parsers/MWTimeIsoParser.php
M lib/tests/phpunit/parsers/MwTimeIsoParserTest.php
2 files changed, 70 insertions(+), 25 deletions(-)


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

diff --git a/lib/includes/parsers/MWTimeIsoParser.php 
b/lib/includes/parsers/MWTimeIsoParser.php
index b0230bc..526f7bb 100644
--- a/lib/includes/parsers/MWTimeIsoParser.php
+++ b/lib/includes/parsers/MWTimeIsoParser.php
@@ -111,8 +111,8 @@
         * @return TimeValue|bool
         */
        private function reconvertOutputString( $value ) {
-               foreach( self::$precisionMsgKeys as $repeat0Char => $msgKeys ) {
-                       foreach( $msgKeys as $msgKey ) {
+               foreach( self::$precisionMsgKeys as $repeat0Char => 
$msgKeysGroup ) {
+                       foreach( $msgKeysGroup as $msgKey ) {
                                $msg = new Message( $msgKey );
                                //FIXME: Use the language passed in options!
                                //The only reason we are not currently doing 
this is due to the formatting not currently Localizing
@@ -120,28 +120,19 @@
                                //$msg->inLanguage( $this->lang ); // todo 
check other translations?
                                $msg->inLanguage( 'en' );
                                $msgText = $msg->text();
-                               $isBceMsg = strstr( $msgKey, '-BCE-' );
+                               $isBceMsg = $this->isBceMsg( $msgKey );
 
                                list( $start, $end ) = explode( '$1' , $msgText 
, 2 );
                                if( preg_match( '/^\s*' . preg_quote( $start ) 
. '(.+?)' . preg_quote( $end ) . '\s*$/i', $value, $matches ) ) {
                                        list( , $number ) = $matches;
-                                       $number = 
$this->lang->parseFormattedNumber( $number );
-
-                                       return $this->getTimeFromYear(
-                                               $number . str_repeat( '0', 
$repeat0Char ),
-                                               $isBceMsg
-                                       );
+                                       return $this->parseNumber( $number, 
$repeat0Char, $isBceMsg );
                                }
+
                                // If the msg string ends with BCE also check 
for BC
                                if( substr_compare( $end, 'BCE', - 3, 3 ) === 0 
) {
                                        if( preg_match( '/^\s*' . preg_quote( 
$start ) . '(.+?)' . preg_quote( substr( $end, 0, -1 ) ) . '\s*$/i', $value, 
$matches ) ) {
                                                list( , $number ) = $matches;
-                                               $number = 
$this->lang->parseFormattedNumber( $number );
-
-                                               return $this->getTimeFromYear(
-                                                       $number . str_repeat( 
'0', $repeat0Char ),
-                                                       $isBceMsg
-                                               );
+                                               return $this->parseNumber( 
$number, $repeat0Char, $isBceMsg );
                                        }
 
                                }
@@ -149,6 +140,32 @@
 
                }
                return false;
+       }
+
+       /**
+        * @param string $number
+        * @param int $repeat0Char
+        * @param boolean $isBceMsg
+        *
+        * @return TimeValue
+        */
+       private function parseNumber( $number, $repeat0Char, $isBceMsg ) {
+               $number = $this->lang->parseFormattedNumber( $number );
+               $year = $number . str_repeat( '0', $repeat0Char );
+
+               $precision = $this->determinePrecision( $year );
+               $this->setPrecision( $precision );
+
+               return $this->getTimeFromYear( $year, $isBceMsg );
+       }
+
+       /**
+        * @param string $msgKey
+        *
+        * @return boolean
+        */
+       private function isBceMsg( $msgKey ) {
+               return strstr( $msgKey, '-BCE-' );
        }
 
        /**
@@ -163,8 +180,36 @@
                } else {
                        $sign = EraParser::CURRENT_ERA;
                }
+
                $timeString = $sign . $year . '-00-00T00:00:00Z';
+
                return $this->timeValueTimeParser->parse( $timeString );
        }
 
+       /**
+        * @param string $year
+        *
+        * @return int
+        */
+       private function determinePrecision( $year ) {
+               $rightZeros = strlen( $year ) - strlen( rtrim( $year, '0' ) );
+               $precision = TimeValue::PRECISION_YEAR - $rightZeros;
+
+               if( $precision < TimeValue::PRECISION_Ga ) {
+                       $precision = TimeValue::PRECISION_Ga;
+               }
+
+               return $precision;
+       }
+
+       /**
+        * @param int $precision
+        */
+       private function setPrecision( $precision ) {
+               $this->timeValueTimeParser->getOptions()->setOption(
+                       \ValueParsers\TimeParser::OPT_PRECISION,
+                       $precision
+               );
+       }
+
 }
diff --git a/lib/tests/phpunit/parsers/MwTimeIsoParserTest.php 
b/lib/tests/phpunit/parsers/MwTimeIsoParserTest.php
index 194352c..56e8a10 100644
--- a/lib/tests/phpunit/parsers/MwTimeIsoParserTest.php
+++ b/lib/tests/phpunit/parsers/MwTimeIsoParserTest.php
@@ -83,11 +83,11 @@
                        '55. millennium' =>
                                array( '+0000000000055000-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_ka , TimeFormatter::CALENDAR_GREGORIAN ),
                        '10. century' =>
-                               array( '+0000000000001000-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_YEAR , TimeFormatter::CALENDAR_GREGORIAN ),
+                               array( '+0000000000001000-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_ka , TimeFormatter::CALENDAR_GREGORIAN ),
                        '12. century' =>
-                               array( '+0000000000001200-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_YEAR , TimeFormatter::CALENDAR_GREGORIAN ),
+                               array( '+0000000000001200-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_100a , TimeFormatter::CALENDAR_GREGORIAN ),
                        '10s' =>
-                               array( '+0000000000000010-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_YEAR , TimeFormatter::CALENDAR_GREGORIAN ),
+                               array( '+0000000000000010-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_10a , TimeFormatter::CALENDAR_GREGORIAN ),
                        '12s' =>
                                array( '+0000000000000012-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_YEAR , TimeFormatter::CALENDAR_GREGORIAN ),
 
@@ -129,11 +129,11 @@
                        '55. millennium BCE' =>
                                array( '-0000000000055000-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_ka , TimeFormatter::CALENDAR_GREGORIAN ),
                        '10. century BCE' =>
-                               array( '-0000000000001000-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_YEAR , TimeFormatter::CALENDAR_GREGORIAN ),
+                               array( '-0000000000001000-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_ka , TimeFormatter::CALENDAR_GREGORIAN ),
                        '12. century BCE' =>
-                               array( '-0000000000001200-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_YEAR , TimeFormatter::CALENDAR_GREGORIAN ),
+                               array( '-0000000000001200-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_100a , TimeFormatter::CALENDAR_GREGORIAN ),
                        '10s BCE' =>
-                               array( '-0000000000000010-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_YEAR , TimeFormatter::CALENDAR_GREGORIAN ),
+                               array( '-0000000000000010-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_10a , TimeFormatter::CALENDAR_GREGORIAN ),
                        '12s BCE' =>
                                array( '-0000000000000012-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_YEAR , TimeFormatter::CALENDAR_GREGORIAN ),
                        // also parse BC
@@ -142,11 +142,11 @@
                        '55. millennium BC' =>
                                array( '-0000000000055000-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_ka , TimeFormatter::CALENDAR_GREGORIAN ),
                        '10. century BC' =>
-                               array( '-0000000000001000-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_YEAR , TimeFormatter::CALENDAR_GREGORIAN ),
+                               array( '-0000000000001000-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_ka , TimeFormatter::CALENDAR_GREGORIAN ),
                        '12. century BC' =>
-                               array( '-0000000000001200-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_YEAR , TimeFormatter::CALENDAR_GREGORIAN ),
+                               array( '-0000000000001200-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_100a , TimeFormatter::CALENDAR_GREGORIAN ),
                        '10s BC' =>
-                               array( '-0000000000000010-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_YEAR , TimeFormatter::CALENDAR_GREGORIAN ),
+                               array( '-0000000000000010-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_10a , TimeFormatter::CALENDAR_GREGORIAN ),
                        '12s BC' =>
                                array( '-0000000000000012-00-00T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_YEAR , TimeFormatter::CALENDAR_GREGORIAN ),
                );
@@ -183,4 +183,4 @@
                return $argLists;
        }
 
-}
\ No newline at end of file
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I74f88c0f629614e93bc33d4b1d893da594a0276e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: mw1.24-wmf2
Gerrit-Owner: Aude <aude.w...@gmail.com>

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

Reply via email to