function convertTitle( $t ) { return $t->getPrefixedText(); }
function getVariants() { return array( $this->mLang->getCode() ); }
function getPreferredVariant() { return $this->mLang->getCode(); }
+ function getDefaultVariant() { return $this->mLang->getCode(); }
+ function getURLVariant() { return ''; }
function getConvRuleTitle() { return false; }
function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) { }
function getExtraHashOptions() { return ''; }
function getParsedTitle() { return ''; }
function markNoConversion( $text, $noParse = false ) { return $text; }
function convertCategoryKey( $key ) { return $key; }
- function convertLinkToAllVariants( $text ) { return autoConvertToAllVariants( $text ); }
+ function convertLinkToAllVariants( $text ) { return $this->autoConvertToAllVariants( $text ); }
function armourMath( $text ) { return $text; }
}
/**
* Get a cached language object for a given language code
+ * @param $code String
+ * @return Language
*/
static function factory( $code ) {
if ( !isset( self::$mLangObjCache[$code] ) ) {
/**
* Create a language object for a given language code
+ * @param $code String
+ * @return Language
*/
protected static function newFromCode( $code ) {
global $IP;
static $recursionLevel = 0;
+
+ // Protect against path traversal below
+ if ( !Language::isValidCode( $code )
+ || strcspn( $code, "/\\\000" ) !== strlen( $code ) )
+ {
+ throw new MWException( "Invalid language code \"$code\"" );
+ }
+
if ( $code == 'en' ) {
$class = 'Language';
} else {
return $lang;
}
+ /**
+ * Returns true if a language code string is of a valid form, whether or
+ * not it exists.
+ */
+ public static function isValidCode( $code ) {
+ return strcspn( $code, "/\\\000" ) === strlen( $code );
+ }
+
/**
* Get the LocalisationCache instance
*/
$this->namespaceNames[NS_PROJECT_TALK] =
$this->fixVariableInNamespace( $talk );
}
-
+
# Sometimes a language will be localised but not actually exist on this wiki.
foreach( $this->namespaceNames as $key => $text ) {
if ( !isset( $validNamespaces[$key] ) ) {
return $names;
}
+ /**
+ * Get translated language names. This is done on best effort and
+ * by default this is exactly the same as Language::getLanguageNames.
+ * The CLDR extension provides translated names.
+ * @param $code String Language code.
+ * @return Array language code => language name
+ * @since 1.18.0
+ */
+ public static function getTranslatedLanguageNames( $code ) {
+ $names = array();
+ wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $code ) );
+
+ foreach ( self::getLanguageNames() as $code => $name ) {
+ if ( !isset( $names[$code] ) ) $names[$code] = $name;
+ }
+
+ return $names;
+ }
+
/**
* Get a message from the MediaWiki namespace.
*
} else {
$s .= $this->formatNum( $num, true );
}
- $num = false;
}
}
return $s;
return $s;
}
- /**
+ /**
* Hebrew Gematria number formatting up to 9999
*/
static function hebrewNumeral( $num ) {
}
function getMessage( $key ) {
- return self::$dataCache->getSubitem( $this->mCode, 'messages', $key );
+ // Don't change getPreferredVariant() to getCode() / mCode, because:
+
+ // 1. Some language like Chinese has multiple variant languages. Only
+ // getPreferredVariant() (in LanguageConverter) could return a
+ // sub-language which would be more suitable for the user.
+ // 2. To languages without multiple variants, getPreferredVariant()
+ // (in FakeConverter) functions exactly same as getCode() / mCode,
+ // it won't break anything.
+
+ // The same below.
+ return self::$dataCache->getSubitem( $this->getPreferredVariant(), 'messages', $key );
}
function getAllMessages() {
- return self::$dataCache->getItem( $this->mCode, 'messages' );
+ return self::$dataCache->getItem( $this->getPreferredVariant(), 'messages' );
}
function iconv( $in, $out, $string ) {
}
$isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
- '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
+ '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
if ( $isutf8 ) {
return $s;
}
# We got the first byte only of a multibyte char; remove it.
$string = substr( $string, 0, -1 );
} elseif ( $char >= 0x80 &&
- preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
- '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) )
+ preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
+ '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) )
{
# We chopped in the middle of a character; remove it
$string = $m[1];
}
/**
- * Get the list of variants supported by this langauge
+ * Get the list of variants supported by this language
* see sample implementation in LanguageZh.php
*
* @return array an array of language codes
return $this->mConverter->getVariants();
}
- function getPreferredVariant( $fromUser = true, $fromHeader = false ) {
- return $this->mConverter->getPreferredVariant( $fromUser, $fromHeader );
+ function getPreferredVariant() {
+ return $this->mConverter->getPreferredVariant();
+ }
+
+ function getDefaultVariant() {
+ return $this->mConverter->getDefaultVariant();
+ }
+
+ function getURLVariant() {
+ return $this->mConverter->getURLVariant();
}
/**
* If a language supports multiple variants, converts text
* into an array of all possible variants of the text:
* 'variant' => text in that variant
+ *
+ * @deprecated Use autoConvertToAllVariants()
*/
function convertLinkToAllVariants( $text ) {
return $this->mConverter->convertLinkToAllVariants( $text );
* @return string $prefix . $mangledCode . $suffix
*/
static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
+ // Protect against path traversal
+ if ( !Language::isValidCode( $code )
+ || strcspn( $code, "/\\\000" ) !== strlen( $code ) )
+ {
+ throw new MWException( "Invalid language code \"$code\"" );
+ }
+
return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
}
throw new MWException(
"Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
}
- extract( $arr );
+ $wikiUpperChars = $arr['wikiUpperChars'];
+ $wikiLowerChars = $arr['wikiLowerChars'];
wfProfileOut( __METHOD__ );
return array( $wikiUpperChars, $wikiLowerChars );
}
function formatTimePeriod( $seconds ) {
- if ( $seconds < 10 ) {
- return $this->formatNum( sprintf( "%.1f", $seconds ) ) . $this->getMessageFromDB( 'seconds-abbrev' );
- } elseif ( $seconds < 60 ) {
+ if ( round( $seconds * 10 ) < 100 ) {
+ return $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) ) . $this->getMessageFromDB( 'seconds-abbrev' );
+ } elseif ( round( $seconds ) < 60 ) {
return $this->formatNum( round( $seconds ) ) . $this->getMessageFromDB( 'seconds-abbrev' );
- } elseif ( $seconds < 3600 ) {
+ } elseif ( round( $seconds ) < 3600 ) {
$minutes = floor( $seconds / 60 );
$secondsPart = round( fmod( $seconds, 60 ) );
if ( $secondsPart == 60 ) {
function getConvRuleTitle() {
return $this->mConverter->getConvRuleTitle();
}
-
- /**
- * Given a string, convert it to a (hopefully short) key that can be used
- * for efficient sorting. A binary sort according to the sortkeys
- * corresponds to a logical sort of the corresponding strings. Current
- * code expects that a null character should sort before all others, but
- * has no other particular expectations (and that one can be changed if
- * necessary).
- *
- * @param string $string UTF-8 string
- * @return string Binary sortkey
- */
- public function convertToSortkey( $string ) {
- # Fake function for now
- return strtoupper( $string );
- }
-
- /**
- * Given a string, return the logical "first letter" to be used for
- * grouping on category pages and so on. This has to be coordinated
- * carefully with convertToSortkey(), or else the sorted list might jump
- * back and forth between the same "initial letters" or other pathological
- * behavior. For instance, if you just return the first character, but "a"
- * sorts the same as "A" based on convertToSortkey(), then you might get a
- * list like
- *
- * == A ==
- * * [[Aardvark]]
- *
- * == a ==
- * * [[antelope]]
- *
- * == A ==
- * * [[Ape]]
- *
- * etc., assuming for the sake of argument that $wgCapitalLinks is false.
- *
- * @param string $string UTF-8 string
- * @return string UTF-8 string corresponding to the first letter of input
- */
- public function firstLetterForLists( $string ) {
- if ( $string[0] == "\0" ) {
- $string = substr( $string, 1 );
- }
- return strtoupper( $this->firstChar( $string ) );
- }
}