jenkins-bot has submitted this change and it was merged. Change subject: Prepare YearMonthTimeParser to get rid of MediaWiki core dependency ......................................................................
Prepare YearMonthTimeParser to get rid of MediaWiki core dependency This is pure refactoring. The goal is to make this parser independent from MediaWiki core. Note that this is only a first step. Please review and merge this as it is. More will be done in later patches. This patch is motivated by https://github.com/DataValues/Time/pull/93 where I do the same to the YearMonthDayTimeParser. Both should use the same array( localized month names => numbers ) in the end. This array will be constructed in the TimeParserFactory (that's the reason for the @see). Change-Id: I2e35005a557180ff955678cfc0ed0e3d8a3e13b8 --- M repo/includes/Parsers/YearMonthTimeParser.php 1 file changed, 28 insertions(+), 13 deletions(-) Approvals: Hoo man: Looks good to me, approved Addshore: Looks good to me, but someone else must approve jenkins-bot: Verified diff --git a/repo/includes/Parsers/YearMonthTimeParser.php b/repo/includes/Parsers/YearMonthTimeParser.php index 9d41570..411cd04 100644 --- a/repo/includes/Parsers/YearMonthTimeParser.php +++ b/repo/includes/Parsers/YearMonthTimeParser.php @@ -16,6 +16,7 @@ * * @licence GNU GPL v2+ * @author Adam Shorland + * @author Thiemo Mättig * * @todo move me to DataValues-time * @todo match BCE dates in here @@ -25,9 +26,9 @@ const FORMAT_NAME = 'yearmonth'; /** - * @var Language + * @var int[] */ - private $lang; + private $monthNameUnlocalizations; /** * @var ValueParser @@ -40,11 +41,32 @@ public function __construct( ParserOptions $options = null ) { parent::__construct( $options ); - $this->lang = Language::factory( $this->getOption( ValueParser::OPT_LANG ) ); + $languageCode = $this->getOption( ValueParser::OPT_LANG ); + $this->monthNameUnlocalizations = $this->getMonthNameUnlocalizations( $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; } /** @@ -118,20 +140,13 @@ * Check for both the full name and abbreviations * * @param string|int $year - * @param string|int $month + * @param string $month * * @return TimeValue|bool */ private function parseYearMonth( $year, $month ) { - $names = $this->lang->getMonthNamesArray(); - for ( $i = 1; $i <= 12; $i++ ) { - if ( strcasecmp( $names[$i], $month ) === 0 ) { - return $this->getTimeFromYearMonth( $year, $i ); - } - } - $nameAbbrevs = $this->lang->getMonthAbbreviationsArray(); - for ( $i = 1; $i <= 12; $i++ ) { - if ( strcasecmp( $nameAbbrevs[$i], $month ) === 0 ) { + foreach ( $this->monthNameUnlocalizations as $monthName => $i ) { + if ( strcasecmp( $monthName, $month ) === 0 ) { return $this->getTimeFromYearMonth( $year, $i ); } } -- To view, visit https://gerrit.wikimedia.org/r/242108 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I2e35005a557180ff955678cfc0ed0e3d8a3e13b8 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Hoo man <h...@online.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits