]> git.mxchange.org Git - friendica.git/blob - src/Worker/Expire.php
61326fa88e552d66e673022ecb1fca70247b2e00
[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 class Expire {
15         public static function execute($param = '', $hook_name = '') {
16                 global $a;
17
18                 require_once 'include/datetime.php';
19                 require_once 'include/items.php';
20
21                 load_hooks();
22
23                 if ($param == 'delete') {
24                         logger('Delete expired items', LOGGER_DEBUG);
25                         // physically remove anything that has been deleted for more than two months
26                         $r = dba::p("SELECT `id` FROM `item` WHERE `deleted` AND `changed` < UTC_TIMESTAMP() - INTERVAL 60 DAY");
27                         while ($row = dba::fetch($r)) {
28                                 dba::delete('item', array('id' => $row['id']));
29                         }
30                         dba::close($r);
31
32                         logger('Delete expired items - done', LOGGER_DEBUG);
33
34                         // make this optional as it could have a performance impact on large sites
35                         if (intval(Config::get('system', 'optimize_items'))) {
36                                 dba::e("OPTIMIZE TABLE `item`");
37                         }
38                         return;
39                 } elseif (intval($param) > 0) {
40                         $user = dba::select('user', array('uid', 'username', 'expire'), array('uid' => $param), array('limit' => 1));
41                         if (DBM::is_result($user)) {
42                                 logger('Expire items for user '.$user['uid'].' ('.$user['username'].') - interval: '.$user['expire'], LOGGER_DEBUG);
43                                 item_expire($user['uid'], $user['expire']);
44                                 logger('Expire items for user '.$user['uid'].' ('.$user['username'].') - done ', LOGGER_DEBUG);
45                         }
46                         return;
47                 } elseif (!empty($hook_name) && ($param == 'hook') && is_array($a->hooks) && array_key_exists("expire", $a->hooks)) {
48                         foreach ($a->hooks["expire"] as $hook) {
49                                 if ($hook[1] == $hook_name) {
50                                         logger("Calling expire hook '" . $hook[1] . "'", LOGGER_DEBUG);
51                                         call_single_hook($a, $name, $hook, $data);
52                                 }
53                         }
54                         return;
55                 }
56
57                 logger('expire: start');
58
59                 Worker::add(array('priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true),
60                                 'Expire', 'delete');
61
62                 $r = dba::p("SELECT `uid`, `username` FROM `user` WHERE `expire` != 0");
63                 while ($row = dba::fetch($r)) {
64                         logger('Calling expiry for user '.$row['uid'].' ('.$row['username'].')', LOGGER_DEBUG);
65                         Worker::add(array('priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true),
66                                         'Expire', (int)$row['uid']);
67                 }
68                 dba::close($r);
69
70                 logger('expire: calling hooks');
71
72                 if (is_array($a->hooks) && array_key_exists('expire', $a->hooks)) {
73                         foreach ($a->hooks['expire'] as $hook) {
74                                 logger("Calling expire hook for '" . $hook[1] . "'", LOGGER_DEBUG);
75                                 Worker::add(array('priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true),
76                                                 'Expire', 'hook', $hook[1]);
77                         }
78                 }
79
80                 logger('expire: end');
81
82                 return;
83         }
84 }