]> git.mxchange.org Git - friendica.git/blob - src/Worker/Expire.php
d40d72eca10101a2921e636139aef1175566adde
[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\BaseObject;
10 use Friendica\Core\Addon;
11 use Friendica\Core\Config;
12 use Friendica\Core\Worker;
13 use Friendica\Database\DBA;
14 use Friendica\Database\DBM;
15 use Friendica\Model\Item;
16
17 require_once 'include/dba.php';
18
19 class Expire
20 {
21         public static function execute($param = '', $hook_name = '')
22         {
23                 $a = BaseObject::getApp();
24
25                 require_once 'include/items.php';
26
27                 Addon::loadHooks();
28
29                 if ($param == 'delete') {
30                         logger('Delete expired items', LOGGER_DEBUG);
31                         // physically remove anything that has been deleted for more than two months
32                         $condition = ["`deleted` AND `changed` < UTC_TIMESTAMP() - INTERVAL 60 DAY"];
33                         $rows = DBA::select('item', ['id', 'iaid', 'icid', 'psid'],  $condition);
34                         while ($row = DBA::fetch($rows)) {
35                                 DBA::delete('item', ['id' => $row['id']]);
36                                 if (!empty($row['iaid']) && !DBA::exists('item', ['iaid' => $row['iaid']])) {
37                                         DBA::delete('item-activity', ['id' => $row['iaid']]);
38                                 }
39                                 if (!empty($row['icid']) && !DBA::exists('item', ['icid' => $row['icid']])) {
40                                         DBA::delete('item-content', ['id' => $row['icid']]);
41                                 }
42                                 // When the permission set will be used in photo and events as well.
43                                 // this query here needs to be extended.
44                                 if (!empty($row['psid']) && !DBA::exists('item', ['psid' => $row['psid']])) {
45                                         DBA::delete('permissionset', ['id' => $row['psid']]);
46                                 }
47                         }
48                         DBA::close($rows);
49
50                         // Normally we shouldn't have orphaned data at all.
51                         // If we do have some, then we have to check why.
52                         logger('Deleting orphaned item activities - start', LOGGER_DEBUG);
53                         $condition = ["NOT EXISTS (SELECT `iaid` FROM `item` WHERE `item`.`iaid` = `item-activity`.`id`)"];
54                         DBA::delete('item-activity', $condition);
55                         logger('Orphaned item activities deleted: ' . DBA::affectedRows(), LOGGER_DEBUG);
56
57                         logger('Deleting orphaned item content - start', LOGGER_DEBUG);
58                         $condition = ["NOT EXISTS (SELECT `icid` FROM `item` WHERE `item`.`icid` = `item-content`.`id`)"];
59                         DBA::delete('item-content', $condition);
60                         logger('Orphaned item content deleted: ' . DBA::affectedRows(), LOGGER_DEBUG);
61
62                         // make this optional as it could have a performance impact on large sites
63                         if (intval(Config::get('system', 'optimize_items'))) {
64                                 DBA::e("OPTIMIZE TABLE `item`");
65                         }
66
67                         logger('Delete expired items - done', LOGGER_DEBUG);
68                         return;
69                 } elseif (intval($param) > 0) {
70                         $user = DBA::selectFirst('user', ['uid', 'username', 'expire'], ['uid' => $param]);
71                         if (DBM::is_result($user)) {
72                                 logger('Expire items for user '.$user['uid'].' ('.$user['username'].') - interval: '.$user['expire'], LOGGER_DEBUG);
73                                 Item::expire($user['uid'], $user['expire']);
74                                 logger('Expire items for user '.$user['uid'].' ('.$user['username'].') - done ', LOGGER_DEBUG);
75                         }
76                         return;
77                 } elseif (!empty($hook_name) && ($param == 'hook') && is_array($a->hooks) && array_key_exists("expire", $a->hooks)) {
78                         foreach ($a->hooks["expire"] as $hook) {
79                                 if ($hook[1] == $hook_name) {
80                                         logger("Calling expire hook '" . $hook[1] . "'", LOGGER_DEBUG);
81                                         Addon::callSingleHook($a, $hook_name, $hook, $data);
82                                 }
83                         }
84                         return;
85                 }
86
87                 logger('expire: start');
88
89                 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
90                                 'Expire', 'delete');
91
92                 $r = DBA::p("SELECT `uid`, `username` FROM `user` WHERE `expire` != 0");
93                 while ($row = DBA::fetch($r)) {
94                         logger('Calling expiry for user '.$row['uid'].' ('.$row['username'].')', LOGGER_DEBUG);
95                         Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
96                                         'Expire', (int)$row['uid']);
97                 }
98                 DBA::close($r);
99
100                 logger('expire: calling hooks');
101
102                 if (is_array($a->hooks) && array_key_exists('expire', $a->hooks)) {
103                         foreach ($a->hooks['expire'] as $hook) {
104                                 logger("Calling expire hook for '" . $hook[1] . "'", LOGGER_DEBUG);
105                                 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
106                                                 'Expire', 'hook', $hook[1]);
107                         }
108                 }
109
110                 logger('expire: end');
111
112                 return;
113         }
114 }