include "init.php";
Env::useHelper('format');
define('SCRIPT_MEMORY_LIMIT', 1024 * 1024 * 1024);
// 1 GB
@set_time_limit(0);
ini_set('memory_limit', SCRIPT_MEMORY_LIMIT / (1024 * 1024) + 50 . 'M');
DB::execute("CREATE TABLE IF NOT EXISTS `fixed_objects` (\r\n  `object_id` INTEGER UNSIGNED,\r\n  PRIMARY KEY (`object_id`)\r\n) ENGINE = InnoDB;");
$drop_tmp_table = true;
//$object_ids = Objects::instance()->findAll(array('columns' => array('id','object_type_id'), 'conditions' => 'id NOT IN (SELECT object_id FROM fixed_objects)'));
$objects = DB::executeAll("\r\n\t\tSELECT id, object_type_id, member_id \r\n\t\tFROM " . TABLE_PREFIX . "objects o\r\n\t\t\tLEFT JOIN " . TABLE_PREFIX . "object_members om ON o.id = om.object_id\r\n\t\tWHERE id NOT IN (SELECT object_id FROM fixed_objects)\r\n\t\tGROUP BY o.id\r\n\t\t\r\n\t\t");
$processed_objects = array();
$i = 0;
$msg = "";
echo "\nObjects to process: " . count($objects) . "\n-----------------------------------------------------------------";
foreach ($objects as $key => $object) {
    ContentDataObjects::addObjToSharingTable($object['id'], $object['object_type_id'], !is_null($object['member_id']));
    $processed_objects[] = $object['id'];
    $i++;
    $memory_limit_exceeded = memory_get_usage(true) > SCRIPT_MEMORY_LIMIT;
    if ($i % 100 == 0 || $memory_limit_exceeded) {
        echo "\n{$i} objects processed. Mem usage: " . format_filesize(memory_get_usage(true));
        if (count($processed_objects) > 0) {
            DB::execute("INSERT INTO fixed_objects (object_id) VALUES (" . implode('),(', $processed_objects) . ");");
            $processed_objects = array();
        }
        ob_flush();
        if ($memory_limit_exceeded) {
            $msg = "Memory limit exceeded: " . format_filesize(memory_get_usage(true));
            $drop_tmp_table = false;
            break;
        }
 /**
  * 
  * 
  */
 function addToSharingTable()
 {
     $oid = $this->getId();
     $tid = $this->getObjectTypeId();
     $obj_mem_ids = $this->getMemberIds();
     ContentDataObjects::addObjToSharingTable($oid, $tid, count($obj_mem_ids));
 }