3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Worker;
24 use Friendica\Core\Logger;
25 use Friendica\Core\Worker;
26 use Friendica\Database\DBA;
30 * The script is called from time to time to clean the database entries and remove orphaned data.
33 public static function execute($stage = 0) {
35 if (!DI::config()->get('system', 'dbclean', false)) {
40 self::forkCleanProcess();
42 self::removeOrphans($stage);
47 * Fork the different DBClean processes
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);
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);
63 * Remove orphaned database entries
65 * @param integer $stage What should be deleted?
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)
77 * 9: Old global item entries from expired threads.
78 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
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);
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);
88 if ($days_unclaimed == 0) {
89 $days_unclaimed = $days;
93 if ($days_unclaimed <= 0) {
97 $last_id = DI::config()->get('system', 'dbclean-last-id-1', 0);
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);
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"]]);
112 Worker::add(PRIORITY_MEDIUM, 'DBClean', 1, $last_id);
114 Logger::log("No global item orphans found");
117 Logger::log("Done deleting ".$count." old global item entries from item table without user copy. Last ID: ".$last_id);
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);
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);
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"]]);
135 Worker::add(PRIORITY_MEDIUM, 'DBClean', 2, $last_id);
137 Logger::log("No item orphans without parents found");
140 Logger::log("Done deleting ".$count." items without parents. Last ID: ".$last_id);
142 DI::config()->set('system', 'dbclean-last-id-2', $last_id);
144 if ($count < $limit) {
145 DI::config()->set('system', 'finished-dbclean-2', true);
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`)");
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) {
169 $last_id = DI::config()->get('system', 'dbclean-last-id-8', 0);
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);
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"]]);
191 Worker::add(PRIORITY_MEDIUM, 'DBClean', 8, $last_id);
193 Logger::log("No expired threads found");
196 Logger::log("Done deleting ".$count." expired threads. Last ID: ".$last_id);
198 DI::config()->set('system', 'dbclean-last-id-8', $last_id);
199 } elseif ($stage == 9) {
204 $last_id = DI::config()->get('system', 'dbclean-last-id-9', 0);
205 $till_id = DI::config()->get('system', 'dbclean-last-id-8', 0);
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);
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"]]);
220 Worker::add(PRIORITY_MEDIUM, 'DBClean', 9, $last_id);
222 Logger::log("No global item entries from expired threads");
225 Logger::log("Done deleting ".$count." old global item entries from expired threads. Last ID: ".$last_id);
227 DI::config()->set('system', 'dbclean-last-id-9', $last_id);