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;
29 use Friendica\Model\Item;
32 * Expires old item entries
36 public static function execute($param = '', $hook_function = '')
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', 'guid'], $condition);
47 while ($row = DBA::fetch($rows)) {
48 Logger::info('Delete expired item', ['id' => $row['id'], 'guid' => $row['guid']]);
49 DBA::delete('item', ['id' => $row['id']]);
53 // Normally we shouldn't have orphaned data at all.
54 // If we do have some, then we have to check why.
55 Logger::log('Deleting orphaned item activities - start', Logger::DEBUG);
56 $condition = ["NOT EXISTS (SELECT `iaid` FROM `item` WHERE `item`.`iaid` = `item-activity`.`id`)"];
57 DBA::delete('item-activity', $condition);
58 Logger::log('Orphaned item activities deleted: ' . DBA::affectedRows(), Logger::DEBUG);
60 Logger::log('Deleting orphaned item content - start', Logger::DEBUG);
61 $condition = ["NOT EXISTS (SELECT `icid` FROM `item` WHERE `item`.`icid` = `item-content`.`id`)"];
62 DBA::delete('item-content', $condition);
63 Logger::log('Orphaned item content deleted: ' . DBA::affectedRows(), Logger::DEBUG);
65 // make this optional as it could have a performance impact on large sites
66 if (intval(DI::config()->get('system', 'optimize_items'))) {
67 DBA::e("OPTIMIZE TABLE `item`");
70 Logger::log('Delete expired items - done', Logger::DEBUG);
72 } elseif (intval($param) > 0) {
73 $user = DBA::selectFirst('user', ['uid', 'username', 'expire'], ['uid' => $param]);
74 if (DBA::isResult($user)) {
75 Logger::log('Expire items for user '.$user['uid'].' ('.$user['username'].') - interval: '.$user['expire'], Logger::DEBUG);
76 Item::expire($user['uid'], $user['expire']);
77 Logger::log('Expire items for user '.$user['uid'].' ('.$user['username'].') - done ', Logger::DEBUG);
80 } elseif ($param == 'hook' && !empty($hook_function)) {
81 foreach (Hook::getByName('expire') as $hook) {
82 if ($hook[1] == $hook_function) {
83 Logger::log("Calling expire hook '" . $hook[1] . "'", Logger::DEBUG);
84 Hook::callSingle($a, 'expire', $hook, $data);
90 Logger::log('expire: start');
92 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
95 $r = DBA::p("SELECT `uid`, `username` FROM `user` WHERE `expire` != 0");
96 while ($row = DBA::fetch($r)) {
97 Logger::log('Calling expiry for user '.$row['uid'].' ('.$row['username'].')', Logger::DEBUG);
98 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
99 'Expire', (int)$row['uid']);
103 Logger::log('expire: calling hooks');
104 foreach (Hook::getByName('expire') as $hook) {
105 Logger::log("Calling expire hook for '" . $hook[1] . "'", Logger::DEBUG);
106 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
107 'Expire', 'hook', $hook[1]);
110 Logger::log('expire: end');