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',
27 * Extract delivery data from the provided item fields
29 * @param array $fields
32 public static function extractFields(array &$fields)
35 foreach (array_merge(ItemDeliveryData::FIELD_LIST, ItemDeliveryData::LEGACY_FIELD_LIST) as $key => $field) {
36 if (is_int($key) && isset($fields[$field])) {
37 // Legacy field moved from item table
38 $delivery_data[$field] = $fields[$field];
39 $fields[$field] = null;
40 } elseif (isset($fields[$field])) {
41 // New delivery field with virtual field name in item fields
42 $delivery_data[$key] = $fields[$field];
43 unset($fields[$field]);
47 return $delivery_data;
51 * Increments the queue_done for the given item ID.
53 * Avoids racing condition between multiple delivery threads.
55 * @param integer $item_id
59 public static function incrementQueueDone($item_id)
61 return DBA::e('UPDATE `item-delivery-data` SET `queue_done` = `queue_done` + 1 WHERE `iid` = ?', $item_id);
65 * Insert a new item delivery data entry
67 * @param integer $item_id
68 * @param array $fields
72 public static function insert($item_id, array $fields)
74 if (empty($item_id)) {
75 throw new BadMethodCallException('Empty item_id');
78 $fields['iid'] = $item_id;
80 return DBA::insert('item-delivery-data', $fields);
84 * Update/Insert item delivery data
86 * If you want to update queue_done, please use incrementQueueDone instead.
88 * @param integer $item_id
89 * @param array $fields
93 public static function update($item_id, array $fields)
95 if (empty($item_id)) {
96 throw new BadMethodCallException('Empty item_id');
100 // Nothing to do, update successful
104 return DBA::update('item-delivery-data', $fields, ['iid' => $item_id], true);
108 * Delete item delivery data
110 * @param integer $item_id
114 public static function delete($item_id)
116 if (empty($item_id)) {
117 throw new BadMethodCallException('Empty item_id');
120 return DBA::delete('item-delivery-data', ['iid' => $item_id]);