summaryrefslogtreecommitdiff
path: root/languages/Language.php
diff options
context:
space:
mode:
authorPierre Schmitz <pierre@archlinux.de>2007-09-14 13:18:58 +0200
committerPierre Schmitz <pierre@archlinux.de>2007-09-14 13:18:58 +0200
commit8f416baead93a48e5799e44b8bd2e2c4859f4e04 (patch)
treecd47ac55eb80a39e3225e8b4f3161b88ea16c2cf /languages/Language.php
parentd7d08bd1a17618c7d77a6b9b2989e9f7293d6ed6 (diff)
auf Version 1.11 aktualisiert; Login-Bug behoben
Diffstat (limited to 'languages/Language.php')
-rw-r--r--languages/Language.php159
1 files changed, 133 insertions, 26 deletions
diff --git a/languages/Language.php b/languages/Language.php
index 1eb09b67..3f9d98fb 100644
--- a/languages/Language.php
+++ b/languages/Language.php
@@ -59,6 +59,7 @@ class FakeConverter {
class Language {
var $mConverter, $mVariants, $mCode, $mLoaded = false;
+ var $mMagicExtensions = array(), $mMagicHookDone = false;
static public $mLocalisationKeys = array( 'fallback', 'namespaceNames',
'skinNames', 'mathNames',
@@ -338,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] ) );
@@ -349,6 +350,7 @@ class Language {
}
}
}
+ closedir( $dir );
return $names;
}
@@ -420,8 +422,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 );
@@ -434,7 +440,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
@@ -442,7 +449,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;
}
/**
@@ -468,6 +479,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
@@ -508,31 +522,31 @@ 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 '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 );
+ $num = gmdate( 'W', $unix );
break;
case 'F':
$s .= $this->getMonthName( substr( $ts, 4, 2 ) );
@@ -548,11 +562,11 @@ class Language {
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 );
+ $num = gmdate( 'L', $unix );
break;
case 'Y':
$num = substr( $ts, 0, 4 );
@@ -588,11 +602,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 );
@@ -1115,8 +1129,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] ) ) {
@@ -1140,6 +1154,24 @@ class Language {
}
/**
+ * 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 );
+ }
+ $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
*/
@@ -1258,13 +1290,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;
@@ -1811,6 +1851,73 @@ 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 );
+ }
}
-?>
+
+