-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebuild_sharing_table.php
59 lines (49 loc) · 1.92 KB
/
rebuild_sharing_table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
chdir(dirname(__FILE__));
header("Content-type: text/plain");
define("CONSOLE_MODE", true);
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` (
`object_id` INTEGER UNSIGNED,
PRIMARY KEY (`object_id`)
) ENGINE = InnoDB;");
$drop_tmp_table = true;
$object_ids = Objects::instance()->findAll(array('id' => true, 'conditions' => 'id NOT IN (SELECT object_id FROM fixed_objects)'));
$processed_objects = array();
$i = 0;
$msg = "";
echo "\nObjects to process: " . count($object_ids) . "\n-----------------------------------------------------------------";
foreach ($object_ids as $object_id) {
$object = Objects::findObject($object_id);
if ($object instanceof ContentDataObject) {
$object->addToSharingTable();
}
$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;
}
}
}
echo "\n-----------------------------------------------------------------\nFinished: $i objects processed. $msg\n";
if (count($processed_objects) > 0) {
DB::execute("INSERT INTO fixed_objects (object_id) VALUES (".implode('),(', $processed_objects).");");
$processed_objects = array();
if ($drop_tmp_table) {
DB::execute("DROP TABLE `fixed_objects`;");
}
}