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',
26 const ACTIVITYPUB = 1;
28 const LEGACY_DFRN = 3;
33 * Extract delivery data from the provided item fields
35 * @param array $fields
38 public static function extractFields(array &$fields)
41 foreach (array_merge(ItemDeliveryData::FIELD_LIST, ItemDeliveryData::LEGACY_FIELD_LIST) as $key => $field) {
42 if (is_int($key) && isset($fields[$field])) {
43 // Legacy field moved from item table
44 $delivery_data[$field] = $fields[$field];
45 $fields[$field] = null;
46 } elseif (isset($fields[$field])) {
47 // New delivery field with virtual field name in item fields
48 $delivery_data[$key] = $fields[$field];
49 unset($fields[$field]);
53 return $delivery_data;
57 * Increments the queue_done for the given item ID.
59 * Avoids racing condition between multiple delivery threads.
61 * @param integer $item_id
62 * @param integer $protocol
66 public static function incrementQueueDone($item_id, $protocol = 0)
71 case self::ACTIVITYPUB:
72 $sql = ", `activitypub` = `activitypub` + 1";
75 $sql = ", `dfrn` = `dfrn` + 1";
77 case self::LEGACY_DFRN:
78 $sql = ", `legacy_dfrn` = `legacy_dfrn` + 1";
81 $sql = ", `diaspora` = `diaspora` + 1";
84 $sql = ", `ostatus` = `ostatus` + 1";
88 return DBA::e('UPDATE `item-delivery-data` SET `queue_done` = `queue_done` + 1' . $sql . ' WHERE `iid` = ?', $item_id);
92 * Insert a new item delivery data entry
94 * @param integer $item_id
95 * @param array $fields
99 public static function insert($item_id, array $fields)
101 if (empty($item_id)) {
102 throw new BadMethodCallException('Empty item_id');
105 $fields['iid'] = $item_id;
107 return DBA::insert('item-delivery-data', $fields);
111 * Update/Insert item delivery data
113 * If you want to update queue_done, please use incrementQueueDone instead.
115 * @param integer $item_id
116 * @param array $fields
120 public static function update($item_id, array $fields)
122 if (empty($item_id)) {
123 throw new BadMethodCallException('Empty item_id');
126 if (empty($fields)) {
127 // Nothing to do, update successful
131 return DBA::update('item-delivery-data', $fields, ['iid' => $item_id], true);
135 * Delete item delivery data
137 * @param integer $item_id
141 public static function delete($item_id)
143 if (empty($item_id)) {
144 throw new BadMethodCallException('Empty item_id');
147 return DBA::delete('item-delivery-data', ['iid' => $item_id]);