summaryrefslogtreecommitdiff
path: root/includes/batch.inc
diff options
context:
space:
mode:
Diffstat (limited to 'includes/batch.inc')
-rw-r--r--includes/batch.inc33
1 files changed, 31 insertions, 2 deletions
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;
@@ -324,6 +325,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.
*/
function &_batch_current_set() {