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