From 3b06d6fbc98101ec6cfce52432ccb146fbb7d098 Mon Sep 17 00:00:00 2001 From: Sam Reed Date: Sat, 31 Dec 2011 20:20:15 +0000 Subject: [PATCH] Followup r106791, r107692 and bug 33014 Make Language::formatNum() handle TB through YB Rewrote code to be simpler and less indenty Though, something like formatBitrate might be be better in future... We'll see! --- languages/Language.php | 33 ++++++++---------- tests/phpunit/languages/LanguageTest.php | 44 ++++++++++++++---------- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/languages/Language.php b/languages/Language.php index c313182243..95488ef2a8 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -3826,32 +3826,29 @@ class Language { /** * Format a size in bytes for output, using an appropriate - * unit (B, KB, MB or GB) according to the magnitude in question + * unit (B, KB, MB, GB, TB, PB, EB, ZB or YB) according to the magnitude in question * * @param $size int Size to format * @return string Plain text (not HTML) */ function formatSize( $size ) { + $sizes = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' ); + $index = 0; + + $maxIndex = count( $sizes ) - 1; + while ( $size >= 1024 && $index < $maxIndex ) { + $index++; + $size /= 1024; + } + // 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'; + if ( $index > 1 ) { + // For MB and bigger two decimal places are smarter + $round = 2; } + $msg = "size-{$sizes[$index]}bytes"; + $size = round( $size, $round ); $text = $this->getMessageFromDB( $msg ); return str_replace( '$1', $this->formatNum( $size ), $text ); diff --git a/tests/phpunit/languages/LanguageTest.php b/tests/phpunit/languages/LanguageTest.php index 918f931ae3..ba6626a9ba 100644 --- a/tests/phpunit/languages/LanguageTest.php +++ b/tests/phpunit/languages/LanguageTest.php @@ -245,39 +245,45 @@ class LanguageTest extends MediaWikiTestCase { ), array( 1024, - "1,024 B", - "1,024 bytes" - ), - array( - 1024 + 1, "1 KB", "1 kilobyte" ), array( 1024 * 1024, - "1,024 KB", - "1,024 kilobyte" - ), - array( - ( 1024 * 1024 ) + 1, "1 MB", - "1 megabyte" + "1,024 megabytes" ), array( 1024 * 1024 * 1024, - "1,024 MB", - "1,024 megabyte" + "1 GB", + "1 gigabytes" ), array( - ( 1024 * 1024 * 1024 ) + 1, - "1 GB", - "1 gigabyte" + pow( 1024, 4 ), + "1 TB", + "1 terabyte" + ), + array( + pow( 1024, 5 ), + "1 PB", + "1 petabyte" + ), + array( + pow( 1024, 6 ), + "1 EB", + "1,024 exabyte" + ), + array( + pow( 1024, 7 ), + "1 ZB", + "1 zetabyte" ), array( - ( 1024 * 1024 * 1024 * 1024 ) + 1, - "1,024 GB", - "1,024 gigabyte" + pow( 1024, 8 ), + "1 YB", + "1 yottabyte" ), + // How big!? THIS BIG! ); } } -- 2.20.1