4 * @file src/Model/Item.php
7 namespace Friendica\Model;
9 use Friendica\Core\Worker;
10 use Friendica\Model\Term;
11 use Friendica\Model\Contact;
12 use Friendica\Database\DBM;
15 require_once 'include/tags.php';
16 require_once 'include/threads.php';
17 require_once 'include/items.php';
22 * @brief Update existing item entries
24 * @param array $fields The fields that are to be changed
25 * @param array $condition The condition for finding the item entries
27 * In the future we may have to change permissions as well.
28 * Then we had to add the user id as third parameter.
30 * A return value of "0" doesn't mean an error - but that 0 rows had been changed.
32 * @return integer|boolean number of affected rows - or "false" if there was an error
34 public static function update(array $fields, array $condition)
36 if (empty($condition) || empty($fields)) {
40 $success = dba::update('item', $fields, $condition);
46 $rows = dba::affected_rows();
48 // We cannot simply expand the condition to check for origin entries
49 // The condition needn't to be a simple array but could be a complex condition.
50 $items = dba::select('item', ['id', 'origin'], $condition);
51 while ($item = dba::fetch($items)) {
52 // We only need to notfiy others when it is an original entry from us
53 if (!$item['origin']) {
57 create_tags_from_item($item['id']);
58 Term::createFromItem($item['id']);
59 update_thread($item['id']);
61 Worker::add(PRIORITY_HIGH, "Notifier", 'edit_post', $item['id']);
68 * @brief Delete an item and notify others about it - if it was ours
70 * @param integer $item_id Item ID that should be delete
72 * @return $boolean success
74 public static function delete($item_id, $priority = PRIORITY_HIGH)
76 // locate item to be deleted
77 $fields = ['id', 'uid', 'parent', 'parent-uri', 'origin', 'deleted', 'file', 'resource-id', 'event-id', 'attach'];
78 $item = dba::selectFirst('item', $fields, ['id' => $item_id]);
79 if (!DBM::is_result($item)) {
83 if ($item['deleted']) {
87 $parent = dba::selectFirst('item', ['origin'], ['id' => $item['parent']]);
88 if (!DBM::is_result($parent)) {
89 $parent = ['origin' => false];
92 logger('delete item: ' . $item['id'], LOGGER_DEBUG);
94 // clean up categories and tags so they don't end up as orphans
97 $cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER);
99 foreach ($matches as $mtch) {
100 file_tag_unsave_file($item['uid'], $item['id'], $mtch[1],true);
106 $cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER);
108 foreach ($matches as $mtch) {
109 file_tag_unsave_file($item['uid'], $item['id'], $mtch[1],false);
114 * If item is a link to a photo resource, nuke all the associated photos
115 * (visitors will not have photo resources)
116 * This only applies to photos uploaded from the photos page. Photos inserted into a post do not
117 * generate a resource-id and therefore aren't intimately linked to the item.
119 if (strlen($item['resource-id'])) {
120 dba::delete('photo', ['resource-id' => $item['resource-id'], 'uid' => $item['uid']]);
123 // If item is a link to an event, nuke the event record.
124 if (intval($item['event-id'])) {
125 dba::delete('event', ['id' => $item['event-id'], 'uid' => $item['uid']]);
128 // If item has attachments, drop them
129 foreach (explode(", ", $item['attach']) as $attach) {
130 preg_match("|attach/(\d+)|", $attach, $matches);
131 dba::delete('attach', ['id' => $matches[1], 'uid' => $item['uid']]);
134 // When it is our item we don't delete it here, since we have to send delete messages
135 if ($item['origin'] || $parent['origin']) {
136 // Set the item to "deleted"
137 dba::update('item', ['deleted' => true, 'title' => '', 'body' => '',
138 'edited' => datetime_convert(), 'changed' => datetime_convert()],
139 ['id' => $item['id']]);
141 create_tags_from_item($item['id']);
142 Term::createFromItem($item['id']);
143 delete_thread($item['id'], $item['parent-uri']);
145 // If it's the parent of a comment thread, kill all the kids
146 if ($item['id'] == $item['parent']) {
147 $items = dba::select('item', ['id'], ['parent' => $item['parent']]);
148 while ($row = dba::fetch($items)) {
149 self::delete($row['id'], $priority);
153 // send the notification upstream/downstream
154 Worker::add(['priority' => $priority, 'dont_fork' => true], "Notifier", "drop", intval($item['id']));
156 // delete it immediately. All related children will be deleted as well.
157 dba::delete('item', ['id' => $item['id']]);
164 * @brief Add a shadow entry for a given item id that is a thread starter
166 * We store every public item entry additionally with the user id "0".
167 * This is used for the community page and for the search.
168 * It is planned that in the future we will store public item entries only once.
170 * @param integer $itemid Item ID that should be added
172 public static function addShadow($itemid)
174 $fields = ['uid', 'wall', 'private', 'moderated', 'visible', 'contact-id', 'deleted', 'network', 'author-id', 'owner-id'];
175 $condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid];
176 $item = dba::selectFirst('item', $fields, $condition);
178 if (!DBM::is_result($item)) {
182 // is it already a copy?
183 if (($itemid == 0) || ($item['uid'] == 0)) {
187 // Is it a visible public post?
188 if (!$item["visible"] || $item["deleted"] || $item["moderated"] || $item["private"]) {
192 // is it an entry from a connector? Only add an entry for natively connected networks
193 if (!in_array($item["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""])) {
197 // Is the public contact configured as hidden?
198 if (Contact::isHidden($item["owner-id"]) || Contact::isHidden($item["author-id"])) {
202 // Only do these checks if the post isn't a wall post
203 if (!$item["wall"]) {
204 // Check, if hide-friends is activated - then don't do a shadow entry
205 if (dba::exists('profile', ['is-default' => true, 'uid' => $item['uid'], 'hide-friends' => true])) {
209 // Check if the contact is hidden or blocked
210 if (!dba::exists('contact', ['hidden' => false, 'blocked' => false, 'id' => $item['contact-id']])) {
215 // Only add a shadow, if the profile isn't hidden
216 if (dba::exists('user', ['uid' => $item['uid'], 'hidewall' => true])) {
220 $item = dba::selectFirst('item', [], ['id' => $itemid]);
222 if (DBM::is_result($item) && ($item["allow_cid"] == '') && ($item["allow_gid"] == '') &&
223 ($item["deny_cid"] == '') && ($item["deny_gid"] == '')) {
225 if (!dba::exists('item', ['uri' => $item['uri'], 'uid' => 0])) {
226 // Preparing public shadow (removing user specific data)
231 $item['contact-id'] = Contact::getIdForURL($item['author-link'], 0);
233 if (in_array($item['type'], ["net-comment", "wall-comment"])) {
234 $item['type'] = 'remote-comment';
235 } elseif ($item['type'] == 'wall') {
236 $item['type'] = 'remote';
239 $public_shadow = item_store($item, false, false, true);
241 logger("Stored public shadow for thread ".$itemid." under id ".$public_shadow, LOGGER_DEBUG);
247 * @brief Add a shadow entry for a given item id that is a comment
249 * This function does the same like the function above - but for comments
251 * @param integer $itemid Item ID that should be added
253 public static function addShadowPost($itemid)
255 $item = dba::selectFirst('item', [], ['id' => $itemid]);
256 if (!DBM::is_result($item)) {
260 // Is it a toplevel post?
261 if ($item['id'] == $item['parent']) {
262 self::addShadow($itemid);
266 // Is this a shadow entry?
267 if ($item['uid'] == 0)
270 // Is there a shadow parent?
271 if (!dba::exists('item', ['uri' => $item['parent-uri'], 'uid' => 0])) {
275 // Is there already a shadow entry?
276 if (dba::exists('item', ['uri' => $item['uri'], 'uid' => 0])) {
280 // Preparing public shadow (removing user specific data)
285 $item['contact-id'] = Contact::getIdForURL($item['author-link'], 0);
287 if (in_array($item['type'], ["net-comment", "wall-comment"])) {
288 $item['type'] = 'remote-comment';
289 } elseif ($item['type'] == 'wall') {
290 $item['type'] = 'remote';
293 $public_shadow = item_store($item, false, false, true);
295 logger("Stored public shadow for comment ".$item['uri']." under id ".$public_shadow, LOGGER_DEBUG);