From 767a9310e2bd88e26ab3ab92b7cb6a52a935787b Mon Sep 17 00:00:00 2001 From: brettp Date: Sun, 13 Feb 2011 04:34:20 +0000 Subject: Fixed ElggBatch problems when specifying a limit of 0. Fixed a shoot yourself in the foot situation if passing a dumb number to chunk size. git-svn-id: http://code.elgg.org/elgg/trunk@8196 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/classes/ElggBatch.php | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) (limited to 'engine/classes') diff --git a/engine/classes/ElggBatch.php b/engine/classes/ElggBatch.php index bbc4de0bc..a69fb9dc9 100644 --- a/engine/classes/ElggBatch.php +++ b/engine/classes/ElggBatch.php @@ -138,6 +138,10 @@ class ElggBatch $this->callback = $callback; $this->chunkSize = $chunk_size; + if ($this->chunkSize <= 0) { + $this->chunkSize = 25; + } + // store these so we can compare later $this->offset = elgg_get_array_value('offset', $options, 0); $this->limit = elgg_get_array_value('limit', $options, 10); @@ -173,24 +177,24 @@ class ElggBatch return false; } - if ($this->retrievedResults >= $this->limit) { - return false; - } + $limit = $this->chunkSize; - // if original limit < chunk size, set limit to original limit - if ($this->limit < $this->chunkSize) { - $limit = $this->limit; - } + // if someone passed limit = 0 they want everything. + if ($this->limit != 0) { + if ($this->retrievedResults >= $this->limit) { + return false; + } - // if the number of results we'll fetch is greater than the original limit, - // set the limit to the number of results remaining in the original limit - elseif ($this->retrievedResults + $this->chunkSize > $this->limit) { - $limit = $this->limit - $this->retrievedResults; - } + // if original limit < chunk size, set limit to original limit + if ($this->limit < $this->chunkSize) { + $limit = $this->limit; + } - // everything else is the chunk size - else { - $limit = $this->chunkSize; + // if the number of results we'll fetch is greater than the original limit, + // set the limit to the number of results remaining in the original limit + elseif ($this->retrievedResults + $this->chunkSize > $this->limit) { + $limit = $this->limit - $this->retrievedResults; + } } $current_options = array( @@ -199,7 +203,6 @@ class ElggBatch ); $options = array_merge($this->options, $current_options); - $getter = $this->getter; if (is_string($getter)) { @@ -268,7 +271,7 @@ class ElggBatch */ public function next() { // if we'll be at the end. - if ($this->processedResults + 1 >= $this->limit) { + if ($this->processedResults + 1 >= $this->limit && $this->limit > 0) { $this->results = array(); return false; } -- cgit v1.2.3