3 * @file include/dbclean.php
4 * @brief The script is called from time to time to clean the database entries and remove orphaned data.
7 use \Friendica\Core\Config;
8 use \Friendica\Core\PConfig;
10 require_once("boot.php");
12 function dbclean_run(&$argv, &$argc) {
20 @include(".htconfig.php");
21 require_once("include/dba.php");
22 $db = new dba($db_host, $db_user, $db_pass, $db_data);
23 unset($db_host, $db_user, $db_pass, $db_data);
26 load_config('config');
27 load_config('system');
29 if (!Config::get('system', 'dbclean', false)) {
34 $stage = intval($argv[1]);
39 if (get_config("system", "worker") AND ($stage == 0)) {
40 proc_run(PRIORITY_LOW, 'include/dbclean.php', 1);
41 proc_run(PRIORITY_LOW, 'include/dbclean.php', 2);
42 proc_run(PRIORITY_LOW, 'include/dbclean.php', 3);
43 proc_run(PRIORITY_LOW, 'include/dbclean.php', 4);
44 proc_run(PRIORITY_LOW, 'include/dbclean.php', 5);
45 proc_run(PRIORITY_LOW, 'include/dbclean.php', 6);
46 proc_run(PRIORITY_LOW, 'include/dbclean.php', 7);
48 remove_orphans($stage);
53 * @brief Remove orphaned database entries
55 function remove_orphans($stage = 0) {
60 if (($stage == 1) OR ($stage == 0)) {
61 logger("Deleting old global item entries from item table without user copy");
62 if ($db->q("SELECT `id` FROM `item` WHERE `uid` = 0
63 AND NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0)
64 AND `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY LIMIT 10000", true)) {
65 $count = $db->num_rows();
66 logger("found global item orphans: ".$count);
67 while ($orphan = $db->qfetch()) {
68 q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
72 logger("Done deleting old global item entries from item table without user copy");
75 if (($stage == 2) OR ($stage == 0)) {
76 logger("Deleting items without parents");
77 if ($db->q("SELECT `id` FROM `item` WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`) LIMIT 10000", true)) {
78 $count = $db->num_rows();
79 logger("found item orphans without parents: ".$count);
80 while ($orphan = $db->qfetch()) {
81 q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
85 logger("Done deleting items without parents");
88 if (($stage == 3) OR ($stage == 0)) {
89 logger("Deleting orphaned data from thread table");
90 if ($db->q("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`)", true)) {
91 $count = $db->num_rows();
92 logger("found thread orphans: ".$count);
93 while ($orphan = $db->qfetch()) {
94 q("DELETE FROM `thread` WHERE `iid` = %d", intval($orphan["iid"]));
98 logger("Done deleting orphaned data from thread table");
101 if (($stage == 4) OR ($stage == 0)) {
102 logger("Deleting orphaned data from notify table");
103 if ($db->q("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`)", true)) {
104 $count = $db->num_rows();
105 logger("found notify orphans: ".$count);
106 while ($orphan = $db->qfetch()) {
107 q("DELETE FROM `notify` WHERE `iid` = %d", intval($orphan["iid"]));
111 logger("Done deleting orphaned data from notify table");
114 if (($stage == 5) OR ($stage == 0)) {
115 logger("Deleting orphaned data from notify-threads table");
116 if ($db->q("SELECT `id` FROM `notify-threads` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`)", true)) {
117 $count = $db->num_rows();
118 logger("found notify-threads orphans: ".$count);
119 while ($orphan = $db->qfetch()) {
120 q("DELETE FROM `notify-threads` WHERE `id` = %d", intval($orphan["id"]));
124 logger("Done deleting orphaned data from notify-threads table");
128 if (($stage == 6) OR ($stage == 0)) {
129 logger("Deleting orphaned data from sign table");
130 if ($db->q("SELECT `iid` FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`)", true)) {
131 $count = $db->num_rows();
132 logger("found sign orphans: ".$count);
133 while ($orphan = $db->qfetch()) {
134 q("DELETE FROM `sign` WHERE `iid` = %d", intval($orphan["iid"]));
138 logger("Done deleting orphaned data from sign table");
142 if (($stage == 7) OR ($stage == 0)) {
143 logger("Deleting orphaned data from term table");
144 if ($db->q("SELECT `oid` FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`)", true)) {
145 $count = $db->num_rows();
146 logger("found term orphans: ".$count);
147 while ($orphan = $db->qfetch()) {
148 q("DELETE FROM `term` WHERE `oid` = %d", intval($orphan["oid"]));
152 logger("Done deleting orphaned data from term table");
155 // Call it again if not all entries were purged
156 if (($stage != 0) AND ($count > 0) AND get_config("system", "worker")) {
157 proc_run(PRIORITY_LOW, 'include/dbclean.php');
162 if (array_search(__file__,get_included_files())===0){
163 dbclean_run($_SERVER["argv"],$_SERVER["argc"]);