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);
28 if (!Config::get('system', 'dbclean', false)) {
33 $stage = intval($argv[1]);
38 if (Config::get("system", "worker") AND ($stage == 0)) {
39 proc_run(PRIORITY_LOW, 'include/dbclean.php', 1);
40 proc_run(PRIORITY_LOW, 'include/dbclean.php', 2);
41 proc_run(PRIORITY_LOW, 'include/dbclean.php', 3);
42 proc_run(PRIORITY_LOW, 'include/dbclean.php', 4);
43 proc_run(PRIORITY_LOW, 'include/dbclean.php', 5);
44 proc_run(PRIORITY_LOW, 'include/dbclean.php', 6);
45 proc_run(PRIORITY_LOW, 'include/dbclean.php', 7);
47 remove_orphans($stage);
52 * @brief Remove orphaned database entries
54 function remove_orphans($stage = 0) {
59 // With activated worker we split the deletion in many small tasks
60 if (Config::get("system", "worker")) {
66 if (($stage == 1) OR ($stage == 0)) {
67 logger("Deleting old global item entries from item table without user copy");
68 if ($db->q("SELECT `id` FROM `item` WHERE `uid` = 0
69 AND NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0)
70 AND `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY LIMIT ".intval($limit), true)) {
71 $count = $db->num_rows();
72 logger("found global item orphans: ".$count);
73 while ($orphan = $db->qfetch()) {
74 q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
78 logger("Done deleting old global item entries from item table without user copy");
81 if (($stage == 2) OR ($stage == 0)) {
82 logger("Deleting items without parents");
83 if ($db->q("SELECT `id` FROM `item` WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`) LIMIT ".intval($limit), true)) {
84 $count = $db->num_rows();
85 logger("found item orphans without parents: ".$count);
86 while ($orphan = $db->qfetch()) {
87 q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
91 logger("Done deleting items without parents");
94 if (($stage == 3) OR ($stage == 0)) {
95 logger("Deleting orphaned data from thread table");
96 if ($db->q("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`) LIMIT ".intval($limit), true)) {
97 $count = $db->num_rows();
98 logger("found thread orphans: ".$count);
99 while ($orphan = $db->qfetch()) {
100 q("DELETE FROM `thread` WHERE `iid` = %d", intval($orphan["iid"]));
104 logger("Done deleting orphaned data from thread table");
107 if (($stage == 4) OR ($stage == 0)) {
108 logger("Deleting orphaned data from notify table");
109 if ($db->q("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`) LIMIT ".intval($limit), true)) {
110 $count = $db->num_rows();
111 logger("found notify orphans: ".$count);
112 while ($orphan = $db->qfetch()) {
113 q("DELETE FROM `notify` WHERE `iid` = %d", intval($orphan["iid"]));
117 logger("Done deleting orphaned data from notify table");
120 if (($stage == 5) OR ($stage == 0)) {
121 logger("Deleting orphaned data from notify-threads table");
122 if ($db->q("SELECT `id` FROM `notify-threads` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`) LIMIT ".intval($limit), true)) {
123 $count = $db->num_rows();
124 logger("found notify-threads orphans: ".$count);
125 while ($orphan = $db->qfetch()) {
126 q("DELETE FROM `notify-threads` WHERE `id` = %d", intval($orphan["id"]));
130 logger("Done deleting orphaned data from notify-threads table");
134 if (($stage == 6) OR ($stage == 0)) {
135 logger("Deleting orphaned data from sign table");
136 if ($db->q("SELECT `iid` FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`) LIMIT ".intval($limit), true)) {
137 $count = $db->num_rows();
138 logger("found sign orphans: ".$count);
139 while ($orphan = $db->qfetch()) {
140 q("DELETE FROM `sign` WHERE `iid` = %d", intval($orphan["iid"]));
144 logger("Done deleting orphaned data from sign table");
148 if (($stage == 7) OR ($stage == 0)) {
149 logger("Deleting orphaned data from term table");
150 if ($db->q("SELECT `oid` FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`) LIMIT ".intval($limit), true)) {
151 $count = $db->num_rows();
152 logger("found term orphans: ".$count);
153 while ($orphan = $db->qfetch()) {
154 q("DELETE FROM `term` WHERE `oid` = %d", intval($orphan["oid"]));
158 logger("Done deleting orphaned data from term table");
161 // Call it again if not all entries were purged
162 if (($stage != 0) AND ($count > 0) AND Config::get("system", "worker")) {
163 proc_run(PRIORITY_MEDIUM, 'include/dbclean.php');
168 if (array_search(__file__,get_included_files())===0){
169 dbclean_run($_SERVER["argv"],$_SERVER["argc"]);