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

Change subject: Avoid exponential years in MwTimeIsoFormatter
......................................................................


Avoid exponential years in MwTimeIsoFormatter

It seems that some PHP versions magically format very large floating
point numbers as "1,0E+16". We do not want that for years.

This is already covered by a test.

Bug: T111343
Change-Id: Ie61114808e68c3032f1c21213f624ba6e9a79240
---
M lib/includes/formatters/MwTimeIsoFormatter.php
M lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php
2 files changed, 56 insertions(+), 29 deletions(-)

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



diff --git a/lib/includes/formatters/MwTimeIsoFormatter.php 
b/lib/includes/formatters/MwTimeIsoFormatter.php
index 18eb622..0571d67 100644
--- a/lib/includes/formatters/MwTimeIsoFormatter.php
+++ b/lib/includes/formatters/MwTimeIsoFormatter.php
@@ -201,8 +201,12 @@
         * @return string
         */
        private function getLocalizedYear( $isoTimestamp, $precision ) {
+               preg_match( '/^(\D*)(\d*)/', $isoTimestamp, $matches );
+               list( , $sign, $year ) = $matches;
+               $isBCE = $sign === '-';
+
                $shift = 1e+0;
-               $unshift = 1e-0;
+               $unshift = 1e+0;
                $func = 'round';
 
                switch ( $precision ) {
@@ -252,39 +256,40 @@
                                break;
                }
 
-               $isBCE = substr( $isoTimestamp, 0, 1 ) === '-';
-               $year = abs( floatval( $isoTimestamp ) );
+               if ( $shift !== 1e+0 || $unshift !== 1e+0 ) {
+                       switch ( $func ) {
+                               case 'ceil':
+                                       $shifted = ceil( $year / $shift ) * 
$unshift;
+                                       break;
+                               case 'floor':
+                                       $shifted = floor( $year / $shift ) * 
$unshift;
+                                       break;
+                               default:
+                                       $shifted = round( $year / $shift ) * 
$unshift;
+                       }
 
-               switch ( $func ) {
-                       case 'ceil':
-                               $number = round( ceil( $year / $shift ) * 
$unshift );
-                               break;
-                       case 'floor':
-                               $number = round( floor( $year / $shift ) * 
$unshift );
-                               break;
-                       default:
-                               $number = round( round( $year / $shift ) * 
$unshift );
+                       // Year to small for precision, fall back to year
+                       if ( $shifted == 0
+                               && ( $precision < TimeValue::PRECISION_YEAR
+                                       || ( $isBCE && $precision === 
TimeValue::PRECISION_YEAR )
+                               )
+                       ) {
+                               $msg = null;
+                       } else {
+                               $year = sprintf( '%.0f', $shifted );
+                       }
                }
 
-               // Year to small for precision, fall back to year
-               if ( empty( $number )
-                       && ( $precision < TimeValue::PRECISION_YEAR
-                               || ( $isBCE && $precision === 
TimeValue::PRECISION_YEAR )
-                       )
-               ) {
-                       $msg = null;
-                       $number = $year;
-                       $isBCE = $isBCE && !empty( $number );
-               }
+               $year = str_pad( ltrim( $year, '0' ), 1, '0', STR_PAD_LEFT );
 
                if ( empty( $msg ) ) {
                        // TODO: This needs a message.
-                       return $number . ( $isBCE ? ' BCE' : '' );
+                       return $year . ( $isBCE ? ' BCE' : '' );
                }
 
                return $this->getMessage(
                        'wikibase-time-precision-' . ( $isBCE ? 'BCE-' : '' ) . 
$msg,
-                       $number
+                       $year
                );
        }
 
diff --git a/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php 
b/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php
index 981a61a..0277353 100644
--- a/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php
+++ b/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php
@@ -344,6 +344,10 @@
                                '1996',
                        ),
                        array(
+                               '+5-01-00T00:00:00Z', 
TimeValue::PRECISION_MONTH,
+                               'January 5',
+                       ),
+                       array(
                                '+00000001996-01-00T00:00:00Z', 
TimeValue::PRECISION_MONTH,
                                'January 1996',
                        ),
@@ -375,9 +379,27 @@
                                '+2147483648-00-00T00:00:00Z', 
TimeValue::PRECISION_YEAR,
                                '2147483648',
                        ),
+
+                       // No exponents (e.g. 1.0E+16) please
                        array(
-                               '+9999999999999999-00-00T00:00:00Z', 
TimeValue::PRECISION_YEAR,
+                               '+9999999999999999-01-01T00:00:00Z', 
TimeValue::PRECISION_YEAR10K,
+                               '10000000000000000 years CE',
+                       ),
+                       array(
+                               '+9999999999999999-01-01T00:00:00Z', 
TimeValue::PRECISION_YEAR100,
+                               '100000000000000. century',
+                       ),
+                       array(
+                               '+9999999999999999-01-01T00:00:00Z', 
TimeValue::PRECISION_YEAR,
                                '9999999999999999',
+                       ),
+                       array(
+                               '+9999999999999999-01-01T00:00:00Z', 
TimeValue::PRECISION_MONTH,
+                               'January 9999999999999999',
+                       ),
+                       array(
+                               '+9999999999999999-01-01T00:00:00Z', 
TimeValue::PRECISION_DAY,
+                               '1 January 9999999999999999',
                        ),
 
                        // Precision to low, falling back to year
@@ -421,11 +443,11 @@
                        // Better than the raw ISO string
                        array(
                                '-00000000000-01-01T01:01:01Z', 
TimeValue::PRECISION_YEAR1G,
-                               '0',
+                               '0 BCE',
                        ),
                        array(
                                '-0-01-01T01:01:01Z', 
TimeValue::PRECISION_YEAR1G,
-                               '0',
+                               '0 BCE',
                        ),
                        array(
                                '+100000000-00-00T00:00:00Z', 
TimeValue::PRECISION_YEAR1G,
@@ -457,7 +479,7 @@
                        ),
                        array(
                                '-0-00-00T00:00:42Z', TimeValue::PRECISION_YEAR,
-                               '0',
+                               '0 BCE',
                        ),
 
                        // Stuff we do not want to format so must return it :<
@@ -527,7 +549,7 @@
                $formatter = new MwTimeIsoFormatter( $options );
                $actual = $formatter->format( $timeValue );
 
-               $this->assertEquals( $expected, $actual, 'Testing ' . 
$timeValue->getTime() . ', precision ' . $timeValue->getPrecision() );
+               $this->assertSame( $expected, $actual, 'Testing ' . 
$timeValue->getTime() . ', precision ' . $timeValue->getPrecision() );
        }
 
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie61114808e68c3032f1c21213f624ba6e9a79240
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de>
Gerrit-Reviewer: Addshore <addshorew...@gmail.com>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@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