Aude has uploaded a new change for review. https://gerrit.wikimedia.org/r/130426
Change subject: Handle localised dates (e.g. 'century') more consistently ...................................................................... Handle localised dates (e.g. 'century') more consistently Consistently use TimeValue::PRECISION_100a for century, or for '2000s' (or anything ###s), assume decade precision, etc. If someone enters, '1995s', we also consider it here decade precision, although may want rethink how to parse such input. Bug: 64558 Change-Id: I4932dc65174efd24b3adb3b95c41ff919234b2df (cherry picked from commit 6d63b9be447c28d45eba2804d6cb52b47ec3004f) --- M lib/includes/parsers/MWTimeIsoParser.php M lib/tests/phpunit/parsers/MwTimeIsoParserTest.php 2 files changed, 29 insertions(+), 36 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/26/130426/1 diff --git a/lib/includes/parsers/MWTimeIsoParser.php b/lib/includes/parsers/MWTimeIsoParser.php index 526f7bb..7843812 100644 --- a/lib/includes/parsers/MWTimeIsoParser.php +++ b/lib/includes/parsers/MWTimeIsoParser.php @@ -32,28 +32,36 @@ * parsed with the given message keys */ private static $precisionMsgKeys = array( - 9 => array( + TimeValue::PRECISION_Ga => array( 'wikibase-time-precision-Gannum', 'wikibase-time-precision-BCE-Gannum', ), - 6 => array( + TimeValue::PRECISION_Ma => array( 'wikibase-time-precision-Mannum', 'wikibase-time-precision-BCE-Mannum', ), - 3 => array( + TimeValue::PRECISION_ka => array( 'wikibase-time-precision-millennium', 'wikibase-time-precision-BCE-millennium', ), - 2 => array( + TimeValue::PRECISION_100a => array( 'wikibase-time-precision-century', 'wikibase-time-precision-BCE-century', ), - 0 => array( + TimeValue::PRECISION_10a => array( 'wikibase-time-precision-annum', 'wikibase-time-precision-BCE-annum', 'wikibase-time-precision-10annum', 'wikibase-time-precision-BCE-10annum', ), + ); + + private static $paddedZeros = array( + TimeValue::PRECISION_Ga => 9, + TimeValue::PRECISION_Ma => 6, + TimeValue::PRECISION_ka => 3, + TimeValue::PRECISION_100a => 2, + TimeValue::PRECISION_10a => 0 ); /** @@ -111,7 +119,7 @@ * @return TimeValue|bool */ private function reconvertOutputString( $value ) { - foreach( self::$precisionMsgKeys as $repeat0Char => $msgKeysGroup ) { + foreach( self::$precisionMsgKeys as $precision => $msgKeysGroup ) { foreach( $msgKeysGroup as $msgKey ) { $msg = new Message( $msgKey ); //FIXME: Use the language passed in options! @@ -125,14 +133,14 @@ list( $start, $end ) = explode( '$1' , $msgText , 2 ); if( preg_match( '/^\s*' . preg_quote( $start ) . '(.+?)' . preg_quote( $end ) . '\s*$/i', $value, $matches ) ) { list( , $number ) = $matches; - return $this->parseNumber( $number, $repeat0Char, $isBceMsg ); + return $this->parseNumber( $number, $precision, $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; - return $this->parseNumber( $number, $repeat0Char, $isBceMsg ); + return $this->parseNumber( $number, $precision, $isBceMsg ); } } @@ -144,16 +152,15 @@ /** * @param string $number - * @param int $repeat0Char + * @param int $precision * @param boolean $isBceMsg * * @return TimeValue */ - private function parseNumber( $number, $repeat0Char, $isBceMsg ) { + private function parseNumber( $number, $precision, $isBceMsg ) { $number = $this->lang->parseFormattedNumber( $number ); - $year = $number . str_repeat( '0', $repeat0Char ); + $year = $number . str_repeat( '0', self::$paddedZeros[$precision] ); - $precision = $this->determinePrecision( $year ); $this->setPrecision( $precision ); return $this->getTimeFromYear( $year, $isBceMsg ); @@ -184,22 +191,6 @@ $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; } /** diff --git a/lib/tests/phpunit/parsers/MwTimeIsoParserTest.php b/lib/tests/phpunit/parsers/MwTimeIsoParserTest.php index ef5c19c..d0096f1 100644 --- a/lib/tests/phpunit/parsers/MwTimeIsoParserTest.php +++ b/lib/tests/phpunit/parsers/MwTimeIsoParserTest.php @@ -83,13 +83,17 @@ '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_ka , TimeFormatter::CALENDAR_GREGORIAN ), + array( '+0000000000001000-00-00T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_100a , TimeFormatter::CALENDAR_GREGORIAN ), '12. century' => array( '+0000000000001200-00-00T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_100a , TimeFormatter::CALENDAR_GREGORIAN ), + '1980s' => + array( '+0000000000001980-00-00T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_10a , TimeFormatter::CALENDAR_GREGORIAN ), + '2000s' => + array( '+0000000000002000-00-00T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_10a , TimeFormatter::CALENDAR_GREGORIAN ), '10s' => 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 ), + array( '+0000000000000012-00-00T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_10a , TimeFormatter::CALENDAR_GREGORIAN ), // - dates '13 billion years ago' => @@ -129,28 +133,26 @@ '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_ka , TimeFormatter::CALENDAR_GREGORIAN ), + array( '-0000000000001000-00-00T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_100a , TimeFormatter::CALENDAR_GREGORIAN ), '12. century BCE' => 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_10a , TimeFormatter::CALENDAR_GREGORIAN ), '12s BCE' => - array( '-0000000000000012-00-00T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_YEAR , TimeFormatter::CALENDAR_GREGORIAN ), + array( '-0000000000000012-00-00T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_10a , TimeFormatter::CALENDAR_GREGORIAN ), // also parse BC '5. millennium BC' => array( '-0000000000005000-00-00T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_ka , TimeFormatter::CALENDAR_GREGORIAN ), '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_ka , TimeFormatter::CALENDAR_GREGORIAN ), + array( '-0000000000001000-00-00T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_100a , TimeFormatter::CALENDAR_GREGORIAN ), '12. century BC' => 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_10a , TimeFormatter::CALENDAR_GREGORIAN ), '12s BC' => - array( '-0000000000000012-00-00T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_YEAR , TimeFormatter::CALENDAR_GREGORIAN ), - '1980s' => - array( '+0000000000001980-00-00T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_10a , TimeFormatter::CALENDAR_GREGORIAN ) + array( '-0000000000000012-00-00T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_10a , TimeFormatter::CALENDAR_GREGORIAN ), ); foreach ( $valid as $value => $expected ) { -- To view, visit https://gerrit.wikimedia.org/r/130426 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I4932dc65174efd24b3adb3b95c41ff919234b2df 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