]> git.mxchange.org Git - friendica.git/blob - include/dbclean.php
it should be "gid" not "cid"
[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         load_config('config');
27         load_config('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 (get_config("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         if (($stage == 1) OR ($stage == 0)) {
61                 logger("Deleting old global item entries from item table without user copy");
62                 if ($db->q("SELECT `id` FROM `item` WHERE `uid` = 0
63                                 AND NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0)
64                                 AND `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY LIMIT 10000", true)) {
65                         $count = $db->num_rows();
66                         logger("found global item orphans: ".$count);
67                         while ($orphan = $db->qfetch()) {
68                                 q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
69                         }
70                 }
71                 $db->qclose();
72                 logger("Done deleting old global item entries from item table without user copy");
73         }
74
75         if (($stage == 2) OR ($stage == 0)) {
76                 logger("Deleting items without parents");
77                 if ($db->q("SELECT `id` FROM `item` WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`) LIMIT 10000", true)) {
78                         $count = $db->num_rows();
79                         logger("found item orphans without parents: ".$count);
80                         while ($orphan = $db->qfetch()) {
81                                 q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
82                         }
83                 }
84                 $db->qclose();
85                 logger("Done deleting items without parents");
86         }
87
88         if (($stage == 3) OR ($stage == 0)) {
89                 logger("Deleting orphaned data from thread table");
90                 if ($db->q("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`)", true)) {
91                         $count = $db->num_rows();
92                         logger("found thread orphans: ".$count);
93                         while ($orphan = $db->qfetch()) {
94                                 q("DELETE FROM `thread` WHERE `iid` = %d", intval($orphan["iid"]));
95                         }
96                 }
97                 $db->qclose();
98                 logger("Done deleting orphaned data from thread table");
99         }
100
101         if (($stage == 4) OR ($stage == 0)) {
102                 logger("Deleting orphaned data from notify table");
103                 if ($db->q("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`)", true)) {
104                         $count = $db->num_rows();
105                         logger("found notify orphans: ".$count);
106                         while ($orphan = $db->qfetch()) {
107                                 q("DELETE FROM `notify` WHERE `iid` = %d", intval($orphan["iid"]));
108                         }
109                 }
110                 $db->qclose();
111                 logger("Done deleting orphaned data from notify table");
112         }
113
114         if (($stage == 5) OR ($stage == 0)) {
115                 logger("Deleting orphaned data from notify-threads table");
116                 if ($db->q("SELECT `id` FROM `notify-threads` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`)", true)) {
117                         $count = $db->num_rows();
118                         logger("found notify-threads orphans: ".$count);
119                         while ($orphan = $db->qfetch()) {
120                                 q("DELETE FROM `notify-threads` WHERE `id` = %d", intval($orphan["id"]));
121                         }
122                 }
123                 $db->qclose();
124                 logger("Done deleting orphaned data from notify-threads table");
125         }
126
127
128         if (($stage == 6) OR ($stage == 0)) {
129                 logger("Deleting orphaned data from sign table");
130                 if ($db->q("SELECT `iid` FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`)", true)) {
131                         $count = $db->num_rows();
132                         logger("found sign orphans: ".$count);
133                         while ($orphan = $db->qfetch()) {
134                                 q("DELETE FROM `sign` WHERE `iid` = %d", intval($orphan["iid"]));
135                         }
136                 }
137                 $db->qclose();
138                 logger("Done deleting orphaned data from sign table");
139         }
140
141
142         if (($stage == 7) OR ($stage == 0)) {
143                 logger("Deleting orphaned data from term table");
144                 if ($db->q("SELECT `oid` FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`)", true)) {
145                         $count = $db->num_rows();
146                         logger("found term orphans: ".$count);
147                         while ($orphan = $db->qfetch()) {
148                                 q("DELETE FROM `term` WHERE `oid` = %d", intval($orphan["oid"]));
149                         }
150                 }
151                 $db->qclose();
152                 logger("Done deleting orphaned data from term table");
153         }
154
155         // Call it again if not all entries were purged
156         if (($stage != 0) AND ($count > 0) AND get_config("system", "worker")) {
157                 proc_run(PRIORITY_LOW, 'include/dbclean.php');
158         }
159
160 }
161
162 if (array_search(__file__,get_included_files())===0){
163   dbclean_run($_SERVER["argv"],$_SERVER["argc"]);
164   killme();
165 }
166 ?>