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 // Adding a shadow item entry
21 if (($itemid == 0) OR ($item['uid'] == 0))
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`",
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']);
35 $item = q("SELECT * FROM `item` WHERE `id` = %d",
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)) {
43 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = 0 LIMIT 1",
47 // Preparing public shadow (removing user specific data)
48 require_once("include/items.php");
49 unset($item[0]['id']);
51 $item[0]['contact-id'] = 0;
53 $public_shadow = item_store($item[0],false);
54 logger("add_thread: Stored public shadow for post ".$itemid." under id ".$public_shadow, LOGGER_DEBUG);
59 function update_thread_uri($itemuri, $uid) {
60 $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
63 foreach ($messages as $message)
64 update_thread($message["id"]);
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));
81 foreach ($item AS $field => $data)
82 if ($field != "uri") {
86 $sql .= "`".$field."` = '".dbesc($data)."'";
89 $result = q("UPDATE `thread` SET ".$sql." WHERE `iid` = %d", intval($itemid));
91 logger("update_thread: Update thread for item ".$itemid." - ".print_r($result, true)." ".print_r($item, true), LOGGER_DEBUG);
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"]));
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"]),
109 logger("update_thread: Updating public shadow for post ".$item["id"]." Result: ".print_r($result, true), LOGGER_DEBUG);
114 foreach ($item AS $field => $data)
115 if ($field != "id") {
119 $sql .= "`".$field."` = '".dbesc($data)."'";
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);
127 function delete_thread_uri($itemuri, $uid) {
128 $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
131 foreach ($messages as $message)
132 delete_thread($message["id"]);
135 function delete_thread($itemid) {
136 $item = q("SELECT `uri`, `uid` FROM `thread` WHERE `iid` = %d", intval($itemid));
138 $result = q("DELETE FROM `thread` WHERE `iid` = %d", intval($itemid));
140 logger("delete_thread: Deleted thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
143 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND NOT (`uid` IN (%d, 0))",
148 $r = q("DELETE FROM `item` WHERE `uri` = '%s' AND `uid` = 0)",
151 logger("delete_thread: Deleted shadow for item ".$item["uri"]." - ".print_r($result, true), LOGGER_DEBUG);
156 function update_threads() {
159 logger("update_threads: start");
161 $messages = $db->q("SELECT `id` FROM `item` WHERE `id` = `parent`", true);
163 logger("update_threads: fetched messages: ".count($messages));
165 while ($message = $db->qfetch())
166 add_thread($message["id"]);
170 function update_threads_mention() {
173 $users = q("SELECT `uid`, `nickname` FROM `user` ORDER BY `uid`");
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);
182 foreach ($parents AS $parent)
183 q("UPDATE `thread` SET `mention` = 1 WHERE `iid` = %d", $parent["parent"]);