Thiemo Mättig (WMDE) has uploaded a new change for review.

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

Change subject: Use MonthNameProvider::getMonthNumbers in YearMonthTimeParser
......................................................................

Use MonthNameProvider::getMonthNumbers in YearMonthTimeParser

This finally makes this class independent from core, allowing us to
move it to DataValues Time.

Change-Id: I20abdcd16610920156a28389feeebaef34bfa19e
---
M repo/includes/Parsers/TimeParserFactory.php
M repo/includes/Parsers/YearMonthTimeParser.php
M repo/tests/phpunit/includes/Parsers/YearMonthTimeParserTest.php
3 files changed, 26 insertions(+), 28 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/74/271474/1

diff --git a/repo/includes/Parsers/TimeParserFactory.php 
b/repo/includes/Parsers/TimeParserFactory.php
index af8a4d0..bf8a83b 100644
--- a/repo/includes/Parsers/TimeParserFactory.php
+++ b/repo/includes/Parsers/TimeParserFactory.php
@@ -75,7 +75,7 @@
                $parsers = array();
 
                // Year-month parser must be first, otherwise "May 2014" may be 
parsed as "2014-05-01".
-               $parsers[] = new YearMonthTimeParser( $this->options );
+               $parsers[] = new YearMonthTimeParser( $this->monthNameProvider, 
$this->options );
                $parsers[] = $isoTimestampParser;
                $parsers[] = new MwTimeIsoParser( $this->options );
                $parsers[] = new YearMonthDayTimeParser( $eraParser );
diff --git a/repo/includes/Parsers/YearMonthTimeParser.php 
b/repo/includes/Parsers/YearMonthTimeParser.php
index 442286d..e5ae186 100644
--- a/repo/includes/Parsers/YearMonthTimeParser.php
+++ b/repo/includes/Parsers/YearMonthTimeParser.php
@@ -3,7 +3,6 @@
 namespace Wikibase\Repo\Parsers;
 
 use DataValues\TimeValue;
-use Language;
 use ValueParsers\CalendarModelParser;
 use ValueParsers\IsoTimestampParser;
 use ValueParsers\ParseException;
@@ -26,9 +25,9 @@
        const FORMAT_NAME = 'year-month';
 
        /**
-        * @var int[]
+        * @var int[] Array mapping localized month names to the month's 
numbers 1 to 12.
         */
-       private $monthNameUnlocalizations;
+       private $monthNumbers;
 
        /**
         * @var ValueParser
@@ -37,36 +36,22 @@
 
        /**
         * @see StringValueParser::__construct
+        *
+        * @param MonthNameProvider $monthNameProvider
+        * @param ParserOptions|null $options
         */
-       public function __construct( ParserOptions $options = null ) {
+       public function __construct(
+               MonthNameProvider $monthNameProvider,
+               ParserOptions $options = null
+       ) {
                parent::__construct( $options );
 
                $languageCode = $this->getOption( ValueParser::OPT_LANG );
-               $this->monthNameUnlocalizations = 
$this->getMonthNameUnlocalizations( $languageCode );
+               $this->monthNumbers = $monthNameProvider->getMonthNumbers( 
$languageCode );
                $this->isoTimestampParser = new IsoTimestampParser(
                        new CalendarModelParser( $this->options ),
                        $this->options
                );
-       }
-
-       /**
-        * @see TimeParserFactory::getMwMonthNameReplacements
-        *
-        * @param string $languageCode
-        *
-        * @return int[]
-        */
-       private function getMonthNameUnlocalizations( $languageCode ) {
-               $language = Language::factory( $languageCode );
-
-               $replacements = array();
-
-               for ( $i = 1; $i <= 12; $i++ ) {
-                       $replacements[$language->getMonthName( $i )] = $i;
-                       $replacements[$language->getMonthAbbreviation( $i )] = 
$i;
-               }
-
-               return $replacements;
        }
 
        /**
@@ -145,7 +130,7 @@
         * @return TimeValue|bool
         */
        private function parseYearMonth( $year, $month ) {
-               foreach ( $this->monthNameUnlocalizations as $monthName => $i ) 
{
+               foreach ( $this->monthNumbers as $monthName => $i ) {
                        if ( strcasecmp( $monthName, $month ) === 0 ) {
                                return $this->getTimeFromYearMonth( $year, $i );
                        }
diff --git a/repo/tests/phpunit/includes/Parsers/YearMonthTimeParserTest.php 
b/repo/tests/phpunit/includes/Parsers/YearMonthTimeParserTest.php
index b3d968e..a0ee79b 100644
--- a/repo/tests/phpunit/includes/Parsers/YearMonthTimeParserTest.php
+++ b/repo/tests/phpunit/includes/Parsers/YearMonthTimeParserTest.php
@@ -16,6 +16,7 @@
  *
  * @licence GNU GPL v2+
  * @author Addshore
+ * @author Thiemo Mättig
  */
 class YearMonthTimeParserTest extends StringValueParserTest {
 
@@ -32,7 +33,19 @@
         * @return YearMonthTimeParser
         */
        protected function getInstance() {
-               return new YearMonthTimeParser();
+               $monthNameProvider = $this->getMockBuilder( 
'Wikibase\Repo\Parsers\MonthNameProvider' )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $monthNameProvider->expects( $this->once() )
+                       ->method( 'getMonthNumbers' )
+                       ->with( 'en' )
+                       ->will( $this->returnValue( array(
+                               'January' => 1,
+                               'Jan' => 1,
+                               'April' => 4,
+                       ) ) );
+
+               return new YearMonthTimeParser( $monthNameProvider );
        }
 
        /**

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I20abdcd16610920156a28389feeebaef34bfa19e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to