$sql = "";
                    $first_row = true;
                }
            }
        }
        $processed_objects[] = $cobj->getId();
        // check memory to stop script
        if (memory_get_usage(true) > SCRIPT_MEMORY_LIMIT) {
            $processed_objects_ids = "(" . implode("),(", $processed_objects) . ")";
            DB::execute("INSERT INTO " . TABLE_PREFIX . "processed_objects (object_id) VALUES {$processed_objects_ids} ON DUPLICATE KEY UPDATE object_id=object_id");
            $rest = Objects::count("id NOT IN(SELECT object_id FROM " . TABLE_PREFIX . "processed_objects)");
            $row = DB::executeOne("SELECT COUNT(object_id) AS 'row_count' FROM " . TABLE_PREFIX . "processed_objects");
            $proc_count = $row['row_count'];
            $status_message = "Memory limit exceeded (" . format_filesize(memory_get_usage(true)) . "). Script terminated. Processed Objects: {$proc_count}. Total: " . ($proc_count + $rest) . ". Please execute 'Fill searchable objects and sharing table' again.";
            $_SESSION['hide_back_button'] = 1;
            complete_migration_print("\n" . date("H:i:s") . " - Memory limit exceeded (" . format_filesize(memory_get_usage(true)) . ") script terminated. Processed Objects: " . count($processed_objects) . ". Total: {$proc_count}.");
            $processed_objects = array();
            break;
        }
        $cant++;
    }
    $cobj = null;
}
// add mails to sharing table for account owners
if ($sql != "") {
    $sql .= " ON DUPLICATE KEY UPDATE group_id=group_id;";
    DB::execute($sql);
    $sql = "";
}
if (count($processed_objects) > 0) {
    $processed_objects_ids = "(" . implode("),(", $processed_objects) . ")";
            foreach ($_SESSION['additional_steps'] as $k => $step) {
                if ($step['url'] == 'complete_migration.php') {
                    unset($_SESSION['additional_steps'][$k]);
                }
            }
        } else {
            if (!isset($_SESSION['additional_steps'])) {
                $_SESSION['additional_steps'] = array();
            }
            $add_step = true;
            foreach ($_SESSION['additional_steps'] as $step) {
                if ($step['url'] == 'complete_migration.php') {
                    $add_step = false;
                }
            }
            if ($add_step) {
                $_SESSION['additional_steps'][] = array('url' => 'complete_migration.php', 'name' => 'Fill searchable objects and sharing table', 'filename' => ROOT . "/" . PUBLIC_FOLDER . "/upgrade/complete_migration.php");
            }
        }
        if (!isset($_SESSION['status_messages'])) {
            $_SESSION['status_messages'] = array();
        }
        if (isset($status_message)) {
            $_SESSION['status_messages']['complete_migration'] = $status_message;
        }
        redirect_to(ROOT_URL . "/" . PUBLIC_FOLDER . "/upgrade/", false);
    }
} catch (Exception $e) {
    complete_migration_print("ERROR: " . $e->getMessage() . "\n");
    complete_migration_print("Trace:\n" . $e->getTraceAsString() . "\n");
}
		}
		$processed_objects[] = $cobj->getId();
		
		// check memory to stop script
		if (count($processed_objects) >= OBJECT_COUNT || memory_get_usage(true) > SCRIPT_MEMORY_LIMIT) {
			$processed_objects_ids = "(" . implode("),(", $processed_objects) . ")";
			DB::execute("INSERT INTO ".TABLE_PREFIX."processed_objects (object_id) VALUES $processed_objects_ids ON DUPLICATE KEY UPDATE object_id=object_id");
			
			$rest = Objects::count("id NOT IN(SELECT object_id FROM ".TABLE_PREFIX."processed_objects)");
			$row = DB::executeOne("SELECT COUNT(object_id) AS 'row_count' FROM ".TABLE_PREFIX."processed_objects");
			$proc_count = $row['row_count'];
			
			$status_message = "Memory limit exceeded (".format_filesize(memory_get_usage(true))."). Script terminated. Processed Objects: $proc_count. Total: ".($proc_count+$rest).". Please execute 'Fill searchable objects and sharing table' again.";
			$_SESSION['hide_back_button'] = 1;
			
			complete_migration_print("\n".date("H:i:s")." - Iteration finished or Memory limit exceeded (".format_filesize(memory_get_usage(true)).") script terminated.\nProcessed Objects: ".count($processed_objects). ".\nTotal processed objects: $proc_count.\n$rest objects left.\n$separator\n");
			$processed_objects = array();
			break;
		}
		$cant++;
		
	}
	$cobj = null;
}

// add mails to sharing table for account owners
if ($sql != "") {
	$sql .= " ON DUPLICATE KEY UPDATE group_id=group_id;";
	DB::execute($sql);
	$sql = "";
}