]> git.mxchange.org Git - friendica.git/blob - src/Worker/Expire.php
Merge pull request #5253 from rabuzarus/20180616_-_magic_auth_test_2
[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\Addon;
10 use Friendica\Core\Config;
11 use Friendica\Core\Worker;
12 use Friendica\Database\DBM;
13 use Friendica\Model\Item;
14 use dba;
15
16 require_once 'include/dba.php';
17
18 class Expire {
19         public static function execute($param = '', $hook_name = '') {
20                 global $a;
21
22                 require_once 'include/items.php';
23
24                 Addon::loadHooks();
25
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']]);
32                         }
33                         dba::close($r);
34
35                         logger('Delete expired items - done', LOGGER_DEBUG);
36
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`");
40                         }
41                         return;
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);
48                         }
49                         return;
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);
55                                 }
56                         }
57                         return;
58                 }
59
60                 logger('expire: start');
61
62                 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
63                                 'Expire', 'delete');
64
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']);
70                 }
71                 dba::close($r);
72
73                 logger('expire: calling hooks');
74
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]);
80                         }
81                 }
82
83                 logger('expire: end');
84
85                 return;
86         }
87 }