3 * @file src/Worker/Expire.php
4 * @brief Expires old item entries
7 namespace Friendica\Worker;
9 use Friendica\BaseObject;
10 use Friendica\Core\Addon;
11 use Friendica\Core\Config;
12 use Friendica\Core\Worker;
13 use Friendica\Database\DBA;
14 use Friendica\Database\DBM;
15 use Friendica\Model\Item;
17 require_once 'include/dba.php';
21 public static function execute($param = '', $hook_name = '')
23 $a = BaseObject::getApp();
25 require_once 'include/items.php';
29 if ($param == 'delete') {
30 logger('Delete expired items', LOGGER_DEBUG);
31 // physically remove anything that has been deleted for more than two months
32 $condition = ["`deleted` AND `changed` < UTC_TIMESTAMP() - INTERVAL 60 DAY"];
33 $rows = DBA::select('item', ['id', 'iaid', 'icid', 'psid'], $condition);
34 while ($row = DBA::fetch($rows)) {
35 DBA::delete('item', ['id' => $row['id']]);
36 if (!empty($row['iaid']) && !DBA::exists('item', ['iaid' => $row['iaid']])) {
37 DBA::delete('item-activity', ['id' => $row['iaid']]);
39 if (!empty($row['icid']) && !DBA::exists('item', ['icid' => $row['icid']])) {
40 DBA::delete('item-content', ['id' => $row['icid']]);
42 // When the permission set will be used in photo and events as well.
43 // this query here needs to be extended.
44 if (!empty($row['psid']) && !DBA::exists('item', ['psid' => $row['psid']])) {
45 DBA::delete('permissionset', ['id' => $row['psid']]);
50 // Normally we shouldn't have orphaned data at all.
51 // If we do have some, then we have to check why.
52 logger('Deleting orphaned item activities - start', LOGGER_DEBUG);
53 $condition = ["NOT EXISTS (SELECT `iaid` FROM `item` WHERE `item`.`iaid` = `item-activity`.`id`)"];
54 DBA::delete('item-activity', $condition);
55 logger('Orphaned item activities deleted: ' . DBA::affectedRows(), LOGGER_DEBUG);
57 logger('Deleting orphaned item content - start', LOGGER_DEBUG);
58 $condition = ["NOT EXISTS (SELECT `icid` FROM `item` WHERE `item`.`icid` = `item-content`.`id`)"];
59 DBA::delete('item-content', $condition);
60 logger('Orphaned item content deleted: ' . DBA::affectedRows(), LOGGER_DEBUG);
62 // make this optional as it could have a performance impact on large sites
63 if (intval(Config::get('system', 'optimize_items'))) {
64 DBA::e("OPTIMIZE TABLE `item`");
67 logger('Delete expired items - done', LOGGER_DEBUG);
69 } elseif (intval($param) > 0) {
70 $user = DBA::selectFirst('user', ['uid', 'username', 'expire'], ['uid' => $param]);
71 if (DBM::is_result($user)) {
72 logger('Expire items for user '.$user['uid'].' ('.$user['username'].') - interval: '.$user['expire'], LOGGER_DEBUG);
73 Item::expire($user['uid'], $user['expire']);
74 logger('Expire items for user '.$user['uid'].' ('.$user['username'].') - done ', LOGGER_DEBUG);
77 } elseif (!empty($hook_name) && ($param == 'hook') && is_array($a->hooks) && array_key_exists("expire", $a->hooks)) {
78 foreach ($a->hooks["expire"] as $hook) {
79 if ($hook[1] == $hook_name) {
80 logger("Calling expire hook '" . $hook[1] . "'", LOGGER_DEBUG);
81 Addon::callSingleHook($a, $hook_name, $hook, $data);
87 logger('expire: start');
89 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
92 $r = DBA::p("SELECT `uid`, `username` FROM `user` WHERE `expire` != 0");
93 while ($row = DBA::fetch($r)) {
94 logger('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']);
100 logger('expire: calling hooks');
102 if (is_array($a->hooks) && array_key_exists('expire', $a->hooks)) {
103 foreach ($a->hooks['expire'] as $hook) {
104 logger("Calling expire hook for '" . $hook[1] . "'", LOGGER_DEBUG);
105 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
106 'Expire', 'hook', $hook[1]);
110 logger('expire: end');