Followup r106791, r107692 and bug 33014
authorSam Reed <reedy@users.mediawiki.org>
Sat, 31 Dec 2011 20:20:15 +0000 (20:20 +0000)
committerSam Reed <reedy@users.mediawiki.org>
Sat, 31 Dec 2011 20:20:15 +0000 (20:20 +0000)
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
tests/phpunit/languages/LanguageTest.php

index c313182..95488ef 100644 (file)
@@ -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 );
index 918f931..ba6626a 100644 (file)
@@ -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!
                );
        }
 }