* Some updates
[lhc/web/wiklou.git] / languages / Language.php
index 2a8fb99..d36b549 100644 (file)
@@ -1,7 +1,6 @@
 <?php
 /**
- * @package MediaWiki
- * @subpackage Language
+ * @addtogroup Language
  */
 
 if( !defined( 'MEDIAWIKI' ) ) {
@@ -23,10 +22,9 @@ 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
@@ -49,9 +47,10 @@ class FakeConverter {
        function findVariantLink(&$l, &$n) {}
        function getExtraHashOptions() {return '';}
        function getParsedTitle() {return '';}
-       function markNoConversion($text) {return $text;}
+       function markNoConversion($text, $noParse=false) {return $text;}
        function convertCategoryKey( $key ) {return $key; }
-
+       function convertLinkToAllVariants($text){ return array( $this->mLang->getCode() => $text); }
+       function armourMath($text){ return $text; }
 }
 
 #--------------------------------------------------------------------------
@@ -62,18 +61,20 @@ class Language {
        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(
@@ -112,11 +113,11 @@ class Language {
                } else {
                        $class = 'Language' . str_replace( '-', '_', ucfirst( $code ) );
                        // Preload base classes to work around APC/PHP5 bug
-                       if ( file_exists( "$IP/languages/$class.deps.php" ) ) {
-                               include_once("$IP/languages/$class.deps.php");
+                       if ( file_exists( "$IP/languages/classes/$class.deps.php" ) ) {
+                               include_once("$IP/languages/classes/$class.deps.php");
                        }
-                       if ( file_exists( "$IP/languages/$class.php" ) ) {
-                               include_once("$IP/languages/$class.php");
+                       if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
+                               include_once("$IP/languages/classes/$class.php");
                        }
                }
 
@@ -161,6 +162,11 @@ class Language {
                return User::getDefaultOptions();
        }
 
+       function getFallbackLanguageCode() {
+               $this->load();
+               return $this->fallback;
+       }
+
        /**
         * Exports $wgBookstoreListEn
         * @return array
@@ -222,19 +228,31 @@ class Language {
        }
 
        /**
-        * 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;
        }
 
        /**
@@ -244,16 +262,25 @@ class Language {
         * @return string
         */
        function getVariantname( $code ) {
-               return wfMsg( "variantname-$code" );
+               return $this->getMessageFromDB( "variantname-$code" );
        }
 
        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() {
@@ -276,6 +303,11 @@ class Language {
                return $this->dateFormats;
        }
 
+       function getDefaultDateFormat() {
+               $this->load();
+               return $this->defaultDateFormat;
+       }
+
        function getDatePreferenceMigrationMap() {
                $this->load();
                return $this->datePreferenceMigrationMap;
@@ -292,23 +324,24 @@ class Language {
        }
 
        function getUserToggle( $tog ) {
-               return wfMsg( "tog-$tog" );
+               return $this->getMessageFromDB( "tog-$tog" );
        }
 
        /**
         * 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;
                }
                
                global $IP;
-               $messageFiles = glob( "$IP/languages/Messages*.php" );
+               $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] ) ) {
@@ -320,8 +353,8 @@ class Language {
        }
 
        /**
-        * Ugly hack to get a message maybe from the MediaWiki namespace, if this 
-        * language object is the content or user language. 
+        * Ugly hack to get a message maybe from the MediaWiki namespace, if this
+        * language object is the content or user language.
         */
        function getMessageFromDB( $msg ) {
                global $wgContLang, $wgLang;
@@ -387,8 +420,12 @@ class Language {
                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 );
@@ -417,10 +454,12 @@ class Language {
         * internationalisation, a reduced set of format characters, and a better 
         * escaping format.
         *
-        * Supported format characters are dDjlFmMnYyHis. See the PHP manual for 
-        * definitions. There are a number of extensions, which start with "x":
+        * Supported format characters are dDjlNwzWFmMntLYyaAgGhHiscrU. See the 
+        * PHP manual for definitions. There are a number of extensions, which 
+        * start with "x":
         *
         *    xn   Do not translate digits of the next numeric format character
+        *    xN   Toggle raw digit (xn) flag, stays set until explicitly unset
         *    xr   Use roman numerals for the next numeric format character
         *    xx   Literal x
         *    xg   Genitive month name
@@ -443,6 +482,8 @@ class Language {
                $s = '';
                $raw = false;
                $roman = false;
+               $unix = false;
+               $rawToggle = false;
                for ( $p = 0; $p < strlen( $format ); $p++ ) {
                        $num = false;
                        $code = $format[$p];
@@ -457,6 +498,9 @@ class Language {
                                case 'xn':
                                        $raw = true;
                                        break;
+                               case 'xN':
+                                       $rawToggle = !$rawToggle;
+                                       break;
                                case 'xr':
                                        $roman = true;
                                        break;
@@ -467,14 +511,33 @@ class Language {
                                        $num = substr( $ts, 6, 2 );
                                        break;
                                case 'D':
-                                       $s .= $this->getWeekdayAbbreviation( self::calculateWeekday( $ts ) );
+                                       if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
+                                       $s .= $this->getWeekdayAbbreviation( date( 'w', $unix ) + 1 );
                                        break;
                                case 'j':
                                        $num = intval( substr( $ts, 6, 2 ) );
                                        break;
                                case 'l':
-                                       $s .= $this->getWeekdayName( self::calculateWeekday( $ts ) );
+                                       if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
+                                       $s .= $this->getWeekdayName( date( 'w', $unix ) + 1 );
+                                       break;
+                               case 'N':
+                                       if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
+                                       $w = date( 'w', $unix );
+                                       $num = $w ? $w : 7;
                                        break;
+                               case 'w':
+                                       if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
+                                       $num = date( 'w', $unix );
+                                       break;
+                               case 'z':
+                                       if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
+                                       $num = date( 'z', $unix );
+                                       break;
+                               case 'W':
+                                       if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
+                                       $num = date( 'W', $unix );
+                                       break;                                  
                                case 'F':
                                        $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
                                        break;
@@ -487,24 +550,58 @@ class Language {
                                case 'n':
                                        $num = intval( substr( $ts, 4, 2 ) );
                                        break;
+                               case 't':
+                                       if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
+                                       $num = date( 't', $unix );
+                                       break;
+                               case 'L':
+                                       if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
+                                       $num = date( 'L', $unix );
+                                       break;                                  
                                case 'Y':
                                        $num = substr( $ts, 0, 4 );
                                        break;
                                case 'y':
                                        $num = substr( $ts, 2, 2 );
                                        break;
-                               case 'H':
-                                       $num = substr( $ts, 8, 2 );
+                               case 'a':
+                                       $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'am' : 'pm';
+                                       break;
+                               case 'A':
+                                       $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'AM' : 'PM';
+                                       break;
+                               case 'g':
+                                       $h = substr( $ts, 8, 2 );
+                                       $num = $h % 12 ? $h % 12 : 12;
                                        break;
                                case 'G':
                                        $num = intval( substr( $ts, 8, 2 ) );
                                        break;
+                               case 'h':
+                                       $h = substr( $ts, 8, 2 );
+                                       $num = sprintf( '%02d', $h % 12 ? $h % 12 : 12 );
+                                       break;                                  
+                               case 'H':
+                                       $num = substr( $ts, 8, 2 );
+                                       break;
                                case 'i':
                                        $num = substr( $ts, 10, 2 );
                                        break;
                                case 's':
                                        $num = substr( $ts, 12, 2 );
                                        break;
+                               case 'c':
+                                       if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
+                                       $s .= date( 'c', $unix );
+                                       break;
+                               case 'r':
+                                       if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
+                                       $s .= date( 'r', $unix );
+                                       break;
+                               case 'U':
+                                       if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
+                                       $num = $unix;
+                                       break;
                                case '\\':
                                        # Backslash escaping
                                        if ( $p < strlen( $format ) - 1 ) {
@@ -533,11 +630,11 @@ class Language {
                                        $s .= $format[$p];
                        }
                        if ( $num !== false ) {
-                               if ( $raw ) {
+                               if ( $rawToggle || $raw ) {
                                        $s .= $num;
                                        $raw = false;
                                } elseif ( $roman ) {
-                                       $s .= Language::romanNumeral( $num );
+                                       $s .= self::romanNumeral( $num );
                                        $roman = false;
                                } else {
                                        $s .= $this->formatNum( $num, true );
@@ -549,35 +646,31 @@ class Language {
        }
 
        /**
-        * Roman number formatting up to 100
+        * Roman number formatting up to 3000
         */
        static function romanNumeral( $num ) {
-               static $units = array( 0, 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' );
-               static $decades = array( 0, 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' );
+               static $table = array(
+                       array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
+                       array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
+                       array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
+                       array( '', 'M', 'MM', 'MMM' )
+               );
+                       
                $num = intval( $num );
-               if ( $num > 100 || $num <= 0 ) {
+               if ( $num > 3000 || $num <= 0 ) {
                        return $num;
                }
+
                $s = '';
-               if ( $num >= 10 ) {
-                       $s .= $decades[floor( $num / 10 )];
-                       $num = $num % 10;
-               }
-               if ( $num >= 1 ) {
-                       $s .= $units[$num];
+               for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
+                       if ( $num >= $pow10 ) {
+                               $s .= $table[$i][floor($num / $pow10)];
+                       }
+                       $num = $num % $pow10;
                }
                return $s;
        }
 
-       /**
-        * Calculate the day of the week for a 14-character timestamp
-        * 1 for Sunday through to 7 for Saturday
-        * This takes about 100us on a slow computer
-        */
-       static function calculateWeekday( $ts ) {
-               return date( 'w', wfTimestamp( TS_UNIX, $ts ) ) + 1;
-       }
-
        /**
         * This is meant to be used by time(), date(), and timeanddate() to get
         * the date preference they're supposed to use, it should be used in
@@ -680,6 +773,9 @@ class Language {
        */
        function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false) {
                $this->load();
+
+               $ts = wfTimestamp( TS_MW, $ts );
+
                if ( $adj ) { 
                        $ts = $this->userAdjust( $ts, $timecorrection ); 
                }
@@ -694,7 +790,7 @@ class Language {
 
        function getMessage( $key ) {
                $this->load();
-               return @$this->messages[$key];
+               return isset( $this->messages[$key] ) ? $this->messages[$key] : null;
        }
 
        function getAllMessages() {
@@ -704,7 +800,35 @@ class Language {
 
        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 ucwordbreaksCallbackAscii($matches){
+               return $this->ucfirst($matches[1]);
+       }
+       
+       function ucwordbreaksCallbackMB($matches){
+               return mb_strtoupper($matches[0]);
+       }
+       
+       function ucCallback($matches){
+               list( $wikiUpperChars ) = self::getCaseMaps();
+               return strtr( $matches[1], $wikiUpperChars );
+       }
+       
+       function lcCallback($matches){
+               list( , $wikiLowerChars ) = self::getCaseMaps();
+               return strtr( $matches[1], $wikiLowerChars );
+       }
+       
+       function ucwordsCallbackMB($matches){
+               return mb_strtoupper($matches[0]);
+       }
+       
+       function ucwordsCallbackWiki($matches){
+               list( $wikiUpperChars ) = self::getCaseMaps();
+               return strtr( $matches[0], $wikiUpperChars );
        }
 
        function ucfirst( $str ) {
@@ -712,25 +836,29 @@ class Language {
        }
 
        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 ? '^' : '';
-                               return preg_replace(
-                                       "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/e",
-                                       "strtr( \"\$1\" , \$wikiUpperChars )",
+                               return preg_replace_callback(
+                                       "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
+                                       array($this,"ucCallback"),
                                        $str
                                );
-                       } else
+                       } else {
                                return $first ? ucfirst( $str ) : strtoupper( $str );
+                       }
+               }
        }
        
        function lcfirst( $str ) {
@@ -750,9 +878,9 @@ class Language {
                        if ( self::isMultibyte( $str ) ) {
                                list( , $wikiLowerChars ) = self::getCaseMaps();
                                $x = $first ? '^' : '';
-                               return preg_replace(
-                                       "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/e",
-                                       "strtr( \"\$1\" , \$wikiLowerChars )",
+                               return preg_replace_callback(
+                                       "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
+                                       array($this,"lcCallback"),
                                        $str
                                );
                        } else
@@ -760,7 +888,78 @@ class Language {
        }
 
        function isMultibyte( $str ) {
-               return (bool)preg_match( '/^[\x80-\xff]/', $str );
+               return (bool)preg_match( '/[\x80-\xff]/', $str );
+       }
+
+       function ucwords($str) {
+               if ( self::isMultibyte( $str ) ) {
+                       $str = self::lc($str);
+
+                       // regexp to find first letter in each word (i.e. after each space)
+                       $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
+
+                       // function to use to capitalize a single char
+                       if ( function_exists( 'mb_strtoupper' ) )
+                               return preg_replace_callback(
+                                       $replaceRegexp,
+                                       array($this,"ucwordsCallbackMB"),
+                                       $str
+                               );
+                       else 
+                               return preg_replace_callback(
+                                       $replaceRegexp,
+                                       array($this,"ucwordsCallbackWiki"),
+                                       $str
+                               );
+               }
+               else
+                       return ucwords( strtolower( $str ) );
+       }
+
+  # capitalize words at word breaks
+       function ucwordbreaks($str){
+               if (self::isMultibyte( $str ) ) {
+                       $str = self::lc($str);
+
+                       // since \b doesn't work for UTF-8, we explicitely define word break chars
+                       $breaks= "[ \-\(\)\}\{\.,\?!]";
+
+                       // find first letter after word break
+                       $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
+
+                       if ( function_exists( 'mb_strtoupper' ) )
+                               return preg_replace_callback(
+                                       $replaceRegexp,
+                                       array($this,"ucwordbreaksCallbackMB"),
+                                       $str
+                               );
+                       else 
+                               return preg_replace_callback(
+                                       $replaceRegexp,
+                                       array($this,"ucwordsCallbackWiki"),
+                                       $str
+                               );
+               }
+               else
+                       return preg_replace_callback(
+                       '/\b([\w\x80-\xff]+)\b/',
+                       array($this,"ucwordbreaksCallbackAscii"),
+                       $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 ) {
@@ -792,6 +991,11 @@ class Language {
         * @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
 
@@ -825,6 +1029,7 @@ class Language {
         * @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);
 
@@ -931,10 +1136,27 @@ class Language {
                        }
                }
 
+               if( !is_array( $rawEntry ) ) {
+                       error_log( "\"$rawEntry\" is not a valid magic thingie for \"$mw->mId\"" );
+               }
                $mw->mCaseSensitive = $rawEntry[0];
                $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
         *
@@ -987,6 +1209,17 @@ class Language {
                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
         *
@@ -1021,7 +1254,7 @@ class Language {
                        if ($i == $m) {
                                $s = $l[$i];
                        } else if ($i == $m - 1) {
-                               $s = $l[$i] . ' ' . wfMsg('and') . ' ' . $s;
+                               $s = $l[$i] . ' ' . $this->getMessageFromDB( 'and' ) . ' ' . $s;
                        } else {
                                $s = $l[$i] . ', ' . $s;
                        }
@@ -1046,6 +1279,7 @@ class Language {
                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 );
@@ -1098,21 +1332,24 @@ class Language {
         * @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 = wfMsg( 'ipboptions' );
+               $scBlockExpiryOptions = $this->getMessageFromDB( 'ipboptions' );
 
                if ( $scBlockExpiryOptions == '-') {
                        return $str;
@@ -1122,9 +1359,12 @@ class Language {
                        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;
@@ -1161,6 +1401,17 @@ class Language {
                return $this->mConverter->parserConvert( $text, $parser );
        }
 
+       # 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.
         * @param string $text
@@ -1205,6 +1456,17 @@ class Language {
                $this->mConverter->findVariantLink($link, $nt);
        }
 
+       /**
+        * If a language supports multiple variants, converts text
+        * into an array of all possible variants of the text:
+        *  'variant' => text in that variant
+        */
+
+       function convertLinkToAllVariants($text){
+               return $this->mConverter->convertLinkToAllVariants($text);
+       }
+
+
        /**
         * returns language specific options used by User::getPageRenderHash()
         * for example, the preferred language variant
@@ -1234,8 +1496,8 @@ class Language {
         * @param string $text text to be tagged for no conversion
         * @return string the tagged text
        */
-       function markNoConversion( $text ) {
-               return $this->mConverter->markNoConversion( $text );
+       function markNoConversion( $text, $noParse=false ) {
+               return $this->mConverter->markNoConversion( $text, $noParse );
        }
 
        /**
@@ -1269,6 +1531,16 @@ class Language {
                return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
        }
 
+       static function getMessagesFileName( $code ) {
+               global $IP;
+               return self::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
+       }
+
+       static function getClassFileName( $code ) {
+               global $IP;
+               return self::getFileName( "$IP/languages/classes/Language", $code, '.php' );
+       }
+       
        static function getLocalisationArray( $code, $disableCache = false ) {
                self::loadLocalisation( $code, $disableCache );
                return self::$mLocalisationCache[$code];
@@ -1281,7 +1553,7 @@ class Language {
         */
        static function loadLocalisation( $code, $disableCache = false ) {
                static $recursionGuard = array();
-               global $wgMemc, $wgDBname, $IP;
+               global $wgMemc;
 
                if ( !$code ) {
                        throw new MWException( "Invalid language code requested" );
@@ -1299,30 +1571,28 @@ class Language {
                        $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'];
                                }
@@ -1331,33 +1601,33 @@ class Language {
                        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
-               global $IP;
-               $filename = self::getFileName( "$IP/languages/Messages", $code, '.php' );
+               $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] ) ) {
                                throw new MWException( "Error: Circular fallback reference in language code $code" );
                        }
                        $recursionGuard[$code] = true;
-                       $newDeps = self::loadLocalisation( $fallback );
+                       $newDeps = self::loadLocalisation( $fallback, $disableCache );
                        unset( $recursionGuard[$code] );
 
                        $secondary = self::$mLocalisationCache[$fallback];
@@ -1371,6 +1641,8 @@ class Language {
                                                        $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 {
@@ -1390,6 +1662,9 @@ class Language {
                # Add dependencies to the cache entry
                $cache['deps'] = $deps;
 
+               # Replace spaces with underscores in namespace names
+               $cache['namespaceNames'] = str_replace( ' ', '_', $cache['namespaceNames'] );
+               
                # Save to both caches
                self::$mLocalisationCache[$code] = $cache;
                if ( !$disableCache ) {
@@ -1415,7 +1690,7 @@ class Language {
                }
                $expired = false;
                foreach ( $cache['deps'] as $file => $mtime ) {
-                       if ( filemtime( $file ) > $mtime ) {
+                       if ( !file_exists( $file ) || filemtime( $file ) > $mtime ) {
                                $expired = true;
                                break;
                        }
@@ -1439,6 +1714,14 @@ class Language {
                return self::$mLocalisationCache[$code]['messages'];
        }
 
+       /** 
+        * Get a message for a given language
+        */
+       static function getMessageFor( $key, $code ) {
+               self::loadLocalisation( $code );
+               return isset( self::$mLocalisationCache[$code]['messages'][$key] ) ? self::$mLocalisationCache[$code]['messages'][$key] : null;
+       }
+
        /**
         * Load localisation data for this object
         */
@@ -1459,7 +1742,7 @@ class Language {
         * 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 ) {
@@ -1518,7 +1801,6 @@ class Language {
 
        static function getCaseMaps() {
                static $wikiUpperChars, $wikiLowerChars;
-               global $IP;
                if ( isset( $wikiUpperChars ) ) {
                        return array( $wikiUpperChars, $wikiLowerChars );
                }