# Read language names
global $wgLanguageNames;
-require_once( __DIR__ . '/Names.php' );
+require_once __DIR__ . '/Names.php';
if ( function_exists( 'mb_strtoupper' ) ) {
mb_internal_encoding( 'UTF-8' );
'seconds' => 1,
);
+ /**
+ * Cache for language fallbacks.
+ * @see Language::getFallbacksIncludingSiteLanguage
+ * @since 1.21
+ * @var array
+ */
+ static private $fallbackLanguageCache = array();
+
/**
* Get a cached or new language object for a given language code
* @param $code String
* @return bool
*/
public static function isValidCode( $code ) {
- return
- // 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
+ static $cache = array();
+ if ( isset( $cache[$code] ) ) {
+ return $cache[$code];
+ }
+ // 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
+ $cache[$code] =
strcspn( $code, ":/\\\000&<>'\"" ) === strlen( $code )
&& !preg_match( Title::getTitleInvalidRegex(), $code );
+
+ return $cache[$code];
}
/**
static $coreLanguageNames;
if ( $coreLanguageNames === null ) {
- include( MWInit::compiledPath( 'languages/Names.php' ) );
+ include MWInit::compiledPath( 'languages/Names.php' );
}
if ( isset( $coreLanguageNames[$tag] )
return;
}
- if ( !defined( 'MW_COMPILED' ) ) {
- if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
- include_once( "$IP/languages/classes/$class.php" );
- }
+ if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
+ include_once "$IP/languages/classes/$class.php";
}
}
static $coreLanguageNames;
if ( $coreLanguageNames === null ) {
- include( MWInit::compiledPath( 'languages/Names.php' ) );
+ include MWInit::compiledPath( 'languages/Names.php' );
}
$names = array();
* @param $zone DateTimeZone: Timezone of $ts
* @todo handling of "o" format character for Iranian, Hebrew, Hijri & Thai?
*
+ * @throws MWException
* @return string
*/
function sprintfDate( $format, $ts, DateTimeZone $zone = null ) {
$thai = false;
$minguo = false;
$tenno = false;
+
+ if ( strlen( $ts ) !== 14 ) {
+ throw new MWException( __METHOD__ . ": The timestamp $ts should have 14 characters" );
+ }
+
+ if ( !ctype_digit( $ts ) ) {
+ throw new MWException( __METHOD__ . ": The timestamp $ts should be a number" );
+ }
+
for ( $p = 0; $p < strlen( $format ); $p++ ) {
$num = false;
$code = $format[$p];
}
$start = substr( $str, 0, strlen( $str ) - 2 );
$end = substr( $str, strlen( $str ) - 2 );
- switch( $end ) {
+ switch ( $end ) {
case 'כ':
$str = $start . 'ך';
break;
* @return string Correct form of plural for $count in this language
*/
function convertPlural( $count, $forms ) {
- if ( !count( $forms ) ) {
- return '';
- }
-
// Handle explicit n=pluralform cases
foreach ( $forms as $index => $form ) {
if ( preg_match( '/\d+=/i', $form ) ) {
unset( $forms[$index] );
}
}
+
$forms = array_values( $forms );
+ if ( !count( $forms ) ) {
+ return '';
+ }
$pluralForm = $this->getPluralRuleIndexNumber( $count );
$pluralForm = min( $pluralForm, count( $forms ) - 1 );
}
}
+ /**
+ * Get the ordered list of fallback languages, ending with the fallback
+ * language chain for the site language.
+ *
+ * @since 1.22
+ * @param string $code Language code
+ * @return array array( fallbacks, site fallbacks )
+ */
+ public static function getFallbacksIncludingSiteLanguage( $code ) {
+ global $wgLanguageCode;
+
+ // Usually, we will only store a tiny number of fallback chains, so we
+ // keep them in static memory.
+ $cacheKey = "{$code}-{$wgLanguageCode}";
+
+ if ( !array_key_exists( $cacheKey, self::$fallbackLanguageCache ) ) {
+ $fallbacks = self::getFallbacksFor( $code );
+
+ // Append the site's fallback chain, including the site language itself
+ $siteFallbacks = self::getFallbacksFor( $wgLanguageCode );
+ array_unshift( $siteFallbacks, $wgLanguageCode );
+
+ // Eliminate any languages already included in the chain
+ $siteFallbacks = array_diff( $siteFallbacks, $fallbacks );
+
+ self::$fallbackLanguageCache[$cacheKey] = array( $fallbacks, $siteFallbacks );
+ }
+ return self::$fallbackLanguageCache[$cacheKey];
+ }
+
/**
* Get all messages for a given language
* WARNING: this may take a long time. If you just need all message *keys*