]> git.mxchange.org Git - friendica.git/blob - include/threads.php
Update function for creating the shadow entries.
[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, NETWORK_FEED, "")))
36                 return;
37
38         // Check, if hide-friends is activated - then don't do a shadow entry
39         // Only do this check if the post isn't a wall post
40         if (!$item["wall"]) {
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         }
46
47         // Only add a shadow, if the profile isn't hidden
48         $r = q("SELECT `uid` FROM `user` where `uid` = %d AND NOT `hidewall`", $item['uid']);
49         if (!count($r))
50                 return;
51
52         $item = q("SELECT * FROM `item` WHERE `id` = %d",
53                 intval($itemid));
54
55         if (count($item) AND ($item[0]["allow_cid"] == '')  AND ($item[0]["allow_gid"] == '') AND
56                 ($item[0]["deny_cid"] == '') AND ($item[0]["deny_gid"] == '')) {
57
58                 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = 0 LIMIT 1",
59                         dbesc($item['uri']));
60
61                 if (!$r) {
62                         // Preparing public shadow (removing user specific data)
63                         require_once("include/items.php");
64                         unset($item[0]['id']);
65                         $item[0]['uid'] = 0;
66                         $item[0]['contact-id'] = 0;
67                         $public_shadow = item_store($item[0], false, false, true);
68
69                         logger("add_thread: Stored public shadow for post ".$itemid." under id ".$public_shadow, LOGGER_DEBUG);
70                 }
71         }
72 }
73
74 function update_thread_uri($itemuri, $uid) {
75         $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
76
77         if(count($messages))
78                 foreach ($messages as $message)
79                         update_thread($message["id"]);
80 }
81
82 function update_thread($itemid, $setmention = false) {
83         $items = q("SELECT `uid`, `uri`, `created`, `edited`, `commented`, `received`, `changed`, `wall`, `private`, `pubmail`, `moderated`, `visible`, `spam`, `starred`, `bookmark`, `contact-id`,
84                         `deleted`, `origin`, `forum_mode`, `network`  FROM `item` WHERE `id` = %d AND (`parent` = %d OR `parent` = 0) LIMIT 1", intval($itemid), intval($itemid));
85
86         if (!$items)
87                 return;
88
89         $item = $items[0];
90
91         if ($setmention)
92                 $item["mention"] = 1;
93
94         $sql = "";
95
96         foreach ($item AS $field => $data)
97                 if ($field != "uri") {
98                         if ($sql != "")
99                                 $sql .= ", ";
100
101                         $sql .= "`".$field."` = '".dbesc($data)."'";
102                 }
103
104         $result = q("UPDATE `thread` SET ".$sql." WHERE `iid` = %d", intval($itemid));
105
106         logger("update_thread: Update thread for item ".$itemid." - ".print_r($result, true)." ".print_r($item, true), LOGGER_DEBUG);
107
108         // Updating a shadow item entry
109         $items = q("SELECT `id`, `title`, `body`, `created`, `edited`, `commented`, `received`, `changed`, `wall`, `private`, `pubmail`,
110                         `moderated`, `visible`, `spam`, `starred`, `bookmark`, `deleted`, `origin`, `forum_mode`, `network`
111                         FROM `item` WHERE `uri` = '%s' AND `uid` = 0 LIMIT 1", dbesc($item["uri"]));
112
113         if (!$items)
114                 return;
115
116         $item = $items[0];
117
118         $result = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `network` = '%s' WHERE `id` = %d",
119                         dbesc($item["title"]),
120                         dbesc($item["body"]),
121                         dbesc($item["network"]),
122                         intval($item["id"])
123                 );
124         logger("update_thread: Updating public shadow for post ".$item["id"]." Result: ".print_r($result, true), LOGGER_DEBUG);
125
126         /*
127         $sql = "";
128
129         foreach ($item AS $field => $data)
130                 if ($field != "id") {
131                         if ($sql != "")
132                                 $sql .= ", ";
133
134                         $sql .= "`".$field."` = '".dbesc($data)."'";
135                 }
136         //logger("update_thread: Updating public shadow for post ".$item["id"]." SQL: ".$sql, LOGGER_DEBUG);
137         $result = q("UPDATE `item` SET ".$sql." WHERE `id` = %d", intval($item["id"]));
138         logger("update_thread: Updating public shadow for post ".$item["id"]." Result: ".print_r($result, true), LOGGER_DEBUG);
139         */
140 }
141
142 function delete_thread_uri($itemuri, $uid) {
143         $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
144
145         if(count($messages))
146                 foreach ($messages as $message)
147                         delete_thread($message["id"]);
148 }
149
150 function delete_thread($itemid) {
151         $item = q("SELECT `uri`, `uid` FROM `thread` WHERE `iid` = %d", intval($itemid));
152
153         $result = q("DELETE FROM `thread` WHERE `iid` = %d", intval($itemid));
154
155         logger("delete_thread: Deleted thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
156
157         if (count($item)) {
158                 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND NOT (`uid` IN (%d, 0))",
159                                 dbesc($item["uri"]),
160                                 intval($item["uid"])
161                         );
162                 if (!count($r)) {
163                         $r = q("DELETE FROM `item` WHERE `uri` = '%s' AND `uid` = 0)",
164                                 dbesc($item["uri"])
165                         );
166                         logger("delete_thread: Deleted shadow for item ".$item["uri"]." - ".print_r($result, true), LOGGER_DEBUG);
167                 }
168         }
169 }
170
171 function update_threads() {
172         global $db;
173
174         logger("update_threads: start");
175
176         $messages = $db->q("SELECT `id` FROM `item` WHERE `id` = `parent`", true);
177
178         logger("update_threads: fetched messages: ".count($messages));
179
180         while ($message = $db->qfetch())
181                 add_thread($message["id"]);
182         $db->qclose();
183 }
184
185 function update_threads_mention() {
186         $a = get_app();
187
188         $users = q("SELECT `uid`, `nickname` FROM `user` ORDER BY `uid`");
189
190         foreach ($users AS $user) {
191                 $self = normalise_link($a->get_baseurl() . '/profile/' . $user['nickname']);
192                 $selfhttps = str_replace("http://", "https://", $self);
193                 $parents = q("SELECT DISTINCT(`parent`) FROM `item` WHERE `uid` = %d AND
194                                 ((`owner-link` IN ('%s', '%s')) OR (`author-link` IN ('%s', '%s')))",
195                                 $user["uid"], $self, $selfhttps, $self, $selfhttps);
196
197                 foreach ($parents AS $parent)
198                         q("UPDATE `thread` SET `mention` = 1 WHERE `iid` = %d", $parent["parent"]);
199         }
200 }
201
202
203 function update_shadow_copy() {
204         global $db;
205
206         logger("start");
207
208         $messages = $db->q(sprintf("SELECT `iid` FROM `thread` WHERE `uid` != 0 AND `network` IN ('', '%s', '%s', '%s', '%s')
209                                         AND `visible` AND NOT `deleted` AND NOT `moderated` AND NOT `private` ORDER BY `created`",
210                                 NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_FEED, NETWORK_OSTATUS), true);
211
212         logger("fetched messages: ".count($messages));
213         while ($message = $db->qfetch())
214                 add_thread($message["iid"], true);
215
216         $db->qclose();
217 }
218 ?>