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