]> git.mxchange.org Git - friendica.git/blob - src/Worker/DBClean.php
Merge pull request #9397 from vinzv/9238-red-color-unread-messages-faded
[friendica.git] / src / Worker / DBClean.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Worker;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\Worker;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28
29 /**
30  * The script is called from time to time to clean the database entries and remove orphaned data.
31  */
32 class DBClean {
33         public static function execute($stage = 0) {
34
35                 if (!DI::config()->get('system', 'dbclean', false)) {
36                         return;
37                 }
38
39                 if ($stage == 0) {
40                         self::forkCleanProcess();
41                 } else {
42                         self::removeOrphans($stage);
43                 }
44         }
45
46         /**
47          * Fork the different DBClean processes
48          */
49         private static function forkCleanProcess() {
50                 // Get the expire days for step 8 and 9
51                 $days = DI::config()->get('system', 'dbclean-expire-days', 0);
52
53                 for ($i = 1; $i <= 9; $i++) {
54                         // Execute the background script for a step when it isn't finished.
55                         // Execute step 8 and 9 only when $days is defined.
56                         if (!DI::config()->get('system', 'finished-dbclean-'.$i, false) && (($i < 8) || ($i > 9) || ($days > 0))) {
57                                 Worker::add(PRIORITY_LOW, 'DBClean', $i);
58                         }
59                 }
60         }
61
62         /**
63          * Remove orphaned database entries
64          *
65          * @param integer $stage What should be deleted?
66          *
67          * Values for $stage:
68          * ------------------
69          *  1:    Old global item entries from item table without user copy.
70          *  2:    Items without parents.
71          *  3:    Legacy functionality (removed)
72          *  4:    Orphaned data from notify table.
73          *  5:    Legacy functionality (removed)
74          *  6:    Legacy functionality (removed)
75          *  7:    Legacy functionality (removed)
76          *  8:    Expired threads.
77          *  9:    Old global item entries from expired threads.
78          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
79          */
80         private static function removeOrphans($stage) {
81                 // We split the deletion in many small tasks
82                 $limit = DI::config()->get('system', 'dbclean-expire-limit', 1000);
83
84                 // Get the expire days for step 8 and 9
85                 $days = DI::config()->get('system', 'dbclean-expire-days', 0);
86                 $days_unclaimed = DI::config()->get('system', 'dbclean-expire-unclaimed', 90);
87
88                 if ($days_unclaimed == 0) {
89                         $days_unclaimed = $days;
90                 }
91
92                 if ($stage == 1) {
93                         if ($days_unclaimed <= 0) {
94                                 return;
95                         }
96
97                         $last_id = DI::config()->get('system', 'dbclean-last-id-1', 0);
98
99                         Logger::log("Deleting old global item entries from item table without user copy. Last ID: ".$last_id);
100                         $r = DBA::p("SELECT `id`, `guid` FROM `item` WHERE `uid` = 0 AND
101                                                 NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0) AND
102                                                 `received` < UTC_TIMESTAMP() - INTERVAL ? DAY AND `id` >= ?
103                                         ORDER BY `id` LIMIT ?", $days_unclaimed, $last_id, $limit);
104                         $count = DBA::numRows($r);
105                         if ($count > 0) {
106                                 Logger::log("found global item orphans: ".$count);
107                                 while ($orphan = DBA::fetch($r)) {
108                                         $last_id = $orphan["id"];
109                                         Logger::info('Delete global orphan item', ['id' => $orphan['id'], 'guid' => $orphan['guid']]);
110                                         DBA::delete('item', ['id' => $orphan["id"]]);
111                                 }
112                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 1, $last_id);
113                         } else {
114                                 Logger::log("No global item orphans found");
115                         }
116                         DBA::close($r);
117                         Logger::log("Done deleting ".$count." old global item entries from item table without user copy. Last ID: ".$last_id);
118
119                         DI::config()->set('system', 'dbclean-last-id-1', $last_id);
120                 } elseif ($stage == 2) {
121                         $last_id = DI::config()->get('system', 'dbclean-last-id-2', 0);
122
123                         Logger::log("Deleting items without parents. Last ID: ".$last_id);
124                         $r = DBA::p("SELECT `id`, `guid` FROM `item`
125                                         WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`)
126                                         AND `id` >= ? ORDER BY `id` LIMIT ?", $last_id, $limit);
127                         $count = DBA::numRows($r);
128                         if ($count > 0) {
129                                 Logger::log("found item orphans without parents: ".$count);
130                                 while ($orphan = DBA::fetch($r)) {
131                                         $last_id = $orphan["id"];
132                                         Logger::info('Delete orphan item', ['id' => $orphan['id'], 'guid' => $orphan['guid']]);
133                                         DBA::delete('item', ['id' => $orphan["id"]]);
134                                 }
135                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 2, $last_id);
136                         } else {
137                                 Logger::log("No item orphans without parents found");
138                         }
139                         DBA::close($r);
140                         Logger::log("Done deleting ".$count." items without parents. Last ID: ".$last_id);
141
142                         DI::config()->set('system', 'dbclean-last-id-2', $last_id);
143
144                         if ($count < $limit) {
145                                 DI::config()->set('system', 'finished-dbclean-2', true);
146                         }
147                 } elseif ($stage == 3) {
148                         // The legacy functionality had been removed
149                         DI::config()->set('system', 'finished-dbclean-3', true);
150                 } elseif ($stage == 4) {
151                         DBA::p("DELETE FROM `notify` WHERE NOT `type` IN (1, 2, 16, 32, 512) AND NOT `iid` IN (SELECT `id` FROM `item`)");
152
153                         Logger::notice("Deleted orphaned data from notify table.");
154                         DI::config()->set('system', 'finished-dbclean-4', true);
155                 } elseif ($stage == 5) {
156                         // The legacy functionality had been removed
157                         DI::config()->set('system', 'finished-dbclean-5', true);
158                 } elseif ($stage == 6) {
159                         // The legacy functionality had been removed
160                         DI::config()->set('system', 'finished-dbclean-6', true);
161                 } elseif ($stage == 7) {
162                         // The legacy functionality had been removed
163                         DI::config()->set('system', 'finished-dbclean-7', true);
164                 } elseif ($stage == 8) {
165                         if ($days <= 0) {
166                                 return;
167                         }
168
169                         $last_id = DI::config()->get('system', 'dbclean-last-id-8', 0);
170
171                         Logger::log("Deleting expired threads. Last ID: ".$last_id);
172                         $r = DBA::p("SELECT `thread`.`iid` FROM `thread`
173                                         INNER JOIN `contact` ON `thread`.`contact-id` = `contact`.`id` AND NOT `notify_new_posts`
174                                         WHERE `thread`.`received` < UTC_TIMESTAMP() - INTERVAL ? DAY
175                                                 AND NOT `thread`.`mention` AND NOT `thread`.`starred`
176                                                 AND NOT `thread`.`wall` AND NOT `thread`.`origin`
177                                                 AND `thread`.`uid` != 0 AND `thread`.`iid` >= ?
178                                                 AND NOT `thread`.`iid` IN (SELECT `parent` FROM `item`
179                                                                 WHERE (`item`.`starred` OR (`item`.`resource-id` != '')
180                                                                         OR (`item`.`file` != '') OR (`item`.`event-id` != '')
181                                                                         OR (`item`.`attach` != '') OR `item`.`wall` OR `item`.`origin`)
182                                                                         AND `item`.`parent` = `thread`.`iid`)
183                                         ORDER BY `thread`.`iid` LIMIT ?", $days, $last_id, $limit);
184                         $count = DBA::numRows($r);
185                         if ($count > 0) {
186                                 Logger::log("found expired threads: ".$count);
187                                 while ($thread = DBA::fetch($r)) {
188                                         $last_id = $thread["iid"];
189                                         DBA::delete('item', ['parent' => $thread["iid"]]);
190                                 }
191                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 8, $last_id);
192                         } else {
193                                 Logger::log("No expired threads found");
194                         }
195                         DBA::close($r);
196                         Logger::log("Done deleting ".$count." expired threads. Last ID: ".$last_id);
197
198                         DI::config()->set('system', 'dbclean-last-id-8', $last_id);
199                 } elseif ($stage == 9) {
200                         if ($days <= 0) {
201                                 return;
202                         }
203
204                         $last_id = DI::config()->get('system', 'dbclean-last-id-9', 0);
205                         $till_id = DI::config()->get('system', 'dbclean-last-id-8', 0);
206
207                         Logger::log("Deleting old global item entries from expired threads from ID ".$last_id." to ID ".$till_id);
208                         $r = DBA::p("SELECT `id`, `guid` FROM `item` WHERE `uid` = 0 AND
209                                                 NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0) AND
210                                                 `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY AND `id` >= ? AND `id` <= ?
211                                         ORDER BY `id` LIMIT ?", $last_id, $till_id, $limit);
212                         $count = DBA::numRows($r);
213                         if ($count > 0) {
214                                 Logger::log("found global item entries from expired threads: ".$count);
215                                 while ($orphan = DBA::fetch($r)) {
216                                         $last_id = $orphan["id"];
217                                         Logger::info('Delete expired thread item', ['id' => $orphan['id'], 'guid' => $orphan['guid']]);
218                                         DBA::delete('item', ['id' => $orphan["id"]]);
219                                 }
220                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 9, $last_id);
221                         } else {
222                                 Logger::log("No global item entries from expired threads");
223                         }
224                         DBA::close($r);
225                         Logger::log("Done deleting ".$count." old global item entries from expired threads. Last ID: ".$last_id);
226
227                         DI::config()->set('system', 'dbclean-last-id-9', $last_id);
228                 }
229         }
230 }