]> git.mxchange.org Git - friendica.git/blob - src/Worker/ExpirePosts.php
Changed worker order
[friendica.git] / src / Worker / ExpirePosts.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\Database\DBStructure;
28 use Friendica\DI;
29 use Friendica\Model\Item;
30 use Friendica\Model\Post;
31
32 class ExpirePosts
33 {
34         /**
35          * Expire posts and remove unused item-uri entries
36          *
37          * @return void
38          */
39         public static function execute()
40         {
41                 self::deleteExpiredOriginPosts();
42
43                 self::deleteUnusedItemUri();
44
45                 self::deleteExpiredExternalPosts();
46
47                 // Set the expiry for origin posta
48                 Worker::add(PRIORITY_LOW, 'Expire');
49
50                 // update nodeinfo data after everything is cleaned up
51                 Worker::add(PRIORITY_LOW, 'NodeInfo');
52         }
53
54         /**
55          * Delete expired origin posts and orphaned post related table entries
56          *
57          * @return void
58          */
59         private static function deleteExpiredOriginPosts()
60         {
61                 Logger::info('Delete expired posts');
62                 // physically remove anything that has been deleted for more than two months
63                 $condition = ["`gravity` = ? AND `deleted` AND `changed` < UTC_TIMESTAMP() - INTERVAL 60 DAY", GRAVITY_PARENT];
64                 $rows = Post::select(['guid', 'uri-id', 'uid'],  $condition);
65                 while ($row = Post::fetch($rows)) {
66                         Logger::info('Delete expired item', ['uri-id' => $row['uri-id'], 'guid' => $row['guid']]);
67                         if (DBStructure::existsTable('item')) {
68                                 DBA::delete('item', ['parent-uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
69                         }
70                         Post\User::delete(['parent-uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
71                 }
72                 DBA::close($rows);
73
74                 Logger::info('Deleting orphaned post entries - start');
75                 $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post`.`uri-id`)"];
76                 DBA::delete('post', $condition);
77                 Logger::info('Orphaned post entries deleted', ['rows' => DBA::affectedRows()]);
78
79                 Logger::info('Deleting orphaned post-content entries - start');
80                 $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-content`.`uri-id`)"];
81                 DBA::delete('post-content', $condition);
82                 Logger::info('Orphaned post-content entries deleted', ['rows' => DBA::affectedRows()]);
83
84                 Logger::info('Deleting orphaned post-thread entries - start');
85                 $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-thread`.`uri-id`)"];
86                 DBA::delete('post-thread', $condition);
87                 Logger::info('Orphaned post-thread entries deleted', ['rows' => DBA::affectedRows()]);
88
89                 Logger::info('Deleting orphaned post-thread-user entries - start');
90                 $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-thread-user`.`uri-id`)"];
91                 DBA::delete('post-thread-user', $condition);
92                 Logger::info('Orphaned post-thread-user entries deleted', ['rows' => DBA::affectedRows()]);
93
94                 Logger::info('Delete expired posts - done');
95         }
96
97         /**
98          * Delete unused item-uri entries
99          */
100         private static function deleteUnusedItemUri()
101         {
102                 $a = DI::app();
103
104                 // We have to avoid deleting newly created "item-uri" entries.
105                 // So we fetch a post that had been stored yesterday and only delete older ones.
106                 $item = Post::selectFirst(['uri-id'], ["`uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY", 0, 1],
107                         ['order' => ['received' => true]]);
108                 if (empty($item['uri-id'])) {
109                         Logger::warning('No item with uri-id found - we better quit here');
110                         return;
111                 }
112                 Logger::notice('Start deleting orphaned URI-ID', ['last-id' => $item['uri-id']]);
113                 $uris = DBA::select('item-uri', ['id'], ["`id` < ?
114                         AND NOT EXISTS(SELECT `uri-id` FROM `post` WHERE `uri-id` = `item-uri`.`id`)
115                         AND NOT EXISTS(SELECT `parent-uri-id` FROM `post` WHERE `parent-uri-id` = `item-uri`.`id`)
116                         AND NOT EXISTS(SELECT `thr-parent-id` FROM `post` WHERE `thr-parent-id` = `item-uri`.`id`)
117                         AND NOT EXISTS(SELECT `external-id` FROM `post` WHERE `external-id` = `item-uri`.`id`)", $item['uri-id']]);
118
119                 $affected_count = 0;
120                 while ($rows = DBA::toArray($uris, false, 100)) {
121                         $ids = array_column($rows, 'id');
122                         DBA::delete('item-uri', ['id' => $ids]);
123                         $affected_count += DBA::affectedRows();
124                         Logger::info('Deleted', ['rows' => $affected_count]);
125                 }
126                 DBA::close($uris);
127                 Logger::notice('Orphaned URI-ID entries removed', ['rows' => $affected_count]);
128         }
129
130         /**
131          * Delete old external post entries
132          */
133         private static function deleteExpiredExternalPosts()
134         {
135                 $expire_days = DI::config()->get('system', 'dbclean-expire-days');
136                 $expire_days_unclaimed = DI::config()->get('system', 'dbclean-expire-unclaimed');
137                 if (empty($expire_days_unclaimed)) {
138                         $expire_days_unclaimed = $expire_days;
139                 }
140
141                 $limit = DI::config()->get('system', 'dbclean-expire-limit');
142                 if (empty($limit)) {
143                         return;
144                 }
145
146                 if (!empty($expire_days)) {
147                         Logger::notice('Start collecting expired threads', ['expiry_days' => $expire_days]);
148                         $uris = DBA::select('item-uri', ['id'], ["`id` IN
149                                 (SELECT `uri-id` FROM `post-thread` WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
150                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-thread-user`
151                                                 WHERE (`mention` OR `starred` OR `wall` OR `pinned`) AND `uri-id` = `post-thread`.`uri-id`)
152                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-category`
153                                                 WHERE `uri-id` = `post-thread`.`uri-id`)
154                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-media`
155                                                 WHERE `uri-id` = `post-thread`.`uri-id`)
156                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` INNER JOIN `contact` ON `contact`.`id` = `contact-id` AND `notify_new_posts`
157                                                 WHERE `parent-uri-id` = `post-thread`.`uri-id`)
158                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user`
159                                                 WHERE (`origin` OR `event-id` != 0 OR `post-type` = ?) AND `parent-uri-id` = `post-thread`.`uri-id`)
160                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-content`
161                                                 WHERE `resource-id` != 0 AND `uri-id` = `post-thread`.`uri-id`))",
162                                 $expire_days, Item::PT_PERSONAL_NOTE]);
163
164                         Logger::notice('Start deleting expired threads');
165                         $affected_count = 0;
166                         while ($rows = DBA::toArray($uris, false, 100)) {
167                                 $ids = array_column($rows, 'id');
168                                 DBA::delete('item-uri', ['id' => $ids]);
169                                 $affected_count += DBA::affectedRows();
170                         }
171                         DBA::close($uris);
172
173                         Logger::notice('Deleted expired threads', ['rows' => $affected_count]);
174                 }
175
176                 if (!empty($expire_days_unclaimed)) {
177                         Logger::notice('Start collecting unclaimed public items', ['expiry_days' => $expire_days_unclaimed]);
178                         $uris = DBA::select('item-uri', ['id'], ["`id` IN
179                                 (SELECT `uri-id` FROM `post-user` WHERE `gravity` = ? AND `uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
180                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` != ?
181                                                 AND `i`.`parent-uri-id` = `post-user`.`uri-id`)
182                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` = ?
183                                                 AND `i`.`parent-uri-id` = `post-user`.`uri-id` AND `i`.`received` > UTC_TIMESTAMP() - INTERVAL ? DAY))",
184                                 GRAVITY_PARENT, 0, $expire_days_unclaimed, 0, 0, $expire_days_unclaimed]);
185
186                         Logger::notice('Start deleting unclaimed public items');
187                         $affected_count = 0;
188                         while ($rows = DBA::toArray($uris, false, 100)) {
189                                 $ids = array_column($rows, 'id');
190                                 DBA::delete('item-uri', ['id' => $ids]);
191                                 $affected_count += DBA::affectedRows();
192                         }
193                         DBA::close($uris);
194                         Logger::notice('Deleted unclaimed public items', ['rows' => $affected_count]);
195                 }
196         }
197 }