Henning Snater has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/74599


Change subject: Implemented ValueFormatter for Time DataValue
......................................................................

Implemented ValueFormatter for Time DataValue

Change-Id: Ib069a01cba87ffe6f0874a6b84258a40dd96c3c3
---
M ValueFormatters/ValueFormatters.classes.php
M ValueFormatters/ValueFormatters.mw.php
M ValueFormatters/ValueFormatters.php
A ValueFormatters/includes/formatters/TimeFormatter.php
A ValueFormatters/tests/phpunit/formatters/TimeFormatterTest.php
5 files changed, 261 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DataValues 
refs/changes/99/74599/1

diff --git a/ValueFormatters/ValueFormatters.classes.php 
b/ValueFormatters/ValueFormatters.classes.php
index 1713cc7..db9f9aa 100644
--- a/ValueFormatters/ValueFormatters.classes.php
+++ b/ValueFormatters/ValueFormatters.classes.php
@@ -36,6 +36,7 @@
        'ValueFormatters\GeoCoordinateFormatter' => 
'includes/formatters/GeoCoordinateFormatter.php',
        'ValueFormatters\IriFormatter' => 
'includes/formatters/IriFormatter.php',
        'ValueFormatters\StringFormatter' => 
'includes/formatters/StringFormatter.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..d0e7813 100644
--- a/ValueFormatters/ValueFormatters.php
+++ b/ValueFormatters/ValueFormatters.php
@@ -54,13 +54,6 @@
 
 global $wgValueFormatters;
 
-/**
- * @deprecated since 0.1 This is a global registry that provides no control 
over object lifecycle
- */
-$wgValueFormatters = array(
-       'globecoordinate' => 'ValueFormatters\GeoCoordinateFormatter'
-);
-
 spl_autoload_register( function ( $className ) {
        // @codeCoverageIgnoreStart
        static $classes = false;
diff --git a/ValueFormatters/includes/formatters/TimeFormatter.php 
b/ValueFormatters/includes/formatters/TimeFormatter.php
new file mode 100644
index 0000000..cdb7f76
--- /dev/null
+++ b/ValueFormatters/includes/formatters/TimeFormatter.php
@@ -0,0 +1,118 @@
+<?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';
+
+       /**
+        * @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',
+               ) );
+       }
+
+       /**
+        * @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' );
+               }
+
+               $precision = $value->getPrecision();
+               $language = $this->getOption( self::OPT_LANGUAGE );
+
+               // TODO: Localize dates not featuring a positive 4-digit year.
+               // TODO: Support precision above year
+               if(
+                       preg_match( '/^\+0*(\d{4})-/', $value->getTime(), 
$matches )
+                       && !is_null( $language )
+                       && $precision >= 9
+               ) {
+                       // Positive 4-digit year allows using Language object.
+                       $strippedTime = preg_replace( '/^(\+0*)(\d{4})/', '$2', 
$value->getTime() );
+
+                       // TODO: Get rid of MediaWiki dependency TS_MW:
+                       $timestamp = wfTimestamp( TS_MW, $strippedTime );
+                       $dateFormat = $language->getDateFormatString( 'date', 
$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.
+                       $formatted = $language->sprintfDate( trim( $dateFormat 
), $timestamp );
+               } else {
+                       $formatted = $value->getTime();
+               }
+
+               $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/tests/phpunit/formatters/TimeFormatterTest.php 
b/ValueFormatters/tests/phpunit/formatters/TimeFormatterTest.php
new file mode 100644
index 0000000..7292326
--- /dev/null
+++ b/ValueFormatters/tests/phpunit/formatters/TimeFormatterTest.php
@@ -0,0 +1,140 @@
+<?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(
+                       '16 July 2013 (Gregorian)' => array(
+                               '+00000002013-07-16T00:00:00Z',
+                               0,
+                               0,
+                               0,
+                               11,
+                               TimeFormatter::CALENDAR_GREGORIAN
+                       ),
+                       '1 January 0000 (Gregorian)' => array(
+                               '+00000000000-01-01T00:00:00Z',
+                               0,
+                               0,
+                               0,
+                               11,
+                               TimeFormatter::CALENDAR_GREGORIAN
+                       ),
+                       '14 January 0001 (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
+                       ),
+                       'July 2013 (Gregorian)' => array(
+                               '+00000002013-07-16T00:00:00Z',
+                               0,
+                               0,
+                               0,
+                               10,
+                               TimeFormatter::CALENDAR_GREGORIAN
+                       ),
+                       '2013 (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( array(
+                       TimeFormatter::OPT_LANGUAGE => \Language::factory( 'en' 
)
+               ) );
+
+               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/74599
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib069a01cba87ffe6f0874a6b84258a40dd96c3c3
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

Reply via email to