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