80 private $results = [];
87 private $getter = null;
94 private $options = [];
101 private $chunkSize = 25;
108 private $callback = null;
129 private $retrievedResults = 0;
136 private $resultIndex = 0;
143 private $chunkIndex = 0;
150 private $processedResults = 0;
157 private $validGetter = null;
171 private $incrementOffset =
true;
178 private $incompleteEntities = [];
185 private $totalIncompletes = 0;
209 public function __construct(callable $getter, $options, $callback = null, $chunk_size = 25,
210 $inc_offset =
true) {
212 $this->getter = $getter;
213 $this->options = $options;
214 $this->callback = $callback;
215 $this->chunkSize = $chunk_size;
218 if ($this->chunkSize <= 0) {
219 $this->chunkSize = 25;
228 if ($callback && is_callable($callback)) {
229 $batch = new \ElggBatch($getter, $options, null, $chunk_size, $inc_offset);
234 $result = call_user_func($callback, $result, $getter, $options);
236 if (!isset($all_results)) {
237 if ($result ===
true || $result ===
false || $result === null) {
244 if (($result ===
true || $result ===
false || $result === null) && !is_array($all_results)) {
245 $all_results = $result && $all_results;
251 $this->callbackResult = $all_results;
260 private function getNextResultsChunk() {
265 if (!isset($this->validGetter)) {
266 $this->validGetter = is_callable($this->getter);
269 if (!$this->validGetter) {
273 $limit = $this->chunkSize;
276 if ($this->limit != 0) {
277 if ($this->retrievedResults >= $this->limit) {
283 if ($this->limit < $this->chunkSize) {
284 $limit = $this->limit;
285 }
elseif ($this->retrievedResults + $this->chunkSize > $this->limit) {
287 $limit = $this->limit - $this->retrievedResults;
291 if ($this->incrementOffset) {
292 $offset = $this->offset + $this->retrievedResults;
294 $offset = $this->offset + $this->totalIncompletes;
300 '__ElggBatch' => $this,
303 $options = array_merge($this->options, $current_options);
305 $this->incompleteEntities = [];
306 $this->results = call_user_func($this->getter, $options);
311 $num_results =
count($this->results);
312 $num_incomplete =
count($this->incompleteEntities);
314 $this->totalIncompletes += $num_incomplete;
316 if (!empty($this->incompleteEntities)) {
318 array_splice($this->results, 0, 0, array_pad([], $num_incomplete, null));
320 reset($this->results);
321 for ($i = 0; $i < $num_incomplete; $i++) {
322 next($this->results);
326 if ($this->results) {
330 $this->resultIndex = $num_incomplete;
332 $this->retrievedResults += ($num_results + $num_incomplete);
333 if ($num_results == 0) {
336 return $this->getNextResultsChunk();
354 $this->incrementOffset = (bool) $increment;
363 $this->chunkSize =
$size;
373 $this->resultIndex = 0;
374 $this->retrievedResults = 0;
375 $this->processedResults = 0;
378 if ($this->chunkIndex == 0 || $this->limit > $this->chunkSize) {
379 $this->chunkIndex = 0;
380 $this->getNextResultsChunk();
388 return current($this->results);
395 return $this->processedResults;
403 if (($this->processedResults + 1) >= $this->limit && $this->limit > 0) {
409 if (($this->resultIndex + 1) >= $this->chunkSize) {
410 if (!$this->getNextResultsChunk()) {
419 $this->resultIndex++;
423 $this->processedResults++;
431 if (!is_array($this->results)) {
435 return (
$key !== null &&
$key !==
false);
448 if (!is_callable($this->getter)) {
449 $inspector = new \Elgg\Debug\Inspector();
450 throw new RuntimeException(
"Getter is not callable: " . $inspector->describeCallable($this->getter));
453 $options = array_merge($this->options, [
'count' =>
true]);
455 return call_user_func($this->getter, $options);
__construct(callable $getter, $options, $callback=null, $chunk_size=25, $inc_offset=true)
Batches operations on any elgg_get_*() or compatible function that supports an options array...
$CONFIG default_limit
The default "limit" used in site queries.
rewind()
Implements Iterator.
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif(is_callable([$item, 'getType']))
count()
Count the total results available at this moment.
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
setChunkSize($size=25)
Set chunk size.
_elgg_config()
Get the Elgg config service.
_elgg_services()
Get the global service provider.
Specifies a countable iterator, usually of result rows from a DB.
setIncrementOffset($increment=true)
Increment the offset from the original options array? Setting to false is required for callbacks that...