3 * @file src/Worker/Expire.php
4 * @brief Expires old item entries
7 namespace Friendica\Worker;
9 use Friendica\Core\Addon;
10 use Friendica\Core\Config;
11 use Friendica\Core\Worker;
12 use Friendica\Database\DBM;
13 use Friendica\Model\Item;
16 require_once 'include/dba.php';
19 public static function execute($param = '', $hook_name = '') {
22 require_once 'include/items.php';
26 if ($param == 'delete') {
27 logger('Delete expired items', LOGGER_DEBUG);
28 // physically remove anything that has been deleted for more than two months
29 $r = dba::p("SELECT `id` FROM `item` WHERE `deleted` AND `changed` < UTC_TIMESTAMP() - INTERVAL 60 DAY");
30 while ($row = dba::fetch($r)) {
31 dba::delete('item', ['id' => $row['id']]);
35 logger('Delete expired items - done', LOGGER_DEBUG);
37 // make this optional as it could have a performance impact on large sites
38 if (intval(Config::get('system', 'optimize_items'))) {
39 dba::e("OPTIMIZE TABLE `item`");
42 } elseif (intval($param) > 0) {
43 $user = dba::selectFirst('user', ['uid', 'username', 'expire'], ['uid' => $param]);
44 if (DBM::is_result($user)) {
45 logger('Expire items for user '.$user['uid'].' ('.$user['username'].') - interval: '.$user['expire'], LOGGER_DEBUG);
46 Item::expire($user['uid'], $user['expire']);
47 logger('Expire items for user '.$user['uid'].' ('.$user['username'].') - done ', LOGGER_DEBUG);
50 } elseif (!empty($hook_name) && ($param == 'hook') && is_array($a->hooks) && array_key_exists("expire", $a->hooks)) {
51 foreach ($a->hooks["expire"] as $hook) {
52 if ($hook[1] == $hook_name) {
53 logger("Calling expire hook '" . $hook[1] . "'", LOGGER_DEBUG);
54 Addon::callSingleHook($a, $hook_name, $hook, $data);
60 logger('expire: start');
62 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
65 $r = dba::p("SELECT `uid`, `username` FROM `user` WHERE `expire` != 0");
66 while ($row = dba::fetch($r)) {
67 logger('Calling expiry for user '.$row['uid'].' ('.$row['username'].')', LOGGER_DEBUG);
68 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
69 'Expire', (int)$row['uid']);
73 logger('expire: calling hooks');
75 if (is_array($a->hooks) && array_key_exists('expire', $a->hooks)) {
76 foreach ($a->hooks['expire'] as $hook) {
77 logger("Calling expire hook for '" . $hook[1] . "'", LOGGER_DEBUG);
78 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
79 'Expire', 'hook', $hook[1]);
83 logger('expire: end');