]> git.mxchange.org Git - friendica.git/blob - include/dbclean.php
64185b39d9ccdf6fa71740a2ae82dd858f98790f
[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                 for ($i = 1; $i <= 7; $i++) {
22                         if (!Config::get('system', 'finished-dbclean-'.$i)) {
23                                 proc_run(PRIORITY_LOW, 'include/dbclean.php', $i);
24                         }
25                 }
26         } else {
27                 remove_orphans($stage);
28         }
29 }
30
31 /**
32  * @brief Remove orphaned database entries
33  */
34 function remove_orphans($stage = 0) {
35         global $db;
36
37         $count = 0;
38
39         // We split the deletion in many small tasks
40         $limit = 1000;
41
42         if ($stage == 1) {
43                 logger("Deleting old global item entries from item table without user copy");
44                 $r = dba::p("SELECT `id` FROM `item` WHERE `uid` = 0
45                                 AND NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0)
46                                 AND `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY LIMIT ".intval($limit));
47                 $count = dba::num_rows($r);
48                 if ($count > 0) {
49                         logger("found global item orphans: ".$count);
50                         while ($orphan = dba::fetch($r)) {
51                                 dba::delete('item', array('id' => $orphan["id"]));
52                         }
53                 } else {
54                         logger("No global item orphans found");
55
56                         // We will eventually set this value when we found a good way to delete these items in another way.
57                         // Config::set('system', 'finished-dbclean-1', true);
58                 }
59                 dba::close($r);
60                 logger("Done deleting ".$count." old global item entries from item table without user copy");
61         } elseif ($stage == 2) {
62                 logger("Deleting items without parents");
63                 $r = dba::p("SELECT `id` FROM `item` WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`) LIMIT ".intval($limit));
64                 $count = dba::num_rows($r);
65                 if ($count > 0) {
66                         logger("found item orphans without parents: ".$count);
67                         while ($orphan = dba::fetch($r)) {
68                                 dba::delete('item', array('id' => $orphan["id"]));
69                         }
70                 } else {
71                         logger("No item orphans without parents found");
72                         Config::set('system', 'finished-dbclean-2', true);
73                 }
74                 dba::close($r);
75                 logger("Done deleting ".$count." items without parents");
76         } elseif ($stage == 3) {
77                 logger("Deleting orphaned data from thread table");
78                 $r = dba::p("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`) LIMIT ".intval($limit));
79                 $count = dba::num_rows($r);
80                 if ($count > 0) {
81                         logger("found thread orphans: ".$count);
82                         while ($orphan = dba::fetch($r)) {
83                                 dba::delete('thread', array('iid' => $orphan["iid"]));
84                         }
85                 } else {
86                         logger("No thread orphans found");
87                         Config::set('system', 'finished-dbclean-3', true);
88                 }
89
90                 dba::close($r);
91                 logger("Done deleting ".$count." orphaned data from thread table");
92         } elseif ($stage == 4) {
93                 logger("Deleting orphaned data from notify table");
94                 $r = dba::p("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`) LIMIT ".intval($limit));
95                 $count = dba::num_rows($r);
96                 if ($count > 0) {
97                         logger("found notify orphans: ".$count);
98                         while ($orphan = dba::fetch($r)) {
99                                 dba::delete('notify', array('iid' => $orphan["iid"]));
100                         }
101                 } else {
102                         logger("No notify orphans found");
103                         Config::set('system', 'finished-dbclean-4', true);
104                 }
105                 dba::close($r);
106                 logger("Done deleting ".$count." orphaned data from notify table");
107         } elseif ($stage == 5) {
108                 logger("Deleting orphaned data from notify-threads table");
109                 $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));
110                 $count = dba::num_rows($r);
111                 if ($count > 0) {
112                         logger("found notify-threads orphans: ".$count);
113                         while ($orphan = dba::fetch($r)) {
114                                 dba::delete('notify-threads', array('id' => $orphan["id"]));
115                         }
116                 } else {
117                         logger("No notify-threads orphans found");
118                         Config::set('system', 'finished-dbclean-5', true);
119                 }
120                 dba::close($r);
121                 logger("Done deleting ".$count." orphaned data from notify-threads table");
122         } elseif ($stage == 6) {
123                 logger("Deleting orphaned data from sign table");
124                 $r = dba::p("SELECT `iid` FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`) LIMIT ".intval($limit));
125                 $count = dba::num_rows($r);
126                 if ($count > 0) {
127                         logger("found sign orphans: ".$count);
128                         while ($orphan = dba::fetch($r)) {
129                                 dba::delete('sign', array('iid' => $orphan["iid"]));
130                         }
131                 } else {
132                         logger("No sign orphans found");
133                         Config::set('system', 'finished-dbclean-6', true);
134                 }
135                 dba::close($r);
136                 logger("Done deleting ".$count." orphaned data from sign table");
137         } elseif ($stage == 7) {
138                 logger("Deleting orphaned data from term table");
139                 $r = dba::p("SELECT `oid` FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`) LIMIT ".intval($limit));
140                 $count = dba::num_rows($r);
141                 if ($count > 0) {
142                         logger("found term orphans: ".$count);
143                         while ($orphan = dba::fetch($r)) {
144                                 dba::delete('term', array('oid' => $orphan["oid"]));
145                         }
146                 } else {
147                         logger("No term orphans found");
148                         Config::set('system', 'finished-dbclean-7', true);
149                 }
150                 dba::close($r);
151                 logger("Done deleting ".$count." orphaned data from term table");
152         }
153
154         // Call it again if not all entries were purged
155         if (($stage != 0) AND ($count > 0)) {
156                 proc_run(PRIORITY_MEDIUM, 'include/dbclean.php');
157         }
158
159 }