r107733 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107732‎ | r107733 | r107734 >
Date:20:20, 31 December 2011
Author:reedy
Status:resolved (Comments)
Tags:
Comment:
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!
Modified paths:
  • /trunk/phase3/languages/Language.php (modified) (history)
  • /trunk/phase3/tests/phpunit/languages/LanguageTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/languages/LanguageTest.php
@@ -245,39 +245,45 @@
246246 ),
247247 array(
248248 1024,
249 - "1,024 B",
250 - "1,024 bytes"
251 - ),
252 - array(
253 - 1024 + 1,
254249 "1 KB",
255250 "1 kilobyte"
256251 ),
257252 array(
258253 1024 * 1024,
259 - "1,024 KB",
260 - "1,024 kilobyte"
261 - ),
262 - array(
263 - ( 1024 * 1024 ) + 1,
264254 "1 MB",
265 - "1 megabyte"
 255+ "1,024 megabytes"
266256 ),
267257 array(
268258 1024 * 1024 * 1024,
269 - "1,024 MB",
270 - "1,024 megabyte"
 259+ "1 GB",
 260+ "1 gigabytes"
271261 ),
272262 array(
273 - ( 1024 * 1024 * 1024 ) + 1,
274 - "1 GB",
275 - "1 gigabyte"
 263+ pow( 1024, 4 ),
 264+ "1 TB",
 265+ "1 terabyte"
276266 ),
277267 array(
278 - ( 1024 * 1024 * 1024 * 1024 ) + 1,
279 - "1,024 GB",
280 - "1,024 gigabyte"
 268+ pow( 1024, 5 ),
 269+ "1 PB",
 270+ "1 petabyte"
281271 ),
 272+ array(
 273+ pow( 1024, 6 ),
 274+ "1 EB",
 275+ "1,024 exabyte"
 276+ ),
 277+ array(
 278+ pow( 1024, 7 ),
 279+ "1 ZB",
 280+ "1 zetabyte"
 281+ ),
 282+ array(
 283+ pow( 1024, 8 ),
 284+ "1 YB",
 285+ "1 yottabyte"
 286+ ),
 287+ // How big!? THIS BIG!
282288 );
283289 }
284290 }
Index: trunk/phase3/languages/Language.php
@@ -3826,32 +3826,29 @@
38273827
38283828 /**
38293829 * Format a size in bytes for output, using an appropriate
3830 - * unit (B, KB, MB or GB) according to the magnitude in question
 3830+ * unit (B, KB, MB, GB, TB, PB, EB, ZB or YB) according to the magnitude in question
38313831 *
38323832 * @param $size int Size to format
38333833 * @return string Plain text (not HTML)
38343834 */
38353835 function formatSize( $size ) {
 3836+ $sizes = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' );
 3837+ $index = 0;
 3838+
 3839+ $maxIndex = count( $sizes ) - 1;
 3840+ while ( $size >= 1024 && $index < $maxIndex ) {
 3841+ $index++;
 3842+ $size /= 1024;
 3843+ }
 3844+
38363845 // For small sizes no decimal places necessary
38373846 $round = 0;
3838 - if ( $size > 1024 ) {
3839 - $size = $size / 1024;
3840 - if ( $size > 1024 ) {
3841 - $size = $size / 1024;
3842 - // For MB and bigger two decimal places are smarter
3843 - $round = 2;
3844 - if ( $size > 1024 ) {
3845 - $size = $size / 1024;
3846 - $msg = 'size-gigabytes';
3847 - } else {
3848 - $msg = 'size-megabytes';
3849 - }
3850 - } else {
3851 - $msg = 'size-kilobytes';
3852 - }
3853 - } else {
3854 - $msg = 'size-bytes';
 3847+ if ( $index > 1 ) {
 3848+ // For MB and bigger two decimal places are smarter
 3849+ $round = 2;
38553850 }
 3851+ $msg = "size-{$sizes[$index]}bytes";
 3852+
38563853 $size = round( $size, $round );
38573854 $text = $this->getMessageFromDB( $msg );
38583855 return str_replace( '$1', $this->formatNum( $size ), $text );

Follow-up revisions

RevisionCommit summaryAuthorDate
r107735bug 33014 and r107734...reedy20:35, 31 December 2011
r107751Fixup and add rest of tests...reedy22:30, 31 December 2011
r107752RELEASE-NOTES-1.19 to go with formatSize/formatBitrate work...reedy22:34, 31 December 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r106791Show the diff helper labels only oncenikerabbit12:52, 20 December 2011
r107692Added size units for tera through yottareedy02:22, 31 December 2011

Comments

#Comment by Duplicatebug (talk | contribs)   22:23, 6 January 2012

Please let grep find that message names. Thanks.

Status & tagging log