3 use Friendica\Database\DBM;
5 function add_thread($itemid, $onlyshadow = false) {
6 $items = q("SELECT `uid`, `created`, `edited`, `commented`, `received`, `changed`, `wall`, `private`, `pubmail`,
7 `moderated`, `visible`, `spam`, `starred`, `bookmark`, `contact-id`, `gcontact-id`,
8 `deleted`, `origin`, `forum_mode`, `mention`, `network`, `author-id`, `owner-id`
9 FROM `item` WHERE `id` = %d AND (`parent` = %d OR `parent` = 0) LIMIT 1", intval($itemid), intval($itemid));
15 $item['iid'] = $itemid;
18 $result = dba::insert('thread', $item);
20 logger("Add thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
24 function update_thread_uri($itemuri, $uid) {
25 $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
27 if (DBM::is_result($messages)) {
28 foreach ($messages as $message) {
29 update_thread($message["id"]);
34 function update_thread($itemid, $setmention = false) {
35 $items = q("SELECT `uid`, `guid`, `title`, `body`, `created`, `edited`, `commented`, `received`, `changed`, `wall`, `private`, `pubmail`, `moderated`, `visible`, `spam`, `starred`, `bookmark`, `contact-id`, `gcontact-id`,
36 `deleted`, `origin`, `forum_mode`, `network`, `rendered-html`, `rendered-hash` FROM `item` WHERE `id` = %d AND (`parent` = %d OR `parent` = 0) LIMIT 1", intval($itemid), intval($itemid));
38 if (!DBM::is_result($items)) {
50 foreach ($item AS $field => $data)
51 if (!in_array($field, ["guid", "title", "body", "rendered-html", "rendered-hash"])) {
56 $sql .= "`".$field."` = '".dbesc($data)."'";
59 $result = q("UPDATE `thread` SET ".$sql." WHERE `iid` = %d", intval($itemid));
61 logger("Update thread for item ".$itemid." - guid ".$item["guid"]." - ".print_r($result, true)." ".print_r($item, true), LOGGER_DEBUG);
63 // Updating a shadow item entry
64 $items = q("SELECT `id` FROM `item` WHERE `guid` = '%s' AND `uid` = 0 LIMIT 1", dbesc($item["guid"]));
66 if (!DBM::is_result($items)) {
70 $result = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `rendered-html` = '%s', `rendered-hash` = '%s' WHERE `id` = %d",
71 dbesc($item["title"]),
73 dbesc($item["rendered-html"]),
74 dbesc($item["rendered-hash"]),
75 intval($items[0]["id"])
77 logger("Updating public shadow for post ".$items[0]["id"]." - guid ".$item["guid"]." Result: ".print_r($result, true), LOGGER_DEBUG);
80 function delete_thread_uri($itemuri, $uid) {
81 $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
83 if (DBM::is_result($messages)) {
84 foreach ($messages as $message) {
85 delete_thread($message["id"], $itemuri);
90 function delete_thread($itemid, $itemuri = "") {
91 $item = q("SELECT `uid` FROM `thread` WHERE `iid` = %d", intval($itemid));
93 if (!DBM::is_result($item)) {
94 logger('No thread found for id '.$itemid, LOGGER_DEBUG);
98 // Using dba::delete at this time could delete the associated item entries
99 $result = dba::e("DELETE FROM `thread` WHERE `iid` = ?", $itemid);
101 logger("delete_thread: Deleted thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
103 if ($itemuri != "") {
104 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND NOT `deleted` AND NOT (`uid` IN (%d, 0))",
108 if (!DBM::is_result($r)) {
109 dba::delete('item', ['uri' => $itemuri, 'uid' => 0]);
110 logger("delete_thread: Deleted shadow for item ".$itemuri, LOGGER_DEBUG);