65 private $results = array();
72 private $getter = null;
79 private $chunkSize = 25;
86 private $callback = null;
107 private $retrievedResults = 0;
114 private $resultIndex = 0;
121 private $chunkIndex = 0;
128 private $processedResults = 0;
135 private $validGetter = null;
149 private $incrementOffset =
true;
156 private $incompleteEntities = array();
163 private $totalIncompletes = 0;
188 $inc_offset =
true) {
190 $this->getter = $getter;
192 $this->callback = $callback;
193 $this->chunkSize = $chunk_size;
196 if ($this->chunkSize <= 0) {
197 $this->chunkSize = 25;
206 if ($callback && is_callable($callback)) {
212 $result = call_user_func($callback, $result, $getter,
$options);
214 if (!isset($all_results)) {
215 if ($result ===
true || $result ===
false || $result === null) {
218 $all_results = array();
222 if (($result ===
true || $result ===
false || $result === null) && !is_array($all_results)) {
223 $all_results = $result && $all_results;
229 $this->callbackResult = $all_results;
241 $this->incompleteEntities[] =
$row;
249 private function getNextResultsChunk() {
252 $this->results = array();
254 if (!isset($this->validGetter)) {
255 $this->validGetter = is_callable($this->getter);
258 if (!$this->validGetter) {
262 $limit = $this->chunkSize;
265 if ($this->limit != 0) {
266 if ($this->retrievedResults >= $this->limit) {
272 if ($this->limit < $this->chunkSize) {
273 $limit = $this->limit;
274 } elseif ($this->retrievedResults + $this->chunkSize > $this->limit) {
276 $limit = $this->limit - $this->retrievedResults;
280 if ($this->incrementOffset) {
281 $offset = $this->offset + $this->retrievedResults;
283 $offset = $this->offset + $this->totalIncompletes;
286 $current_options = array(
289 '__ElggBatch' => $this,
292 $options = array_merge($this->options, $current_options);
294 $this->incompleteEntities = array();
295 $this->results = call_user_func($this->getter,
$options);
300 $num_results = count($this->results);
301 $num_incomplete = count($this->incompleteEntities);
303 $this->totalIncompletes += $num_incomplete;
305 if ($this->incompleteEntities) {
307 array_splice($this->results, 0, 0, array_pad(array(), $num_incomplete, null));
309 reset($this->results);
310 for ($i = 0; $i < $num_incomplete; $i++) {
311 next($this->results);
315 if ($this->results) {
319 $this->resultIndex = $num_incomplete;
321 $this->retrievedResults += ($num_results + $num_incomplete);
322 if ($num_results == 0) {
325 return $this->getNextResultsChunk();
343 $this->incrementOffset = (bool) $increment;
357 $this->resultIndex = 0;
358 $this->retrievedResults = 0;
359 $this->processedResults = 0;
362 if ($this->chunkIndex == 0 || $this->limit > $this->chunkSize) {
363 $this->chunkIndex = 0;
364 $this->getNextResultsChunk();
375 return current($this->results);
385 return $this->processedResults;
396 if (($this->processedResults + 1) >= $this->limit && $this->limit > 0) {
397 $this->results = array();
402 if (($this->resultIndex + 1) >= $this->chunkSize) {
403 if (!$this->getNextResultsChunk()) {
404 $this->results = array();
412 $this->resultIndex++;
416 $this->processedResults++;
427 if (!is_array($this->results)) {
431 return (
$key !== null &&
$key !==
false);
__construct($getter, $options, $callback=null, $chunk_size=25, $inc_offset=true)
Batches operations on any elgg_get_*() or compatible function that supports an options array...
valid()
PHP Iterator Interface.
current()
PHP Iterator Interface.
reportIncompleteEntity(stdClass $row)
Tell the process that an entity was incomplete during a fetch.
elgg_extract($key, array $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
rewind()
Implements Iterator.
next()
PHP Iterator Interface.
key()
PHP Iterator Interface.
setIncrementOffset($increment=true)
Increment the offset from the original options array? Setting to false is required for callbacks that...