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 * Insert a new item delivery data entry
109 * @param integer $item_id
110 * @param array $fields
114 public static function insert($item_id, array $fields)
116 if (empty($item_id)) {
117 throw new BadMethodCallException('Empty item_id');
120 $fields['iid'] = $item_id;
122 return DBA::insert('item-delivery-data', $fields);
126 * Update/Insert item delivery data
128 * If you want to update queue_done, please use incrementQueueDone instead.
130 * @param integer $item_id
131 * @param array $fields
135 public static function update($item_id, array $fields)
137 if (empty($item_id)) {
138 throw new BadMethodCallException('Empty item_id');
141 if (empty($fields)) {
142 // Nothing to do, update successful
146 return DBA::update('item-delivery-data', $fields, ['iid' => $item_id], true);
150 * Delete item delivery data
152 * @param integer $item_id
156 public static function delete($item_id)
158 if (empty($item_id)) {
159 throw new BadMethodCallException('Empty item_id');
162 return DBA::delete('item-delivery-data', ['iid' => $item_id]);