@@ -8,55 +8,55 @@ |
||
8 | 8 | */ |
9 | 9 | class ViewColumn implements TableColumn { |
10 | 10 | |
11 | - /** |
|
12 | - * @var string |
|
13 | - */ |
|
14 | - private $heading; |
|
15 | - |
|
16 | - /** |
|
17 | - * @var string |
|
18 | - */ |
|
19 | - private $view; |
|
20 | - |
|
21 | - /** |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - private $vars; |
|
25 | - |
|
26 | - /** |
|
27 | - * Constructor |
|
28 | - * |
|
29 | - * @param string $view The view to render the value |
|
30 | - * @param string $heading Heading |
|
31 | - * @param array $vars Vars to merge into the view vars |
|
32 | - */ |
|
33 | - public function __construct($view, $heading = null, $vars = []) { |
|
34 | - $this->view = $view; |
|
35 | - $this->vars = $vars; |
|
36 | - |
|
37 | - if (!is_string($heading)) { |
|
38 | - $heading = elgg_echo("ViewColumn:view:$view"); |
|
39 | - } |
|
40 | - $this->heading = $heading; |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * {@inheritdoc} |
|
45 | - */ |
|
46 | - public function renderHeading() { |
|
47 | - return $this->heading; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * {@inheritdoc} |
|
52 | - */ |
|
53 | - public function renderCell($item, $type, $item_vars) { |
|
54 | - $vars = array_merge($this->vars, [ |
|
55 | - 'item' => $item, |
|
56 | - 'item_vars' => $item_vars, |
|
57 | - 'type' => $type, |
|
58 | - ]); |
|
59 | - |
|
60 | - return elgg_view($this->view, $vars); |
|
61 | - } |
|
11 | + /** |
|
12 | + * @var string |
|
13 | + */ |
|
14 | + private $heading; |
|
15 | + |
|
16 | + /** |
|
17 | + * @var string |
|
18 | + */ |
|
19 | + private $view; |
|
20 | + |
|
21 | + /** |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + private $vars; |
|
25 | + |
|
26 | + /** |
|
27 | + * Constructor |
|
28 | + * |
|
29 | + * @param string $view The view to render the value |
|
30 | + * @param string $heading Heading |
|
31 | + * @param array $vars Vars to merge into the view vars |
|
32 | + */ |
|
33 | + public function __construct($view, $heading = null, $vars = []) { |
|
34 | + $this->view = $view; |
|
35 | + $this->vars = $vars; |
|
36 | + |
|
37 | + if (!is_string($heading)) { |
|
38 | + $heading = elgg_echo("ViewColumn:view:$view"); |
|
39 | + } |
|
40 | + $this->heading = $heading; |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * {@inheritdoc} |
|
45 | + */ |
|
46 | + public function renderHeading() { |
|
47 | + return $this->heading; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * {@inheritdoc} |
|
52 | + */ |
|
53 | + public function renderCell($item, $type, $item_vars) { |
|
54 | + $vars = array_merge($this->vars, [ |
|
55 | + 'item' => $item, |
|
56 | + 'item_vars' => $item_vars, |
|
57 | + 'type' => $type, |
|
58 | + ]); |
|
59 | + |
|
60 | + return elgg_view($this->view, $vars); |
|
61 | + } |
|
62 | 62 | } |
@@ -72,422 +72,422 @@ |
||
72 | 72 | */ |
73 | 73 | class ElggBatch implements BatchResult { |
74 | 74 | |
75 | - /** |
|
76 | - * The objects to iterate over. |
|
77 | - * |
|
78 | - * @var array |
|
79 | - */ |
|
80 | - private $results = array(); |
|
81 | - |
|
82 | - /** |
|
83 | - * The function used to get results. |
|
84 | - * |
|
85 | - * @var callable |
|
86 | - */ |
|
87 | - private $getter = null; |
|
88 | - |
|
89 | - /** |
|
90 | - * The given $options to alter and pass to the getter. |
|
91 | - * |
|
92 | - * @var array |
|
93 | - */ |
|
94 | - private $options = array(); |
|
95 | - |
|
96 | - /** |
|
97 | - * The number of results to grab at a time. |
|
98 | - * |
|
99 | - * @var int |
|
100 | - */ |
|
101 | - private $chunkSize = 25; |
|
102 | - |
|
103 | - /** |
|
104 | - * A callback function to pass results through. |
|
105 | - * |
|
106 | - * @var callable |
|
107 | - */ |
|
108 | - private $callback = null; |
|
109 | - |
|
110 | - /** |
|
111 | - * Start after this many results. |
|
112 | - * |
|
113 | - * @var int |
|
114 | - */ |
|
115 | - private $offset = 0; |
|
116 | - |
|
117 | - /** |
|
118 | - * Stop after this many results. |
|
119 | - * |
|
120 | - * @var int |
|
121 | - */ |
|
122 | - private $limit = 0; |
|
123 | - |
|
124 | - /** |
|
125 | - * Number of processed results. |
|
126 | - * |
|
127 | - * @var int |
|
128 | - */ |
|
129 | - private $retrievedResults = 0; |
|
130 | - |
|
131 | - /** |
|
132 | - * The index of the current result within the current chunk |
|
133 | - * |
|
134 | - * @var int |
|
135 | - */ |
|
136 | - private $resultIndex = 0; |
|
137 | - |
|
138 | - /** |
|
139 | - * The index of the current chunk |
|
140 | - * |
|
141 | - * @var int |
|
142 | - */ |
|
143 | - private $chunkIndex = 0; |
|
144 | - |
|
145 | - /** |
|
146 | - * The number of results iterated through |
|
147 | - * |
|
148 | - * @var int |
|
149 | - */ |
|
150 | - private $processedResults = 0; |
|
151 | - |
|
152 | - /** |
|
153 | - * Is the getter a valid callback |
|
154 | - * |
|
155 | - * @var bool |
|
156 | - */ |
|
157 | - private $validGetter = null; |
|
158 | - |
|
159 | - /** |
|
160 | - * The result of running all entities through the callback function. |
|
161 | - * |
|
162 | - * @var mixed |
|
163 | - */ |
|
164 | - public $callbackResult = null; |
|
165 | - |
|
166 | - /** |
|
167 | - * If false, offset will not be incremented. This is used for callbacks/loops that delete. |
|
168 | - * |
|
169 | - * @var bool |
|
170 | - */ |
|
171 | - private $incrementOffset = true; |
|
172 | - |
|
173 | - /** |
|
174 | - * Entities that could not be instantiated during a fetch |
|
175 | - * |
|
176 | - * @var \stdClass[] |
|
177 | - */ |
|
178 | - private $incompleteEntities = array(); |
|
179 | - |
|
180 | - /** |
|
181 | - * Total number of incomplete entities fetched |
|
182 | - * |
|
183 | - * @var int |
|
184 | - */ |
|
185 | - private $totalIncompletes = 0; |
|
186 | - |
|
187 | - /** |
|
188 | - * Batches operations on any elgg_get_*() or compatible function that supports |
|
189 | - * an options array. |
|
190 | - * |
|
191 | - * Instead of returning all objects in memory, it goes through $chunk_size |
|
192 | - * objects, then requests more from the server. This avoids OOM errors. |
|
193 | - * |
|
194 | - * @param string $getter The function used to get objects. Usually |
|
195 | - * an elgg_get_*() function, but can be any valid PHP callback. |
|
196 | - * @param array $options The options array to pass to the getter function. If limit is |
|
197 | - * not set, 10 is used as the default. In most cases that is not |
|
198 | - * what you want. |
|
199 | - * @param mixed $callback An optional callback function that all results will be passed |
|
200 | - * to upon load. The callback needs to accept $result, $getter, |
|
201 | - * $options. |
|
202 | - * @param int $chunk_size The number of entities to pull in before requesting more. |
|
203 | - * You have to balance this between running out of memory in PHP |
|
204 | - * and hitting the db server too often. |
|
205 | - * @param bool $inc_offset Increment the offset on each fetch. This must be false for |
|
206 | - * callbacks that delete rows. You can set this after the |
|
207 | - * object is created with {@link \ElggBatch::setIncrementOffset()}. |
|
208 | - */ |
|
209 | - public function __construct($getter, $options, $callback = null, $chunk_size = 25, |
|
210 | - $inc_offset = true) { |
|
75 | + /** |
|
76 | + * The objects to iterate over. |
|
77 | + * |
|
78 | + * @var array |
|
79 | + */ |
|
80 | + private $results = array(); |
|
81 | + |
|
82 | + /** |
|
83 | + * The function used to get results. |
|
84 | + * |
|
85 | + * @var callable |
|
86 | + */ |
|
87 | + private $getter = null; |
|
88 | + |
|
89 | + /** |
|
90 | + * The given $options to alter and pass to the getter. |
|
91 | + * |
|
92 | + * @var array |
|
93 | + */ |
|
94 | + private $options = array(); |
|
95 | + |
|
96 | + /** |
|
97 | + * The number of results to grab at a time. |
|
98 | + * |
|
99 | + * @var int |
|
100 | + */ |
|
101 | + private $chunkSize = 25; |
|
102 | + |
|
103 | + /** |
|
104 | + * A callback function to pass results through. |
|
105 | + * |
|
106 | + * @var callable |
|
107 | + */ |
|
108 | + private $callback = null; |
|
109 | + |
|
110 | + /** |
|
111 | + * Start after this many results. |
|
112 | + * |
|
113 | + * @var int |
|
114 | + */ |
|
115 | + private $offset = 0; |
|
116 | + |
|
117 | + /** |
|
118 | + * Stop after this many results. |
|
119 | + * |
|
120 | + * @var int |
|
121 | + */ |
|
122 | + private $limit = 0; |
|
123 | + |
|
124 | + /** |
|
125 | + * Number of processed results. |
|
126 | + * |
|
127 | + * @var int |
|
128 | + */ |
|
129 | + private $retrievedResults = 0; |
|
130 | + |
|
131 | + /** |
|
132 | + * The index of the current result within the current chunk |
|
133 | + * |
|
134 | + * @var int |
|
135 | + */ |
|
136 | + private $resultIndex = 0; |
|
137 | + |
|
138 | + /** |
|
139 | + * The index of the current chunk |
|
140 | + * |
|
141 | + * @var int |
|
142 | + */ |
|
143 | + private $chunkIndex = 0; |
|
144 | + |
|
145 | + /** |
|
146 | + * The number of results iterated through |
|
147 | + * |
|
148 | + * @var int |
|
149 | + */ |
|
150 | + private $processedResults = 0; |
|
151 | + |
|
152 | + /** |
|
153 | + * Is the getter a valid callback |
|
154 | + * |
|
155 | + * @var bool |
|
156 | + */ |
|
157 | + private $validGetter = null; |
|
158 | + |
|
159 | + /** |
|
160 | + * The result of running all entities through the callback function. |
|
161 | + * |
|
162 | + * @var mixed |
|
163 | + */ |
|
164 | + public $callbackResult = null; |
|
165 | + |
|
166 | + /** |
|
167 | + * If false, offset will not be incremented. This is used for callbacks/loops that delete. |
|
168 | + * |
|
169 | + * @var bool |
|
170 | + */ |
|
171 | + private $incrementOffset = true; |
|
172 | + |
|
173 | + /** |
|
174 | + * Entities that could not be instantiated during a fetch |
|
175 | + * |
|
176 | + * @var \stdClass[] |
|
177 | + */ |
|
178 | + private $incompleteEntities = array(); |
|
179 | + |
|
180 | + /** |
|
181 | + * Total number of incomplete entities fetched |
|
182 | + * |
|
183 | + * @var int |
|
184 | + */ |
|
185 | + private $totalIncompletes = 0; |
|
186 | + |
|
187 | + /** |
|
188 | + * Batches operations on any elgg_get_*() or compatible function that supports |
|
189 | + * an options array. |
|
190 | + * |
|
191 | + * Instead of returning all objects in memory, it goes through $chunk_size |
|
192 | + * objects, then requests more from the server. This avoids OOM errors. |
|
193 | + * |
|
194 | + * @param string $getter The function used to get objects. Usually |
|
195 | + * an elgg_get_*() function, but can be any valid PHP callback. |
|
196 | + * @param array $options The options array to pass to the getter function. If limit is |
|
197 | + * not set, 10 is used as the default. In most cases that is not |
|
198 | + * what you want. |
|
199 | + * @param mixed $callback An optional callback function that all results will be passed |
|
200 | + * to upon load. The callback needs to accept $result, $getter, |
|
201 | + * $options. |
|
202 | + * @param int $chunk_size The number of entities to pull in before requesting more. |
|
203 | + * You have to balance this between running out of memory in PHP |
|
204 | + * and hitting the db server too often. |
|
205 | + * @param bool $inc_offset Increment the offset on each fetch. This must be false for |
|
206 | + * callbacks that delete rows. You can set this after the |
|
207 | + * object is created with {@link \ElggBatch::setIncrementOffset()}. |
|
208 | + */ |
|
209 | + public function __construct($getter, $options, $callback = null, $chunk_size = 25, |
|
210 | + $inc_offset = true) { |
|
211 | 211 | |
212 | - $this->getter = $getter; |
|
213 | - $this->options = $options; |
|
214 | - $this->callback = $callback; |
|
215 | - $this->chunkSize = $chunk_size; |
|
216 | - $this->setIncrementOffset($inc_offset); |
|
217 | - |
|
218 | - if ($this->chunkSize <= 0) { |
|
219 | - $this->chunkSize = 25; |
|
220 | - } |
|
221 | - |
|
222 | - // store these so we can compare later |
|
223 | - $this->offset = elgg_extract('offset', $options, 0); |
|
224 | - $this->limit = elgg_extract('limit', $options, elgg_get_config('default_limit')); |
|
225 | - |
|
226 | - // if passed a callback, create a new \ElggBatch with the same options |
|
227 | - // and pass each to the callback. |
|
228 | - if ($callback && is_callable($callback)) { |
|
229 | - $batch = new \ElggBatch($getter, $options, null, $chunk_size, $inc_offset); |
|
230 | - |
|
231 | - $all_results = null; |
|
232 | - |
|
233 | - foreach ($batch as $result) { |
|
234 | - $result = call_user_func($callback, $result, $getter, $options); |
|
235 | - |
|
236 | - if (!isset($all_results)) { |
|
237 | - if ($result === true || $result === false || $result === null) { |
|
238 | - $all_results = $result; |
|
239 | - } else { |
|
240 | - $all_results = array(); |
|
241 | - } |
|
242 | - } |
|
243 | - |
|
244 | - if (($result === true || $result === false || $result === null) && !is_array($all_results)) { |
|
245 | - $all_results = $result && $all_results; |
|
246 | - } else { |
|
247 | - $all_results[] = $result; |
|
248 | - } |
|
249 | - } |
|
250 | - |
|
251 | - $this->callbackResult = $all_results; |
|
252 | - } |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Tell the process that an entity was incomplete during a fetch |
|
257 | - * |
|
258 | - * @param \stdClass $row |
|
259 | - * |
|
260 | - * @access private |
|
261 | - */ |
|
262 | - public function reportIncompleteEntity(\stdClass $row) { |
|
263 | - $this->incompleteEntities[] = $row; |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * Fetches the next chunk of results |
|
268 | - * |
|
269 | - * @return bool |
|
270 | - */ |
|
271 | - private function getNextResultsChunk() { |
|
272 | - |
|
273 | - // always reset results. |
|
274 | - $this->results = array(); |
|
275 | - |
|
276 | - if (!isset($this->validGetter)) { |
|
277 | - $this->validGetter = is_callable($this->getter); |
|
278 | - } |
|
279 | - |
|
280 | - if (!$this->validGetter) { |
|
281 | - return false; |
|
282 | - } |
|
283 | - |
|
284 | - $limit = $this->chunkSize; |
|
285 | - |
|
286 | - // if someone passed limit = 0 they want everything. |
|
287 | - if ($this->limit != 0) { |
|
288 | - if ($this->retrievedResults >= $this->limit) { |
|
289 | - return false; |
|
290 | - } |
|
291 | - |
|
292 | - // if original limit < chunk size, set limit to original limit |
|
293 | - // else if the number of results we'll fetch if greater than the original limit |
|
294 | - if ($this->limit < $this->chunkSize) { |
|
295 | - $limit = $this->limit; |
|
296 | - } elseif ($this->retrievedResults + $this->chunkSize > $this->limit) { |
|
297 | - // set the limit to the number of results remaining in the original limit |
|
298 | - $limit = $this->limit - $this->retrievedResults; |
|
299 | - } |
|
300 | - } |
|
301 | - |
|
302 | - if ($this->incrementOffset) { |
|
303 | - $offset = $this->offset + $this->retrievedResults; |
|
304 | - } else { |
|
305 | - $offset = $this->offset + $this->totalIncompletes; |
|
306 | - } |
|
307 | - |
|
308 | - $current_options = array( |
|
309 | - 'limit' => $limit, |
|
310 | - 'offset' => $offset, |
|
311 | - '__ElggBatch' => $this, |
|
312 | - ); |
|
313 | - |
|
314 | - $options = array_merge($this->options, $current_options); |
|
315 | - |
|
316 | - $this->incompleteEntities = array(); |
|
317 | - $this->results = call_user_func($this->getter, $options); |
|
318 | - |
|
319 | - // batch result sets tend to be large; we don't want to cache these. |
|
320 | - _elgg_services()->db->disableQueryCache(); |
|
321 | - |
|
322 | - $num_results = count($this->results); |
|
323 | - $num_incomplete = count($this->incompleteEntities); |
|
324 | - |
|
325 | - $this->totalIncompletes += $num_incomplete; |
|
326 | - |
|
327 | - if ($this->incompleteEntities) { |
|
328 | - // pad the front of the results with nulls representing the incompletes |
|
329 | - array_splice($this->results, 0, 0, array_pad(array(), $num_incomplete, null)); |
|
330 | - // ...and skip past them |
|
331 | - reset($this->results); |
|
332 | - for ($i = 0; $i < $num_incomplete; $i++) { |
|
333 | - next($this->results); |
|
334 | - } |
|
335 | - } |
|
336 | - |
|
337 | - if ($this->results) { |
|
338 | - $this->chunkIndex++; |
|
339 | - |
|
340 | - // let the system know we've jumped past the nulls |
|
341 | - $this->resultIndex = $num_incomplete; |
|
342 | - |
|
343 | - $this->retrievedResults += ($num_results + $num_incomplete); |
|
344 | - if ($num_results == 0) { |
|
345 | - // This fetch was *all* incompletes! We need to fetch until we can either |
|
346 | - // offer at least one row to iterate over, or give up. |
|
347 | - return $this->getNextResultsChunk(); |
|
348 | - } |
|
349 | - _elgg_services()->db->enableQueryCache(); |
|
350 | - return true; |
|
351 | - } else { |
|
352 | - _elgg_services()->db->enableQueryCache(); |
|
353 | - return false; |
|
354 | - } |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * Increment the offset from the original options array? Setting to |
|
359 | - * false is required for callbacks that delete rows. |
|
360 | - * |
|
361 | - * @param bool $increment Set to false when deleting data |
|
362 | - * @return void |
|
363 | - */ |
|
364 | - public function setIncrementOffset($increment = true) { |
|
365 | - $this->incrementOffset = (bool) $increment; |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Implements Iterator |
|
370 | - */ |
|
371 | - |
|
372 | - /** |
|
373 | - * {@inheritdoc} |
|
374 | - */ |
|
375 | - public function rewind() { |
|
376 | - $this->resultIndex = 0; |
|
377 | - $this->retrievedResults = 0; |
|
378 | - $this->processedResults = 0; |
|
379 | - |
|
380 | - // only grab results if we haven't yet or we're crossing chunks |
|
381 | - if ($this->chunkIndex == 0 || $this->limit > $this->chunkSize) { |
|
382 | - $this->chunkIndex = 0; |
|
383 | - $this->getNextResultsChunk(); |
|
384 | - } |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * {@inheritdoc} |
|
389 | - */ |
|
390 | - public function current() { |
|
391 | - return current($this->results); |
|
392 | - } |
|
393 | - |
|
394 | - /** |
|
395 | - * {@inheritdoc} |
|
396 | - */ |
|
397 | - public function key() { |
|
398 | - return $this->processedResults; |
|
399 | - } |
|
400 | - |
|
401 | - /** |
|
402 | - * {@inheritdoc} |
|
403 | - */ |
|
404 | - public function next() { |
|
405 | - // if we'll be at the end. |
|
406 | - if (($this->processedResults + 1) >= $this->limit && $this->limit > 0) { |
|
407 | - $this->results = array(); |
|
408 | - return false; |
|
409 | - } |
|
410 | - |
|
411 | - // if we'll need new results. |
|
412 | - if (($this->resultIndex + 1) >= $this->chunkSize) { |
|
413 | - if (!$this->getNextResultsChunk()) { |
|
414 | - $this->results = array(); |
|
415 | - return false; |
|
416 | - } |
|
417 | - |
|
418 | - $result = current($this->results); |
|
419 | - } else { |
|
420 | - // the function above resets the indexes, so only inc if not |
|
421 | - // getting new set |
|
422 | - $this->resultIndex++; |
|
423 | - $result = next($this->results); |
|
424 | - } |
|
425 | - |
|
426 | - $this->processedResults++; |
|
427 | - return $result; |
|
428 | - } |
|
429 | - |
|
430 | - /** |
|
431 | - * {@inheritdoc} |
|
432 | - */ |
|
433 | - public function valid() { |
|
434 | - if (!is_array($this->results)) { |
|
435 | - return false; |
|
436 | - } |
|
437 | - $key = key($this->results); |
|
438 | - return ($key !== null && $key !== false); |
|
439 | - } |
|
440 | - |
|
441 | - /** |
|
442 | - * Count the total results available at this moment. |
|
443 | - * |
|
444 | - * As this performs a separate query, the count returned may not match the number of results you can |
|
445 | - * fetch via iteration on a very active DB. |
|
446 | - * |
|
447 | - * @see Countable::count() |
|
448 | - * @return int |
|
449 | - */ |
|
450 | - public function count() { |
|
451 | - if (!is_callable($this->getter)) { |
|
452 | - $inspector = new \Elgg\Debug\Inspector(); |
|
453 | - throw new RuntimeException("Getter is not callable: " . $inspector->describeCallable($this->getter)); |
|
454 | - } |
|
455 | - |
|
456 | - $options = array_merge($this->options, ['count' => true]); |
|
457 | - |
|
458 | - return call_user_func($this->getter, $options); |
|
459 | - } |
|
460 | - |
|
461 | - /** |
|
462 | - * Read a property |
|
463 | - * |
|
464 | - * @param string $name |
|
465 | - * @return mixed |
|
466 | - * @access private |
|
467 | - */ |
|
468 | - public function __get($name) { |
|
469 | - if ($name === 'options') { |
|
470 | - elgg_deprecated_notice("The ElggBatch 'options' property is private and should not be used", "2.3"); |
|
471 | - return $this->options; |
|
472 | - } |
|
473 | - |
|
474 | - _elgg_services()->logger->warn("Read of non-existent property '$name'"); |
|
475 | - return null; |
|
476 | - } |
|
477 | - |
|
478 | - /** |
|
479 | - * Write a property |
|
480 | - * |
|
481 | - * @param string $name |
|
482 | - * @param mixed $value |
|
483 | - * @return void |
|
484 | - * @access private |
|
485 | - */ |
|
486 | - public function __set($name, $value) { |
|
487 | - if ($name === 'options') { |
|
488 | - elgg_deprecated_notice("The ElggBatch 'options' property is private and should not be used", "2.3"); |
|
489 | - } |
|
490 | - |
|
491 | - $this->{$name} = $value; |
|
492 | - } |
|
212 | + $this->getter = $getter; |
|
213 | + $this->options = $options; |
|
214 | + $this->callback = $callback; |
|
215 | + $this->chunkSize = $chunk_size; |
|
216 | + $this->setIncrementOffset($inc_offset); |
|
217 | + |
|
218 | + if ($this->chunkSize <= 0) { |
|
219 | + $this->chunkSize = 25; |
|
220 | + } |
|
221 | + |
|
222 | + // store these so we can compare later |
|
223 | + $this->offset = elgg_extract('offset', $options, 0); |
|
224 | + $this->limit = elgg_extract('limit', $options, elgg_get_config('default_limit')); |
|
225 | + |
|
226 | + // if passed a callback, create a new \ElggBatch with the same options |
|
227 | + // and pass each to the callback. |
|
228 | + if ($callback && is_callable($callback)) { |
|
229 | + $batch = new \ElggBatch($getter, $options, null, $chunk_size, $inc_offset); |
|
230 | + |
|
231 | + $all_results = null; |
|
232 | + |
|
233 | + foreach ($batch as $result) { |
|
234 | + $result = call_user_func($callback, $result, $getter, $options); |
|
235 | + |
|
236 | + if (!isset($all_results)) { |
|
237 | + if ($result === true || $result === false || $result === null) { |
|
238 | + $all_results = $result; |
|
239 | + } else { |
|
240 | + $all_results = array(); |
|
241 | + } |
|
242 | + } |
|
243 | + |
|
244 | + if (($result === true || $result === false || $result === null) && !is_array($all_results)) { |
|
245 | + $all_results = $result && $all_results; |
|
246 | + } else { |
|
247 | + $all_results[] = $result; |
|
248 | + } |
|
249 | + } |
|
250 | + |
|
251 | + $this->callbackResult = $all_results; |
|
252 | + } |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Tell the process that an entity was incomplete during a fetch |
|
257 | + * |
|
258 | + * @param \stdClass $row |
|
259 | + * |
|
260 | + * @access private |
|
261 | + */ |
|
262 | + public function reportIncompleteEntity(\stdClass $row) { |
|
263 | + $this->incompleteEntities[] = $row; |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * Fetches the next chunk of results |
|
268 | + * |
|
269 | + * @return bool |
|
270 | + */ |
|
271 | + private function getNextResultsChunk() { |
|
272 | + |
|
273 | + // always reset results. |
|
274 | + $this->results = array(); |
|
275 | + |
|
276 | + if (!isset($this->validGetter)) { |
|
277 | + $this->validGetter = is_callable($this->getter); |
|
278 | + } |
|
279 | + |
|
280 | + if (!$this->validGetter) { |
|
281 | + return false; |
|
282 | + } |
|
283 | + |
|
284 | + $limit = $this->chunkSize; |
|
285 | + |
|
286 | + // if someone passed limit = 0 they want everything. |
|
287 | + if ($this->limit != 0) { |
|
288 | + if ($this->retrievedResults >= $this->limit) { |
|
289 | + return false; |
|
290 | + } |
|
291 | + |
|
292 | + // if original limit < chunk size, set limit to original limit |
|
293 | + // else if the number of results we'll fetch if greater than the original limit |
|
294 | + if ($this->limit < $this->chunkSize) { |
|
295 | + $limit = $this->limit; |
|
296 | + } elseif ($this->retrievedResults + $this->chunkSize > $this->limit) { |
|
297 | + // set the limit to the number of results remaining in the original limit |
|
298 | + $limit = $this->limit - $this->retrievedResults; |
|
299 | + } |
|
300 | + } |
|
301 | + |
|
302 | + if ($this->incrementOffset) { |
|
303 | + $offset = $this->offset + $this->retrievedResults; |
|
304 | + } else { |
|
305 | + $offset = $this->offset + $this->totalIncompletes; |
|
306 | + } |
|
307 | + |
|
308 | + $current_options = array( |
|
309 | + 'limit' => $limit, |
|
310 | + 'offset' => $offset, |
|
311 | + '__ElggBatch' => $this, |
|
312 | + ); |
|
313 | + |
|
314 | + $options = array_merge($this->options, $current_options); |
|
315 | + |
|
316 | + $this->incompleteEntities = array(); |
|
317 | + $this->results = call_user_func($this->getter, $options); |
|
318 | + |
|
319 | + // batch result sets tend to be large; we don't want to cache these. |
|
320 | + _elgg_services()->db->disableQueryCache(); |
|
321 | + |
|
322 | + $num_results = count($this->results); |
|
323 | + $num_incomplete = count($this->incompleteEntities); |
|
324 | + |
|
325 | + $this->totalIncompletes += $num_incomplete; |
|
326 | + |
|
327 | + if ($this->incompleteEntities) { |
|
328 | + // pad the front of the results with nulls representing the incompletes |
|
329 | + array_splice($this->results, 0, 0, array_pad(array(), $num_incomplete, null)); |
|
330 | + // ...and skip past them |
|
331 | + reset($this->results); |
|
332 | + for ($i = 0; $i < $num_incomplete; $i++) { |
|
333 | + next($this->results); |
|
334 | + } |
|
335 | + } |
|
336 | + |
|
337 | + if ($this->results) { |
|
338 | + $this->chunkIndex++; |
|
339 | + |
|
340 | + // let the system know we've jumped past the nulls |
|
341 | + $this->resultIndex = $num_incomplete; |
|
342 | + |
|
343 | + $this->retrievedResults += ($num_results + $num_incomplete); |
|
344 | + if ($num_results == 0) { |
|
345 | + // This fetch was *all* incompletes! We need to fetch until we can either |
|
346 | + // offer at least one row to iterate over, or give up. |
|
347 | + return $this->getNextResultsChunk(); |
|
348 | + } |
|
349 | + _elgg_services()->db->enableQueryCache(); |
|
350 | + return true; |
|
351 | + } else { |
|
352 | + _elgg_services()->db->enableQueryCache(); |
|
353 | + return false; |
|
354 | + } |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * Increment the offset from the original options array? Setting to |
|
359 | + * false is required for callbacks that delete rows. |
|
360 | + * |
|
361 | + * @param bool $increment Set to false when deleting data |
|
362 | + * @return void |
|
363 | + */ |
|
364 | + public function setIncrementOffset($increment = true) { |
|
365 | + $this->incrementOffset = (bool) $increment; |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Implements Iterator |
|
370 | + */ |
|
371 | + |
|
372 | + /** |
|
373 | + * {@inheritdoc} |
|
374 | + */ |
|
375 | + public function rewind() { |
|
376 | + $this->resultIndex = 0; |
|
377 | + $this->retrievedResults = 0; |
|
378 | + $this->processedResults = 0; |
|
379 | + |
|
380 | + // only grab results if we haven't yet or we're crossing chunks |
|
381 | + if ($this->chunkIndex == 0 || $this->limit > $this->chunkSize) { |
|
382 | + $this->chunkIndex = 0; |
|
383 | + $this->getNextResultsChunk(); |
|
384 | + } |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * {@inheritdoc} |
|
389 | + */ |
|
390 | + public function current() { |
|
391 | + return current($this->results); |
|
392 | + } |
|
393 | + |
|
394 | + /** |
|
395 | + * {@inheritdoc} |
|
396 | + */ |
|
397 | + public function key() { |
|
398 | + return $this->processedResults; |
|
399 | + } |
|
400 | + |
|
401 | + /** |
|
402 | + * {@inheritdoc} |
|
403 | + */ |
|
404 | + public function next() { |
|
405 | + // if we'll be at the end. |
|
406 | + if (($this->processedResults + 1) >= $this->limit && $this->limit > 0) { |
|
407 | + $this->results = array(); |
|
408 | + return false; |
|
409 | + } |
|
410 | + |
|
411 | + // if we'll need new results. |
|
412 | + if (($this->resultIndex + 1) >= $this->chunkSize) { |
|
413 | + if (!$this->getNextResultsChunk()) { |
|
414 | + $this->results = array(); |
|
415 | + return false; |
|
416 | + } |
|
417 | + |
|
418 | + $result = current($this->results); |
|
419 | + } else { |
|
420 | + // the function above resets the indexes, so only inc if not |
|
421 | + // getting new set |
|
422 | + $this->resultIndex++; |
|
423 | + $result = next($this->results); |
|
424 | + } |
|
425 | + |
|
426 | + $this->processedResults++; |
|
427 | + return $result; |
|
428 | + } |
|
429 | + |
|
430 | + /** |
|
431 | + * {@inheritdoc} |
|
432 | + */ |
|
433 | + public function valid() { |
|
434 | + if (!is_array($this->results)) { |
|
435 | + return false; |
|
436 | + } |
|
437 | + $key = key($this->results); |
|
438 | + return ($key !== null && $key !== false); |
|
439 | + } |
|
440 | + |
|
441 | + /** |
|
442 | + * Count the total results available at this moment. |
|
443 | + * |
|
444 | + * As this performs a separate query, the count returned may not match the number of results you can |
|
445 | + * fetch via iteration on a very active DB. |
|
446 | + * |
|
447 | + * @see Countable::count() |
|
448 | + * @return int |
|
449 | + */ |
|
450 | + public function count() { |
|
451 | + if (!is_callable($this->getter)) { |
|
452 | + $inspector = new \Elgg\Debug\Inspector(); |
|
453 | + throw new RuntimeException("Getter is not callable: " . $inspector->describeCallable($this->getter)); |
|
454 | + } |
|
455 | + |
|
456 | + $options = array_merge($this->options, ['count' => true]); |
|
457 | + |
|
458 | + return call_user_func($this->getter, $options); |
|
459 | + } |
|
460 | + |
|
461 | + /** |
|
462 | + * Read a property |
|
463 | + * |
|
464 | + * @param string $name |
|
465 | + * @return mixed |
|
466 | + * @access private |
|
467 | + */ |
|
468 | + public function __get($name) { |
|
469 | + if ($name === 'options') { |
|
470 | + elgg_deprecated_notice("The ElggBatch 'options' property is private and should not be used", "2.3"); |
|
471 | + return $this->options; |
|
472 | + } |
|
473 | + |
|
474 | + _elgg_services()->logger->warn("Read of non-existent property '$name'"); |
|
475 | + return null; |
|
476 | + } |
|
477 | + |
|
478 | + /** |
|
479 | + * Write a property |
|
480 | + * |
|
481 | + * @param string $name |
|
482 | + * @param mixed $value |
|
483 | + * @return void |
|
484 | + * @access private |
|
485 | + */ |
|
486 | + public function __set($name, $value) { |
|
487 | + if ($name === 'options') { |
|
488 | + elgg_deprecated_notice("The ElggBatch 'options' property is private and should not be used", "2.3"); |
|
489 | + } |
|
490 | + |
|
491 | + $this->{$name} = $value; |
|
492 | + } |
|
493 | 493 | } |
@@ -450,7 +450,7 @@ |
||
450 | 450 | public function count() { |
451 | 451 | if (!is_callable($this->getter)) { |
452 | 452 | $inspector = new \Elgg\Debug\Inspector(); |
453 | - throw new RuntimeException("Getter is not callable: " . $inspector->describeCallable($this->getter)); |
|
453 | + throw new RuntimeException("Getter is not callable: ".$inspector->describeCallable($this->getter)); |
|
454 | 454 | } |
455 | 455 | |
456 | 456 | $options = array_merge($this->options, ['count' => true]); |