]> git.mxchange.org Git - friendica.git/blob - include/threads.php
Class file relocations
[friendica.git] / include / threads.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\System;
5 use Friendica\Database\DBM;
6
7 function add_thread($itemid, $onlyshadow = false) {
8         $items = q("SELECT `uid`, `created`, `edited`, `commented`, `received`, `changed`, `wall`, `private`, `pubmail`,
9                         `moderated`, `visible`, `spam`, `starred`, `bookmark`, `contact-id`, `gcontact-id`,
10                         `deleted`, `origin`, `forum_mode`, `mention`, `network`, `author-id`, `owner-id`
11                 FROM `item` WHERE `id` = %d AND (`parent` = %d OR `parent` = 0) LIMIT 1", intval($itemid), intval($itemid));
12
13         if (!$items)
14                 return;
15
16         $item = $items[0];
17         $item['iid'] = $itemid;
18
19         if (!$onlyshadow) {
20                 $result = dba::insert('thread', $item);
21
22                 logger("Add thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
23         }
24 }
25
26 /**
27  * @brief Add a shadow entry for a given item id that is a thread starter
28  *
29  * We store every public item entry additionally with the user id "0".
30  * This is used for the community page and for the search.
31  * It is planned that in the future we will store public item entries only once.
32  *
33  * @param integer $itemid Item ID that should be added
34  */
35 function add_shadow_thread($itemid) {
36         $items = q("SELECT `uid`, `wall`, `private`, `moderated`, `visible`, `contact-id`, `deleted`, `network`, `author-id`, `owner-id`
37                 FROM `item` WHERE `id` = %d AND (`parent` = %d OR `parent` = 0) LIMIT 1", intval($itemid), intval($itemid));
38
39         if (!DBM::is_result($items)) {
40                 return;
41         }
42
43         $item = $items[0];
44
45         // is it already a copy?
46         if (($itemid == 0) || ($item['uid'] == 0)) {
47                 return;
48         }
49
50         // Is it a visible public post?
51         if (!$item["visible"] || $item["deleted"] || $item["moderated"] || $item["private"]) {
52                 return;
53         }
54
55         // is it an entry from a connector? Only add an entry for natively connected networks
56         if (!in_array($item["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
57                 return;
58         }
59
60         // Is the public contact configured as hidden?
61         if (hiddenContact($item["owner-id"]) || hiddenContact($item["author-id"])) {
62                 return;
63         }
64
65         // Only do these checks if the post isn't a wall post
66         if (!$item["wall"]) {
67                 // Check, if hide-friends is activated - then don't do a shadow entry
68                 $r = q("SELECT `hide-friends` FROM `profile` WHERE `is-default` AND `uid` = %d AND NOT `hide-friends`",
69                         $item['uid']);
70
71                 if (!DBM::is_result($r)) {
72                         return;
73                 }
74
75                 // Check if the contact is hidden or blocked
76                 $r = q("SELECT `id` FROM `contact` WHERE NOT `hidden` AND NOT `blocked` AND `id` = %d",
77                         $item['contact-id']);
78
79                 if (!DBM::is_result($r)) {
80                         return;
81                 }
82         }
83
84         // Only add a shadow, if the profile isn't hidden
85         $r = q("SELECT `uid` FROM `user` where `uid` = %d AND NOT `hidewall`", $item['uid']);
86         if (!DBM::is_result($r)) {
87                 return;
88         }
89
90         $item = q("SELECT * FROM `item` WHERE `id` = %d", intval($itemid));
91
92         if (count($item) && ($item[0]["allow_cid"] == '')  && ($item[0]["allow_gid"] == '') &&
93                 ($item[0]["deny_cid"] == '') && ($item[0]["deny_gid"] == '')) {
94
95                 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = 0 LIMIT 1",
96                         dbesc($item['uri']));
97
98                 if (!DBM::is_result($r)) {
99                         // Preparing public shadow (removing user specific data)
100                         require_once("include/items.php");
101                         require_once("include/Contact.php");
102
103                         unset($item[0]['id']);
104                         $item[0]['uid'] = 0;
105                         $item[0]['origin'] = 0;
106                         $item[0]['wall'] = 0;
107                         $item[0]['contact-id'] = get_contact($item[0]['author-link'], 0);
108
109                         if (in_array($item[0]['type'], array("net-comment", "wall-comment"))) {
110                                 $item[0]['type'] = 'remote-comment';
111                         } elseif ($item[0]['type'] == 'wall') {
112                                 $item[0]['type'] = 'remote';
113                         }
114
115                         $public_shadow = item_store($item[0], false, false, true);
116
117                         logger("Stored public shadow for thread ".$itemid." under id ".$public_shadow, LOGGER_DEBUG);
118                 }
119         }
120 }
121
122 /**
123  * @brief Add a shadow entry for a given item id that is a comment
124  *
125  * This function does the same like the function above - but for comments
126  *
127  * @param integer $itemid Item ID that should be added
128  */
129 function add_shadow_entry($itemid) {
130
131         $items = q("SELECT * FROM `item` WHERE `id` = %d", intval($itemid));
132
133         if (!DBM::is_result($items)) {
134                 return;
135         }
136
137         $item = $items[0];
138
139         // Is it a toplevel post?
140         if ($item['id'] == $item['parent']) {
141                 add_shadow_thread($itemid);
142                 return;
143         }
144
145         // Is this a shadow entry?
146         if ($item['uid'] == 0)
147                 return;
148
149         // Is there a shadow parent?
150         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = 0 LIMIT 1", dbesc($item['parent-uri']));
151         if (!DBM::is_result($r))
152                 return;
153
154         // Is there already a shadow entry?
155         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = 0 LIMIT 1", dbesc($item['uri']));
156         if (DBM::is_result($r))
157                 return;
158
159         // Preparing public shadow (removing user specific data)
160         require_once("include/items.php");
161         require_once("include/Contact.php");
162
163         unset($item['id']);
164         $item['uid'] = 0;
165         $item['origin'] = 0;
166         $item['wall'] = 0;
167         $item['contact-id'] = get_contact($item['author-link'], 0);
168
169         if (in_array($item['type'], array("net-comment", "wall-comment"))) {
170                 $item['type'] = 'remote-comment';
171         } elseif ($item['type'] == 'wall') {
172                 $item['type'] = 'remote';
173         }
174
175         $public_shadow = item_store($item, false, false, true);
176
177         logger("Stored public shadow for comment ".$item['uri']." under id ".$public_shadow, LOGGER_DEBUG);
178 }
179
180 function update_thread_uri($itemuri, $uid) {
181         $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
182
183         if (DBM::is_result($messages)) {
184                 foreach ($messages as $message) {
185                         update_thread($message["id"]);
186                 }
187         }
188 }
189
190 function update_thread($itemid, $setmention = false) {
191         $items = q("SELECT `uid`, `guid`, `title`, `body`, `created`, `edited`, `commented`, `received`, `changed`, `wall`, `private`, `pubmail`, `moderated`, `visible`, `spam`, `starred`, `bookmark`, `contact-id`, `gcontact-id`,
192                         `deleted`, `origin`, `forum_mode`, `network`, `rendered-html`, `rendered-hash` FROM `item` WHERE `id` = %d AND (`parent` = %d OR `parent` = 0) LIMIT 1", intval($itemid), intval($itemid));
193
194         if (!DBM::is_result($items)) {
195                 return;
196         }
197
198         $item = $items[0];
199
200         if ($setmention) {
201                 $item["mention"] = 1;
202         }
203
204         $sql = "";
205
206         foreach ($item AS $field => $data)
207                 if (!in_array($field, array("guid", "title", "body", "rendered-html", "rendered-hash"))) {
208                         if ($sql != "") {
209                                 $sql .= ", ";
210                         }
211
212                         $sql .= "`".$field."` = '".dbesc($data)."'";
213                 }
214
215         $result = q("UPDATE `thread` SET ".$sql." WHERE `iid` = %d", intval($itemid));
216
217         logger("Update thread for item ".$itemid." - guid ".$item["guid"]." - ".print_r($result, true)." ".print_r($item, true), LOGGER_DEBUG);
218
219         // Updating a shadow item entry
220         $items = q("SELECT `id` FROM `item` WHERE `guid` = '%s' AND `uid` = 0 LIMIT 1", dbesc($item["guid"]));
221
222         if (!DBM::is_result($items)) {
223                 return;
224         }
225
226         $result = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `rendered-html` = '%s', `rendered-hash` = '%s' WHERE `id` = %d",
227                         dbesc($item["title"]),
228                         dbesc($item["body"]),
229                         dbesc($item["rendered-html"]),
230                         dbesc($item["rendered-hash"]),
231                         intval($items[0]["id"])
232                 );
233         logger("Updating public shadow for post ".$items[0]["id"]." - guid ".$item["guid"]." Result: ".print_r($result, true), LOGGER_DEBUG);
234 }
235
236 function delete_thread_uri($itemuri, $uid) {
237         $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
238
239         if (DBM::is_result($messages)) {
240                 foreach ($messages as $message) {
241                         delete_thread($message["id"], $itemuri);
242                 }
243         }
244 }
245
246 function delete_thread($itemid, $itemuri = "") {
247         $item = q("SELECT `uid` FROM `thread` WHERE `iid` = %d", intval($itemid));
248
249         if (!DBM::is_result($item)) {
250                 logger('No thread found for id '.$itemid, LOGGER_DEBUG);
251                 return;
252         }
253
254         // Using dba::delete at this time could delete the associated item entries
255         $result = q("DELETE FROM `thread` WHERE `iid` = %d", intval($itemid));
256
257         logger("delete_thread: Deleted thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
258
259         if ($itemuri != "") {
260                 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND NOT `deleted` AND NOT (`uid` IN (%d, 0))",
261                                 dbesc($itemuri),
262                                 intval($item["uid"])
263                         );
264                 if (!DBM::is_result($r)) {
265                         dba::delete('item', array('uri' => $itemuri, 'uid' => 0));
266                         logger("delete_thread: Deleted shadow for item ".$itemuri, LOGGER_DEBUG);
267                 }
268         }
269 }
270
271 function update_threads() {
272         logger("update_threads: start");
273
274         $messages = dba::select('item', array('id'), array("`id` = `parent`"));
275
276         logger("update_threads: fetched messages: ".dba::num_rows($messages));
277
278         while ($message = dba::fetch($messages)) {
279                 add_thread($message["id"]);
280                 add_shadow_thread($message["id"]);
281         }
282         dba::close($messages);
283 }
284
285 function update_threads_mention() {
286         $users = q("SELECT `uid`, `nickname` FROM `user` ORDER BY `uid`");
287
288         foreach ($users AS $user) {
289                 $self = normalise_link(System::baseUrl() . '/profile/' . $user['nickname']);
290                 $selfhttps = str_replace("http://", "https://", $self);
291                 $parents = q("SELECT DISTINCT(`parent`) FROM `item` WHERE `uid` = %d AND
292                                 ((`owner-link` IN ('%s', '%s')) OR (`author-link` IN ('%s', '%s')))",
293                                 $user["uid"], $self, $selfhttps, $self, $selfhttps);
294
295                 foreach ($parents AS $parent)
296                         q("UPDATE `thread` SET `mention` = 1 WHERE `iid` = %d", $parent["parent"]);
297         }
298 }
299
300
301 function update_shadow_copy() {
302         logger("start");
303
304         $condition = "`uid` != 0 AND `network` IN ('', ?, ?, ?) AND `visible` AND NOT `deleted` AND NOT `moderated` AND NOT `private`";
305         $messages = dba::select('thread', array('iid'), array($condition, NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS),
306                                 array('order' => 'created'));
307
308         logger("fetched messages: ".dba::num_rows($messages));
309         while ($message = dba::fetch($messages))
310                 add_shadow_thread($message["iid"]);
311
312         dba::close($messages);
313 }