[MediaWiki-commits] [Gerrit] Implemented MwTimeIsoFormatter in Wikibase - change (mediawiki...Wikibase)
jenkins-bot has submitted this change and it was merged. Change subject: Implemented MwTimeIsoFormatter in Wikibase .. Implemented MwTimeIsoFormatter in Wikibase (requires change I0dd02909834ce8b428daea86e8bd2bf87ba9a085) Implements ValueFormatters extension's TimeIsoFormatter interface in the Wikibase extension making use of native MediaWiki functionality. Invoking the TimeFormatter is done in I72bd2c4. Change-Id: I0d36f6a5ec0d7d28deaeed6a71e279b73fd2e449 --- M lib/WikibaseLib.classes.php A lib/includes/formatters/MwTimeIsoFormatter.php A lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php 3 files changed, 187 insertions(+), 0 deletions(-) Approvals: Tobias Gritschacher: Looks good to me, approved jenkins-bot: Verified diff --git a/lib/WikibaseLib.classes.php b/lib/WikibaseLib.classes.php index 4f4b844..89e105d 100644 --- a/lib/WikibaseLib.classes.php +++ b/lib/WikibaseLib.classes.php @@ -87,6 +87,7 @@ // includes/formatters 'Wikibase\Lib\EntityIdFormatter' => 'includes/formatters/EntityIdFormatter.php', 'Wikibase\Lib\EntityIdLabelFormatter' => 'includes/formatters/EntityIdLabelFormatter.php', + 'Wikibase\Lib\MwTimeIsoFormatter' => 'includes/formatters/MwTimeIsoFormatter.php', // includes/modules 'Wikibase\RepoAccessModule' => 'includes/modules/RepoAccessModule.php', diff --git a/lib/includes/formatters/MwTimeIsoFormatter.php b/lib/includes/formatters/MwTimeIsoFormatter.php new file mode 100644 index 000..e457cb8 --- /dev/null +++ b/lib/includes/formatters/MwTimeIsoFormatter.php @@ -0,0 +1,82 @@ +http://www.gnu.org/copyleft/gpl.html + * + * @since 0.4 + * + * @file + * @ingroup WikibaseLib + * + * @licence GNU GPL v2+ + * @author H. Snater < mediaw...@snater.com > + */ +class MwTimeIsoFormatter implements TimeIsoFormatter { + + /** +* MediaWiki language object. +* @var \Language +*/ + private $language; + + public function __construct( $language ) { + $this->language = $language; + } + + /** +* @see TimeIsoFormatter::formatDate +*/ + public function formatDate( $extendedIsoTimestamp, $precision ) { + if( + // TODO: Localize dates not featuring a positive 4-digit year. + preg_match( '/^\+0*(\d{4})-/', $extendedIsoTimestamp, $matches ) + // TODO: Support precision above year + && $precision >= 9 + ) { + // Positive 4-digit year allows using Language object. + $strippedTime = preg_replace( '/^(\+0*)(\d{4})/', '$2', $extendedIsoTimestamp ); + + $timestamp = wfTimestamp( TS_MW, $strippedTime ); + $dateFormat = $this->language->getDateFormatString( + 'date', + $this->language->getDefaultDateFormat() + ); + + // TODO: Implement more sophisticated replace algorithm since characters may be escaped + // or, even better, find a way to avoid having to do replacements. + if( $precision < 11 ) { + // Remove day placeholder: + $dateFormat = preg_replace( '/((x\w{1})?(j|t)|d)/', '', $dateFormat ); + } + + if( $precision < 10 ) { + // Remove month placeholder: + $dateFormat = preg_replace( '/((x\w{1})?(F|n)|m)/', '', $dateFormat ); + } + + // TODO: Currently, the year will always be formatted with 4 digits. Years < 1000 will + // features leading zero(s) that would need to be stripped. + return $this->language->sprintfDate( trim( $dateFormat ), $timestamp ); + } else { + return $extendedIsoTimestamp; + } + } + +} diff --git a/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php b/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php new file mode 100644 index 000..9ee2c08 --- /dev/null +++ b/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php @@ -0,0 +1,104 @@ +http://www.gnu.org/copyleft/gpl.html + * + * @file + * @since 0.4 + * + * @ingroup WikibaseLibTest + * + * @group ValueFormatters + * @group DataValueExtensions + * @group WikibaseLib + * + * @licence GNU GPL v2+ + * @author H. Snater < mediaw...@snater.com > + */ +class MwTimeIsoFormatterTest extends \PHPUnit_Framework_TestCase { + + /** +* Returns an array of test parameters. +* +* @since 0.4 +* +* @return array +*/ + public function formatDateProvider()
[MediaWiki-commits] [Gerrit] Implemented MwTimeIsoFormatter in Wikibase - change (mediawiki...Wikibase)
Henning Snater has uploaded a new change for review. https://gerrit.wikimedia.org/r/76921 Change subject: Implemented MwTimeIsoFormatter in Wikibase .. Implemented MwTimeIsoFormatter in Wikibase Implements ValueFormatters extension's TimeIsoFormatter interface in the Wikibase extension making use of native MediaWiki functionality. Invoking the TimeFormatter is done in I72bd2c4. Change-Id: I0d36f6a5ec0d7d28deaeed6a71e279b73fd2e449 --- M lib/WikibaseLib.classes.php A lib/includes/formatters/MwTimeIsoFormatter.php A lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php 3 files changed, 187 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/21/76921/1 diff --git a/lib/WikibaseLib.classes.php b/lib/WikibaseLib.classes.php index 3330036..bc1fbfb 100644 --- a/lib/WikibaseLib.classes.php +++ b/lib/WikibaseLib.classes.php @@ -86,6 +86,7 @@ // includes/formatters 'Wikibase\Lib\EntityIdFormatter' => 'includes/formatters/EntityIdFormatter.php', 'Wikibase\Lib\EntityIdLabelFormatter' => 'includes/formatters/EntityIdLabelFormatter.php', + 'Wikibase\Lib\MwTimeIsoFormatter' => 'includes/formatters/MwTimeIsoFormatter.php', // includes/modules 'Wikibase\RepoAccessModule' => 'includes/modules/RepoAccessModule.php', diff --git a/lib/includes/formatters/MwTimeIsoFormatter.php b/lib/includes/formatters/MwTimeIsoFormatter.php new file mode 100644 index 000..e457cb8 --- /dev/null +++ b/lib/includes/formatters/MwTimeIsoFormatter.php @@ -0,0 +1,82 @@ +http://www.gnu.org/copyleft/gpl.html + * + * @since 0.4 + * + * @file + * @ingroup WikibaseLib + * + * @licence GNU GPL v2+ + * @author H. Snater < mediaw...@snater.com > + */ +class MwTimeIsoFormatter implements TimeIsoFormatter { + + /** +* MediaWiki language object. +* @var \Language +*/ + private $language; + + public function __construct( $language ) { + $this->language = $language; + } + + /** +* @see TimeIsoFormatter::formatDate +*/ + public function formatDate( $extendedIsoTimestamp, $precision ) { + if( + // TODO: Localize dates not featuring a positive 4-digit year. + preg_match( '/^\+0*(\d{4})-/', $extendedIsoTimestamp, $matches ) + // TODO: Support precision above year + && $precision >= 9 + ) { + // Positive 4-digit year allows using Language object. + $strippedTime = preg_replace( '/^(\+0*)(\d{4})/', '$2', $extendedIsoTimestamp ); + + $timestamp = wfTimestamp( TS_MW, $strippedTime ); + $dateFormat = $this->language->getDateFormatString( + 'date', + $this->language->getDefaultDateFormat() + ); + + // TODO: Implement more sophisticated replace algorithm since characters may be escaped + // or, even better, find a way to avoid having to do replacements. + if( $precision < 11 ) { + // Remove day placeholder: + $dateFormat = preg_replace( '/((x\w{1})?(j|t)|d)/', '', $dateFormat ); + } + + if( $precision < 10 ) { + // Remove month placeholder: + $dateFormat = preg_replace( '/((x\w{1})?(F|n)|m)/', '', $dateFormat ); + } + + // TODO: Currently, the year will always be formatted with 4 digits. Years < 1000 will + // features leading zero(s) that would need to be stripped. + return $this->language->sprintfDate( trim( $dateFormat ), $timestamp ); + } else { + return $extendedIsoTimestamp; + } + } + +} diff --git a/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php b/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php new file mode 100644 index 000..9ee2c08 --- /dev/null +++ b/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php @@ -0,0 +1,104 @@ +http://www.gnu.org/copyleft/gpl.html + * + * @file + * @since 0.4 + * + * @ingroup WikibaseLibTest + * + * @group ValueFormatters + * @group DataValueExtensions + * @group WikibaseLib + * + * @licence GNU GPL v2+ + * @author H. Snater < mediaw...@snater.com > + */ +class MwTimeIsoFormatterTest extends \PHPUnit_Framework_TestCase { + + /** +* Returns an array of test parameters. +* +* @since 0.4 +* +* @return array +*/ + public function formatDateProvider() { +