X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=languages%2FLanguage.php;h=58972410398a2e74d9c2f69f57ccf6967bcba137;hp=eab09a1d1be9882caf7b31a6a68935a00887b2ef;hb=74d04edec385aa86ee01943b9a27475d79f74e78;hpb=73380233aab4e2009acdbaa8d6ac928c499408af diff --git a/languages/Language.php b/languages/Language.php index eab09a1d1b..5897241039 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -33,6 +33,25 @@ use CLDRPluralRuleParser\Evaluator; * @ingroup Language */ class Language { + /** + * Return autonyms in fetchLanguageName(s). + * @since 1.32 + */ + const AS_AUTONYMS = null; + + /** + * Return all known languages in fetchLanguageName(s). + * @since 1.32 + */ + const ALL = 'all'; + + /** + * Return in fetchLanguageName(s) only the languages for which we have at + * least some localisation. + * @since 1.32 + */ + const SUPPORTED = 'mwfile'; + /** * @var LanguageConverter */ @@ -61,6 +80,18 @@ class Language { static public $mLangObjCache = []; + /** + * Return a fallback chain for messages in getFallbacksFor + * @since 1.32 + */ + const MESSAGES_FALLBACKS = 0; + + /** + * Return a strict fallback chain in getFallbacksFor + * @since 1.32 + */ + const STRICT_FALLBACKS = 1; + static public $mWeekdayMsgs = [ 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday' @@ -809,16 +840,16 @@ class Language { /** * Get an array of language names, indexed by code. * @param null|string $inLanguage Code of language in which to return the names - * Use null for autonyms (native names) + * Use self::AS_AUTONYMS for autonyms (native names) * @param string $include One of: - * 'all' all available languages + * self::ALL all available languages * 'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames (default) - * 'mwfile' only if the language is in 'mw' *and* has a message file + * self::SUPPORTED only if the language is in 'mw' *and* has a message file * @return array Language code => language name (sorted by key) * @since 1.20 */ - public static function fetchLanguageNames( $inLanguage = null, $include = 'mw' ) { - $cacheKey = $inLanguage === null ? 'null' : $inLanguage; + public static function fetchLanguageNames( $inLanguage = self::AS_AUTONYMS, $include = 'mw' ) { + $cacheKey = $inLanguage === self::AS_AUTONYMS ? 'null' : $inLanguage; $cacheKey .= ":$include"; if ( self::$languageNameCache === null ) { self::$languageNameCache = new HashBagOStuff( [ 'maxKeys' => 20 ] ); @@ -835,18 +866,21 @@ class Language { /** * Uncached helper for fetchLanguageNames * @param null|string $inLanguage Code of language in which to return the names - * Use null for autonyms (native names) + * Use self::AS_AUTONYMS for autonyms (native names) * @param string $include One of: - * 'all' all available languages + * self::ALL all available languages * 'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames (default) - * 'mwfile' only if the language is in 'mw' *and* has a message file + * self::SUPPORTED only if the language is in 'mw' *and* has a message file * @return array Language code => language name (sorted by key) */ - private static function fetchLanguageNamesUncached( $inLanguage = null, $include = 'mw' ) { + private static function fetchLanguageNamesUncached( + $inLanguage = self::AS_AUTONYMS, + $include = 'mw' + ) { global $wgExtraLanguageNames, $wgUsePigLatinVariant; // If passed an invalid language code to use, fallback to en - if ( $inLanguage !== null && !self::isValidCode( $inLanguage ) ) { + if ( $inLanguage !== self::AS_AUTONYMS && !self::isValidCode( $inLanguage ) ) { $inLanguage = 'en'; } @@ -871,7 +905,7 @@ class Language { } } - if ( $include === 'all' ) { + if ( $include === self::ALL ) { ksort( $names ); return $names; } @@ -882,7 +916,7 @@ class Language { $returnMw[$coreCode] = $names[$coreCode]; } - if ( $include === 'mwfile' ) { + if ( $include === self::SUPPORTED ) { $namesMwFile = []; # We do this using a foreach over the codes instead of a directory # loop so that messages files in extensions will work correctly. @@ -905,12 +939,17 @@ class Language { /** * @param string $code The code of the language for which to get the name - * @param null|string $inLanguage Code of language in which to return the name (null for autonyms) - * @param string $include 'all', 'mw' or 'mwfile'; see fetchLanguageNames() + * @param null|string $inLanguage Code of language in which to return the name + * (SELF::AS_AUTONYMS for autonyms) + * @param string $include See fetchLanguageNames() * @return string Language name or empty * @since 1.20 */ - public static function fetchLanguageName( $code, $inLanguage = null, $include = 'all' ) { + public static function fetchLanguageName( + $code, + $inLanguage = self::AS_AUTONYMS, + $include = self::ALL + ) { $code = strtolower( $code ); $array = self::fetchLanguageNames( $inLanguage, $include ); return !array_key_exists( $code, $array ) ? '' : $array[$code]; @@ -4194,8 +4233,13 @@ class Language { /** * convert text to different variants of a language. * - * @param string $text - * @return string + * @warning Glossary state is maintained between calls. This means + * if you pass unescaped text to this method it can cause an XSS + * in later calls to this method, even if the later calls have properly + * escaped the input. Never feed this method user controlled text that + * is not properly escaped! + * @param string $text Content that has been already escaped for use in HTML + * @return string HTML */ public function convert( $text ) { return $this->mConverter->convert( $text ); @@ -4556,15 +4600,29 @@ class Language { * * @since 1.19 * @param string $code Language code - * @return array Non-empty array, ending in "en" + * @param int $mode Fallback mode, either MESSAGES_FALLBACKS (which always falls back to 'en'), + * or STRICT_FALLBACKS (whic honly falls back to 'en' when explicitly defined) + * @throws MWException + * @return array List of language codes */ - public static function getFallbacksFor( $code ) { + public static function getFallbacksFor( $code, $mode = self::MESSAGES_FALLBACKS ) { if ( $code === 'en' || !self::isValidBuiltInCode( $code ) ) { return []; } - // For unknown languages, fallbackSequence returns an empty array, - // hardcode fallback to 'en' in that case. - return self::getLocalisationCache()->getItem( $code, 'fallbackSequence' ) ?: [ 'en' ]; + switch ( $mode ) { + case self::MESSAGES_FALLBACKS: + // For unknown languages, fallbackSequence returns an empty array, + // hardcode fallback to 'en' in that case as English messages are + // always defined. + return self::getLocalisationCache()->getItem( $code, 'fallbackSequence' ) ?: [ 'en' ]; + case self::STRICT_FALLBACKS: + // Use this mode when you don't want to fallback to English unless + // explicitly defined, for example when you have language-variant icons + // and an international language-independent fallback. + return self::getLocalisationCache()->getItem( $code, 'originalFallbackSequence' ); + default: + throw new MWException( "Invalid fallback mode \"$mode\"" ); + } } /**