80 private $results = array();
87 private $getter = null;
94 private $options = array();
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 = array();
185 private $totalIncompletes = 0;
209 public function __construct($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) {
240 $all_results = array();
244 if (($result ===
true || $result ===
false || $result === null) && !is_array($all_results)) {
245 $all_results = $result && $all_results;
251 $this->callbackResult = $all_results;
263 $this->incompleteEntities[] =
$row;
271 private function getNextResultsChunk() {
274 $this->results = array();
276 if (!isset($this->validGetter)) {
277 $this->validGetter = is_callable($this->getter);
280 if (!$this->validGetter) {
284 $limit = $this->chunkSize;
287 if ($this->limit != 0) {
288 if ($this->retrievedResults >= $this->limit) {
294 if ($this->limit < $this->chunkSize) {
295 $limit = $this->limit;
296 } elseif ($this->retrievedResults + $this->chunkSize > $this->limit) {
298 $limit = $this->limit - $this->retrievedResults;
302 if ($this->incrementOffset) {
303 $offset = $this->offset + $this->retrievedResults;
305 $offset = $this->offset + $this->totalIncompletes;
308 $current_options = array(
311 '__ElggBatch' => $this,
314 $options = array_merge($this->options, $current_options);
316 $this->incompleteEntities = array();
317 $this->results = call_user_func($this->getter, $options);
322 $num_results =
count($this->results);
323 $num_incomplete =
count($this->incompleteEntities);
325 $this->totalIncompletes += $num_incomplete;
327 if ($this->incompleteEntities) {
329 array_splice($this->results, 0, 0, array_pad(array(), $num_incomplete, null));
331 reset($this->results);
332 for ($i = 0; $i < $num_incomplete; $i++) {
333 next($this->results);
337 if ($this->results) {
341 $this->resultIndex = $num_incomplete;
343 $this->retrievedResults += ($num_results + $num_incomplete);
344 if ($num_results == 0) {
347 return $this->getNextResultsChunk();
365 $this->incrementOffset = (bool) $increment;
376 $this->resultIndex = 0;
377 $this->retrievedResults = 0;
378 $this->processedResults = 0;
381 if ($this->chunkIndex == 0 || $this->limit > $this->chunkSize) {
382 $this->chunkIndex = 0;
383 $this->getNextResultsChunk();
391 return current($this->results);
398 return $this->processedResults;
406 if (($this->processedResults + 1) >= $this->limit && $this->limit > 0) {
407 $this->results = array();
412 if (($this->resultIndex + 1) >= $this->chunkSize) {
413 if (!$this->getNextResultsChunk()) {
414 $this->results = array();
422 $this->resultIndex++;
426 $this->processedResults++;
434 if (!is_array($this->results)) {
438 return (
$key !== null &&
$key !==
false);
451 if (!is_callable($this->getter)) {
452 $inspector = new \Elgg\Debug\Inspector();
453 throw new RuntimeException(
"Getter is not callable: " . $inspector->describeCallable($this->getter));
456 $options = array_merge($this->options, [
'count' =>
true]);
458 return call_user_func($this->getter, $options);
469 if (
$name ===
'options') {
471 return $this->options;
474 _elgg_services()->logger->warn(
"Read of non-existent property '$name'");
487 if (
$name ===
'options') {
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
__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...
if($guid==elgg_get_logged_in_user_guid()) $name
rewind()
Implements Iterator.
reportIncompleteEntity(\stdClass $row)
Tell the process that an entity was incomplete during a fetch.
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
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.
__get($name)
Read a property.
__set($name, $value)
Write a property.
Specifies a countable iterator, usually of result rows from a DB.
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
setIncrementOffset($increment=true)
Increment the offset from the original options array? Setting to false is required for callbacks that...