X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=languages%2FLanguage.php;h=ac8d4cb1d396936ba82c214672d30653894cf307;hb=22806b0a4509e97b56fb52b387e17e3c80fb7eb2;hp=3eb6546490a4621379ee20f7af0c74d8edd6e039;hpb=30f28501243ba58faf4063b701cf3148bd1d1280;p=lhc%2Fweb%2Fwiklou.git diff --git a/languages/Language.php b/languages/Language.php index 3eb6546490..067231557b 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -45,7 +45,9 @@ class Language { public $dateFormatStrings = []; public $mExtendedSpecialPageAliases; - protected $namespaceNames, $mNamespaceIds, $namespaceAliases; + /** @var array|null */ + protected $namespaceNames; + protected $mNamespaceIds, $namespaceAliases; /** * ReplacementArray object caches @@ -413,10 +415,10 @@ class Language { function __construct() { $this->mConverter = new FakeConverter( $this ); // Set the code to the name of the descendant - if ( get_class( $this ) == 'Language' ) { + if ( static::class === 'Language' ) { $this->mCode = 'en'; } else { - $this->mCode = str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) ); + $this->mCode = str_replace( '_', '-', strtolower( substr( static::class, 8 ) ) ); } self::getLocalisationCache(); } @@ -463,11 +465,11 @@ class Language { if ( is_null( $this->namespaceNames ) ) { global $wgMetaNamespace, $wgMetaNamespaceTalk, $wgExtraNamespaces; - $this->namespaceNames = self::$dataCache->getItem( $this->mCode, 'namespaceNames' ); $validNamespaces = MWNamespace::getCanonicalNamespaces(); - /** @suppress PhanTypeInvalidLeftOperand */ - $this->namespaceNames = $wgExtraNamespaces + $this->namespaceNames + $validNamespaces; + $this->namespaceNames = $wgExtraNamespaces + + self::$dataCache->getItem( $this->mCode, 'namespaceNames' ); + $this->namespaceNames += $validNamespaces; $this->namespaceNames[NS_PROJECT] = $wgMetaNamespace; if ( $wgMetaNamespaceTalk ) { @@ -2098,17 +2100,15 @@ class Language { $data = explode( '|', $tz, 3 ); if ( $data[0] == 'ZoneInfo' ) { - MediaWiki\suppressWarnings(); - $userTZ = timezone_open( $data[2] ); - MediaWiki\restoreWarnings(); - if ( $userTZ !== false ) { - $date = date_create( $ts, timezone_open( 'UTC' ) ); - date_timezone_set( $date, $userTZ ); - $date = date_format( $date, 'YmdHis' ); - return $date; + try { + $userTZ = new DateTimeZone( $data[2] ); + $date = new DateTime( $ts, new DateTimeZone( 'UTC' ) ); + $date->setTimezone( $userTZ ); + return $date->format( 'YmdHis' ); + } catch ( Exception $e ) { + // Unrecognized timezone, default to 'Offset' with the stored offset. + $data[0] = 'Offset'; } - # Unrecognized timezone, default to 'Offset' with the stored offset. - $data[0] = 'Offset'; } if ( $data[0] == 'System' || $tz == '' ) { @@ -2158,12 +2158,10 @@ class Language { * the date preference they're supposed to use, it should be used in * all children. * - * - * function timeanddate([...], $format = true) { - * $datePreference = $this->dateFormat($format); - * [...] - * } - * + * function timeanddate([...], $format = true) { + * $datePreference = $this->dateFormat($format); + * [...] + * } * * @param int|string|bool $usePrefs If true, the user's preference is used * if false, the site/language default is used @@ -2578,7 +2576,7 @@ class Language { /** * @param string $key - * @return array|null + * @return string|null */ public function getMessage( $key ) { return self::$dataCache->getSubitem( $this->mCode, 'messages', $key ); @@ -3286,14 +3284,14 @@ class Language { public function parseFormattedNumber( $number ) { $s = $this->digitTransformTable(); if ( $s ) { - // eliminate empty array values such as ''. (bug 64347) + // eliminate empty array values such as ''. (T66347) $s = array_filter( $s ); $number = strtr( $number, array_flip( $s ) ); } $s = $this->separatorTransformTable(); if ( $s ) { - // eliminate empty array values such as ''. (bug 64347) + // eliminate empty array values such as ''. (T66347) $s = array_filter( $s ); $number = strtr( $number, array_flip( $s ) ); } @@ -3497,7 +3495,7 @@ class Language { $string = $ellipsis . $string; } } - # Do not truncate if the ellipsis makes the string longer/equal (bug 22181). + # Do not truncate if the ellipsis makes the string longer/equal (T24181). # This check is *not* redundant if $adjustLength, due to the single case where # LEN($ellipsis) > ABS($limit arg); $stringOriginal could be shorter than $string. if ( strlen( $string ) < strlen( $stringOriginal ) ) { @@ -3735,6 +3733,43 @@ class Language { return $wgGrammarForms[$this->getCode()][$case][$word]; } + $grammarTransformations = $this->getGrammarTransformations(); + + if ( isset( $grammarTransformations[$case] ) ) { + $forms = $grammarTransformations[$case]; + + // Some names of grammar rules are aliases for other rules. + // In such cases the value is a string rather than object, + // so load the actual rules. + if ( is_string( $forms ) ) { + $forms = $grammarTransformations[$forms]; + } + + foreach ( array_values( $forms ) as $rule ) { + $form = $rule[0]; + + if ( $form === '@metadata' ) { + continue; + } + + $replacement = $rule[1]; + + $regex = '/' . addcslashes( $form, '/' ) . '/u'; + $patternMatches = preg_match( $regex, $word ); + + if ( $patternMatches === false ) { + wfLogWarning( + 'An error occurred while processing grammar. ' . + "Word: '$word'. Regex: /$form/." + ); + } elseif ( $patternMatches === 1 ) { + $word = preg_replace( $regex, $replacement, $word ); + + break; + } + } + } + return $word; } @@ -3938,10 +3973,11 @@ class Language { * * @param string $str The validated block duration in English * @param User $user User object to use timezone from or null for $wgUser + * @param int $now Current timestamp, for formatting relative block durations * @return string Somehow translated block duration * @see LanguageFi.php for example implementation */ - function translateBlockExpiry( $str, User $user = null ) { + function translateBlockExpiry( $str, User $user = null, $now = 0 ) { $duration = SpecialBlock::getSuggestedDurations( $this ); foreach ( $duration as $show => $value ) { if ( strcmp( $str, $value ) == 0 ) { @@ -3958,12 +3994,13 @@ class Language { } // If all else fails, return a standard duration or timestamp description. - $time = strtotime( $str, 0 ); + $time = strtotime( $str, $now ); if ( $time === false ) { // Unknown format. Return it as-is in case. return $str; - } elseif ( $time !== strtotime( $str, 1 ) ) { // It's a relative timestamp. - // $time is relative to 0 so it's a duration length. - return $this->formatDuration( $time ); + } elseif ( $time !== strtotime( $str, $now + 1 ) ) { // It's a relative timestamp. + // The result differs based on current time, so the difference + // is a fixed duration length. + return $this->formatDuration( $time - $now ); } else { // It's an absolute timestamp. if ( $time === 0 ) { // wfTimestamp() handles 0 as current time instead of epoch. @@ -4469,7 +4506,7 @@ class Language { # such as action=raw much more expensive than they need to be. # This will hopefully cover most cases. $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i', - [ &$this, 'replaceGrammarInNamespace' ], $talk ); + [ $this, 'replaceGrammarInNamespace' ], $talk ); return str_replace( ' ', '_', $talk ); }