4 * @file src/Model/ItemDeliveryData.php
7 namespace Friendica\Model;
9 use Friendica\Database\DBA;
10 use \BadMethodCallException;
12 class ItemDeliveryData
14 const LEGACY_FIELD_LIST = [
15 // Legacy fields moved from item table
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',
27 const ACTIVITYPUB = 1;
29 const LEGACY_DFRN = 3;
34 * Extract delivery data from the provided item fields
36 * @param array $fields
39 public static function extractFields(array &$fields)
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]);
54 return $delivery_data;
58 * Increments the queue_done for the given item ID.
60 * Avoids racing condition between multiple delivery threads.
62 * @param integer $item_id
63 * @param integer $protocol
67 public static function incrementQueueDone($item_id, $protocol = 0)
72 case self::ACTIVITYPUB:
73 $sql = ", `activitypub` = `activitypub` + 1";
76 $sql = ", `dfrn` = `dfrn` + 1";
78 case self::LEGACY_DFRN:
79 $sql = ", `legacy_dfrn` = `legacy_dfrn` + 1";
82 $sql = ", `diaspora` = `diaspora` + 1";
85 $sql = ", `ostatus` = `ostatus` + 1";
89 return DBA::e('UPDATE `item-delivery-data` SET `queue_done` = `queue_done` + 1' . $sql . ' WHERE `iid` = ?', $item_id);
93 * Increments the queue_failed for the given item ID.
95 * Avoids racing condition between multiple delivery threads.
97 * @param integer $item_id
101 public static function incrementQueueFailed($item_id)
103 return DBA::e('UPDATE `item-delivery-data` SET `queue_failed` = `queue_failed` + 1 WHERE `iid` = ?', $item_id);
107 * Increments the queue_count for the given item ID.
109 * @param integer $item_id
110 * @param integer $increment
114 public static function incrementQueueCount(int $item_id, int $increment = 1)
116 return DBA::e('UPDATE `item-delivery-data` SET `queue_count` = `queue_count` + ? WHERE `iid` = ?', $increment, $item_id);
120 * Insert a new item delivery data entry
122 * @param integer $item_id
123 * @param array $fields
127 public static function insert($item_id, array $fields)
129 if (empty($item_id)) {
130 throw new BadMethodCallException('Empty item_id');
133 $fields['iid'] = $item_id;
135 return DBA::insert('item-delivery-data', $fields);
139 * Update/Insert item delivery data
141 * If you want to update queue_done, please use incrementQueueDone instead.
143 * @param integer $item_id
144 * @param array $fields
148 public static function update($item_id, array $fields)
150 if (empty($item_id)) {
151 throw new BadMethodCallException('Empty item_id');
154 if (empty($fields)) {
155 // Nothing to do, update successful
159 return DBA::update('item-delivery-data', $fields, ['iid' => $item_id], true);
163 * Delete item delivery data
165 * @param integer $item_id
169 public static function delete($item_id)
171 if (empty($item_id)) {
172 throw new BadMethodCallException('Empty item_id');
175 return DBA::delete('item-delivery-data', ['iid' => $item_id]);