Daniel Kinzler has submitted this change and it was merged. Change subject: Only use date formats that can roundtrip ......................................................................
Only use date formats that can roundtrip This partially reverts Id09e26e. Now the formatter checks for a few specific aspects of the format given in the respective MessagesXx.php: * Day can only be "d" (with leading zeros) or "j" (without). * Month can only be "F" (normal), "M" (abbreviation) or "xg" (genitive). * Both support optional dots or commas. * Spaces between the parts are enforced. * "Y" (full year) is enforced. Main change still is the added support for genitive month names which requires I232fd3b to make full roundtrips possible. See the comments in Id09e26e for more examples of strange date formats. Change-Id: I9aefa02a8355c9cca6ac750b2bf82a7734338640 --- M lib/includes/formatters/MwTimeIsoFormatter.php M lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php 2 files changed, 48 insertions(+), 6 deletions(-) Approvals: WikidataJenkins: Verified Daniel Kinzler: Looks good to me, approved Eranroz: Looks good to me, but someone else must approve Thiemo Mättig (WMDE): Looks good to me, but someone else must approve jenkins-bot: Checked diff --git a/lib/includes/formatters/MwTimeIsoFormatter.php b/lib/includes/formatters/MwTimeIsoFormatter.php index e6d0fcd..1ebf18b 100644 --- a/lib/includes/formatters/MwTimeIsoFormatter.php +++ b/lib/includes/formatters/MwTimeIsoFormatter.php @@ -120,15 +120,43 @@ private function getDateFormat( $precision ) { if ( $precision <= TimeValue::PRECISION_YEAR ) { return 'Y'; - } - - if ( $precision === TimeValue::PRECISION_MONTH ) { + } elseif ( $precision === TimeValue::PRECISION_MONTH ) { $format = $this->language->getDateFormatString( 'monthonly', 'dmy' ); - return $format ?: 'F Y'; + return sprintf( '%s Y', $this->getMonthFormat( $format ) ); + } else { + $format = $this->language->getDateFormatString( 'date', 'dmy' ); + return sprintf( '%s %s Y', $this->getDayFormat( $format ), $this->getMonthFormat( $format ) ); + } + } + + /** + * @see Language::sprintfDate + * + * @param string $dateFormat + * + * @return string A date format for the day that roundtrips the Wikibase TimeParsers. + */ + private function getDayFormat( $dateFormat ) { + if ( preg_match( '/[dj][.,]?/', $dateFormat, $matches ) ) { + return $matches[0]; } - $format = $this->language->getDateFormatString( 'date', 'dmy' ); - return $format ?: 'j F Y'; + return 'j'; + } + + /** + * @see Language::sprintfDate + * + * @param string $dateFormat + * + * @return string A date format for the month that roundtrips the Wikibase TimeParsers. + */ + private function getMonthFormat( $dateFormat ) { + if ( preg_match( '/(?:[FM]|xg)[.,]?/', $dateFormat, $matches ) ) { + return $matches[0]; + } + + return 'F'; } /** diff --git a/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php b/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php index a0a849b..244b72f 100644 --- a/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php +++ b/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php @@ -274,6 +274,20 @@ 'la' ), + // Preserve punctuation as given in MessagesXx.php but skip suffixes and words + array( + '+2013-08-16T00:00:00Z', TimeValue::PRECISION_DAY, + '16 Avgust. 2013', + true, + 'kaa' + ), + array( + '+2013-08-16T00:00:00Z', TimeValue::PRECISION_DAY, + '16 agosto 2013', + true, + 'pt' + ), + // Valid values with day, month and/or year zero array( '+00000001995-00-00T00:00:00Z', TimeValue::PRECISION_YEAR, -- To view, visit https://gerrit.wikimedia.org/r/149294 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I9aefa02a8355c9cca6ac750b2bf82a7734338640 Gerrit-PatchSet: 3 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: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Eranroz <eranro...@gmail.com> Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: WikidataJenkins <wikidata-servi...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits