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';
21 * @brief Update existing item entries
23 * @param array $fields The fields that are to be changed
24 * @param array $condition The condition for finding the item entries
26 * In the future we may have to change permissions as well.
27 * Then we had to add the user id as third parameter.
29 * A return value of "0" doesn't mean an error - but that 0 rows had been changed.
31 * @return integer|boolean number of affected rows - or "false" if there was an error
33 public static function update(array $fields, array $condition)
35 if (empty($condition) || empty($fields)) {
39 $success = dba::update('item', $fields, $condition);
45 $rows = dba::affected_rows();
47 // We cannot simply expand the condition to check for origin entries
48 // The condition needn't to be a simple array but could be a complex condition.
49 $items = dba::select('item', ['id', 'origin'], $condition);
50 while ($item = dba::fetch($items)) {
51 // We only need to notfiy others when it is an original entry from us
52 if (!$item['origin']) {
56 create_tags_from_item($item['id']);
57 Term::createFromItem($item['id']);
58 update_thread($item['id']);
60 Worker::add(PRIORITY_HIGH, "Notifier", 'edit_post', $item['id']);
66 public static function delete(array $condition, $priority = PRIORITY_HIGH)
69 // locate item to be deleted
70 $item = dba::selectFirst('item', [], $condition);
71 if (!DBM::is_result($item)) {
75 if ($item['deleted']) {
79 $owner = $item['uid'];
83 logger('delete item: ' . $item['id'], LOGGER_DEBUG);
85 // clean up categories and tags so they don't end up as orphans
88 $cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER);
90 foreach ($matches as $mtch) {
91 file_tag_unsave_file($item['uid'], $item['id'], $mtch[1],true);
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],false);
105 // * If item is a link to a photo resource, nuke all the associated photos
106 // * (visitors will not have photo resources)
107 // * This only applies to photos uploaded from the photos page. Photos inserted into a post do not
108 // * generate a resource-id and therefore aren't intimately linked to the item.
110 if (strlen($item['resource-id'])) {
111 dba::delete('photo', ['resource-id' => $item['resource-id'], 'uid' => $item['uid']]);
114 // If item is a link to an event, nuke the event record.
115 if (intval($item['event-id'])) {
116 dba::delete('event', ['id' => $item['event-id'], 'uid' => $item['uid']]);
119 // If item has attachments, drop them
120 foreach (explode(", ", $item['attach']) as $attach) {
121 preg_match("|attach/(\d+)|", $attach, $matches);
122 dba::delete('attach', ['id' => $matches[1], 'uid' => $item['uid']]);
125 // Set the item to "deleted"
126 // Don't delete it here, since we have to send delete messages
127 dba::update('item', ['deleted' => true, 'title' => '', 'body' => '',
128 'edited' => datetime_convert(), 'changed' => datetime_convert()],
129 ['id' => $item['id']]);
131 create_tags_from_item($item['id']);
132 Term::createFromItem($item['id']);
133 delete_thread($item['id'], $item['parent-uri']);
135 // Creating list of parents
136 $r = q("SELECT `id` FROM `item` WHERE `parent` = %d AND `uid` = %d",
143 foreach ($r as $row) {
144 if ($parentid != "") {
148 $parentid .= $row["id"];
152 if ($parentid != "") {
153 q("DELETE FROM `sign` WHERE `iid` IN (%s)", dbesc($parentid));
156 // If it's the parent of a comment thread, kill all the kids
157 if ($item['uri'] == $item['parent-uri']) {
158 dba::update('item', ['deleted' => true, 'title' => '', 'body' => '',
159 'edited' => datetime_convert(), 'changed' => datetime_convert()],
160 ['parent-uri' => $item['parent-uri'], 'uid' => $item['uid']]);
162 create_tags_from_itemuri($item['parent-uri'], $item['uid']);
163 Term::createFromItemURI($item['parent-uri'], $item['uid']);
164 delete_thread_uri($item['parent-uri'], $item['uid']);
167 // ensure that last-child is set in case the comment that had it just got wiped.
168 dba::update('item', ['last-child' => false, 'changed' => datetime_convert()],
169 ['parent-uri' => $item['parent-uri'], 'uid' => $item['uid']]);
171 // who is the last child now?
172 $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",
173 dbesc($item['parent-uri']),
176 if (DBM::is_result($r)) {
177 dba::update('item', ['last-child' => true], ['id' => $r[0]['id']]);
181 // send the notification upstream/downstream
182 Worker::add(['priority' => $priority, 'dont_fork' => true], "Notifier", "drop", intval($item['id']));
189 * @brief Add a shadow entry for a given item id that is a thread starter
191 * We store every public item entry additionally with the user id "0".
192 * This is used for the community page and for the search.
193 * It is planned that in the future we will store public item entries only once.
195 * @param integer $itemid Item ID that should be added
197 public static function addShadow($itemid)
199 $fields = ['uid', 'wall', 'private', 'moderated', 'visible', 'contact-id', 'deleted', 'network', 'author-id', 'owner-id'];
200 $condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid];
201 $item = dba::selectFirst('item', $fields, $condition);
203 if (!DBM::is_result($item)) {
207 // is it already a copy?
208 if (($itemid == 0) || ($item['uid'] == 0)) {
212 // Is it a visible public post?
213 if (!$item["visible"] || $item["deleted"] || $item["moderated"] || $item["private"]) {
217 // is it an entry from a connector? Only add an entry for natively connected networks
218 if (!in_array($item["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""])) {
222 // Is the public contact configured as hidden?
223 if (Contact::isHidden($item["owner-id"]) || Contact::isHidden($item["author-id"])) {
227 // Only do these checks if the post isn't a wall post
228 if (!$item["wall"]) {
229 // Check, if hide-friends is activated - then don't do a shadow entry
230 if (dba::exists('profile', ['is-default' => true, 'uid' => $item['uid'], 'hide-friends' => true])) {
234 // Check if the contact is hidden or blocked
235 if (!dba::exists('contact', ['hidden' => false, 'blocked' => false, 'id' => $item['contact-id']])) {
240 // Only add a shadow, if the profile isn't hidden
241 if (dba::exists('user', ['uid' => $item['uid'], 'hidewall' => true])) {
245 $item = dba::selectFirst('item', [], ['id' => $itemid]);
247 if (count($item) && ($item["allow_cid"] == '') && ($item["allow_gid"] == '') &&
248 ($item["deny_cid"] == '') && ($item["deny_gid"] == '')) {
250 if (!dba::exists('item', ['uri' => $item['uri'], 'uid' => 0])) {
251 // Preparing public shadow (removing user specific data)
252 require_once("include/items.php");
258 $item['contact-id'] = Contact::getIdForURL($item['author-link'], 0);
260 if (in_array($item['type'], ["net-comment", "wall-comment"])) {
261 $item['type'] = 'remote-comment';
262 } elseif ($item['type'] == 'wall') {
263 $item['type'] = 'remote';
266 $public_shadow = item_store($item, false, false, true);
268 logger("Stored public shadow for thread ".$itemid." under id ".$public_shadow, LOGGER_DEBUG);
274 * @brief Add a shadow entry for a given item id that is a comment
276 * This function does the same like the function above - but for comments
278 * @param integer $itemid Item ID that should be added
280 public static function addShadowPost($itemid)
282 $item = dba::selectFirst('item', [], ['id' => $itemid]);
283 if (!DBM::is_result($item)) {
287 // Is it a toplevel post?
288 if ($item['id'] == $item['parent']) {
289 self::addShadow($itemid);
293 // Is this a shadow entry?
294 if ($item['uid'] == 0)
297 // Is there a shadow parent?
298 if (!dba::exists('item', ['uri' => $item['parent-uri'], 'uid' => 0])) {
302 // Is there already a shadow entry?
303 if (dba::exists('item', ['uri' => $item['uri'], 'uid' => 0])) {
307 // Preparing public shadow (removing user specific data)
308 require_once("include/items.php");
314 $item['contact-id'] = Contact::getIdForURL($item['author-link'], 0);
316 if (in_array($item['type'], ["net-comment", "wall-comment"])) {
317 $item['type'] = 'remote-comment';
318 } elseif ($item['type'] == 'wall') {
319 $item['type'] = 'remote';
322 $public_shadow = item_store($item, false, false, true);
324 logger("Stored public shadow for comment ".$item['uri']." under id ".$public_shadow, LOGGER_DEBUG);