]> git.mxchange.org Git - friendica.git/blob - include/dbclean.php
29842834cb8e210f9cd5529ed619dd899f530353
[friendica.git] / include / dbclean.php
1 <?php
2 /**
3  * @file include/dbclean.php
4  * @brief The script is called from time to time to clean the database entries and remove orphaned data.
5  */
6
7 use Friendica\Core\Config;
8
9 function dbclean_run(&$argv, &$argc) {
10         if (!Config::get('system', 'dbclean', false)) {
11                 return;
12         }
13
14         if ($argc == 2) {
15                 $stage = intval($argv[1]);
16         } else {
17                 $stage = 0;
18         }
19
20         if ($stage == 0) {
21                 proc_run(PRIORITY_LOW, 'include/dbclean.php', 1);
22                 proc_run(PRIORITY_LOW, 'include/dbclean.php', 2);
23                 proc_run(PRIORITY_LOW, 'include/dbclean.php', 3);
24                 proc_run(PRIORITY_LOW, 'include/dbclean.php', 4);
25                 proc_run(PRIORITY_LOW, 'include/dbclean.php', 5);
26                 proc_run(PRIORITY_LOW, 'include/dbclean.php', 6);
27                 proc_run(PRIORITY_LOW, 'include/dbclean.php', 7);
28         } else {
29                 remove_orphans($stage);
30         }
31 }
32
33 /**
34  * @brief Remove orphaned database entries
35  */
36 function remove_orphans($stage = 0) {
37         global $db;
38
39         $count = 0;
40
41         // We split the deletion in many small tasks
42         $limit = 1000;
43
44         if (($stage == 1) OR ($stage == 0)) {
45                 logger("Deleting old global item entries from item table without user copy");
46                 $r = dba::p("SELECT `id` FROM `item` WHERE `uid` = 0
47                                 AND NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0)
48                                 AND `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY LIMIT ".intval($limit));
49                 $count = dba::num_rows($r);
50                 if ($count > 0) {
51                         logger("found global item orphans: ".$count);
52                         while ($orphan = dba::fetch($r)) {
53                                 dba::delete('item', array('id' => $orphan["id"]));
54                         }
55                 } else {
56                         logger("No global item orphans found");
57                 }
58                 dba::close($r);
59                 logger("Done deleting ".$count." old global item entries from item table without user copy");
60         }
61
62         if (($stage == 2) OR ($stage == 0)) {
63                 logger("Deleting items without parents");
64                 $r = dba::p("SELECT `id` FROM `item` WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`) LIMIT ".intval($limit));
65                 $count = dba::num_rows($r);
66                 if ($count > 0) {
67                         logger("found item orphans without parents: ".$count);
68                         while ($orphan = dba::fetch($r)) {
69                                 dba::delete('item', array('id' => $orphan["id"]));
70                         }
71                 } else {
72                         logger("No item orphans without parents found");
73                 }
74                 dba::close($r);
75                 logger("Done deleting ".$count." items without parents");
76         }
77
78         if (($stage == 3) OR ($stage == 0)) {
79                 logger("Deleting orphaned data from thread table");
80                 $r = dba::p("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`) LIMIT ".intval($limit));
81                 $count = dba::num_rows($r);
82                 if ($count > 0) {
83                         logger("found thread orphans: ".$count);
84                         while ($orphan = dba::fetch($r)) {
85                                 dba::delete('thread', array('iid' => $orphan["iid"]));
86                         }
87                 } else {
88                         logger("No thread orphans found");
89                 }
90
91                 dba::close($r);
92                 logger("Done deleting ".$count." orphaned data from thread table");
93         }
94
95         if (($stage == 4) OR ($stage == 0)) {
96                 logger("Deleting orphaned data from notify table");
97                 $r = dba::p("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`) LIMIT ".intval($limit));
98                 $count = dba::num_rows($r);
99                 if ($count > 0) {
100                         logger("found notify orphans: ".$count);
101                         while ($orphan = dba::fetch($r)) {
102                                 dba::delete('notify', array('iid' => $orphan["iid"]));
103                         }
104                 } else {
105                         logger("No notify orphans found");
106                 }
107                 dba::close($r);
108                 logger("Done deleting ".$count." orphaned data from notify table");
109         }
110
111         if (($stage == 5) OR ($stage == 0)) {
112                 logger("Deleting orphaned data from notify-threads table");
113                 $r = dba::p("SELECT `id` FROM `notify-threads` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`) LIMIT ".intval($limit));
114                 $count = dba::num_rows($r);
115                 if ($count > 0) {
116                         logger("found notify-threads orphans: ".$count);
117                         while ($orphan = dba::fetch($r)) {
118                                 dba::delete('notify-threads', array('id' => $orphan["id"]));
119                         }
120                 } else {
121                         logger("No notify-threads orphans found");
122                 }
123                 dba::close($r);
124                 logger("Done deleting ".$count." orphaned data from notify-threads table");
125         }
126
127
128         if (($stage == 6) OR ($stage == 0)) {
129                 logger("Deleting orphaned data from sign table");
130                 $r = dba::p("SELECT `iid` FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`) LIMIT ".intval($limit));
131                 $count = dba::num_rows($r);
132                 if ($count > 0) {
133                         logger("found sign orphans: ".$count);
134                         while ($orphan = dba::fetch($r)) {
135                                 dba::delete('sign', array('iid' => $orphan["iid"]));
136                         }
137                 } else {
138                         logger("No sign orphans found");
139                 }
140                 dba::close($r);
141                 logger("Done deleting ".$count." orphaned data from sign table");
142         }
143
144
145         if (($stage == 7) OR ($stage == 0)) {
146                 logger("Deleting orphaned data from term table");
147                 $r = dba::p("SELECT `oid` FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`) LIMIT ".intval($limit));
148                 $count = dba::num_rows($r);
149                 if ($count > 0) {
150                         logger("found term orphans: ".$count);
151                         while ($orphan = dba::fetch($r)) {
152                                 dba::delete('term', array('oid' => $orphan["oid"]));
153                         }
154                 } else {
155                         logger("No term orphans found");
156                 }
157                 dba::close($r);
158                 logger("Done deleting ".$count." orphaned data from term table");
159         }
160
161         // Call it again if not all entries were purged
162         if (($stage != 0) AND ($count > 0)) {
163                 proc_run(PRIORITY_MEDIUM, 'include/dbclean.php');
164         }
165
166 }