X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;ds=sidebyside;f=languages%2FLanguage.php;h=262d86fa3e61d5e02c1a8030eda2ce83ccb9b434;hb=740d87661f49cb4730acf2d713810b03f82b798b;hp=92ea75c814b197a972a30e5ee06c5fbfca4b13f4;hpb=7636bea2bf3ccc33389a97d968bdda64036f59c0;p=lhc%2Fweb%2Fwiklou.git diff --git a/languages/Language.php b/languages/Language.php index 92ea75c814..262d86fa3e 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -81,7 +81,7 @@ class Language { public $mVariants, $mCode, $mLoaded = false; public $mMagicExtensions = array(), $mMagicHookDone = false; - private $mHtmlCode = null; + private $mHtmlCode = null, $mParentLanguage = false; public $dateFormatStrings = array(); public $mExtendedSpecialPageAliases; @@ -385,6 +385,12 @@ class Language { public static function isKnownLanguageTag( $tag ) { static $coreLanguageNames; + // Quick escape for invalid input to avoid exceptions down the line + // when code tries to process tags which are not valid at all. + if ( !self::isValidBuiltInCode( $tag ) ) { + return false; + } + if ( $coreLanguageNames === null ) { include MWInit::compiledPath( 'languages/Names.php' ); } @@ -2245,7 +2251,7 @@ class Language { * @param MWTimestamp $relativeTo Base timestamp * @param User $user User preferences to use * @return string Human timestamp - * @since 1.21 + * @since 1.22 */ public function getHumanTimestamp( MWTimestamp $ts, MWTimestamp $relativeTo, User $user ) { $diff = $ts->diff( $relativeTo ); @@ -3610,7 +3616,7 @@ class Language { function convertPlural( $count, $forms ) { // Handle explicit n=pluralform cases foreach ( $forms as $index => $form ) { - if ( preg_match( '/\d+=/i', $form ) ) { + if ( preg_match( '/^\d+=/i', $form ) ) { $pos = strpos( $form, '=' ); if ( substr( $form, 0, $pos ) === (string) $count ) { return substr( $form, $pos + 1 ); @@ -3933,6 +3939,34 @@ class Language { return $this; } + /** + * Get the "parent" language which has a converter to convert a "compatible" language + * (in another variant) to this language (eg. zh for zh-cn, but not en for en-gb). + * + * @return Language|null + * @since 1.22 + */ + public function getParentLanguage() { + if ( $this->mParentLanguage !== false ) { + return $this->mParentLanguage; + } + + $pieces = explode( '-', $this->getCode() ); + $code = $pieces[0]; + if ( !in_array( $code, LanguageConverter::$languagesWithVariants ) ) { + $this->mParentLanguage = null; + return null; + } + $lang = Language::factory( $code ); + if ( !$lang->hasVariant( $this->getCode() ) ) { + $this->mParentLanguage = null; + return null; + } + + $this->mParentLanguage = $lang; + return $lang; + } + /** * Get the RFC 3066 code for this language object * @@ -3967,8 +4001,9 @@ class Language { */ public function setCode( $code ) { $this->mCode = $code; - // Ensure we don't leave an incorrect html code lying around + // Ensure we don't leave incorrect cached data lying around $this->mHtmlCode = null; + $this->mParentLanguage = false; } /** @@ -4498,7 +4533,7 @@ class Language { /** * Get the plural rule types for the language - * @since 1.21 + * @since 1.22 * @return array Associative array with plural form number and plural rule type as key-value pairs */ public function getPluralRuleTypes() { @@ -4529,7 +4564,7 @@ class Language { * Find the plural rule type appropriate for the given number * For example, if the language is set to Arabic, getPluralType(5) should * return 'few'. - * @since 1.21 + * @since 1.22 * @return string The name of the plural rule type, e.g. one, two, few, many */ public function getPluralRuleType( $number ) {