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;
35 * Extract delivery data from the provided item fields
37 * @param array $fields
40 public static function extractFields(array &$fields)
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]);
55 return $delivery_data;
59 * Increments the queue_done for the given item ID.
61 * Avoids racing condition between multiple delivery threads.
63 * @param integer $item_id
64 * @param integer $protocol
68 public static function incrementQueueDone($item_id, $protocol = 0)
73 case self::ACTIVITYPUB:
74 $sql = ", `activitypub` = `activitypub` + 1";
77 $sql = ", `dfrn` = `dfrn` + 1";
79 case self::LEGACY_DFRN:
80 $sql = ", `legacy_dfrn` = `legacy_dfrn` + 1";
83 $sql = ", `diaspora` = `diaspora` + 1";
86 $sql = ", `ostatus` = `ostatus` + 1";
90 return DBA::e('UPDATE `item-delivery-data` SET `queue_done` = `queue_done` + 1' . $sql . ' WHERE `iid` = ?', $item_id);
94 * Increments the queue_failed for the given item ID.
96 * Avoids racing condition between multiple delivery threads.
98 * @param integer $item_id
102 public static function incrementQueueFailed($item_id)
104 return DBA::e('UPDATE `item-delivery-data` SET `queue_failed` = `queue_failed` + 1 WHERE `iid` = ?', $item_id);
108 * Increments the queue_count for the given item ID.
110 * @param integer $item_id
111 * @param integer $increment
115 public static function incrementQueueCount(int $item_id, int $increment = 1)
117 return DBA::e('UPDATE `item-delivery-data` SET `queue_count` = `queue_count` + ? WHERE `iid` = ?', $increment, $item_id);
121 * Insert a new item delivery data entry
123 * @param integer $item_id
124 * @param array $fields
128 public static function insert($item_id, array $fields)
130 if (empty($item_id)) {
131 throw new BadMethodCallException('Empty item_id');
134 $fields['iid'] = $item_id;
136 return DBA::insert('item-delivery-data', $fields);
140 * Update/Insert item delivery data
142 * If you want to update queue_done, please use incrementQueueDone instead.
144 * @param integer $item_id
145 * @param array $fields
149 public static function update($item_id, array $fields)
151 if (empty($item_id)) {
152 throw new BadMethodCallException('Empty item_id');
155 if (empty($fields)) {
156 // Nothing to do, update successful
160 return DBA::update('item-delivery-data', $fields, ['iid' => $item_id], true);
164 * Delete item delivery data
166 * @param integer $item_id
170 public static function delete($item_id)
172 if (empty($item_id)) {
173 throw new BadMethodCallException('Empty item_id');
176 return DBA::delete('item-delivery-data', ['iid' => $item_id]);