]> git.mxchange.org Git - friendica.git/blob - src/Model/ItemDeliveryData.php
7c64427eff7571ebb0608e833b37d63f62890d0c
[friendica.git] / src / Model / ItemDeliveryData.php
1 <?php
2
3 /**
4  * @file src/Model/ItemDeliveryData.php
5  */
6
7 namespace Friendica\Model;
8
9 use Friendica\Database\DBA;
10 use \BadMethodCallException;
11
12 class ItemDeliveryData
13 {
14         const LEGACY_FIELD_LIST = [
15                 // Legacy fields moved from item table
16                 'postopts',
17                 'inform',
18         ];
19
20         const FIELD_LIST = [
21                 // New delivery fields with virtual field name in item fields
22                 'queue_count' => 'delivery_queue_count',
23                 'queue_done'  => 'delivery_queue_done',
24                 'queue_failed'  => 'delivery_queue_failed',
25         ];
26
27         const ACTIVITYPUB = 1;
28         const DFRN = 2;
29         const LEGACY_DFRN = 3;
30         const DIASPORA = 4;
31         const OSTATUS = 5;
32
33         /**
34          * Extract delivery data from the provided item fields
35          *
36          * @param array $fields
37          * @return array
38          */
39         public static function extractFields(array &$fields)
40         {
41                 $delivery_data = [];
42                 foreach (array_merge(ItemDeliveryData::FIELD_LIST, ItemDeliveryData::LEGACY_FIELD_LIST) as $key => $field) {
43                         if (is_int($key) && isset($fields[$field])) {
44                                 // Legacy field moved from item table
45                                 $delivery_data[$field] = $fields[$field];
46                                 $fields[$field] = null;
47                         } elseif (isset($fields[$field])) {
48                                 // New delivery field with virtual field name in item fields
49                                 $delivery_data[$key] = $fields[$field];
50                                 unset($fields[$field]);
51                         }
52                 }
53
54                 return $delivery_data;
55         }
56
57         /**
58          * Increments the queue_done for the given item ID.
59          *
60          * Avoids racing condition between multiple delivery threads.
61          *
62          * @param integer $item_id
63          * @param integer $protocol
64          * @return bool
65          * @throws \Exception
66          */
67         public static function incrementQueueDone($item_id, $protocol = 0)
68         {
69                 $sql = '';
70
71                 switch ($protocol) {
72                         case self::ACTIVITYPUB:
73                                 $sql = ", `activitypub` = `activitypub` + 1";
74                                 break;
75                         case self::DFRN:
76                                 $sql = ", `dfrn` = `dfrn` + 1";
77                                 break;
78                         case self::LEGACY_DFRN:
79                                 $sql = ", `legacy_dfrn` = `legacy_dfrn` + 1";
80                                 break;
81                         case self::DIASPORA:
82                                 $sql = ", `diaspora` = `diaspora` + 1";
83                                 break;
84                         case self::OSTATUS:
85                                 $sql = ", `ostatus` = `ostatus` + 1";
86                                 break;
87                 }
88
89                 return DBA::e('UPDATE `item-delivery-data` SET `queue_done` = `queue_done` + 1' . $sql . ' WHERE `iid` = ?', $item_id);
90         }
91
92         /**
93          * Increments the queue_failed for the given item ID.
94          *
95          * Avoids racing condition between multiple delivery threads.
96          *
97          * @param integer $item_id
98          * @return bool
99          * @throws \Exception
100          */
101         public static function incrementQueueFailed($item_id)
102         {
103                 return DBA::e('UPDATE `item-delivery-data` SET `queue_failed` = `queue_failed` + 1 WHERE `iid` = ?', $item_id);
104         }
105
106         /**
107          * Increments the queue_count for the given item ID.
108          *
109          * @param integer $item_id
110          * @param integer $increment
111          * @return bool
112          * @throws \Exception
113          */
114         public static function incrementQueueCount(int $item_id, int $increment = 1)
115         {
116                 return DBA::e('UPDATE `item-delivery-data` SET `queue_count` = `queue_count` + ? WHERE `iid` = ?', $increment, $item_id);
117         }
118
119         /**
120          * Insert a new item delivery data entry
121          *
122          * @param integer $item_id
123          * @param array   $fields
124          * @return bool
125          * @throws \Exception
126          */
127         public static function insert($item_id, array $fields)
128         {
129                 if (empty($item_id)) {
130                         throw new BadMethodCallException('Empty item_id');
131                 }
132
133                 $fields['iid'] = $item_id;
134
135                 return DBA::insert('item-delivery-data', $fields);
136         }
137
138         /**
139          * Update/Insert item delivery data
140          *
141          * If you want to update queue_done, please use incrementQueueDone instead.
142          *
143          * @param integer $item_id
144          * @param array   $fields
145          * @return bool
146          * @throws \Exception
147          */
148         public static function update($item_id, array $fields)
149         {
150                 if (empty($item_id)) {
151                         throw new BadMethodCallException('Empty item_id');
152                 }
153
154                 if (empty($fields)) {
155                         // Nothing to do, update successful
156                         return true;
157                 }
158
159                 return DBA::update('item-delivery-data', $fields, ['iid' => $item_id], true);
160         }
161
162         /**
163          * Delete item delivery data
164          *
165          * @param integer $item_id
166          * @return bool
167          * @throws \Exception
168          */
169         public static function delete($item_id)
170         {
171                 if (empty($item_id)) {
172                         throw new BadMethodCallException('Empty item_id');
173                 }
174
175                 return DBA::delete('item-delivery-data', ['iid' => $item_id]);
176         }
177 }