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 0000000..e457cb8
--- /dev/null
+++ b/lib/includes/formatters/MwTimeIsoFormatter.php
@@ -0,0 +1,82 @@
+<?php
+
+namespace Wikibase\Lib;
+
+use \ValueFormatters\TimeIsoFormatter;
+
+/**
+ * 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.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 0000000..9ee2c08
--- /dev/null
+++ b/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php
@@ -0,0 +1,104 @@
+<?php
+
+namespace ValueFormatters\Test;
+use Wikibase\Lib\MwTimeIsoFormatter;
+
+/**
+ * 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.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() {
+               $tests = array(
+                       '16 July 2013' => array(
+                               '+00000002013-07-16T00:00:00Z',
+                               11,
+                       ),
+                       '1 January 0000' => array(
+                               '+00000000000-01-01T00:00:00Z',
+                               11,
+                       ),
+                       '14 January 0001' => array(
+                               '+00000000001-01-14T00:00:00Z',
+                               11,
+                       ),
+                       '+00000010000-01-01T00:00:00Z' => array(
+                               '+00000010000-01-01T00:00:00Z',
+                               11,
+                       ),
+                       '-00000000001-01-01T00:00:00Z' => array(
+                               '-00000000001-01-01T00:00:00Z',
+                               11,
+                       ),
+                       'July 2013' => array(
+                               '+00000002013-07-16T00:00:00Z',
+                               10,
+                       ),
+                       '2013' => array(
+                               '+00000002013-07-16T00:00:00Z',
+                               9,
+                       ),
+                       '+00000002013-07-16T00:00:00Z' => array(
+                               '+00000002013-07-16T00:00:00Z',
+                               8,
+                       ),
+               );
+
+               $argLists = array();
+
+               foreach ( $tests as $expected => $args ) {
+                       $argLists[] = array( $expected, $args[0], $args[1] );
+               }
+
+               return $argLists;
+       }
+
+       /**
+        * @dataProvider formatDateProvider
+        *
+        * @since 0.4
+        *
+        * @param string $expected
+        * @param string $extendedIsoString
+        * @param integer $precision
+        */
+       public function testFormatDate( $expected, $extendedIsoString, 
$precision ) {
+               $isoFormatter = new MwTimeIsoFormatter( \Language::factory( 
'en' ) );
+               $this->assertEquals( $expected, $isoFormatter->formatDate( 
$extendedIsoString, $precision ) );
+       }
+
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/76921
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0d36f6a5ec0d7d28deaeed6a71e279b73fd2e449
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <henning.sna...@wikimedia.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to