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