<?php
/**
- * @package MediaWiki
- * @subpackage Language
+ * @addtogroup Language
*/
if( !defined( 'MEDIAWIKI' ) ) {
# Read language names
global $wgLanguageNames;
-require_once( 'Names.php' );
+require_once( dirname(__FILE__) . '/Names.php' ) ;
global $wgInputEncoding, $wgOutputEncoding;
-global $wgDBname, $wgMemc;
/**
* These are always UTF-8, they exist only for backwards compatibility
function markNoConversion($text, $noParse=false) {return $text;}
function convertCategoryKey( $key ) {return $key; }
function convertLinkToAllVariants($text){ return array( $this->mLang->getCode() => $text); }
- function setNoTitleConvert(){}
+ function armourMath($text){ return $text; }
}
#--------------------------------------------------------------------------
var $mConverter, $mVariants, $mCode, $mLoaded = false;
static public $mLocalisationKeys = array( 'fallback', 'namespaceNames',
- 'quickbarSettings', 'skinNames', 'mathNames',
+ 'skinNames', 'mathNames',
'bookstoreList', 'magicWords', 'messages', 'rtl', 'digitTransformTable',
'separatorTransformTable', 'fallback8bitEncoding', 'linkPrefixExtension',
'defaultUserOptionOverrides', 'linkTrail', 'namespaceAliases',
'dateFormats', 'datePreferences', 'datePreferenceMigrationMap',
- 'defaultDateFormat', 'extraUserToggles' );
+ 'defaultDateFormat', 'extraUserToggles', 'specialPageAliases' );
static public $mMergeableMapKeys = array( 'messages', 'namespaceNames', 'mathNames',
'dateFormats', 'defaultUserOptionOverrides', 'magicWords' );
static public $mMergeableListKeys = array( 'extraUserToggles' );
+ static public $mMergeableAliasListKeys = array( 'specialPageAliases' );
+
static public $mLocalisationCache = array();
static public $mWeekdayMsgs = array(
* Create a language object for a given language code
*/
static function factory( $code ) {
+ global $IP;
static $recursionLevel = 0;
if ( $code == 'en' ) {
return User::getDefaultOptions();
}
+ function getFallbackLanguageCode() {
+ $this->load();
+ return $this->fallback;
+ }
+
/**
* Exports $wgBookstoreListEn
* @return array
}
/**
- * Get a namespace key by value, case insensetive.
+ * Get a namespace key by value, case insensitive.
+ * Only matches namespace names for the current language, not the
+ * canonical ones defined in Namespace.php.
+ *
+ * @param string $text
+ * @return mixed An integer if $text is a valid value otherwise false
+ */
+ function getLocalNsIndex( $text ) {
+ $this->load();
+ $lctext = $this->lc($text);
+ return isset( $this->mNamespaceIds[$lctext] ) ? $this->mNamespaceIds[$lctext] : false;
+ }
+
+ /**
+ * Get a namespace key by value, case insensitive. Canonical namespace
+ * names override custom ones defined for the current language.
*
* @param string $text
* @return mixed An integer if $text is a valid value otherwise false
*/
function getNsIndex( $text ) {
$this->load();
- $index = @$this->mNamespaceIds[$this->lc($text)];
- if ( is_null( $index ) ) {
- return false;
- } else {
- return $index;
- }
+ $lctext = $this->lc($text);
+ if( ( $ns = Namespace::getCanonicalIndex( $lctext ) ) !== null ) return $ns;
+ return isset( $this->mNamespaceIds[$lctext] ) ? $this->mNamespaceIds[$lctext] : false;
}
/**
}
function specialPage( $name ) {
+ $aliases = $this->getSpecialPageAliases();
+ if ( isset( $aliases[$name][0] ) ) {
+ $name = $aliases[$name][0];
+ }
return $this->getNsText(NS_SPECIAL) . ':' . $name;
}
function getQuickbarSettings() {
- $this->load();
- return $this->quickbarSettings;
+ return array(
+ $this->getMessage( 'qbsettings-none' ),
+ $this->getMessage( 'qbsettings-fixedleft' ),
+ $this->getMessage( 'qbsettings-fixedright' ),
+ $this->getMessage( 'qbsettings-floatingleft' ),
+ $this->getMessage( 'qbsettings-floatingright' )
+ );
}
function getSkinNames() {
* Get language names, indexed by code.
* If $customisedOnly is true, only returns codes with a messages file
*/
- function getLanguageNames( $customisedOnly = false ) {
+ public static function getLanguageNames( $customisedOnly = false ) {
global $wgLanguageNames;
if ( !$customisedOnly ) {
return $wgLanguageNames;
$messageFiles = glob( "$IP/languages/messages/Messages*.php" );
$names = array();
foreach ( $messageFiles as $file ) {
+ $m = array();
if( preg_match( '/Messages([A-Z][a-z_]+)\.php$/', $file, $m ) ) {
$code = str_replace( '_', '-', strtolower( $m[1] ) );
if ( isset( $wgLanguageNames[$code] ) ) {
if ( $tz === '' ) {
# Global offset in minutes.
if( isset($wgLocalTZoffset) ) {
- $hrDiff = $wgLocalTZoffset % 60;
- $minDiff = $wgLocalTZoffset - ($hrDiff * 60);
+ if( $wgLocalTZoffset >= 0 ) {
+ $hrDiff = floor($wgLocalTZoffset / 60);
+ } else {
+ $hrDiff = ceil($wgLocalTZoffset / 60);
+ }
+ $minDiff = $wgLocalTZoffset % 60;
}
} elseif ( strpos( $tz, ':' ) !== false ) {
$tzArray = explode( ':', $tz );
*/
function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false) {
$this->load();
+
+ $ts = wfTimestamp( TS_MW, $ts );
+
if ( $adj ) {
$ts = $this->userAdjust( $ts, $timecorrection );
}
function getMessage( $key ) {
$this->load();
- return @$this->messages[$key];
+ return isset( $this->messages[$key] ) ? $this->messages[$key] : null;
}
function getAllMessages() {
function iconv( $in, $out, $string ) {
# For most languages, this is a wrapper for iconv
- return iconv( $in, $out, $string );
+ return iconv( $in, $out . '//IGNORE', $string );
}
// callback functions for uc(), lc(), ucwords(), ucwordbreaks()
}
function uc( $str, $first = false ) {
- if ( function_exists( 'mb_strtoupper' ) )
- if ( $first )
- if ( self::isMultibyte( $str ) )
+ if ( function_exists( 'mb_strtoupper' ) ) {
+ if ( $first ) {
+ if ( self::isMultibyte( $str ) ) {
return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
- else
+ } else {
return ucfirst( $str );
- else
+ }
+ } else {
return self::isMultibyte( $str ) ? mb_strtoupper( $str ) : strtoupper( $str );
- else
+ }
+ } else {
if ( self::isMultibyte( $str ) ) {
list( $wikiUpperChars ) = $this->getCaseMaps();
$x = $first ? '^' : '';
array($this,"ucCallback"),
$str
);
- } else
+ } else {
return $first ? ucfirst( $str ) : strtoupper( $str );
+ }
+ }
}
function lcfirst( $str ) {
$str );
}
+ /**
+ * Return a case-folded representation of $s
+ *
+ * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
+ * and $s2 are the same except for the case of their characters. It is not
+ * necessary for the value returned to make sense when displayed.
+ *
+ * Do *not* perform any other normalisation in this function. If a caller
+ * uses this function when it should be using a more general normalisation
+ * function, then fix the caller.
+ */
+ function caseFold( $s ) {
+ return $this->uc( $s );
+ }
+
function checkTitleEncoding( $s ) {
if( is_array( $s ) ) {
wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
* @return string
*/
function stripForSearch( $string ) {
+ global $wgDBtype;
+ if ( $wgDBtype != 'mysql' ) {
+ return $string;
+ }
+
# MySQL fulltext index doesn't grok utf-8, so we
# need to fold cases and convert to hex
* @return string
*/
function firstChar( $s ) {
+ $matches = array();
preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
'[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/', $s, $matches);
$mw->mSynonyms = array_slice( $rawEntry, 1 );
}
+ /**
+ * Get special page names, as an associative array
+ * case folded alias => real name
+ */
+ function getSpecialPageAliases() {
+ $this->load();
+ if ( !isset( $this->mExtendedSpecialPageAliases ) ) {
+ $this->mExtendedSpecialPageAliases = $this->specialPageAliases;
+ wfRunHooks( 'LangugeGetSpecialPageAliases',
+ array( &$this->mExtendedSpecialPageAliases, $this->getCode() ) );
+ }
+ return $this->mExtendedSpecialPageAliases;
+ }
+
/**
* Italic is unsuitable for some languages
*
return $number;
}
+ function parseFormattedNumber( $number ) {
+ $s = $this->digitTransformTable();
+ if (!is_null($s)) { $number = strtr($number, array_flip($s)); }
+
+ $s = $this->separatorTransformTable();
+ if (!is_null($s)) { $number = strtr($number, array_flip($s)); }
+
+ $number = strtr( $number, array (',' => '') );
+ return $number;
+ }
+
/**
* Adds commas to a given number
*
if( $length > 0 ) {
$string = substr( $string, 0, $length );
$char = ord( $string[strlen( $string ) - 1] );
+ $m = array();
if ($char >= 0xc0) {
# We got the first byte only of a multibyte char; remove it.
$string = substr( $string, 0, -1 );
* @param string $wordform1
* @param string $wordform2
* @param string $wordform3 (optional)
+ * @param string $wordform4 (optional)
+ * @param string $wordform5 (optional)
* @return string
*/
- function convertPlural( $count, $w1, $w2, $w3) {
- return $count == '1' ? $w1 : $w2;
+ function convertPlural( $count, $w1, $w2, $w3, $w4, $w5) {
+ return ( $count == '1' || $count == '-1' ) ? $w1 : $w2;
}
/**
* For translaing of expiry times
* @param string The validated block time in English
+ * @param $forContent, avoid html?
* @return Somehow translated block time
* @see LanguageFi.php for example implementation
*/
- function translateBlockExpiry( $str ) {
+ function translateBlockExpiry( $str, $forContent=false ) {
$scBlockExpiryOptions = $this->getMessageFromDB( 'ipboptions' );
if ( strpos($option, ":") === false )
continue;
list($show, $value) = explode(":", $option);
- if ( strcmp ( $str, $value) == 0 )
- return '<span title="' . htmlspecialchars($str). '">' .
- htmlspecialchars( trim( $show ) ) . '</span>';
+ if ( strcmp ( $str, $value) == 0 ) {
+ if ( $forContent )
+ return htmlspecialchars($str) . htmlspecialchars( trim( $show ) );
+ else
+ return '<span title="' . htmlspecialchars($str). '">' . htmlspecialchars( trim( $show ) ) . '</span>';
+ }
}
return $str;
return $this->mConverter->parserConvert( $text, $parser );
}
- # Tell the converter that it shouldn't convert titles
- function setNoTitleConvert(){
- $this->mConverter->setNotitleConvert();
- }
-
# Check if this is a language with variants
function hasVariants(){
return sizeof($this->getVariants())>1;
}
+ # Put custom tags (e.g. -{ }-) around math to prevent conversion
+ function armourMath($text){
+ return $this->mConverter->armourMath($text);
+ }
+
/**
* Perform output conversion on a string, and encode for safe HTML output.
*/
static function loadLocalisation( $code, $disableCache = false ) {
static $recursionGuard = array();
- global $wgMemc, $wgDBname;
+ global $wgMemc;
if ( !$code ) {
throw new MWException( "Invalid language code requested" );
$cache = wfGetPrecompiledData( self::getFileName( "Messages", $code, '.ser' ) );
if ( $cache ) {
self::$mLocalisationCache[$code] = $cache;
- wfDebug( "Got localisation for $code from precompiled data file\n" );
+ wfDebug( "Language::loadLocalisation(): got localisation for $code from precompiled data file\n" );
wfProfileOut( __METHOD__ );
return self::$mLocalisationCache[$code]['deps'];
}
# Try the global cache
- $memcKey = "$wgDBname:localisation:$code";
+ $memcKey = wfMemcKey('localisation', $code );
$cache = $wgMemc->get( $memcKey );
if ( $cache ) {
- $expired = false;
# Check file modification times
foreach ( $cache['deps'] as $file => $mtime ) {
- if ( filemtime( $file ) > $mtime ) {
- $expired = true;
+ if ( !file_exists( $file ) || filemtime( $file ) > $mtime ) {
break;
}
}
if ( self::isLocalisationOutOfDate( $cache ) ) {
$wgMemc->delete( $memcKey );
$cache = false;
- wfDebug( "Localisation cache for $code had expired due to update of $file\n" );
+ wfDebug( "Language::loadLocalisation(): localisation cache for $code had expired due to update of $file\n" );
} else {
self::$mLocalisationCache[$code] = $cache;
- wfDebug( "Got localisation for $code from cache\n" );
+ wfDebug( "Language::loadLocalisation(): got localisation for $code from cache\n" );
wfProfileOut( __METHOD__ );
return $cache['deps'];
}
wfProfileIn( __METHOD__ );
}
+ # Default fallback, may be overridden when the messages file is included
if ( $code != 'en' ) {
$fallback = 'en';
} else {
$fallback = false;
}
-
+
# Load the primary localisation from the source file
$filename = self::getMessagesFileName( $code );
if ( !file_exists( $filename ) ) {
- wfDebug( "No localisation file for $code, using implicit fallback to en\n" );
+ wfDebug( "Language::loadLocalisation(): no localisation file for $code, using implicit fallback to en\n" );
$cache = array();
$deps = array();
} else {
$deps = array( $filename => filemtime( $filename ) );
require( $filename );
$cache = compact( self::$mLocalisationKeys );
- wfDebug( "Got localisation for $code from source\n" );
+ wfDebug( "Language::loadLocalisation(): got localisation for $code from source\n" );
}
-
+
if ( !empty( $fallback ) ) {
# Load the fallback localisation, with a circular reference guard
if ( isset( $recursionGuard[$code] ) ) {
$cache[$key] = $cache[$key] + $secondary[$key];
} elseif ( in_array( $key, self::$mMergeableListKeys ) ) {
$cache[$key] = array_merge( $secondary[$key], $cache[$key] );
+ } elseif ( in_array( $key, self::$mMergeableAliasListKeys ) ) {
+ $cache[$key] = array_merge_recursive( $cache[$key], $secondary[$key] );
}
}
} else {
}
$expired = false;
foreach ( $cache['deps'] as $file => $mtime ) {
- if ( filemtime( $file ) > $mtime ) {
+ if ( !file_exists( $file ) || filemtime( $file ) > $mtime ) {
$expired = true;
break;
}
*/
static function getMessageFor( $key, $code ) {
self::loadLocalisation( $code );
- return @self::$mLocalisationCache[$code]['messages'][$key];
+ return isset( self::$mLocalisationCache[$code]['messages'][$key] ) ? self::$mLocalisationCache[$code]['messages'][$key] : null;
}
/**
* Do any necessary post-cache-load settings adjustment
*/
function fixUpSettings() {
- global $wgExtraNamespaces, $wgMetaNamespace, $wgMetaNamespaceTalk, $wgMessageCache,
+ global $wgExtraNamespaces, $wgMetaNamespace, $wgMetaNamespaceTalk,
$wgNamespaceAliases, $wgAmericanDates;
wfProfileIn( __METHOD__ );
if ( $wgExtraNamespaces ) {