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));
10 $item['iid'] = $itemid;
12 $result = dbq("INSERT INTO `thread` (`"
13 .implode("`, `", array_keys($item))
15 .implode("', '", array_values($item))
18 logger("add_thread: Add thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
20 // Store a shadow copy of public items for displaying a global community page?
21 if (!get_config('system', 'global_community'))
24 // is it already a copy?
25 if (($itemid == 0) OR ($item['uid'] == 0))
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`",
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']);
39 $item = q("SELECT * FROM `item` WHERE `id` = %d",
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)) {
47 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = 0 LIMIT 1",
51 // Preparing public shadow (removing user specific data)
52 require_once("include/items.php");
53 unset($item[0]['id']);
55 $item[0]['contact-id'] = 0;
57 $public_shadow = item_store($item[0],false);
58 logger("add_thread: Stored public shadow for post ".$itemid." under id ".$public_shadow, LOGGER_DEBUG);
63 function update_thread_uri($itemuri, $uid) {
64 $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
67 foreach ($messages as $message)
68 update_thread($message["id"]);
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));
85 foreach ($item AS $field => $data)
86 if ($field != "uri") {
90 $sql .= "`".$field."` = '".dbesc($data)."'";
93 $result = q("UPDATE `thread` SET ".$sql." WHERE `iid` = %d", intval($itemid));
95 logger("update_thread: Update thread for item ".$itemid." - ".print_r($result, true)." ".print_r($item, true), LOGGER_DEBUG);
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"]));
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"]),
113 logger("update_thread: Updating public shadow for post ".$item["id"]." Result: ".print_r($result, true), LOGGER_DEBUG);
118 foreach ($item AS $field => $data)
119 if ($field != "id") {
123 $sql .= "`".$field."` = '".dbesc($data)."'";
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);
131 function delete_thread_uri($itemuri, $uid) {
132 $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
135 foreach ($messages as $message)
136 delete_thread($message["id"]);
139 function delete_thread($itemid) {
140 $item = q("SELECT `uri`, `uid` FROM `thread` WHERE `iid` = %d", intval($itemid));
142 $result = q("DELETE FROM `thread` WHERE `iid` = %d", intval($itemid));
144 logger("delete_thread: Deleted thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
147 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND NOT (`uid` IN (%d, 0))",
152 $r = q("DELETE FROM `item` WHERE `uri` = '%s' AND `uid` = 0)",
155 logger("delete_thread: Deleted shadow for item ".$item["uri"]." - ".print_r($result, true), LOGGER_DEBUG);
160 function update_threads() {
163 logger("update_threads: start");
165 $messages = $db->q("SELECT `id` FROM `item` WHERE `id` = `parent`", true);
167 logger("update_threads: fetched messages: ".count($messages));
169 while ($message = $db->qfetch())
170 add_thread($message["id"]);
174 function update_threads_mention() {
177 $users = q("SELECT `uid`, `nickname` FROM `user` ORDER BY `uid`");
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);
186 foreach ($parents AS $parent)
187 q("UPDATE `thread` SET `mention` = 1 WHERE `iid` = %d", $parent["parent"]);