[MediaWiki-commits] [Gerrit] Set precision in MWTimeIsoParser - change (mediawiki...Wikibase)
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 );
[MediaWiki-commits] [Gerrit] Set precision in MWTimeIsoParser - change (mediawiki...Wikibase)
jenkins-bot has submitted this change and it was merged. 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(-) Approvals: Aude: Looks good to me, approved WikidataJenkins: Verified jenkins-bot: Verified 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 ); } + /** +
[MediaWiki-commits] [Gerrit] Set precision in MWTimeIsoParser - change (mediawiki...Wikibase)
Aude has uploaded a new change for review. https://gerrit.wikimedia.org/r/130097 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' Change-Id: I74f88c0f629614e93bc33d4b1d893da594a0276e --- M lib/includes/parsers/MWTimeIsoParser.php 1 file changed, 79 insertions(+), 16 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/97/130097/1 diff --git a/lib/includes/parsers/MWTimeIsoParser.php b/lib/includes/parsers/MWTimeIsoParser.php index b0230bc..02276a8 100644 --- a/lib/includes/parsers/MWTimeIsoParser.php +++ b/lib/includes/parsers/MWTimeIsoParser.php @@ -10,6 +10,7 @@ use ValueParsers\ParseException; use ValueParsers\ParserOptions; use ValueParsers\StringValueParser; +use ValueParsers\TimeParser; use ValueParsers\ValueParser; /** @@ -29,7 +30,7 @@ /** * @var array message keys showing the number of 0s that need to be appended to years when -* parsed with the given message keys +*parsed with the given message keys */ private static $precisionMsgKeys = array( 9 = array( @@ -111,8 +112,8 @@ * @return TimeValue|bool */ private function reconvertOutputString( $value ) { - foreach( self::$precisionMsgKeys as $repeat0Char = $msgKeys ) { - foreach( $msgKeys as $msgKey ) { + foreach( self::$precisionMsgKeys as $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 +121,18 @@ //$msg-inLanguage( $this-lang ); // todo check other translations? $msg-inLanguage( 'en' ); $msgText = $msg-text(); - $isBceMsg = strstr( $msgKey, '-BCE-' ); 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, $msgKey ); } + // 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, $msgKey ); } } @@ -149,6 +140,34 @@ } return false; + } + + /** +* @param string $number +* @param string $msgKey +* +* @return TimeValue +*/ + private function parseNumber( $number, $msgKey ) { + $number = $this-lang-parseFormattedNumber( $number ); + $paddedZeros = $this-determinePaddedZeros( $msgKey ); + $year = $number . str_repeat( '0', $paddedZeros ); + + $precision = $this-determinePrecision( $year ); + $this-setPrecision( $precision ); + + $isBceMsg = $this-isBceMsg( $msgKey ); + + return $this-getTimeFromYear( $year, $isBceMsg ); + } + + /** +* @param string $msgKey +* +* @return boolean +*/ + private function isBceMsg( $msgKey ) { + return
[MediaWiki-commits] [Gerrit] Set precision in MWTimeIsoParser - change (mediawiki...Wikibase)
jenkins-bot has submitted this change and it was merged. 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(-) Approvals: WikidataJenkins: Verified Thiemo Mättig (WMDE): Looks good to me, approved jenkins-bot: Verified 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 ); }