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