From 9066709443242752af89979ac7df8ed2474069be Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Wed, 6 May 2009 10:41:43 +0000 Subject: - Patch #267333 by cwgordon7, David_Rothstein, lilou, et al: fixed batch api rounding. --- includes/batch.inc | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'includes') diff --git a/includes/batch.inc b/includes/batch.inc index 1e506abaa..ed29b2b23 100644 --- a/includes/batch.inc +++ b/includes/batch.inc @@ -294,8 +294,9 @@ function _batch_process() { $progress_message = $old_set['progress_message']; } - $current = $total - $remaining + $finished; - $percentage = $total ? floor($current / $total * 100) : 100; + $current = $total - $remaining + $finished; + $percentage = _batch_api_percentage($total, $current); + $elapsed = $current_set['elapsed']; // Estimate remaining with percentage in floating format. $estimate = $elapsed * ($total - $current) / $current; @@ -323,6 +324,34 @@ function _batch_process() { } } +/** + * Helper function for _batch_process(): returns the formatted percentage. + * + * @param $total + * The total number of operations. + * @param $current + * The number of the current operation. + * @return + * The properly formatted percentage, as a string. We output percentages + * using the correct number of decimal places so that we never print "100%" + * until we are finished, but we also never print more decimal places than + * are meaningful. + */ +function _batch_api_percentage($total, $current) { + if (!$total || $total == $current) { + // If $total doesn't evaluate as true or is equal to the current set, then + // we're finished, and we can return "100". + $percentage = "100"; + } + else { + // We add a new digit at 200, 2000, etc. (since, for example, 199/200 + // would round up to 100% if we didn't). + $decimal_places = max(0, floor(log10($total / 2.0)) - 1); + $percentage = sprintf('%01.' . $decimal_places . 'f', round($current / $total * 100, $decimal_places)); + } + return $percentage; +} + /** * Return the batch set being currently processed. */ -- cgit v1.2.3