]> git.mxchange.org Git - friendica.git/blob - include/threads.php
Merge remote-tracking branch 'upstream/develop' into 1501-central-item-storage
[friendica.git] / include / threads.php
1 <?php
2 function add_thread($itemid, $onlyshadow = false) {
3         $items = q("SELECT `uid`, `created`, `edited`, `commented`, `received`, `changed`, `wall`, `private`, `pubmail`, `moderated`, `visible`, `spam`, `starred`, `bookmark`, `contact-id`,
4                         `deleted`, `origin`, `forum_mode`, `mention`, `network`  FROM `item` WHERE `id` = %d AND (`parent` = %d OR `parent` = 0) LIMIT 1", intval($itemid), intval($itemid));
5
6         if (!$items)
7                 return;
8
9         $item = $items[0];
10         $item['iid'] = $itemid;
11
12         if (!$onlyshadow) {
13                 $result = dbq("INSERT INTO `thread` (`"
14                                 .implode("`, `", array_keys($item))
15                                 ."`) VALUES ('"
16                                 .implode("', '", array_values($item))
17                                 ."')");
18
19                 logger("add_thread: Add thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
20         }
21
22         // Store a shadow copy of public items for displaying a global community page?
23         if (!get_config('system', 'global_community'))
24                 return;
25
26         // is it already a copy?
27         if (($itemid == 0) OR ($item['uid'] == 0))
28                 return;
29
30         // Is it a visible public post?
31         if (!$item["visible"] OR $item["deleted"] OR $item["moderated"] OR $item["private"])
32                 return;
33
34         // is it an entry from a connector? Only add an entry for natively connected networks
35         if (!in_array($item["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, "")))
36                 return;
37
38         // Only do these checks if the post isn't a wall post
39         if (!$item["wall"]) {
40                 // Check, if hide-friends is activated - then don't do a shadow entry
41                 $r = q("SELECT `hide-friends` FROM `profile` WHERE `is-default` AND `uid` = %d AND NOT `hide-friends`",
42                         $item['uid']);
43                 if (!count($r))
44                         return;
45                 // Check if the contact is hidden or blocked
46                 $r = q("SELECT `id` FROM `contact` WHERE NOT `hidden` AND NOT `blocked` AND `id` = %d",
47                         $item['contact-id']);
48                 if (!count($r))
49                         return;
50         }
51
52         // Only add a shadow, if the profile isn't hidden
53         $r = q("SELECT `uid` FROM `user` where `uid` = %d AND NOT `hidewall`", $item['uid']);
54         if (!count($r))
55                 return;
56
57         $item = q("SELECT * FROM `item` WHERE `id` = %d",
58                 intval($itemid));
59
60         if (count($item) AND ($item[0]["allow_cid"] == '')  AND ($item[0]["allow_gid"] == '') AND
61                 ($item[0]["deny_cid"] == '') AND ($item[0]["deny_gid"] == '')) {
62
63                 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = 0 LIMIT 1",
64                         dbesc($item['uri']));
65
66                 if (!$r) {
67                         // Preparing public shadow (removing user specific data)
68                         require_once("include/items.php");
69                         require_once("include/Contact.php");
70
71                         unset($item[0]['id']);
72                         $item[0]['uid'] = 0;
73                         $item[0]['contact-id'] = get_contact($item[0]['author-link'], 0);
74                         $public_shadow = item_store($item[0], false, false, true);
75
76                         logger("add_thread: Stored public shadow for post ".$itemid." under id ".$public_shadow, LOGGER_DEBUG);
77                 }
78         }
79 }
80
81 function update_thread_uri($itemuri, $uid) {
82         $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
83
84         if(count($messages))
85                 foreach ($messages as $message)
86                         update_thread($message["id"]);
87 }
88
89 function update_thread($itemid, $setmention = false) {
90         $items = q("SELECT `uid`, `uri`, `created`, `edited`, `commented`, `received`, `changed`, `wall`, `private`, `pubmail`, `moderated`, `visible`, `spam`, `starred`, `bookmark`, `contact-id`,
91                         `deleted`, `origin`, `forum_mode`, `network`  FROM `item` WHERE `id` = %d AND (`parent` = %d OR `parent` = 0) LIMIT 1", intval($itemid), intval($itemid));
92
93         if (!$items)
94                 return;
95
96         $item = $items[0];
97
98         if ($setmention)
99                 $item["mention"] = 1;
100
101         $sql = "";
102
103         foreach ($item AS $field => $data)
104                 if ($field != "uri") {
105                         if ($sql != "")
106                                 $sql .= ", ";
107
108                         $sql .= "`".$field."` = '".dbesc($data)."'";
109                 }
110
111         $result = q("UPDATE `thread` SET ".$sql." WHERE `iid` = %d", intval($itemid));
112
113         logger("update_thread: Update thread for item ".$itemid." - ".print_r($result, true)." ".print_r($item, true), LOGGER_DEBUG);
114
115         // Updating a shadow item entry
116         $items = q("SELECT `id`, `title`, `body`, `created`, `edited`, `commented`, `received`, `changed`, `wall`, `private`, `pubmail`,
117                         `moderated`, `visible`, `spam`, `starred`, `bookmark`, `deleted`, `origin`, `forum_mode`, `network`
118                         FROM `item` WHERE `uri` = '%s' AND `uid` = 0 LIMIT 1", dbesc($item["uri"]));
119
120         if (!$items)
121                 return;
122
123         $item = $items[0];
124
125         $result = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `network` = '%s' WHERE `id` = %d",
126                         dbesc($item["title"]),
127                         dbesc($item["body"]),
128                         dbesc($item["network"]),
129                         intval($item["id"])
130                 );
131         logger("update_thread: Updating public shadow for post ".$item["id"]." Result: ".print_r($result, true), LOGGER_DEBUG);
132
133         /*
134         $sql = "";
135
136         foreach ($item AS $field => $data)
137                 if ($field != "id") {
138                         if ($sql != "")
139                                 $sql .= ", ";
140
141                         $sql .= "`".$field."` = '".dbesc($data)."'";
142                 }
143         //logger("update_thread: Updating public shadow for post ".$item["id"]." SQL: ".$sql, LOGGER_DEBUG);
144         $result = q("UPDATE `item` SET ".$sql." WHERE `id` = %d", intval($item["id"]));
145         logger("update_thread: Updating public shadow for post ".$item["id"]." Result: ".print_r($result, true), LOGGER_DEBUG);
146         */
147 }
148
149 function delete_thread_uri($itemuri, $uid) {
150         $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
151
152         if(count($messages))
153                 foreach ($messages as $message)
154                         delete_thread($message["id"], $itemuri);
155 }
156
157 function delete_thread($itemid, $itemuri = "") {
158         $item = q("SELECT `uid` FROM `thread` WHERE `iid` = %d", intval($itemid));
159
160         $result = q("DELETE FROM `thread` WHERE `iid` = %d", intval($itemid));
161
162         logger("delete_thread: Deleted thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
163
164         if ($itemuri != "") {
165                 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND NOT (`uid` IN (%d, 0))",
166                                 dbesc($itemuri),
167                                 intval($item["uid"])
168                         );
169                 if (!count($r)) {
170                         $r = q("DELETE FROM `item` WHERE `uri` = '%s' AND `uid` = 0)",
171                                 dbesc($itemuri)
172                         );
173                         logger("delete_thread: Deleted shadow for item ".$itemuri." - ".print_r($result, true), LOGGER_DEBUG);
174                 }
175         }
176 }
177
178 function update_threads() {
179         global $db;
180
181         logger("update_threads: start");
182
183         $messages = $db->q("SELECT `id` FROM `item` WHERE `id` = `parent`", true);
184
185         logger("update_threads: fetched messages: ".count($messages));
186
187         while ($message = $db->qfetch())
188                 add_thread($message["id"]);
189         $db->qclose();
190 }
191
192 function update_threads_mention() {
193         $a = get_app();
194
195         $users = q("SELECT `uid`, `nickname` FROM `user` ORDER BY `uid`");
196
197         foreach ($users AS $user) {
198                 $self = normalise_link($a->get_baseurl() . '/profile/' . $user['nickname']);
199                 $selfhttps = str_replace("http://", "https://", $self);
200                 $parents = q("SELECT DISTINCT(`parent`) FROM `item` WHERE `uid` = %d AND
201                                 ((`owner-link` IN ('%s', '%s')) OR (`author-link` IN ('%s', '%s')))",
202                                 $user["uid"], $self, $selfhttps, $self, $selfhttps);
203
204                 foreach ($parents AS $parent)
205                         q("UPDATE `thread` SET `mention` = 1 WHERE `iid` = %d", $parent["parent"]);
206         }
207 }
208
209
210 function update_shadow_copy() {
211         global $db;
212
213         logger("start");
214
215         $messages = $db->q(sprintf("SELECT `iid` FROM `thread` WHERE `uid` != 0 AND `network` IN ('', '%s', '%s', '%s')
216                                         AND `visible` AND NOT `deleted` AND NOT `moderated` AND NOT `private` ORDER BY `created`",
217                                 NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS), true);
218
219         logger("fetched messages: ".count($messages));
220         while ($message = $db->qfetch())
221                 add_thread($message["iid"], true);
222
223         $db->qclose();
224 }
225 ?>