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']);
67 * @brief Add a shadow entry for a given item id that is a thread starter
69 * We store every public item entry additionally with the user id "0".
70 * This is used for the community page and for the search.
71 * It is planned that in the future we will store public item entries only once.
73 * @param integer $itemid Item ID that should be added
75 public static function addShadow($itemid)
77 $fields = ['uid', 'wall', 'private', 'moderated', 'visible', 'contact-id', 'deleted', 'network', 'author-id', 'owner-id'];
78 $condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid];
79 $item = dba::selectFirst('item', $fields, $condition);
81 if (!DBM::is_result($item)) {
85 // is it already a copy?
86 if (($itemid == 0) || ($item['uid'] == 0)) {
90 // Is it a visible public post?
91 if (!$item["visible"] || $item["deleted"] || $item["moderated"] || $item["private"]) {
95 // is it an entry from a connector? Only add an entry for natively connected networks
96 if (!in_array($item["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""])) {
100 // Is the public contact configured as hidden?
101 if (Contact::isHidden($item["owner-id"]) || Contact::isHidden($item["author-id"])) {
105 // Only do these checks if the post isn't a wall post
106 if (!$item["wall"]) {
107 // Check, if hide-friends is activated - then don't do a shadow entry
108 if (dba::exists('profile', ['is-default' => true, 'uid' => $item['uid'], 'hide-friends' => true])) {
112 // Check if the contact is hidden or blocked
113 if (!dba::exists('contact', ['hidden' => false, 'blocked' => false, 'id' => $item['contact-id']])) {
118 // Only add a shadow, if the profile isn't hidden
119 if (dba::exists('user', ['uid' => $item['uid'], 'hidewall' => true])) {
123 $item = dba::selectFirst('item', [], ['id' => $itemid]);
125 if (count($item) && ($item["allow_cid"] == '') && ($item["allow_gid"] == '') &&
126 ($item["deny_cid"] == '') && ($item["deny_gid"] == '')) {
128 if (!dba::exists('item', ['uri' => $item['uri'], 'uid' => 0])) {
129 // Preparing public shadow (removing user specific data)
130 require_once("include/items.php");
136 $item['contact-id'] = Contact::getIdForURL($item['author-link'], 0);
138 if (in_array($item['type'], ["net-comment", "wall-comment"])) {
139 $item['type'] = 'remote-comment';
140 } elseif ($item['type'] == 'wall') {
141 $item['type'] = 'remote';
144 $public_shadow = item_store($item, false, false, true);
146 logger("Stored public shadow for thread ".$itemid." under id ".$public_shadow, LOGGER_DEBUG);
152 * @brief Add a shadow entry for a given item id that is a comment
154 * This function does the same like the function above - but for comments
156 * @param integer $itemid Item ID that should be added
158 public static function addShadowPost($itemid)
160 $item = dba::selectFirst('item', [], ['id' => $itemid]);
161 if (!DBM::is_result($item)) {
165 // Is it a toplevel post?
166 if ($item['id'] == $item['parent']) {
167 self::addShadow($itemid);
171 // Is this a shadow entry?
172 if ($item['uid'] == 0)
175 // Is there a shadow parent?
176 if (!dba::exists('item', ['uri' => $item['parent-uri'], 'uid' => 0])) {
180 // Is there already a shadow entry?
181 if (dba::exists('item', ['uri' => $item['uri'], 'uid' => 0])) {
185 // Preparing public shadow (removing user specific data)
186 require_once("include/items.php");
192 $item['contact-id'] = Contact::getIdForURL($item['author-link'], 0);
194 if (in_array($item['type'], ["net-comment", "wall-comment"])) {
195 $item['type'] = 'remote-comment';
196 } elseif ($item['type'] == 'wall') {
197 $item['type'] = 'remote';
200 $public_shadow = item_store($item, false, false, true);
202 logger("Stored public shadow for comment ".$item['uri']." under id ".$public_shadow, LOGGER_DEBUG);