]> git.mxchange.org Git - friendica.git/blob - include/dbclean.php
c80e5a3be357f2c26d997a0827d554b15f976fb7
[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('config');
27         Config::load('system');
28
29         if (!Config::get('system', 'dbclean', false)) {
30                 return;
31         }
32
33         if ($argc == 2) {
34                 $stage = intval($argv[1]);
35         } else {
36                 $stage = 0;
37         }
38
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);
47         } else {
48                 remove_orphans($stage);
49         }
50 }
51
52 /**
53  * @brief Remove orphaned database entries
54  */
55 function remove_orphans($stage = 0) {
56         global $db;
57
58         $count = 0;
59
60         // With activated worker we split the deletion in many small tasks
61         if (Config::get("system", "worker")) {
62                 $limit = 1000;
63         } else {
64                 $limit = 10000;
65         }
66
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"]));
76                         }
77                 }
78                 $db->qclose();
79                 logger("Done deleting old global item entries from item table without user copy");
80         }
81
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"]));
89                         }
90                 }
91                 $db->qclose();
92                 logger("Done deleting items without parents");
93         }
94
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"]));
102                         }
103                 }
104                 $db->qclose();
105                 logger("Done deleting orphaned data from thread table");
106         }
107
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"]));
115                         }
116                 }
117                 $db->qclose();
118                 logger("Done deleting orphaned data from notify table");
119         }
120
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"]));
128                         }
129                 }
130                 $db->qclose();
131                 logger("Done deleting orphaned data from notify-threads table");
132         }
133
134
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"]));
142                         }
143                 }
144                 $db->qclose();
145                 logger("Done deleting orphaned data from sign table");
146         }
147
148
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"]));
156                         }
157                 }
158                 $db->qclose();
159                 logger("Done deleting orphaned data from term table");
160         }
161
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');
165         }
166
167 }
168
169 if (array_search(__file__,get_included_files())===0){
170   dbclean_run($_SERVER["argv"],$_SERVER["argc"]);
171   killme();
172 }
173 ?>