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