X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=languages%2FLanguage.php;h=2e4ef89c5c5d7ffc3b1a48cf1df3dfb78c3c20a9;hb=a3d562ccd1474d5fa9492ea1290796cb1c7cbcfa;hp=7354155e5170a50f7fd0299a4e57129e55e9be95;hpb=57eaa2bf04ce1b48bd89c10defe4de5b7d31f047;p=lhc%2Fweb%2Fwiklou.git diff --git a/languages/Language.php b/languages/Language.php index 7354155e51..2e4ef89c5c 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -137,6 +137,12 @@ class Language { */ static private $fallbackLanguageCache = []; + /** + * Cache for grammar rules data + * @var MapCacheLRU|null + */ + static private $grammarTransformations; + /** * Cache for language names * @var HashBagOStuff|null @@ -332,7 +338,7 @@ class Language { // People think language codes are html safe, so enforce it. // Ideally we should only allow a-zA-Z0-9- // but, .+ and other chars are often used for {{int:}} hacks - // see bugs 37564, 37587, 36938 + // see bugs T39564, T39587, T38938 $cache[$code] = // Protect against path traversal strcspn( $code, ":/\\\000&<>'\"" ) === strlen( $code ) @@ -3730,6 +3736,7 @@ class Language { return $word; } + /** * Get the grammar forms for the content language * @return array Array of grammar forms @@ -3745,6 +3752,46 @@ class Language { return []; } + + /** + * Get the grammar transformations data for the language. + * Used like grammar forms, with {{GRAMMAR}} and cases, + * but uses pairs of regexes and replacements instead of code. + * + * @return array[] Array of grammar transformations. + * @throws MWException + * @since 1.28 + */ + public function getGrammarTransformations() { + $languageCode = $this->getCode(); + + if ( self::$grammarTransformations === null ) { + self::$grammarTransformations = new MapCacheLRU( 10 ); + } + + if ( self::$grammarTransformations->has( $languageCode ) ) { + return self::$grammarTransformations->get( $languageCode ); + } + + $data = []; + + $grammarDataFile = __DIR__ . "/data/grammarTransformations/$languageCode.json"; + if ( is_readable( $grammarDataFile ) ) { + $data = FormatJson::decode( + file_get_contents( $grammarDataFile ), + true + ); + + if ( $data === null ) { + throw new MWException( "Invalid grammar data for \"$languageCode\"." ); + } + + self::$grammarTransformations->set( $languageCode, $data ); + } + + return $data; + } + /** * Provides an alternative text depending on specified gender. * Usage {{gender:username|masculine|feminine|unknown}}. @@ -4461,14 +4508,15 @@ class Language { } /** - * @todo Document + * Formats a time given in seconds into a string representation of that time. + * * @param int|float $seconds - * @param array $format Optional - * If $format['avoid'] === 'avoidseconds': don't mention seconds if $seconds >= 1 hour. - * If $format['avoid'] === 'avoidminutes': don't mention seconds/minutes if $seconds > 48 hours. + * @param array $format An optional argument that formats the returned string in different ways: + * If $format['avoid'] === 'avoidseconds': don't show seconds if $seconds >= 1 hour, + * If $format['avoid'] === 'avoidminutes': don't show seconds/minutes if $seconds > 48 hours, * If $format['noabbrevs'] is true: use 'seconds' and friends instead of 'seconds-abbrev' * and friends. - * For backwards compatibility, $format may also be one of the strings 'avoidseconds' + * @note For backwards compatibility, $format may also be one of the strings 'avoidseconds' * or 'avoidminutes'. * @return string */