]> git.mxchange.org Git - friendica.git/blob - src/Worker/Expire.php
Avoid lock problems, fix foreign key problems with contact-relation
[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 = ["`gravity` = ? AND `deleted` AND `changed` < UTC_TIMESTAMP() - INTERVAL 60 DAY", GRAVITY_PARENT];
48                         $rows = Post::select(['guid', 'uri-id', 'uid'],  $condition);
49                         while ($row = Post::fetch($rows)) {
50                                 Logger::info('Delete expired item', ['uri-id' => $row['uri-id'], 'guid' => $row['guid']]);
51                                 if (DBStructure::existsTable('item')) {
52                                         DBA::delete('item', ['parent-uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
53                                 }
54                                 Post\User::delete(['parent-uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
55                         }
56                         DBA::close($rows);
57
58                         Logger::info('Deleting orphaned post entries- start');
59                         $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post`.`uri-id`)"];
60                         DBA::delete('post', $condition);
61                         Logger::info('Orphaned post entries deleted', ['rows' => DBA::affectedRows()]);
62
63                         Logger::info('Deleting orphaned post-content entries - start');
64                         $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-content`.`uri-id`)"];
65                         DBA::delete('post-content', $condition);
66                         Logger::info('Orphaned post-content entries deleted', ['rows' => DBA::affectedRows()]);
67
68                         Logger::info('Deleting orphaned post-thread entries - start');
69                         $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-thread`.`uri-id`)"];
70                         DBA::delete('post-thread', $condition);
71                         Logger::info('Orphaned post-thread entries deleted', ['rows' => DBA::affectedRows()]);
72
73                         Logger::info('Deleting orphaned post-thread-user entries - start');
74                         $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-thread-user`.`uri-id`)"];
75                         DBA::delete('post-thread-user', $condition);
76                         Logger::info('Orphaned post-thread-user entries deleted', ['rows' => DBA::affectedRows()]);
77
78                         Logger::log('Delete expired items - done', Logger::DEBUG);
79                         return;
80                 } elseif (intval($param) > 0) {
81                         $user = DBA::selectFirst('user', ['uid', 'username', 'expire'], ['uid' => $param]);
82                         if (DBA::isResult($user)) {
83                                 Logger::log('Expire items for user '.$user['uid'].' ('.$user['username'].') - interval: '.$user['expire'], Logger::DEBUG);
84                                 Item::expire($user['uid'], $user['expire']);
85                                 Logger::log('Expire items for user '.$user['uid'].' ('.$user['username'].') - done ', Logger::DEBUG);
86                         }
87                         return;
88                 } elseif ($param == 'hook' && !empty($hook_function)) {
89                         foreach (Hook::getByName('expire') as $hook) {
90                                 if ($hook[1] == $hook_function) {
91                                         Logger::log("Calling expire hook '" . $hook[1] . "'", Logger::DEBUG);
92                                         Hook::callSingle($a, 'expire', $hook, $data);
93                                 }
94                         }
95                         return;
96                 }
97
98                 Logger::log('expire: start');
99
100                 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
101                                 'Expire', 'delete');
102
103                 $r = DBA::p("SELECT `uid`, `username` FROM `user` WHERE `expire` != 0");
104                 while ($row = DBA::fetch($r)) {
105                         Logger::log('Calling expiry for user '.$row['uid'].' ('.$row['username'].')', Logger::DEBUG);
106                         Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
107                                         'Expire', (int)$row['uid']);
108                 }
109                 DBA::close($r);
110
111                 Logger::log('expire: calling hooks');
112                 foreach (Hook::getByName('expire') as $hook) {
113                         Logger::log("Calling expire hook for '" . $hook[1] . "'", Logger::DEBUG);
114                         Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
115                                         'Expire', 'hook', $hook[1]);
116                 }
117
118                 Logger::log('expire: end');
119
120                 return;
121         }
122 }