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