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 Config::load('config');
27 Config::load('system');
29 if (!Config::get('system', 'dbclean', false)) {
34 $stage = intval($argv[1]);
39 if (Config::get("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 // With activated worker we split the deletion in many small tasks
61 if (Config::get("system", "worker")) {
67 if (($stage == 1) OR ($stage == 0)) {
68 logger("Deleting old global item entries from item table without user copy");
69 if ($db->q("SELECT `id` FROM `item` WHERE `uid` = 0
70 AND NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0)
71 AND `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY LIMIT ".intval($limit), true)) {
72 $count = $db->num_rows();
73 logger("found global item orphans: ".$count);
74 while ($orphan = $db->qfetch()) {
75 q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
79 logger("Done deleting old global item entries from item table without user copy");
82 if (($stage == 2) OR ($stage == 0)) {
83 logger("Deleting items without parents");
84 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)) {
85 $count = $db->num_rows();
86 logger("found item orphans without parents: ".$count);
87 while ($orphan = $db->qfetch()) {
88 q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
92 logger("Done deleting items without parents");
95 if (($stage == 3) OR ($stage == 0)) {
96 logger("Deleting orphaned data from thread table");
97 if ($db->q("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`) LIMIT ".intval($limit), true)) {
98 $count = $db->num_rows();
99 logger("found thread orphans: ".$count);
100 while ($orphan = $db->qfetch()) {
101 q("DELETE FROM `thread` WHERE `iid` = %d", intval($orphan["iid"]));
105 logger("Done deleting orphaned data from thread table");
108 if (($stage == 4) OR ($stage == 0)) {
109 logger("Deleting orphaned data from notify table");
110 if ($db->q("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`) LIMIT ".intval($limit), true)) {
111 $count = $db->num_rows();
112 logger("found notify orphans: ".$count);
113 while ($orphan = $db->qfetch()) {
114 q("DELETE FROM `notify` WHERE `iid` = %d", intval($orphan["iid"]));
118 logger("Done deleting orphaned data from notify table");
121 if (($stage == 5) OR ($stage == 0)) {
122 logger("Deleting orphaned data from notify-threads table");
123 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)) {
124 $count = $db->num_rows();
125 logger("found notify-threads orphans: ".$count);
126 while ($orphan = $db->qfetch()) {
127 q("DELETE FROM `notify-threads` WHERE `id` = %d", intval($orphan["id"]));
131 logger("Done deleting orphaned data from notify-threads table");
135 if (($stage == 6) OR ($stage == 0)) {
136 logger("Deleting orphaned data from sign table");
137 if ($db->q("SELECT `iid` FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`) LIMIT ".intval($limit), true)) {
138 $count = $db->num_rows();
139 logger("found sign orphans: ".$count);
140 while ($orphan = $db->qfetch()) {
141 q("DELETE FROM `sign` WHERE `iid` = %d", intval($orphan["iid"]));
145 logger("Done deleting orphaned data from sign table");
149 if (($stage == 7) OR ($stage == 0)) {
150 logger("Deleting orphaned data from term table");
151 if ($db->q("SELECT `oid` FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`) LIMIT ".intval($limit), true)) {
152 $count = $db->num_rows();
153 logger("found term orphans: ".$count);
154 while ($orphan = $db->qfetch()) {
155 q("DELETE FROM `term` WHERE `oid` = %d", intval($orphan["oid"]));
159 logger("Done deleting orphaned data from term table");
162 // Call it again if not all entries were purged
163 if (($stage != 0) AND ($count > 0) AND Config::get("system", "worker")) {
164 proc_run(PRIORITY_MEDIUM, 'include/dbclean.php');
169 if (array_search(__file__,get_included_files())===0){
170 dbclean_run($_SERVER["argv"],$_SERVER["argc"]);