]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Fixes #5357: ElggBatch can now skip incomplete entities
authorSteve Clay <steve@mrclay.org>
Wed, 5 Jun 2013 02:50:58 +0000 (22:50 -0400)
committerSteve Clay <steve@mrclay.org>
Wed, 5 Jun 2013 02:50:58 +0000 (22:50 -0400)
engine/classes/ElggBatch.php
engine/tests/api/helpers.php

index 34520f2bca6eac105e1cc174e179c2bb41456948..83963ccee3ecb26d7e1efc797a79ab056b324aef 100644 (file)
@@ -291,14 +291,19 @@ class ElggBatch
 
                $this->incompleteEntities = array();
                $this->results = call_user_func_array($this->getter, array($options));
-               if ($this->incompleteEntities) {
-                       // @todo what to do here?
-               }
 
-               if ($this->results) {
+               // If there were incomplete entities, we pretend they were at the beginning of the results,
+               // fool the local counter to think it's skipped by them already, and update the running
+               // total as if the results contained the incompletes.
+               if ($this->results || $this->incompleteEntities) {
                        $this->chunkIndex++;
-                       $this->resultIndex = 0;
-                       $this->retrievedResults += count($this->results);
+                       $this->resultIndex = count($this->incompleteEntities);
+                       $this->retrievedResults += (count($this->results) + count($this->incompleteEntities));
+                       if (!$this->results) {
+                               // This fetch was *all* incompletes! We need to fetch until we can either
+                               // offer at least one row to iterate over, or give up.
+                               return $this->getNextResultsChunk();
+                       }
                        return true;
                } else {
                        return false;
index 753d02915f9360d2a18c05e564a4bc6e9ab5415f..43244636b9de312d5798e20fd79d5058a0730471 100644 (file)
@@ -579,7 +579,7 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
        }
 
        public function testElggBatchHandlesBrokenEntities() {
-               $num_test_entities = 4;
+               $num_test_entities = 6;
                $guids = array();
                $now = time();
                for ($i = $num_test_entities; $i > 0; $i--) {
@@ -593,25 +593,28 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
                        _elgg_invalidate_cache_for_entity($entity->guid);
                }
 
-               // break the second entity
+               // break entities such that the first fetch has one incomplete
+               // and the second fetch has only incompletes!
                $db_prefix = elgg_get_config('dbprefix');
-               delete_data("DELETE FROM {$db_prefix}objects_entity WHERE guid = {$guids[1]}");
+               delete_data("
+                       DELETE FROM {$db_prefix}objects_entity
+                       WHERE guid IN ({$guids[1]}, {$guids[2]}, {$guids[3]})
+               ");
 
                $options = array(
                        'type' => 'object',
                        'subtype' => 'test_5357_subtype',
-                       'order' => 'e.time_created ASC',
+                       'order_by' => 'e.time_created ASC',
                );
 
                $entities_visited = array();
-
                $batch = new ElggBatch('elgg_get_entities', $options, null, 2);
                foreach ($batch as $entity) {
-                       $entities_visited[$entity->guid] = true;
+                       $entities_visited[] = $entity->guid;
                }
 
-               // All but the broken entity should have been visited
-               $this->assertEqual(count($entities_visited), $num_test_entities - 1);
+               // The broken entities should not have been visited
+               $this->assertEqual($entities_visited, array($guids[0], $guids[4], $guids[5]));
 
                // cleanup (including leftovers from previous tests)
                $entity_rows = elgg_get_entities(array_merge($options, array(