]> git.mxchange.org Git - friendica.git/blob - src/Model/Item.php
The delete function is now changed to the new function
[friendica.git] / src / Model / Item.php
1 <?php
2
3 /**
4  * @file src/Model/Item.php
5  */
6
7 namespace Friendica\Model;
8
9 use Friendica\Core\Worker;
10 use Friendica\Model\Term;
11 use Friendica\Model\Contact;
12 use Friendica\Database\DBM;
13 use dba;
14
15 require_once 'include/tags.php';
16 require_once 'include/threads.php';
17 require_once 'include/items.php';
18
19 class Item
20 {
21         /**
22          * @brief Update existing item entries
23          *
24          * @param array $fields The fields that are to be changed
25          * @param array $condition The condition for finding the item entries
26          *
27          * In the future we may have to change permissions as well.
28          * Then we had to add the user id as third parameter.
29          *
30          * A return value of "0" doesn't mean an error - but that 0 rows had been changed.
31          *
32          * @return integer|boolean number of affected rows - or "false" if there was an error
33          */
34         public static function update(array $fields, array $condition)
35         {
36                 if (empty($condition) || empty($fields)) {
37                         return false;
38                 }
39
40                 $success = dba::update('item', $fields, $condition);
41
42                 if (!$success) {
43                         return false;
44                 }
45
46                 $rows = dba::affected_rows();
47
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']) {
54                                 continue;
55                         }
56
57                         create_tags_from_item($item['id']);
58                         Term::createFromItem($item['id']);
59                         update_thread($item['id']);
60
61                         Worker::add(PRIORITY_HIGH, "Notifier", 'edit_post', $item['id']);
62                 }
63
64                 return $rows;
65         }
66
67         /**
68          * @brief Delete an item and notify others about it - if it was ours
69          *
70          * @param integer $item_id Item ID that should be delete
71          *
72          * @return $boolean success
73          */
74         public static function delete($item_id, $priority = PRIORITY_HIGH)
75         {
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)) {
80                         return false;
81                 }
82
83                 if ($item['deleted']) {
84                         return false;
85                 }
86
87                 $parent = dba::selectFirst('item', ['origin'], ['id' => $item['parent']]);
88                 if (!DBM::is_result($parent)) {
89                         $parent = ['origin' => false];
90                 }
91
92                 logger('delete item: ' . $item['id'], LOGGER_DEBUG);
93
94                 // clean up categories and tags so they don't end up as orphans
95
96                 $matches = false;
97                 $cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER);
98                 if ($cnt) {
99                         foreach ($matches as $mtch) {
100                                 file_tag_unsave_file($item['uid'], $item['id'], $mtch[1],true);
101                         }
102                 }
103
104                 $matches = false;
105
106                 $cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER);
107                 if ($cnt) {
108                         foreach ($matches as $mtch) {
109                                 file_tag_unsave_file($item['uid'], $item['id'], $mtch[1],false);
110                         }
111                 }
112
113                 /*
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.
118                  */
119                 if (strlen($item['resource-id'])) {
120                         dba::delete('photo', ['resource-id' => $item['resource-id'], 'uid' => $item['uid']]);
121                 }
122
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']]);
126                 }
127
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']]);
132                 }
133
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']]);
140
141                         create_tags_from_item($item['id']);
142                         Term::createFromItem($item['id']);
143                         delete_thread($item['id'], $item['parent-uri']);
144
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);
150                                 }
151                         }
152
153                         // send the notification upstream/downstream
154                         Worker::add(['priority' => $priority, 'dont_fork' => true], "Notifier", "drop", intval($item['id']));
155                 } else {
156                         // delete it immediately. All related children will be deleted as well.
157                         dba::delete('item', ['id' => $item['id']]);
158                 }
159
160                 return true;
161         }
162
163         /**
164          * @brief Add a shadow entry for a given item id that is a thread starter
165          *
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.
169          *
170          * @param integer $itemid Item ID that should be added
171          */
172         public static function addShadow($itemid)
173         {
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);
177
178                 if (!DBM::is_result($item)) {
179                         return;
180                 }
181
182                 // is it already a copy?
183                 if (($itemid == 0) || ($item['uid'] == 0)) {
184                         return;
185                 }
186
187                 // Is it a visible public post?
188                 if (!$item["visible"] || $item["deleted"] || $item["moderated"] || $item["private"]) {
189                         return;
190                 }
191
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, ""])) {
194                         return;
195                 }
196
197                 // Is the public contact configured as hidden?
198                 if (Contact::isHidden($item["owner-id"]) || Contact::isHidden($item["author-id"])) {
199                         return;
200                 }
201
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])) {
206                                 return;
207                         }
208
209                         // Check if the contact is hidden or blocked
210                         if (!dba::exists('contact', ['hidden' => false, 'blocked' => false, 'id' => $item['contact-id']])) {
211                                 return;
212                         }
213                 }
214
215                 // Only add a shadow, if the profile isn't hidden
216                 if (dba::exists('user', ['uid' => $item['uid'], 'hidewall' => true])) {
217                         return;
218                 }
219
220                 $item = dba::selectFirst('item', [], ['id' => $itemid]);
221
222                 if (DBM::is_result($item) && ($item["allow_cid"] == '')  && ($item["allow_gid"] == '') &&
223                         ($item["deny_cid"] == '') && ($item["deny_gid"] == '')) {
224
225                         if (!dba::exists('item', ['uri' => $item['uri'], 'uid' => 0])) {
226                                 // Preparing public shadow (removing user specific data)
227                                 unset($item['id']);
228                                 $item['uid'] = 0;
229                                 $item['origin'] = 0;
230                                 $item['wall'] = 0;
231                                 $item['contact-id'] = Contact::getIdForURL($item['author-link'], 0);
232
233                                 if (in_array($item['type'], ["net-comment", "wall-comment"])) {
234                                         $item['type'] = 'remote-comment';
235                                 } elseif ($item['type'] == 'wall') {
236                                         $item['type'] = 'remote';
237                                 }
238
239                                 $public_shadow = item_store($item, false, false, true);
240
241                                 logger("Stored public shadow for thread ".$itemid." under id ".$public_shadow, LOGGER_DEBUG);
242                         }
243                 }
244         }
245
246         /**
247          * @brief Add a shadow entry for a given item id that is a comment
248          *
249          * This function does the same like the function above - but for comments
250          *
251          * @param integer $itemid Item ID that should be added
252          */
253         public static function addShadowPost($itemid)
254         {
255                 $item = dba::selectFirst('item', [], ['id' => $itemid]);
256                 if (!DBM::is_result($item)) {
257                         return;
258                 }
259
260                 // Is it a toplevel post?
261                 if ($item['id'] == $item['parent']) {
262                         self::addShadow($itemid);
263                         return;
264                 }
265
266                 // Is this a shadow entry?
267                 if ($item['uid'] == 0)
268                         return;
269
270                 // Is there a shadow parent?
271                 if (!dba::exists('item', ['uri' => $item['parent-uri'], 'uid' => 0])) {
272                         return;
273                 }
274
275                 // Is there already a shadow entry?
276                 if (dba::exists('item', ['uri' => $item['uri'], 'uid' => 0])) {
277                         return;
278                 }
279
280                 // Preparing public shadow (removing user specific data)
281                 unset($item['id']);
282                 $item['uid'] = 0;
283                 $item['origin'] = 0;
284                 $item['wall'] = 0;
285                 $item['contact-id'] = Contact::getIdForURL($item['author-link'], 0);
286
287                 if (in_array($item['type'], ["net-comment", "wall-comment"])) {
288                         $item['type'] = 'remote-comment';
289                 } elseif ($item['type'] == 'wall') {
290                         $item['type'] = 'remote';
291                 }
292
293                 $public_shadow = item_store($item, false, false, true);
294
295                 logger("Stored public shadow for comment ".$item['uri']." under id ".$public_shadow, LOGGER_DEBUG);
296         }
297 }