jenkins-bot has submitted this change and it was merged.

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/MediaWikiMonthNameProvider.php
M repo/includes/Parsers/MonthNameProvider.php
M repo/includes/Parsers/TimeParserFactory.php
M repo/includes/Parsers/YearMonthTimeParser.php
M repo/tests/phpunit/includes/Parsers/TimeFormatterParserRoundtripTest.php
M repo/tests/phpunit/includes/Parsers/TimeParserFactoryTest.php
M repo/tests/phpunit/includes/Parsers/YearMonthTimeParserTest.php
7 files changed, 36 insertions(+), 34 deletions(-)

Approvals:
  Hoo man: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/repo/includes/Parsers/MediaWikiMonthNameProvider.php 
b/repo/includes/Parsers/MediaWikiMonthNameProvider.php
index b8c5a8e..1cbe4a6 100644
--- a/repo/includes/Parsers/MediaWikiMonthNameProvider.php
+++ b/repo/includes/Parsers/MediaWikiMonthNameProvider.php
@@ -19,7 +19,7 @@
         *
         * @param string $languageCode
         *
-        * @return string[] Array mapping the month's numbers 1 to 12 to 
localized month names.
+        * @return string[] Array mapping month numbers (1 to 12) to localized 
month names.
         */
        public function getLocalizedMonthNames( $languageCode ) {
                $language = Language::factory( $languageCode );
@@ -41,7 +41,7 @@
         * @param string $languageCode
         *
         * @return int[] Array mapping localized month names (including full 
month names, genitive names
-        * and abbreviations) to the month's numbers 1 to 12.
+        * and abbreviations) to month numbers (1 to 12).
         */
        public function getMonthNumbers( $languageCode ) {
                $language = Language::factory( $languageCode );
diff --git a/repo/includes/Parsers/MonthNameProvider.php 
b/repo/includes/Parsers/MonthNameProvider.php
index 9dd3268..ad0abbc 100644
--- a/repo/includes/Parsers/MonthNameProvider.php
+++ b/repo/includes/Parsers/MonthNameProvider.php
@@ -13,7 +13,7 @@
        /**
         * @param string $languageCode
         *
-        * @return string[] Array mapping the month's numbers 1 to 12 to 
localized month names.
+        * @return string[] Array mapping month numbers (1 to 12) to localized 
month names.
         */
        public function getLocalizedMonthNames( $languageCode );
 
@@ -21,7 +21,7 @@
         * @param string $languageCode
         *
         * @return int[] Array mapping localized month names (possibly 
including full month names,
-        * genitive names and abbreviations) to the month's numbers 1 to 12.
+        * genitive names and abbreviations) to month numbers (1 to 12).
         */
        public function getMonthNumbers( $languageCode );
 
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..93136bf 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 month 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/TimeFormatterParserRoundtripTest.php 
b/repo/tests/phpunit/includes/Parsers/TimeFormatterParserRoundtripTest.php
index 7c54657..79ec969 100644
--- a/repo/tests/phpunit/includes/Parsers/TimeFormatterParserRoundtripTest.php
+++ b/repo/tests/phpunit/includes/Parsers/TimeFormatterParserRoundtripTest.php
@@ -29,15 +29,19 @@
                $monthNameProvider->expects( $this->any() )
                        ->method( 'getLocalizedMonthNames' )
                        ->will( $this->returnValue( array(
+                               1 => 'January',
                                8 => 'August',
+                               12 => 'December',
                        ) ) );
                $monthNameProvider->expects( $this->any() )
                        ->method( 'getMonthNumbers' )
                        ->will( $this->returnValue( array(
+                               'January' => 1,
                                '8月' => 8,
                                'agosto' => 8,
                                'Augusti' => 8,
                                'Avgust' => 8,
+                               'December' => 12,
                        ) ) );
 
                return new TimeParserFactory( $options, $monthNameProvider );
diff --git a/repo/tests/phpunit/includes/Parsers/TimeParserFactoryTest.php 
b/repo/tests/phpunit/includes/Parsers/TimeParserFactoryTest.php
index f9ed02e..093f81b 100644
--- a/repo/tests/phpunit/includes/Parsers/TimeParserFactoryTest.php
+++ b/repo/tests/phpunit/includes/Parsers/TimeParserFactoryTest.php
@@ -106,9 +106,9 @@
                         */
                        '1 1999' =>
                                array( '+1999-01-00T00:00:00Z', 
TimeValue::PRECISION_MONTH ),
-                       'March 1999' =>
+                       'enMonth3 1999' =>
                                array( '+1999-03-00T00:00:00Z', 
TimeValue::PRECISION_MONTH ),
-                       '1999 March' =>
+                       '1999 enMonth3' =>
                                array( '+1999-03-00T00:00:00Z', 
TimeValue::PRECISION_MONTH ),
 
                        /**
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: merged
Gerrit-Change-Id: I20abdcd16610920156a28389feeebaef34bfa19e
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Adrian Heine <[email protected]>
Gerrit-Reviewer: Bene <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to