r108364 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108363‎ | r108364 | r108365 >
Date:20:40, 8 January 2012
Author:reedy
Status:resolved
Tags:
Comment:
Followup r108363 for bug 33571

Refactor out duplicate code
Modified paths:
  • /trunk/phase3/languages/Language.php (modified) (history)

Diff [purge]

Index: trunk/phase3/languages/Language.php
@@ -3816,29 +3816,41 @@
38173817 * @return string
38183818 */
38193819 function formatBitrate( $bps ) {
3820 - if ( $bps <= 0 ) {
3821 - return str_replace( '$1', $this->formatNum( $bps ), $this->getMessageFromDB( 'bitrate-bits' ) );
 3820+ return $this->formatComputingNumbers( $bps, 1000, "bitrate-$1bits" );
 3821+ }
 3822+
 3823+ /**
 3824+ * @param $size int Size of the unit
 3825+ * @param $boundary int Size boundary (1000, or 1024 in most cases)
 3826+ * @param $messageKey string Message key to be uesd
 3827+ * @return string
 3828+ */
 3829+ function formatComputingNumbers( $size, $boundary, $messageKey ) {
 3830+ if ( $size <= 0 ) {
 3831+ return str_replace( '$1', $this->formatNum( $size ),
 3832+ $this->getMessageFromDB( str_replace( '$1', '', $messageKey ) )
 3833+ );
38223834 }
3823 - $units = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' );
 3835+ $sizes = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' );
38243836 $index = 0;
38253837
3826 - $maxIndex = count( $units ) - 1;
3827 - while ( $bps >= 1000 && $index < $maxIndex ) {
 3838+ $maxIndex = count( $sizes ) - 1;
 3839+ while ( $size >= $boundary && $index < $maxIndex ) {
38283840 $index++;
3829 - $bps /= 1000;
 3841+ $size /= $boundary;
38303842 }
38313843
3832 - // For small units no decimal places necessary
 3844+ // For small sizes no decimal places necessary
38333845 $round = 0;
38343846 if ( $index > 1 ) {
38353847 // For MB and bigger two decimal places are smarter
38363848 $round = 2;
38373849 }
3838 - $msg = "bitrate-{$units[$index]}bits";
 3850+ $msg = str_replace( '$1', $messageKey, $sizes[$index] );
38393851
3840 - $bps = round( $bps, $round );
 3852+ $size = round( $size, $round );
38413853 $text = $this->getMessageFromDB( $msg );
3842 - return str_replace( '$1', $this->formatNum( $bps ), $text );
 3854+ return str_replace( '$1', $this->formatNum( $size ), $text );
38433855 }
38443856
38453857 /**
@@ -3849,26 +3861,7 @@
38503862 * @return string Plain text (not HTML)
38513863 */
38523864 function formatSize( $size ) {
3853 - $sizes = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' );
3854 - $index = 0;
3855 -
3856 - $maxIndex = count( $sizes ) - 1;
3857 - while ( $size >= 1024 && $index < $maxIndex ) {
3858 - $index++;
3859 - $size /= 1024;
3860 - }
3861 -
3862 - // For small sizes no decimal places necessary
3863 - $round = 0;
3864 - if ( $index > 1 ) {
3865 - // For MB and bigger two decimal places are smarter
3866 - $round = 2;
3867 - }
3868 - $msg = "size-{$sizes[$index]}bytes";
3869 -
3870 - $size = round( $size, $round );
3871 - $text = $this->getMessageFromDB( $msg );
3872 - return str_replace( '$1', $this->formatNum( $size ), $text );
 3865+ return $this->formatComputingNumbers( $size, 1024, "size-$1bytes" );
38733866 }
38743867
38753868 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r108366Fix str_replace parameter order fail from r108364reedy20:44, 8 January 2012
r108414method documentation for r108364...hashar14:10, 9 January 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r108363* bug 33571: fix yottabits/s in Language::formatBitrate...brion20:15, 8 January 2012

Status & tagging log