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 = ["`deleted` AND `changed` < UTC_TIMESTAMP() - INTERVAL 60 DAY"];
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', ['uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
54 Post\User::delete(['uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
55 Post\ThreadUser::delete(['uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
59 Logger::info('Deleting orphaned post-content - start');
60 /// @todo Replace "item with "post-user" in the future when "item" is removed
61 $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-content`.`uri-id`)"];
62 DBA::delete('post-content', $condition);
63 Logger::info('Orphaned post-content deleted', ['rows' => DBA::affectedRows()]);
65 Logger::info('Deleting orphaned post-thread - start');
66 /// @todo Replace "item with "post-user" in the future when "item" is removed
67 $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-thread`.`uri-id`)"];
68 DBA::delete('post-thread', $condition);
69 Logger::info('Orphaned item content deleted', ['rows' => DBA::affectedRows()]);
71 Logger::log('Delete expired items - done', Logger::DEBUG);
73 } elseif (intval($param) > 0) {
74 $user = DBA::selectFirst('user', ['uid', 'username', 'expire'], ['uid' => $param]);
75 if (DBA::isResult($user)) {
76 Logger::log('Expire items for user '.$user['uid'].' ('.$user['username'].') - interval: '.$user['expire'], Logger::DEBUG);
77 Item::expire($user['uid'], $user['expire']);
78 Logger::log('Expire items for user '.$user['uid'].' ('.$user['username'].') - done ', Logger::DEBUG);
81 } elseif ($param == 'hook' && !empty($hook_function)) {
82 foreach (Hook::getByName('expire') as $hook) {
83 if ($hook[1] == $hook_function) {
84 Logger::log("Calling expire hook '" . $hook[1] . "'", Logger::DEBUG);
85 Hook::callSingle($a, 'expire', $hook, $data);
91 Logger::log('expire: start');
93 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
96 $r = DBA::p("SELECT `uid`, `username` FROM `user` WHERE `expire` != 0");
97 while ($row = DBA::fetch($r)) {
98 Logger::log('Calling expiry for user '.$row['uid'].' ('.$row['username'].')', Logger::DEBUG);
99 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
100 'Expire', (int)$row['uid']);
104 Logger::log('expire: calling hooks');
105 foreach (Hook::getByName('expire') as $hook) {
106 Logger::log("Calling expire hook for '" . $hook[1] . "'", Logger::DEBUG);
107 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
108 'Expire', 'hook', $hook[1]);
111 Logger::log('expire: end');