/**
  * Unit test for eZProductCollectionItem::cleanupList()
  *
  * Outline:
  * 1) Create 40 eZProductCollectionItemOption objects with an item_id
  *    from 1 to 4
  * 2) Call cleanupList with (1, 2) as a parameter
  * 4) Check that the 20 matching items have been removed
  * 5) Check that the 20 other, non-matching items haven't been removed
  */
 public function testCleanupList()
 {
     // Create a few collections
     $row = array('item_id' => null, 'option_item_id' => 1, 'object_attribute_id' => 1, 'name' => __FUNCTION__, 'value' => __FUNCTION__, 'price' => 5.5);
     $deleteIDArray = $keepIDArray = array();
     for ($i = 1; $i <= 40; $i++) {
         $row['item_id'] = ceil($i / 10);
         $item = new eZProductCollectionItemOption($row);
         $item->store();
     }
     eZProductCollectionItemOption::cleanupList(array(1, 2));
     // Check that each item of $deleteIDArray has been removed
     foreach (array(1, 2) as $itemID) {
         $options = eZProductCollectionItemOption::fetchList($itemID);
         $this->assertEquals(0, count($options));
     }
     // And check that each item of $keepIDArray hasn't been deleted
     foreach (array(3, 4) as $itemID) {
         $options = eZProductCollectionItemOption::fetchList($itemID);
         $this->assertEquals(10, count($options));
         foreach ($options as $option) {
             $this->assertInstanceOf('eZProductCollectionItemOption', $option);
         }
     }
 }
 function collectProductCollectionItems($maxTime = false, $sleepTime = false, $limit = false)
 {
     $db = eZDB::instance();
     if ($maxTime === false and $db->hasRequiredServerVersion('4.0', 'mysql')) {
         $sql = "DELETE FROM ezproductcollection_item, ezproductcollection_item_opt\nUSING ezproductcollection\n      LEFT JOIN ezproductcollection_item\n        ON ezproductcollection.id = ezproductcollection_item.productcollection_id\n      LEFT JOIN ezproductcollection_item_opt\n        ON ezproductcollection_item.id = ezproductcollection_item_opt.item_id\nWHERE ezproductcollection.id IS NULL";
         $db->query($sql);
         return;
     }
     // Find all items which are lacking a collection (db leaks)
     if ($db->hasRequiredServerVersion('8', 'oracle')) {
         $sql = "SELECT ezproductcollection_item.productcollection_id\nFROM ezproductcollection, ezproductcollection_item\nWHERE ezproductcollection.id = ezproductcollection_item.productcollection_id (+) AND\n      ezproductcollection.id IS NULL";
     } else {
         $sql = "SELECT ezproductcollection_item.productcollection_id\nFROM ezproductcollection\n     RIGHT JOIN ezproductcollection_item\n       ON ezproductcollection.id = ezproductcollection_item.productcollection_id\nWHERE ezproductcollection.id IS NULL";
     }
     if ($limit === false) {
         $limit = eZDBGarbageCollector::ITEM_LIMIT;
     }
     $end = false;
     if (is_numeric($maxTime)) {
         $end = time() + $maxTime;
     }
     do {
         $rows = $db->arrayQuery($sql, array('offset' => 0, 'limit' => $limit));
         if (count($rows) == 0) {
             break;
         }
         $idList = array();
         foreach ($rows as $row) {
             $idList[] = (int) $row['productcollection_id'];
         }
         eZProductCollectionItemOption::cleanupList($idList);
         $ids = implode(', ', $idList);
         $db->query("DELETE FROM ezproductcollection_item WHERE productcollection_id IN ( {$ids} )");
         // Stop when we used up our time
         if ($end !== false and time() > $end) {
             break;
         }
         // Sleep a little if required, reduces load on server
         if ($sleepTime !== false) {
             sleep($sleepTime);
         }
     } while (true);
 }
 /**
  * Removes all product collection items which related to the product
  * collections specified in the parameter array
  *
  * @param array $productCollectionIDList array of eZProductCollection IDs
  *
  * @return void
  */
 static function cleanupList($productCollectionIDList)
 {
     $db = eZDB::instance();
     $db->begin();
     $inText = $db->generateSQLINStatement($productCollectionIDList, 'productcollection_id', false, false, 'int');
     $rows = $db->arrayQuery("SELECT id FROM ezproductcollection_item WHERE {$inText}");
     if (count($rows) > 0) {
         $itemIDList = array();
         foreach ($rows as $row) {
             $itemIDList[] = $row['id'];
         }
         eZProductCollectionItemOption::cleanupList($itemIDList);
     }
     $db->query("DELETE FROM ezproductcollection_item WHERE {$inText}");
     $db->commit();
 }