Localisation updates from Betawiki.
[lhc/web/wiklou.git] / languages / Language.php
index db24767..338c348 100644 (file)
@@ -8,18 +8,6 @@ if( !defined( 'MEDIAWIKI' ) ) {
        exit( 1 );
 }
 
-#
-# In general you should not make customizations in these language files
-# directly, but should use the MediaWiki: special namespace to customize
-# user interface messages through the wiki.
-# See http://meta.wikipedia.org/wiki/MediaWiki_namespace
-#
-# NOTE TO TRANSLATORS: Do not copy this whole file when making translations!
-# A lot of common constants and a base class with inheritable methods are
-# defined here, which should not be redefined. See the other LanguageXx.php
-# files for examples.
-#
-
 # Read language names
 global $wgLanguageNames;
 require_once( dirname(__FILE__) . '/Names.php' ) ;
@@ -59,6 +47,7 @@ class FakeConverter {
 
 class Language {
        var $mConverter, $mVariants, $mCode, $mLoaded = false;
+       var $mMagicExtensions = array(), $mMagicHookDone = false;
 
        static public $mLocalisationKeys = array( 'fallback', 'namespaceNames',
                'skinNames', 'mathNames', 
@@ -101,6 +90,13 @@ class Language {
                'sep', 'oct', 'nov', 'dec'
        );
 
+       static public $mIranianCalendarMonthMsgs = array(
+               'iranian-calendar-m1', 'iranian-calendar-m2', 'iranian-calendar-m3',
+               'iranian-calendar-m4', 'iranian-calendar-m5', 'iranian-calendar-m6',
+               'iranian-calendar-m7', 'iranian-calendar-m8', 'iranian-calendar-m9',
+               'iranian-calendar-m10', 'iranian-calendar-m11', 'iranian-calendar-m12'
+       );
+
        /**
         * Create a language object for a given language code
         */
@@ -228,7 +224,22 @@ 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
@@ -236,6 +247,7 @@ class Language {
        function getNsIndex( $text ) {
                $this->load();
                $lctext = $this->lc($text);
+               if( ( $ns = Namespace::getCanonicalIndex( $lctext ) ) !== null ) return $ns;
                return isset( $this->mNamespaceIds[$lctext] ) ? $this->mNamespaceIds[$lctext] : false;
        }
 
@@ -299,7 +311,12 @@ class Language {
 
        function getDefaultUserOptionOverrides() {
                $this->load();
-               return $this->defaultUserOptionOverrides;
+               # XXX - apparently some languageas get empty arrays, didn't get to it yet -- midom
+               if (is_array($this->defaultUserOptionOverrides)) {
+                       return $this->defaultUserOptionOverrides;
+               } else {
+                       return array();
+               }
        }
 
        function getExtraUserToggles() {
@@ -322,9 +339,9 @@ class Language {
                }
                
                global $IP;
-               $messageFiles = glob( "$IP/languages/messages/Messages*.php" );
                $names = array();
-               foreach ( $messageFiles as $file ) {
+               $dir = opendir( "$IP/languages/messages" );
+               while( false !== ( $file = readdir( $dir ) ) ) {
                        $m = array();
                        if( preg_match( '/Messages([A-Z][a-z_]+)\.php$/', $file, $m ) ) {
                                $code = str_replace( '_', '-', strtolower( $m[1] ) );
@@ -333,6 +350,7 @@ class Language {
                                }
                        }
                }
+               closedir( $dir );
                return $names;
        }
 
@@ -382,6 +400,11 @@ class Language {
                return $this->getMessageFromDB( self::$mWeekdayAbbrevMsgs[$key-1] );
        }
 
+       function getIranianCalendarMonthName( $key ) {
+               return $this->getMessageFromDB( self::$mIranianCalendarMonthMsgs[$key-1] );
+       }
+
+
        /**
         * Used by date() and time() to adjust the time output.
         * @public
@@ -404,8 +427,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 );
@@ -418,7 +445,8 @@ class Language {
                # No difference ? Return time unchanged
                if ( 0 == $hrDiff && 0 == $minDiff ) { return $ts; }
 
-               # Generate an adjusted date
+               wfSuppressWarnings(); // E_STRICT system time bitching
+               # Generate an adjusted date
                $t = mktime( (
                  (int)substr( $ts, 8, 2) ) + $hrDiff, # Hours
                  (int)substr( $ts, 10, 2 ) + $minDiff, # Minutes
@@ -426,7 +454,11 @@ class Language {
                  (int)substr( $ts, 4, 2 ), # Month
                  (int)substr( $ts, 6, 2 ), # Day
                  (int)substr( $ts, 0, 4 ) ); #Year
-               return date( 'YmdHis', $t );
+               
+               $date = date( 'YmdHis', $t );
+               wfRestoreWarnings();
+               
+               return $date;
        }
 
        /**
@@ -444,6 +476,11 @@ class Language {
         *    xx   Literal x
         *    xg   Genitive month name
         *
+        *    xij  j (day number) in Iranian calendar
+        *    xiF  F (month name) in Iranian calendar
+        *    xin  n (month number) in Iranian calendar
+        *    xiY  Y (full year) in Iranian calendar
+        *
         * Characters enclosed in double quotes will be considered literal (with
         * the quotes themselves removed). Unmatched quotes will be considered
         * literal quotes. Example:
@@ -452,6 +489,9 @@ class Language {
         * i's"                   => 20'11"
         *
         * Backslash escaping is also supported.
+        *
+        * Input timestamp is assumed to be pre-normalized to the desired local
+        * time zone, if any.
         * 
         * @param string $format
         * @param string $ts 14-character timestamp
@@ -464,13 +504,18 @@ class Language {
                $roman = false;
                $unix = false;
                $rawToggle = false;
+               $iranian = false;
                for ( $p = 0; $p < strlen( $format ); $p++ ) {
                        $num = false;
                        $code = $format[$p];
                        if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
                                $code .= $format[++$p];
                        }
-                       
+
+                       if ( $code === 'xi' && $p < strlen( $format ) - 1 ) {
+                               $code .= $format[++$p];
+                       }
+
                        switch ( $code ) {
                                case 'xx':
                                        $s .= 'x';
@@ -492,35 +537,43 @@ class Language {
                                        break;
                                case 'D':
                                        if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
-                                       $s .= $this->getWeekdayAbbreviation( date( 'w', $unix ) + 1 );
+                                       $s .= $this->getWeekdayAbbreviation( gmdate( 'w', $unix ) + 1 );
                                        break;
                                case 'j':
                                        $num = intval( substr( $ts, 6, 2 ) );
                                        break;
+                               case 'xij':
+                                       if ( !$iranian ) $iranian = self::tsToIranian( $ts );
+                                       $num = $iranian[2];
+                                       break;
                                case 'l':
                                        if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
-                                       $s .= $this->getWeekdayName( date( 'w', $unix ) + 1 );
+                                       $s .= $this->getWeekdayName( gmdate( 'w', $unix ) + 1 );
                                        break;
                                case 'N':
                                        if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
-                                       $w = date( 'w', $unix );
+                                       $w = gmdate( 'w', $unix );
                                        $num = $w ? $w : 7;
                                        break;
                                case 'w':
                                        if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
-                                       $num = date( 'w', $unix );
+                                       $num = gmdate( 'w', $unix );
                                        break;
                                case 'z':
                                        if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
-                                       $num = date( 'z', $unix );
+                                       $num = gmdate( 'z', $unix );
                                        break;
                                case 'W':
                                        if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
-                                       $num = date( 'W', $unix );
-                                       break;                                  
+                                       $num = gmdate( 'W', $unix );
+                                       break;
                                case 'F':
                                        $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
                                        break;
+                               case 'xiF':
+                                       if ( !$iranian ) $iranian = self::tsToIranian( $ts );
+                                       $s .= $this->getIranianCalendarMonthName( $iranian[1] );
+                                       break;
                                case 'm':
                                        $num = substr( $ts, 4, 2 );
                                        break;
@@ -530,17 +583,25 @@ class Language {
                                case 'n':
                                        $num = intval( substr( $ts, 4, 2 ) );
                                        break;
+                               case 'xin':
+                                       if ( !$iranian ) $iranian = self::tsToIranian( $ts );
+                                       $num = $iranian[1];
+                                       break;
                                case 't':
                                        if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
-                                       $num = date( 't', $unix );
+                                       $num = gmdate( 't', $unix );
                                        break;
                                case 'L':
                                        if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
-                                       $num = date( 'L', $unix );
-                                       break;                                  
+                                       $num = gmdate( 'L', $unix );
+                                       break;
                                case 'Y':
                                        $num = substr( $ts, 0, 4 );
                                        break;
+                               case 'xiY':
+                                       if ( !$iranian ) $iranian = self::tsToIranian( $ts );
+                                       $num = $iranian[0];
+                                       break;
                                case 'y':
                                        $num = substr( $ts, 2, 2 );
                                        break;
@@ -572,11 +633,11 @@ class Language {
                                        break;
                                case 'c':
                                        if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
-                                       $s .= date( 'c', $unix );
+                                       $s .= gmdate( 'c', $unix );
                                        break;
                                case 'r':
                                        if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
-                                       $s .= date( 'r', $unix );
+                                       $s .= gmdate( 'r', $unix );
                                        break;
                                case 'U':
                                        if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
@@ -625,6 +686,64 @@ class Language {
                return $s;
        }
 
+       private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
+       private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
+       /**
+        * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert 
+        * Gregorian dates to Iranian dates. Originally written in C, it
+        * is released under the terms of GNU Lesser General Public
+        * License. Conversion to PHP was performed by Niklas Laxström.
+        * 
+        * Link: http://www.farsiweb.info/jalali/jalali.c
+        */
+       private static function tsToIranian( $ts ) {
+               $gy = substr( $ts, 0, 4 ) -1600;
+               $gm = substr( $ts, 4, 2 ) -1;
+               $gd = substr( $ts, 6, 2 ) -1;
+
+               # Days passed from the beginning (including leap years)
+               $gDayNo = 365*$gy
+                       + floor(($gy+3) / 4)
+                       - floor(($gy+99) / 100)
+                       + floor(($gy+399) / 400);
+
+
+               // Add days of the past months of this year
+               for( $i = 0; $i < $gm; $i++ ) {
+                       $gDayNo += self::$GREG_DAYS[$i];
+               }
+
+               // Leap years
+               if ( $gm > 1 && (($gy%4===0 && $gy%100!==0 || ($gy%400==0)))) {
+                       $gDayNo++;
+               }
+
+               // Days passed in current month
+               $gDayNo += $gd;
+               
+               $jDayNo = $gDayNo - 79;
+
+               $jNp = floor($jDayNo / 12053);
+               $jDayNo %= 12053;
+
+               $jy = 979 + 33*$jNp + 4*floor($jDayNo/1461);
+               $jDayNo %= 1461;
+
+               if ( $jDayNo >= 366 ) {
+                       $jy += floor(($jDayNo-1)/365);
+                       $jDayNo = floor(($jDayNo-1)%365);
+               }
+
+               for ( $i = 0; $i < 11 && $jDayNo >= self::$IRANIAN_DAYS[$i]; $i++ ) {
+                       $jDayNo -= self::$IRANIAN_DAYS[$i];
+               }
+
+               $jm= $i+1;
+               $jd= $jDayNo+1;
+
+               return array($jy, $jm, $jd);
+       }
+
        /**
         * Roman number formatting up to 3000
         */
@@ -754,10 +873,7 @@ class Language {
        function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false) {
                $this->load();
 
-               ## Account for non-integer timestamps
-               if (substr($ts,4,1) === '-') {
-                       $ts = preg_replace('/\D/', '', $ts);
-               }
+               $ts = wfTimestamp( TS_MW, $ts );
 
                if ( $adj ) { 
                        $ts = $this->userAdjust( $ts, $timecorrection ); 
@@ -1102,8 +1218,8 @@ class Language {
 
        # Fill a MagicWord object with data from here
        function getMagic( &$mw ) {
-               if ( !isset( $this->mMagicExtensions ) ) {
-                       $this->mMagicExtensions = array();
+               if ( !$this->mMagicHookDone ) {
+                       $this->mMagicHookDone = true;
                        wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions, $this->getCode() ) );
                }
                if ( isset( $this->mMagicExtensions[$mw->mId] ) ) {
@@ -1126,6 +1242,27 @@ class Language {
                $mw->mSynonyms = array_slice( $rawEntry, 1 );
        }
 
+       /**
+        * Add magic words to the extension array
+        */
+       function addMagicWordsByLang( $newWords ) {
+               $code = $this->getCode();
+               $fallbackChain = array();
+               while ( $code && !in_array( $code, $fallbackChain ) ) {
+                       $fallbackChain[] = $code;
+                       $code = self::getFallbackFor( $code );
+               }
+               if ( !in_array( 'en', $fallbackChain ) ) {
+                       $fallbackChain[] = 'en';
+               }
+               $fallbackChain = array_reverse( $fallbackChain );
+               foreach ( $fallbackChain as $code ) {
+                       if ( isset( $newWords[$code] ) ) {
+                               $this->mMagicExtensions = $newWords[$code] + $this->mMagicExtensions;
+                       }
+               }
+       }
+
        /**
         * Get special page names, as an associative array
         *   case folded alias => real name
@@ -1134,7 +1271,7 @@ class Language {
                $this->load();
                if ( !isset( $this->mExtendedSpecialPageAliases ) ) {
                        $this->mExtendedSpecialPageAliases = $this->specialPageAliases;
-                       wfRunHooks( 'LangugeGetSpecialPageAliases', 
+                       wfRunHooks( 'LanguageGetSpecialPageAliases', 
                                array( &$this->mExtendedSpecialPageAliases, $this->getCode() ) );
                }
                return $this->mExtendedSpecialPageAliases;
@@ -1245,13 +1382,21 @@ class Language {
                return $s;
        }
 
-       # Crop a string from the beginning or end to a certain number of bytes.
-       # (Bytes are used because our storage has limited byte lengths for some
-       # columns in the database.) Multibyte charsets will need to make sure that
-       # only whole characters are included!
-       #
-       # $length does not include the optional ellipsis.
-       # If $length is negative, snip from the beginning
+       /**
+        * Truncate a string to a specified length in bytes, appending an optional
+        * string (e.g. for ellipses)
+        *
+        * The database offers limited byte lengths for some columns in the database;
+        * multi-byte character sets mean we need to ensure that only whole characters
+        * are included, otherwise broken characters can be passed to the user
+        *
+        * If $length is negative, the string will be truncated from the beginning
+        *       
+        * @param string $string String to truncate
+        * @param int $length Maximum length (excluding ellipses)
+        * @param string $ellipses String to append to the truncated text
+        * @return string
+        */
        function truncate( $string, $length, $ellipsis = "" ) {
                if( $length == 0 ) {
                        return $ellipsis;
@@ -1302,7 +1447,7 @@ class Language {
 
        /**
         * Plural form transformations, needed for some languages.
-        * For example, where are 3 form of plural in Russian and Polish,
+        * For example, there are 3 form of plural in Russian and Polish,
         * depending on "count mod 10". See [[w:Plural]]
         * For English it is pretty simple.
         *
@@ -1326,11 +1471,10 @@ class Language {
        /**
         * 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, $forContent=false ) {
+       function translateBlockExpiry( $str ) {
 
                $scBlockExpiryOptions = $this->getMessageFromDB( 'ipboptions' );
 
@@ -1343,10 +1487,7 @@ class Language {
                                continue;
                        list($show, $value) = explode(":", $option);
                        if ( strcmp ( $str, $value) == 0 ) {
-                               if ( $forContent )
-                                       return htmlspecialchars($str) . htmlspecialchars( trim( $show ) );
-                               else
-                                       return '<span title="' . htmlspecialchars($str). '">' . htmlspecialchars( trim( $show ) ) . '</span>';
+                               return htmlspecialchars( trim( $show ) );
                        }
                }
 
@@ -1798,6 +1939,70 @@ class Language {
                wfProfileOut( __METHOD__ );
                return array( $wikiUpperChars, $wikiLowerChars );
        }
-}
 
-?>
+       function formatTimePeriod( $seconds ) {
+               if ( $seconds < 10 ) {
+                       return $this->formatNum( sprintf( "%.1f", $seconds ) ) . wfMsg( 'seconds-abbrev' );
+               } elseif ( $seconds < 60 ) {
+                       return $this->formatNum( round( $seconds ) ) . wfMsg( 'seconds-abbrev' );
+               } elseif ( $seconds < 3600 ) {
+                       return $this->formatNum( floor( $seconds / 60 ) ) . wfMsg( 'minutes-abbrev' ) . 
+                               $this->formatNum( round( fmod( $seconds, 60 ) ) ) . wfMsg( 'seconds-abbrev' );
+               } else {
+                       $hours = floor( $seconds / 3600 );
+                       $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
+                       $secondsPart = round( $seconds - $hours * 3600 - $minutes * 60 );
+                       return $this->formatNum( $hours ) . wfMsg( 'hours-abbrev' ) . 
+                               $this->formatNum( $minutes ) . wfMsg( 'minutes-abbrev' ) .
+                               $this->formatNum( $secondsPart ) . wfMsg( 'seconds-abbrev' );
+               }
+       }
+
+       function formatBitrate( $bps ) {
+               $units = array( 'bps', 'kbps', 'Mbps', 'Gbps' );
+               if ( $bps <= 0 ) {
+                       return $this->formatNum( $bps ) . $units[0];
+               }
+               $unitIndex = floor( log10( $bps ) / 3 );
+               $mantissa = $bps / pow( 1000, $unitIndex );
+               if ( $mantissa < 10 ) {
+                       $mantissa = round( $mantissa, 1 );
+               } else {
+                       $mantissa = round( $mantissa );
+               }
+               return $this->formatNum( $mantissa ) . $units[$unitIndex];
+       }
+
+       /**
+        * Format a size in bytes for output, using an appropriate
+        * unit (B, KB, MB or GB) according to the magnitude in question
+        *
+        * @param $size Size to format
+        * @return string Plain text (not HTML)
+        */
+       function formatSize( $size ) {
+               // For small sizes no decimal places necessary
+               $round = 0;
+               if( $size > 1024 ) {
+                       $size = $size / 1024;
+                       if( $size > 1024 ) {
+                               $size = $size / 1024;
+                               // For MB and bigger two decimal places are smarter
+                               $round = 2;
+                               if( $size > 1024 ) {
+                                       $size = $size / 1024;
+                                       $msg = 'size-gigabytes';
+                               } else {
+                                       $msg = 'size-megabytes';
+                               }
+                       } else {
+                               $msg = 'size-kilobytes';
+                       }
+               } else {
+                       $msg = 'size-bytes';
+               }
+               $size = round( $size, $round );
+               $text = $this->getMessageFromDB( $msg );
+               return str_replace( '$1', $this->formatNum( $size ), $text );
+       }
+}