]> git.mxchange.org Git - friendica.git/blob - include/dbclean.php
Merge remote-tracking branch 'upstream/develop' into 1701-performance
[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 use \Friendica\Core\PConfig;
9
10 require_once("boot.php");
11
12 function dbclean_run(&$argv, &$argc) {
13         global $a, $db;
14
15         if (is_null($a)) {
16                 $a = new App;
17         }
18
19         if (is_null($db)) {
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);
24         }
25
26         Config::load();
27
28         if (!Config::get('system', 'dbclean', false)) {
29                 return;
30         }
31
32         if ($argc == 2) {
33                 $stage = intval($argv[1]);
34         } else {
35                 $stage = 0;
36         }
37
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);
46         } else {
47                 remove_orphans($stage);
48         }
49 }
50
51 /**
52  * @brief Remove orphaned database entries
53  */
54 function remove_orphans($stage = 0) {
55         global $db;
56
57         $count = 0;
58
59         // With activated worker we split the deletion in many small tasks
60         if (Config::get("system", "worker")) {
61                 $limit = 1000;
62         } else {
63                 $limit = 10000;
64         }
65
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"]));
75                         }
76                 }
77                 $db->qclose();
78                 logger("Done deleting old global item entries from item table without user copy");
79         }
80
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"]));
88                         }
89                 }
90                 $db->qclose();
91                 logger("Done deleting items without parents");
92         }
93
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"]));
101                         }
102                 }
103                 $db->qclose();
104                 logger("Done deleting orphaned data from thread table");
105         }
106
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"]));
114                         }
115                 }
116                 $db->qclose();
117                 logger("Done deleting orphaned data from notify table");
118         }
119
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"]));
127                         }
128                 }
129                 $db->qclose();
130                 logger("Done deleting orphaned data from notify-threads table");
131         }
132
133
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"]));
141                         }
142                 }
143                 $db->qclose();
144                 logger("Done deleting orphaned data from sign table");
145         }
146
147
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"]));
155                         }
156                 }
157                 $db->qclose();
158                 logger("Done deleting orphaned data from term table");
159         }
160
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');
164         }
165
166 }
167
168 if (array_search(__file__,get_included_files())===0){
169   dbclean_run($_SERVER["argv"],$_SERVER["argc"]);
170   killme();
171 }
172 ?>