3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Worker;
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;
30 use Friendica\Model\Item;
31 use Friendica\Model\Post;
34 * Expires old item entries
38 public static function execute($param = '', $hook_function = '')
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']]);
54 Post\User::delete(['parent-uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
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()]);
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()]);
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()]);
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()]);
78 Logger::log('Delete expired items - done', Logger::DEBUG);
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);
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);
98 Logger::log('expire: start');
100 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
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']);
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]);
118 Logger::log('expire: end');