+ /**
+ * @brief Insert a new item content entry
+ *
+ * @param array $item The item fields that are to be inserted
+ */
+ private static function insertActivity(&$item)
+ {
+ $activity_index = self::activityToIndex($item['verb']);
+
+ if ($activity_index < 0) {
+ return false;
+ }
+
+ $fields = ['uri' => $item['uri'], 'activity' => $activity_index,
+ 'uri-hash' => hash('sha1', $item['uri']) . hash('ripemd160', $item['uri'])];
+
+ $saved_item = $item;
+
+ // We just remove everything that is content
+ foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) {
+ unset($item[$field]);
+ }
+
+ // To avoid timing problems, we are using locks.
+ $locked = Lock::set('item_insert_activity');
+ if (!$locked) {
+ logger("Couldn't acquire lock for URI " . $item['uri'] . " - proceeding anyway.");
+ }
+
+ // Do we already have this content?
+ $item_activity = dba::selectFirst('item-activity', ['id'], ['uri' => $item['uri']]);
+ if (DBM::is_result($item_activity)) {
+ $item['iaid'] = $item_activity['id'];
+ logger('Fetched activity for URI ' . $item['uri'] . ' (' . $item['iaid'] . ')');
+ } elseif (dba::insert('item-activity', $fields)) {
+ $item['iaid'] = dba::lastInsertId();
+ logger('Inserted activity for URI ' . $item['uri'] . ' (' . $item['iaid'] . ')');
+ } else {
+ // This shouldn't happen. But if it does, we simply store it in the item-content table
+ logger('Could not insert activity for URI ' . $item['uri'] . ' - should not happen');
+ $item = $saved_item;
+ return false;
+ }
+ if ($locked) {
+ Lock::remove('item_insert_activity');
+ }
+ return true;
+ }
+