Thiemo Mättig (WMDE) has uploaded a new change for review.
https://gerrit.wikimedia.org/r/271473
Change subject: Rework way to specific
MonthNameProvider::getMonthNameReplacements
......................................................................
Rework way to specific MonthNameProvider::getMonthNameReplacements
When I introduced this provider in Ifb927e3 I had more than one use
case in mind, but failed. The return value, mapping localized month
names to English, is to specific and not easy to use in other contexts
where the *number* of the month is needed.
Change-Id: I4c1be4487181d3d812def5d5a3b1c997af4d3a5d
---
M repo/includes/Parsers/MediaWikiMonthNameProvider.php
M repo/includes/Parsers/MonthNameProvider.php
M repo/includes/Parsers/TimeParserFactory.php
M repo/tests/phpunit/includes/Parsers/MediaWikiMonthNameProviderTest.php
M repo/tests/phpunit/includes/Parsers/TimeFormatterParserRoundtripTest.php
M repo/tests/phpunit/includes/Parsers/TimeParserFactoryTest.php
6 files changed, 46 insertions(+), 39 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/73/271473/1
diff --git a/repo/includes/Parsers/MediaWikiMonthNameProvider.php
b/repo/includes/Parsers/MediaWikiMonthNameProvider.php
index c9b5e1d..b8c5a8e 100644
--- a/repo/includes/Parsers/MediaWikiMonthNameProvider.php
+++ b/repo/includes/Parsers/MediaWikiMonthNameProvider.php
@@ -23,7 +23,6 @@
*/
public function getLocalizedMonthNames( $languageCode ) {
$language = Language::factory( $languageCode );
-
$monthNames = array();
for ( $i = 1; $i <= 12; $i++ ) {
@@ -37,29 +36,24 @@
* Creates a replacements array using information retrieved via
MediaWiki's Language object.
* Takes full month names, genitive names and abbreviations into
account.
*
- * @see MonthNameProvider::getMonthNameReplacements
+ * @see MonthNameProvider::getMonthNumbers
*
* @param string $languageCode
- * @param string $canonicalLanguageCode
*
- * @return string[] Array mapping localized month names (including full
month names, genitive
- * names and abbreviations) to the same month names in a canonical
language (usually English).
+ * @return int[] Array mapping localized month names (including full
month names, genitive names
+ * and abbreviations) to the month's numbers 1 to 12.
*/
- public function getMonthNameReplacements( $languageCode,
$canonicalLanguageCode = 'en' ) {
+ public function getMonthNumbers( $languageCode ) {
$language = Language::factory( $languageCode );
- $baseLanguage = Language::factory( $canonicalLanguageCode );
-
- $replacements = array();
+ $numbers = array();
for ( $i = 1; $i <= 12; $i++ ) {
- $canonical = $baseLanguage->getMonthName( $i );
-
- $replacements[$language->getMonthName( $i )] =
$canonical;
- $replacements[$language->getMonthNameGen( $i )] =
$canonical;
- $replacements[$language->getMonthAbbreviation( $i )] =
$canonical;
+ $numbers[$language->getMonthName( $i )] = $i;
+ $numbers[$language->getMonthNameGen( $i )] = $i;
+ $numbers[$language->getMonthAbbreviation( $i )] = $i;
}
- return $replacements;
+ return $numbers;
}
}
diff --git a/repo/includes/Parsers/MonthNameProvider.php
b/repo/includes/Parsers/MonthNameProvider.php
index e4a3786..9dd3268 100644
--- a/repo/includes/Parsers/MonthNameProvider.php
+++ b/repo/includes/Parsers/MonthNameProvider.php
@@ -19,12 +19,10 @@
/**
* @param string $languageCode
- * @param string $canonicalLanguageCode
*
- * @return string[] Array mapping localized month names (possibly
including full month names,
- * genitive names and abbreviations) to the same month names in a
canonical language (usually
- * English).
+ * @return int[] Array mapping localized month names (possibly
including full month names,
+ * genitive names and abbreviations) to the month's numbers 1 to 12.
*/
- public function getMonthNameReplacements( $languageCode,
$canonicalLanguageCode = 'en' );
+ public function getMonthNumbers( $languageCode );
}
diff --git a/repo/includes/Parsers/TimeParserFactory.php
b/repo/includes/Parsers/TimeParserFactory.php
index ef8ba46..af8a4d0 100644
--- a/repo/includes/Parsers/TimeParserFactory.php
+++ b/repo/includes/Parsers/TimeParserFactory.php
@@ -99,10 +99,12 @@
if ( $languageCode === self::CANONICAL_LANGUAGE_CODE ) {
$replacements = array();
} else {
- $replacements =
$this->monthNameProvider->getMonthNameReplacements(
- $languageCode,
+ $canonicalMonthNames =
$this->monthNameProvider->getLocalizedMonthNames(
self::CANONICAL_LANGUAGE_CODE
);
+ $replacements = array_map( function( $i ) use (
$canonicalMonthNames ) {
+ return $canonicalMonthNames[$i];
+ }, $this->monthNameProvider->getMonthNumbers(
$languageCode ) );
}
return new MonthNameUnlocalizer( $replacements );
diff --git
a/repo/tests/phpunit/includes/Parsers/MediaWikiMonthNameProviderTest.php
b/repo/tests/phpunit/includes/Parsers/MediaWikiMonthNameProviderTest.php
index 5b72e55..b3f08f6 100644
--- a/repo/tests/phpunit/includes/Parsers/MediaWikiMonthNameProviderTest.php
+++ b/repo/tests/phpunit/includes/Parsers/MediaWikiMonthNameProviderTest.php
@@ -39,18 +39,18 @@
/**
* @dataProvider replacementsProvider
*/
- public function testGetMonthNameReplacements( $languageCode,
$baseLanguageCode ) {
+ public function testGetMonthNameUnlocalizations( $languageCode ) {
$instance = new MediaWikiMonthNameProvider();
- $actual = $instance->getMonthNameReplacements( $languageCode,
$baseLanguageCode );
+ $actual = $instance->getMonthNumbers( $languageCode );
$this->assertInternalType( 'array', $actual );
- $this->assertContainsOnly( 'string', $actual );
+ $this->assertContainsOnly( 'int', $actual );
$this->assertGreaterThanOrEqual( 12, count( $actual ) );
}
public function replacementsProvider() {
return array(
- array( 'en', 'de' ),
- array( 'de', 'en' ),
+ array( 'en' ),
+ array( 'de' ),
);
}
diff --git
a/repo/tests/phpunit/includes/Parsers/TimeFormatterParserRoundtripTest.php
b/repo/tests/phpunit/includes/Parsers/TimeFormatterParserRoundtripTest.php
index 00296f2..7c54657 100644
--- a/repo/tests/phpunit/includes/Parsers/TimeFormatterParserRoundtripTest.php
+++ b/repo/tests/phpunit/includes/Parsers/TimeFormatterParserRoundtripTest.php
@@ -27,12 +27,17 @@
private function newTimeParserFactory( ParserOptions $options = null ) {
$monthNameProvider = $this->getMock(
'Wikibase\Repo\Parsers\MonthNameProvider' );
$monthNameProvider->expects( $this->any() )
- ->method( 'getMonthNameReplacements' )
+ ->method( 'getLocalizedMonthNames' )
->will( $this->returnValue( array(
- '8月' => 'August',
- 'agosto' => 'August',
- 'Augusti' => 'August',
- 'Avgust' => 'August',
+ 8 => 'August',
+ ) ) );
+ $monthNameProvider->expects( $this->any() )
+ ->method( 'getMonthNumbers' )
+ ->will( $this->returnValue( array(
+ '8月' => 8,
+ 'agosto' => 8,
+ 'Augusti' => 8,
+ 'Avgust' => 8,
) ) );
return new TimeParserFactory( $options, $monthNameProvider );
diff --git a/repo/tests/phpunit/includes/Parsers/TimeParserFactoryTest.php
b/repo/tests/phpunit/includes/Parsers/TimeParserFactoryTest.php
index df192f9..f9ed02e 100644
--- a/repo/tests/phpunit/includes/Parsers/TimeParserFactoryTest.php
+++ b/repo/tests/phpunit/includes/Parsers/TimeParserFactoryTest.php
@@ -28,15 +28,23 @@
$monthNameProvider = $this->getMock(
'Wikibase\Repo\Parsers\MonthNameProvider' );
$monthNameProvider->expects( $this->any() )
- ->method( 'getMonthNameReplacements' )
- ->will( $this->returnCallback( function( $languageCode,
$baseLanguageCode ) {
- $replacements = array();
+ ->method( 'getLocalizedMonthNames' )
+ ->will( $this->returnCallback( function( $languageCode
) {
+ $monthNames = array();
for ( $i = 1; $i <= 12; $i++ ) {
- $canonical = $baseLanguageCode .
'Month' . $i;
- $replacements[$languageCode . 'Month' .
$i] = $canonical;
- $replacements[$languageCode . 'Month' .
$i . 'Gen'] = $canonical;
+ $monthNames[$i] = $languageCode .
'Month' . $i;
}
- return $replacements;
+ return $monthNames;
+ } ) );
+ $monthNameProvider->expects( $this->any() )
+ ->method( 'getMonthNumbers' )
+ ->will( $this->returnCallback( function( $languageCode
) {
+ $numbers = array();
+ for ( $i = 1; $i <= 12; $i++ ) {
+ $numbers[$languageCode . 'Month' . $i]
= $i;
+ $numbers[$languageCode . 'Month' . $i .
'Gen'] = $i;
+ }
+ return $numbers;
} ) );
return new TimeParserFactory( $options, $monthNameProvider );
--
To view, visit https://gerrit.wikimedia.org/r/271473
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I4c1be4487181d3d812def5d5a3b1c997af4d3a5d
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