]> git.mxchange.org Git - friendica.git/blob - src/Worker/Expire.php
9b2308da374155a02d46fcee8ac1a49138615c9b
[friendica.git] / src / Worker / Expire.php
1 <?php
2 /**
3  * @file src/Worker/Expire.php
4  * @brief Expires old item entries
5  */
6
7 namespace Friendica\Worker;
8
9 use Friendica\Core\Config;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBM;
12 use dba;
13
14 require_once 'include/dba.php';
15
16 class Expire {
17         public static function execute($param = '', $hook_name = '') {
18                 global $a;
19
20                 require_once 'include/datetime.php';
21                 require_once 'include/items.php';
22
23                 load_hooks();
24
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']));
31                         }
32                         dba::close($r);
33
34                         logger('Delete expired items - done', LOGGER_DEBUG);
35
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`");
39                         }
40                         return;
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);
47                         }
48                         return;
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);
54                                 }
55                         }
56                         return;
57                 }
58
59                 logger('expire: start');
60
61                 Worker::add(array('priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true),
62                                 'Expire', 'delete');
63
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']);
69                 }
70                 dba::close($r);
71
72                 logger('expire: calling hooks');
73
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]);
79                         }
80                 }
81
82                 logger('expire: end');
83
84                 return;
85         }
86 }