]> git.mxchange.org Git - friendica.git/blob - src/Worker/Expire.php
Prevent permissionset foreign key constraint
[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 = DBA::select('item', ['id', 'guid', 'uri-id', 'uid'],  $condition);
48                         while ($row = DBA::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                         }
53                         DBA::close($rows);
54
55                         // Normally we shouldn't have orphaned data at all.
56                         // If we do have some, then we have to check why.
57                         Logger::log('Deleting orphaned item content - start', Logger::DEBUG);
58                         $condition = ["NOT EXISTS (SELECT `uri-id` FROM `item` WHERE `item`.`uri-id` = `item-content`.`uri-id`)"];
59                         DBA::delete('item-content', $condition);
60                         Logger::log('Orphaned item content deleted: ' . DBA::affectedRows(), Logger::DEBUG);
61
62                         // make this optional as it could have a performance impact on large sites
63                         if (intval(DI::config()->get('system', 'optimize_items'))) {
64                                 DBA::e("OPTIMIZE TABLE `item`");
65                         }
66
67                         Logger::log('Delete expired items - done', Logger::DEBUG);
68                         return;
69                 } elseif (intval($param) > 0) {
70                         $user = DBA::selectFirst('user', ['uid', 'username', 'expire'], ['uid' => $param]);
71                         if (DBA::isResult($user)) {
72                                 Logger::log('Expire items for user '.$user['uid'].' ('.$user['username'].') - interval: '.$user['expire'], Logger::DEBUG);
73                                 Item::expire($user['uid'], $user['expire']);
74                                 Logger::log('Expire items for user '.$user['uid'].' ('.$user['username'].') - done ', Logger::DEBUG);
75                         }
76                         return;
77                 } elseif ($param == 'hook' && !empty($hook_function)) {
78                         foreach (Hook::getByName('expire') as $hook) {
79                                 if ($hook[1] == $hook_function) {
80                                         Logger::log("Calling expire hook '" . $hook[1] . "'", Logger::DEBUG);
81                                         Hook::callSingle($a, 'expire', $hook, $data);
82                                 }
83                         }
84                         return;
85                 }
86
87                 Logger::log('expire: start');
88
89                 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
90                                 'Expire', 'delete');
91
92                 $r = DBA::p("SELECT `uid`, `username` FROM `user` WHERE `expire` != 0");
93                 while ($row = DBA::fetch($r)) {
94                         Logger::log('Calling expiry for user '.$row['uid'].' ('.$row['username'].')', Logger::DEBUG);
95                         Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
96                                         'Expire', (int)$row['uid']);
97                 }
98                 DBA::close($r);
99
100                 Logger::log('expire: calling hooks');
101                 foreach (Hook::getByName('expire') as $hook) {
102                         Logger::log("Calling expire hook for '" . $hook[1] . "'", Logger::DEBUG);
103                         Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
104                                         'Expire', 'hook', $hook[1]);
105                 }
106
107                 Logger::log('expire: end');
108
109                 return;
110         }
111 }