]> git.mxchange.org Git - friendica.git/blob - src/Worker/Expire.php
Merge pull request #9930 from annando/no-item
[friendica.git] / src / Worker / Expire.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\Hook;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Worker;
27 use Friendica\Database\DBA;
28 use Friendica\Database\DBStructure;
29 use Friendica\DI;
30 use Friendica\Model\Item;
31 use Friendica\Model\Post;
32
33 /**
34  * Expires old item entries
35  */
36 class Expire
37 {
38         public static function execute($param = '', $hook_function = '')
39         {
40                 $a = DI::app();
41
42                 Hook::loadHooks();
43
44                 if ($param == 'delete') {
45                         Logger::log('Delete expired items', Logger::DEBUG);
46                         // physically remove anything that has been deleted for more than two months
47                         $condition = ["`deleted` AND `changed` < UTC_TIMESTAMP() - INTERVAL 60 DAY"];
48                         $rows = Post::select(['item-id', 'guid', 'uri-id', 'uid'],  $condition);
49                         while ($row = Post::fetch($rows)) {
50                                 Logger::info('Delete expired item', ['id' => $row['item-id'], 'guid' => $row['guid']]);
51                                 if (DBStructure::existsTable('item')) {
52                                         DBA::delete('item', ['id' => $row['item-id']]);
53                                 }
54                                 Post\User::delete(['uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
55                                 Post\ThreadUser::delete(['uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
56                         }
57                         DBA::close($rows);
58
59                         Logger::info('Deleting orphaned post-content - start');
60                         /// @todo Replace "item with "post-user" in the future when "item" is removed
61                         $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-content`.`uri-id`)"];
62                         DBA::delete('post-content', $condition);
63                         Logger::info('Orphaned post-content deleted', ['rows' => DBA::affectedRows()]);
64
65                         Logger::info('Deleting orphaned post-thread - start');
66                         /// @todo Replace "item with "post-user" in the future when "item" is removed
67                         $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-thread`.`uri-id`)"];
68                         DBA::delete('post-thread', $condition);
69                         Logger::info('Orphaned item content deleted', ['rows' => DBA::affectedRows()]);
70
71                         Logger::log('Delete expired items - done', Logger::DEBUG);
72                         return;
73                 } elseif (intval($param) > 0) {
74                         $user = DBA::selectFirst('user', ['uid', 'username', 'expire'], ['uid' => $param]);
75                         if (DBA::isResult($user)) {
76                                 Logger::log('Expire items for user '.$user['uid'].' ('.$user['username'].') - interval: '.$user['expire'], Logger::DEBUG);
77                                 Item::expire($user['uid'], $user['expire']);
78                                 Logger::log('Expire items for user '.$user['uid'].' ('.$user['username'].') - done ', Logger::DEBUG);
79                         }
80                         return;
81                 } elseif ($param == 'hook' && !empty($hook_function)) {
82                         foreach (Hook::getByName('expire') as $hook) {
83                                 if ($hook[1] == $hook_function) {
84                                         Logger::log("Calling expire hook '" . $hook[1] . "'", Logger::DEBUG);
85                                         Hook::callSingle($a, 'expire', $hook, $data);
86                                 }
87                         }
88                         return;
89                 }
90
91                 Logger::log('expire: start');
92
93                 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
94                                 'Expire', 'delete');
95
96                 $r = DBA::p("SELECT `uid`, `username` FROM `user` WHERE `expire` != 0");
97                 while ($row = DBA::fetch($r)) {
98                         Logger::log('Calling expiry for user '.$row['uid'].' ('.$row['username'].')', Logger::DEBUG);
99                         Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
100                                         'Expire', (int)$row['uid']);
101                 }
102                 DBA::close($r);
103
104                 Logger::log('expire: calling hooks');
105                 foreach (Hook::getByName('expire') as $hook) {
106                         Logger::log("Calling expire hook for '" . $hook[1] . "'", Logger::DEBUG);
107                         Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
108                                         'Expire', 'hook', $hook[1]);
109                 }
110
111                 Logger::log('expire: end');
112
113                 return;
114         }
115 }