Update references to fullcalendar and moment
[friendica.git/.git] / include / threads.php
1 <?php
2
3 use Friendica\Database\DBM;
4
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));
10
11         if (!$items)
12                 return;
13
14         $item = $items[0];
15         $item['iid'] = $itemid;
16
17         if (!$onlyshadow) {
18                 $result = dba::insert('thread', $item);
19
20                 logger("Add thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
21         }
22 }
23
24 function update_thread_uri($itemuri, $uid) {
25         $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
26
27         if (DBM::is_result($messages)) {
28                 foreach ($messages as $message) {
29                         update_thread($message["id"]);
30                 }
31         }
32 }
33
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));
37
38         if (!DBM::is_result($items)) {
39                 return;
40         }
41
42         $item = $items[0];
43
44         if ($setmention) {
45                 $item["mention"] = 1;
46         }
47
48         $sql = "";
49
50         foreach ($item AS $field => $data)
51                 if (!in_array($field, ["guid", "title", "body", "rendered-html", "rendered-hash"])) {
52                         if ($sql != "") {
53                                 $sql .= ", ";
54                         }
55
56                         $sql .= "`".$field."` = '".dbesc($data)."'";
57                 }
58
59         $result = q("UPDATE `thread` SET ".$sql." WHERE `iid` = %d", intval($itemid));
60
61         logger("Update thread for item ".$itemid." - guid ".$item["guid"]." - ".print_r($result, true)." ".print_r($item, true), LOGGER_DEBUG);
62
63         // Updating a shadow item entry
64         $items = q("SELECT `id` FROM `item` WHERE `guid` = '%s' AND `uid` = 0 LIMIT 1", dbesc($item["guid"]));
65
66         if (!DBM::is_result($items)) {
67                 return;
68         }
69
70         $result = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `rendered-html` = '%s', `rendered-hash` = '%s' WHERE `id` = %d",
71                         dbesc($item["title"]),
72                         dbesc($item["body"]),
73                         dbesc($item["rendered-html"]),
74                         dbesc($item["rendered-hash"]),
75                         intval($items[0]["id"])
76                 );
77         logger("Updating public shadow for post ".$items[0]["id"]." - guid ".$item["guid"]." Result: ".print_r($result, true), LOGGER_DEBUG);
78 }
79
80 function delete_thread_uri($itemuri, $uid) {
81         $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
82
83         if (DBM::is_result($messages)) {
84                 foreach ($messages as $message) {
85                         delete_thread($message["id"], $itemuri);
86                 }
87         }
88 }
89
90 function delete_thread($itemid, $itemuri = "") {
91         $item = q("SELECT `uid` FROM `thread` WHERE `iid` = %d", intval($itemid));
92
93         if (!DBM::is_result($item)) {
94                 logger('No thread found for id '.$itemid, LOGGER_DEBUG);
95                 return;
96         }
97
98         // Using dba::delete at this time could delete the associated item entries
99         $result = dba::e("DELETE FROM `thread` WHERE `iid` = ?", $itemid);
100
101         logger("delete_thread: Deleted thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
102
103         if ($itemuri != "") {
104                 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND NOT `deleted` AND NOT (`uid` IN (%d, 0))",
105                                 dbesc($itemuri),
106                                 intval($item["uid"])
107                         );
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);
111                 }
112         }
113 }