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']);
67 public static function delete(array $condition, $priority = PRIORITY_HIGH)
70 // locate item to be deleted
71 $item = dba::selectFirst('item', [], $condition);
72 if (!DBM::is_result($item)) {
76 if ($item['deleted']) {
80 $owner = $item['uid'];
84 logger('delete item: ' . $item['id'], LOGGER_DEBUG);
86 // clean up categories and tags so they don't end up as orphans
89 $cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER);
91 foreach ($matches as $mtch) {
92 file_tag_unsave_file($item['uid'], $item['id'], $mtch[1],true);
98 $cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER);
100 foreach ($matches as $mtch) {
101 file_tag_unsave_file($item['uid'], $item['id'], $mtch[1],false);
106 // * If item is a link to a photo resource, nuke all the associated photos
107 // * (visitors will not have photo resources)
108 // * This only applies to photos uploaded from the photos page. Photos inserted into a post do not
109 // * generate a resource-id and therefore aren't intimately linked to the item.
111 if (strlen($item['resource-id'])) {
112 dba::delete('photo', ['resource-id' => $item['resource-id'], 'uid' => $item['uid']]);
115 // If item is a link to an event, nuke the event record.
116 if (intval($item['event-id'])) {
117 dba::delete('event', ['id' => $item['event-id'], 'uid' => $item['uid']]);
120 // If item has attachments, drop them
121 foreach (explode(", ", $item['attach']) as $attach) {
122 preg_match("|attach/(\d+)|", $attach, $matches);
123 dba::delete('attach', ['id' => $matches[1], 'uid' => $item['uid']]);
126 // Set the item to "deleted"
127 // Don't delete it here, since we have to send delete messages
128 dba::update('item', ['deleted' => true, 'title' => '', 'body' => '',
129 'edited' => datetime_convert(), 'changed' => datetime_convert()],
130 ['id' => $item['id']]);
132 create_tags_from_item($item['id']);
133 Term::createFromItem($item['id']);
134 delete_thread($item['id'], $item['parent-uri']);
136 // Creating list of parents
137 $r = q("SELECT `id` FROM `item` WHERE `parent` = %d AND `uid` = %d",
144 foreach ($r as $row) {
145 if ($parentid != "") {
149 $parentid .= $row["id"];
153 if ($parentid != "") {
154 q("DELETE FROM `sign` WHERE `iid` IN (%s)", dbesc($parentid));
157 // If it's the parent of a comment thread, kill all the kids
158 if ($item['uri'] == $item['parent-uri']) {
159 dba::update('item', ['deleted' => true, 'title' => '', 'body' => '',
160 'edited' => datetime_convert(), 'changed' => datetime_convert()],
161 ['parent-uri' => $item['parent-uri'], 'uid' => $item['uid']]);
163 create_tags_from_itemuri($item['parent-uri'], $item['uid']);
164 Term::createFromItemURI($item['parent-uri'], $item['uid']);
165 delete_thread_uri($item['parent-uri'], $item['uid']);
168 // ensure that last-child is set in case the comment that had it just got wiped.
169 dba::update('item', ['last-child' => false, 'changed' => datetime_convert()],
170 ['parent-uri' => $item['parent-uri'], 'uid' => $item['uid']]);
172 // who is the last child now?
173 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d ORDER BY `edited` DESC LIMIT 1",
174 dbesc($item['parent-uri']),
177 if (DBM::is_result($r)) {
178 dba::update('item', ['last-child' => true], ['id' => $r[0]['id']]);
182 // send the notification upstream/downstream
183 Worker::add(['priority' => $priority, 'dont_fork' => true], "Notifier", "drop", intval($item['id']));
190 * @brief Add a shadow entry for a given item id that is a thread starter
192 * We store every public item entry additionally with the user id "0".
193 * This is used for the community page and for the search.
194 * It is planned that in the future we will store public item entries only once.
196 * @param integer $itemid Item ID that should be added
198 public static function addShadow($itemid)
200 $fields = ['uid', 'wall', 'private', 'moderated', 'visible', 'contact-id', 'deleted', 'network', 'author-id', 'owner-id'];
201 $condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid];
202 $item = dba::selectFirst('item', $fields, $condition);
204 if (!DBM::is_result($item)) {
208 // is it already a copy?
209 if (($itemid == 0) || ($item['uid'] == 0)) {
213 // Is it a visible public post?
214 if (!$item["visible"] || $item["deleted"] || $item["moderated"] || $item["private"]) {
218 // is it an entry from a connector? Only add an entry for natively connected networks
219 if (!in_array($item["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""])) {
223 // Is the public contact configured as hidden?
224 if (Contact::isHidden($item["owner-id"]) || Contact::isHidden($item["author-id"])) {
228 // Only do these checks if the post isn't a wall post
229 if (!$item["wall"]) {
230 // Check, if hide-friends is activated - then don't do a shadow entry
231 if (dba::exists('profile', ['is-default' => true, 'uid' => $item['uid'], 'hide-friends' => true])) {
235 // Check if the contact is hidden or blocked
236 if (!dba::exists('contact', ['hidden' => false, 'blocked' => false, 'id' => $item['contact-id']])) {
241 // Only add a shadow, if the profile isn't hidden
242 if (dba::exists('user', ['uid' => $item['uid'], 'hidewall' => true])) {
246 $item = dba::selectFirst('item', [], ['id' => $itemid]);
248 if (DBM::is_result($item) && ($item["allow_cid"] == '') && ($item["allow_gid"] == '') &&
249 ($item["deny_cid"] == '') && ($item["deny_gid"] == '')) {
251 if (!dba::exists('item', ['uri' => $item['uri'], 'uid' => 0])) {
252 // Preparing public shadow (removing user specific data)
257 $item['contact-id'] = Contact::getIdForURL($item['author-link'], 0);
259 if (in_array($item['type'], ["net-comment", "wall-comment"])) {
260 $item['type'] = 'remote-comment';
261 } elseif ($item['type'] == 'wall') {
262 $item['type'] = 'remote';
265 $public_shadow = item_store($item, false, false, true);
267 logger("Stored public shadow for thread ".$itemid." under id ".$public_shadow, LOGGER_DEBUG);
273 * @brief Add a shadow entry for a given item id that is a comment
275 * This function does the same like the function above - but for comments
277 * @param integer $itemid Item ID that should be added
279 public static function addShadowPost($itemid)
281 $item = dba::selectFirst('item', [], ['id' => $itemid]);
282 if (!DBM::is_result($item)) {
286 // Is it a toplevel post?
287 if ($item['id'] == $item['parent']) {
288 self::addShadow($itemid);
292 // Is this a shadow entry?
293 if ($item['uid'] == 0)
296 // Is there a shadow parent?
297 if (!dba::exists('item', ['uri' => $item['parent-uri'], 'uid' => 0])) {
301 // Is there already a shadow entry?
302 if (dba::exists('item', ['uri' => $item['uri'], 'uid' => 0])) {
306 // Preparing public shadow (removing user specific data)
311 $item['contact-id'] = Contact::getIdForURL($item['author-link'], 0);
313 if (in_array($item['type'], ["net-comment", "wall-comment"])) {
314 $item['type'] = 'remote-comment';
315 } elseif ($item['type'] == 'wall') {
316 $item['type'] = 'remote';
319 $public_shadow = item_store($item, false, false, true);
321 logger("Stored public shadow for comment ".$item['uri']." under id ".$public_shadow, LOGGER_DEBUG);