]> git.mxchange.org Git - friendica.git/blob - include/dbclean.php
Some notification thingy
[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 require_once("boot.php");
7
8 function dbclean_run(&$argv, &$argc) {
9         global $a, $db;
10
11         if (is_null($a)) {
12                 $a = new App;
13         }
14
15         if (is_null($db)) {
16                 @include(".htconfig.php");
17                 require_once("include/dba.php");
18                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
19                 unset($db_host, $db_user, $db_pass, $db_data);
20         }
21
22         load_config('config');
23         load_config('system');
24
25         if (!get_config("system", "dbclean")) {
26                 return;
27         }
28
29         if ($argc == 2) {
30                 $stage = intval($argv[1]);
31         } else {
32                 $stage = 0;
33         }
34
35         if (get_config("system", "worker") AND ($stage == 0)) {
36                 proc_run(PRIORITY_LOW, 'include/dbclean.php', 1);
37                 proc_run(PRIORITY_LOW, 'include/dbclean.php', 2);
38                 proc_run(PRIORITY_LOW, 'include/dbclean.php', 3);
39                 proc_run(PRIORITY_LOW, 'include/dbclean.php', 4);
40                 proc_run(PRIORITY_LOW, 'include/dbclean.php', 5);
41                 proc_run(PRIORITY_LOW, 'include/dbclean.php', 6);
42                 proc_run(PRIORITY_LOW, 'include/dbclean.php', 7);
43         } else {
44                 remove_orphans($stage);
45         }
46 }
47
48 /**
49  * @brief Remove orphaned database entries
50  */
51 function remove_orphans($stage = 0) {
52         global $db;
53
54         $count = 0;
55
56         if (($stage == 1) OR ($stage == 0)) {
57                 logger("Deleting old global item entries from item table without user copy");
58                 if ($db->q("SELECT `id` FROM `item` WHERE `uid` = 0
59                                 AND NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0)
60                                 AND `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY LIMIT 10000", true)) {
61                         $count = $db->num_rows();
62                         logger("found global item orphans: ".$count);
63                         while ($orphan = $db->qfetch()) {
64                                 q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
65                         }
66                 }
67                 $db->qclose();
68                 logger("Done deleting old global item entries from item table without user copy");
69         }
70
71         if (($stage == 2) OR ($stage == 0)) {
72                 logger("Deleting items without parents");
73                 if ($db->q("SELECT `id` FROM `item` WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`) LIMIT 10000", true)) {
74                         $count = $db->num_rows();
75                         logger("found item orphans without parents: ".$count);
76                         while ($orphan = $db->qfetch()) {
77                                 q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
78                         }
79                 }
80                 $db->qclose();
81                 logger("Done deleting items without parents");
82         }
83
84         if (($stage == 3) OR ($stage == 0)) {
85                 logger("Deleting orphaned data from thread table");
86                 if ($db->q("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`)", true)) {
87                         $count = $db->num_rows();
88                         logger("found thread orphans: ".$count);
89                         while ($orphan = $db->qfetch()) {
90                                 q("DELETE FROM `thread` WHERE `iid` = %d", intval($orphan["iid"]));
91                         }
92                 }
93                 $db->qclose();
94                 logger("Done deleting orphaned data from thread table");
95         }
96
97         if (($stage == 4) OR ($stage == 0)) {
98                 logger("Deleting orphaned data from notify table");
99                 if ($db->q("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`)", true)) {
100                         $count = $db->num_rows();
101                         logger("found notify orphans: ".$count);
102                         while ($orphan = $db->qfetch()) {
103                                 q("DELETE FROM `notify` WHERE `iid` = %d", intval($orphan["iid"]));
104                         }
105                 }
106                 $db->qclose();
107                 logger("Done deleting orphaned data from notify table");
108         }
109
110         if (($stage == 5) OR ($stage == 0)) {
111                 logger("Deleting orphaned data from notify-threads table");
112                 if ($db->q("SELECT `id` FROM `notify-threads` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`)", true)) {
113                         $count = $db->num_rows();
114                         logger("found notify-threads orphans: ".$count);
115                         while ($orphan = $db->qfetch()) {
116                                 q("DELETE FROM `notify-threads` WHERE `id` = %d", intval($orphan["id"]));
117                         }
118                 }
119                 $db->qclose();
120                 logger("Done deleting orphaned data from notify-threads table");
121         }
122
123
124         if (($stage == 6) OR ($stage == 0)) {
125                 logger("Deleting orphaned data from sign table");
126                 if ($db->q("SELECT `iid` FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`)", true)) {
127                         $count = $db->num_rows();
128                         logger("found sign orphans: ".$count);
129                         while ($orphan = $db->qfetch()) {
130                                 q("DELETE FROM `sign` WHERE `iid` = %d", intval($orphan["iid"]));
131                         }
132                 }
133                 $db->qclose();
134                 logger("Done deleting orphaned data from sign table");
135         }
136
137
138         if (($stage == 7) OR ($stage == 0)) {
139                 logger("Deleting orphaned data from term table");
140                 if ($db->q("SELECT `oid` FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`)", true)) {
141                         $count = $db->num_rows();
142                         logger("found term orphans: ".$count);
143                         while ($orphan = $db->qfetch()) {
144                                 q("DELETE FROM `term` WHERE `oid` = %d", intval($orphan["oid"]));
145                         }
146                 }
147                 $db->qclose();
148                 logger("Done deleting orphaned data from term table");
149         }
150
151         // Call it again if not all entries were purged
152         if (($stage != 0) AND ($count > 0) AND get_config("system", "worker")) {
153                 proc_run(PRIORITY_LOW, 'include/dbclean.php');
154         }
155
156 }
157
158 if (array_search(__file__,get_included_files())===0){
159   dbclean_run($_SERVER["argv"],$_SERVER["argc"]);
160   killme();
161 }
162 ?>