Henning Snater has uploaded a new change for review.
https://gerrit.wikimedia.org/r/77089
Change subject: ValueFormatter for Time DataValue
......................................................................
ValueFormatter for Time DataValue
(bug 48937)
Change-Id: I0dd02909834ce8b428daea86e8bd2bf87ba9a085
---
M ValueFormatters/ValueFormatters.classes.php
M ValueFormatters/ValueFormatters.mw.php
M ValueFormatters/ValueFormatters.php
A ValueFormatters/includes/formatters/TimeFormatter.php
A ValueFormatters/includes/formatters/TimeIsoFormatter.php
A ValueFormatters/tests/phpunit/formatters/TimeFormatterTest.php
6 files changed, 284 insertions(+), 2 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DataValues
refs/changes/89/77089/1
diff --git a/ValueFormatters/ValueFormatters.classes.php
b/ValueFormatters/ValueFormatters.classes.php
index 1713cc7..ca2ddc3 100644
--- a/ValueFormatters/ValueFormatters.classes.php
+++ b/ValueFormatters/ValueFormatters.classes.php
@@ -36,6 +36,8 @@
'ValueFormatters\GeoCoordinateFormatter' =>
'includes/formatters/GeoCoordinateFormatter.php',
'ValueFormatters\IriFormatter' =>
'includes/formatters/IriFormatter.php',
'ValueFormatters\StringFormatter' =>
'includes/formatters/StringFormatter.php',
+ 'ValueFormatters\TimeIsoFormatter' =>
'includes/formatters/TimeIsoFormatter.php',
+ 'ValueFormatters\TimeFormatter' =>
'includes/formatters/TimeFormatter.php',
'ValueFormatters\Test\ValueFormatterFactoryTest' =>
'tests/phpunit/ValueFormatterFactoryTest.php',
'ValueFormatters\Test\ValueFormatterTestBase' =>
'tests/phpunit/ValueFormatterTestBase.php',
diff --git a/ValueFormatters/ValueFormatters.mw.php
b/ValueFormatters/ValueFormatters.mw.php
index 9c11d74..f138125 100644
--- a/ValueFormatters/ValueFormatters.mw.php
+++ b/ValueFormatters/ValueFormatters.mw.php
@@ -49,7 +49,8 @@
}
$wgValueFormatters = array(
- \DataValues\GeoCoordinateValue::getType() =>
'ValueFormatters\GeoCoordinateFormatter'
+ \DataValues\GeoCoordinateValue::getType() =>
'ValueFormatters\GeoCoordinateFormatter',
+ \DataValues\TimeValue::getType() => 'ValueFormatters\TimeFormatter',
);
/**
diff --git a/ValueFormatters/ValueFormatters.php
b/ValueFormatters/ValueFormatters.php
index 563974a..2c27b01 100644
--- a/ValueFormatters/ValueFormatters.php
+++ b/ValueFormatters/ValueFormatters.php
@@ -58,7 +58,8 @@
* @deprecated since 0.1 This is a global registry that provides no control
over object lifecycle
*/
$wgValueFormatters = array(
- 'globecoordinate' => 'ValueFormatters\GeoCoordinateFormatter'
+ 'globecoordinate' => 'ValueFormatters\GeoCoordinateFormatter',
+ 'timevalue' => 'ValueFormatters\TimeFormatter',
);
spl_autoload_register( function ( $className ) {
diff --git a/ValueFormatters/includes/formatters/TimeFormatter.php
b/ValueFormatters/includes/formatters/TimeFormatter.php
new file mode 100644
index 0000000..5c122cf
--- /dev/null
+++ b/ValueFormatters/includes/formatters/TimeFormatter.php
@@ -0,0 +1,95 @@
+<?php
+
+namespace ValueFormatters;
+
+use DataValues\TimeValue;
+use InvalidArgumentException;
+
+/**
+ * Time formatter.
+ *
+ * Some code in this class has been borrowed from the
+ * MapsCoordinateParser class of the Maps extension for MediaWiki.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.1
+ *
+ * @file
+ * @ingroup ValueFormatters
+ *
+ * @licence GNU GPL v2+
+ * @author H. Snater < [email protected] >
+ */
+class TimeFormatter extends ValueFormatterBase {
+ const CALENDAR_GREGORIAN = 'http://www.wikidata.org/entity/Q1985727';
+ const CALENDAR_JULIAN = 'http://www.wikidata.org/entity/Q1985786';
+
+ const OPT_LANGUAGE = 'language';
+ const OPT_CALENDARNAMES = 'calendars';
+ const OPT_TIME_ISO_FORMATTER = 'time iso formatter';
+
+ /**
+ * @since 0.1
+ *
+ * @param FormatterOptions $options
+ */
+ public function __construct( FormatterOptions $options ) {
+ parent::__construct( $options );
+
+ $this->defaultOption( self::OPT_LANGUAGE, null );
+
+ $this->defaultOption( self::OPT_CALENDARNAMES, array(
+ self::CALENDAR_GREGORIAN => 'Gregorian',
+ self::CALENDAR_JULIAN => 'Julian',
+ ) );
+
+ $this->defaultOption( self::OPT_TIME_ISO_FORMATTER, null );
+ }
+
+ /**
+ * @see ValueFormatter::format
+ *
+ * @since 0.1
+ *
+ * @param TimeValue $value The value to format
+ *
+ * @return string
+ * @throws InvalidArgumentException
+ */
+ public function format( $value ) {
+ if ( !( $value instanceof TimeValue ) ) {
+ throw new InvalidArgumentException(
'ValueFormatters\TimeFormatter can only format '
+ . 'instances of DataValues\TimeValue' );
+ }
+
+ $formatted = $value->getTime();
+
+ $isoFormatter = $this->getOption( self::OPT_TIME_ISO_FORMATTER
);
+
+ if( is_subclass_of( $isoFormatter,
'ValueFormatters\TimeIsoFormatter' ) ) {
+ $formatted = $isoFormatter->formatDate(
+ $value->getTime(), $value->getPrecision()
+ );
+ }
+
+ $calendarNames = $this->getOption( self::OPT_CALENDARNAMES );
+
+ // TODO: Support other calendar models retrieved via
$value->getCalendarModel().
+ return $formatted . ' (' .
$calendarNames[self::CALENDAR_GREGORIAN] . ')';
+ }
+
+}
diff --git a/ValueFormatters/includes/formatters/TimeIsoFormatter.php
b/ValueFormatters/includes/formatters/TimeIsoFormatter.php
new file mode 100644
index 0000000..2d9d001
--- /dev/null
+++ b/ValueFormatters/includes/formatters/TimeIsoFormatter.php
@@ -0,0 +1,45 @@
+<?php
+
+namespace ValueFormatters;
+
+/**
+ * Time formatter.
+ *
+ * Some code in this class has been borrowed from the
+ * MapsCoordinateParser class of the Maps extension for MediaWiki.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.1
+ *
+ * @file
+ * @ingroup ValueFormatters
+ *
+ * @licence GNU GPL v2+
+ * @author H. Snater < [email protected] >
+ */
+interface TimeIsoFormatter {
+
+ /**
+ * Formats a given (extended) ISO timestamp according to a given
precision.
+ * @since 0.1
+ *
+ * @param string $timestamp
+ * @param integer $precision
+ * @return string
+ */
+ public function formatDate( $timestamp, $precision );
+}
\ No newline at end of file
diff --git a/ValueFormatters/tests/phpunit/formatters/TimeFormatterTest.php
b/ValueFormatters/tests/phpunit/formatters/TimeFormatterTest.php
new file mode 100644
index 0000000..2de92f5
--- /dev/null
+++ b/ValueFormatters/tests/phpunit/formatters/TimeFormatterTest.php
@@ -0,0 +1,138 @@
+<?php
+
+namespace ValueFormatters\Test;
+
+use DataValues\TimeValue;
+use ValueFormatters\TimeFormatter;
+
+/**
+ * Unit tests for the ValueFormatters\TimeFormatter class.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @since 0.1
+ *
+ * @ingroup ValueFormattersTest
+ *
+ * @group ValueFormatters
+ * @group DataValueExtensions
+ *
+ * @licence GNU GPL v2+
+ * @author H. Snater < [email protected] >
+ */
+class TimeFormatterTest extends ValueFormatterTestBase {
+
+ /**
+ * @see ValueFormatterTestBase::validProvider
+ *
+ * @since 0.1
+ *
+ * @return array
+ */
+ public function validProvider() {
+ $tests = array(
+ '+00000002013-07-16T00:00:00Z (Gregorian)' => array(
+ '+00000002013-07-16T00:00:00Z',
+ 0,
+ 0,
+ 0,
+ 11,
+ TimeFormatter::CALENDAR_GREGORIAN
+ ),
+ '+00000000000-01-01T00:00:00Z (Gregorian)' => array(
+ '+00000000000-01-01T00:00:00Z',
+ 0,
+ 0,
+ 0,
+ 11,
+ TimeFormatter::CALENDAR_GREGORIAN
+ ),
+ '+00000000001-01-14T00:00:00Z (Gregorian)' => array(
+ '+00000000001-01-14T00:00:00Z',
+ 0,
+ 0,
+ 0,
+ 11,
+ TimeFormatter::CALENDAR_JULIAN
+ ),
+ '+00000010000-01-01T00:00:00Z (Gregorian)' => array(
+ '+00000010000-01-01T00:00:00Z',
+ 0,
+ 0,
+ 0,
+ 11,
+ TimeFormatter::CALENDAR_GREGORIAN
+ ),
+ '-00000000001-01-01T00:00:00Z (Gregorian)' => array(
+ '-00000000001-01-01T00:00:00Z',
+ 0,
+ 0,
+ 0,
+ 11,
+ TimeFormatter::CALENDAR_GREGORIAN
+ ),
+ '+00000002013-07-16T00:00:00Z (Gregorian)' => array(
+ '+00000002013-07-16T00:00:00Z',
+ 0,
+ 0,
+ 0,
+ 10,
+ TimeFormatter::CALENDAR_GREGORIAN
+ ),
+ '+00000002013-07-16T00:00:00Z (Gregorian)' => array(
+ '+00000002013-07-16T00:00:00Z',
+ 0,
+ 0,
+ 0,
+ 9,
+ TimeFormatter::CALENDAR_GREGORIAN
+ ),
+ '+00000002013-07-16T00:00:00Z (Gregorian)' => array(
+ '+00000002013-07-16T00:00:00Z',
+ 0,
+ 0,
+ 0,
+ 8,
+ TimeFormatter::CALENDAR_GREGORIAN
+ ),
+ );
+
+ $argLists = array();
+
+ // TODO: Test with different parser options.
+ $options = new \ValueFormatters\FormatterOptions();
+
+ foreach ( $tests as $expected => $args ) {
+ $timeValue = new TimeValue( $args[0], $args[1],
$args[2], $args[3], $args[4], $args[5] );
+ $argLists[] = array( $timeValue, $expected, $options );
+ }
+
+ return $argLists;
+ }
+
+ /**
+ * @see ValueFormatterTestBase::getFormatterClass
+ *
+ * @since 0.1
+ *
+ * @return string
+ */
+ protected function getFormatterClass() {
+ return 'ValueFormatters\TimeFormatter';
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/77089
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I0dd02909834ce8b428daea86e8bd2bf87ba9a085
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/DataValues
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits