2014-03-16 13:19:19 -04:00
|
|
|
<?php
|
2017-04-30 00:07:00 -04:00
|
|
|
|
2017-11-07 22:57:46 -05:00
|
|
|
use Friendica\Database\DBM;
|
2017-04-30 00:07:00 -04:00
|
|
|
|
2014-12-21 15:19:37 -05:00
|
|
|
function add_thread($itemid, $onlyshadow = false) {
|
2016-06-18 19:07:20 -04:00
|
|
|
$items = q("SELECT `uid`, `created`, `edited`, `commented`, `received`, `changed`, `wall`, `private`, `pubmail`,
|
|
|
|
`moderated`, `visible`, `spam`, `starred`, `bookmark`, `contact-id`, `gcontact-id`,
|
|
|
|
`deleted`, `origin`, `forum_mode`, `mention`, `network`, `author-id`, `owner-id`
|
|
|
|
FROM `item` WHERE `id` = %d AND (`parent` = %d OR `parent` = 0) LIMIT 1", intval($itemid), intval($itemid));
|
2014-03-16 13:19:19 -04:00
|
|
|
|
|
|
|
if (!$items)
|
|
|
|
return;
|
|
|
|
|
|
|
|
$item = $items[0];
|
|
|
|
$item['iid'] = $itemid;
|
|
|
|
|
2014-12-21 15:19:37 -05:00
|
|
|
if (!$onlyshadow) {
|
2017-09-15 15:41:30 -04:00
|
|
|
$result = dba::insert('thread', $item);
|
2014-03-16 13:19:19 -04:00
|
|
|
|
2016-10-23 17:59:40 -04:00
|
|
|
logger("Add thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
|
2014-12-21 15:19:37 -05:00
|
|
|
}
|
2016-10-23 17:59:40 -04:00
|
|
|
}
|
|
|
|
|
2014-03-16 13:19:19 -04:00
|
|
|
function update_thread_uri($itemuri, $uid) {
|
|
|
|
$messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
|
|
|
|
|
2017-11-07 22:57:46 -05:00
|
|
|
if (DBM::is_result($messages)) {
|
2017-04-04 13:48:25 -04:00
|
|
|
foreach ($messages as $message) {
|
2014-03-16 13:19:19 -04:00
|
|
|
update_thread($message["id"]);
|
2017-04-04 13:48:25 -04:00
|
|
|
}
|
|
|
|
}
|
2014-03-16 13:19:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function update_thread($itemid, $setmention = false) {
|
2016-01-10 11:10:56 -05:00
|
|
|
$items = q("SELECT `uid`, `guid`, `title`, `body`, `created`, `edited`, `commented`, `received`, `changed`, `wall`, `private`, `pubmail`, `moderated`, `visible`, `spam`, `starred`, `bookmark`, `contact-id`, `gcontact-id`,
|
2016-01-14 15:56:37 -05:00
|
|
|
`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));
|
2014-03-16 13:19:19 -04:00
|
|
|
|
2017-11-07 22:57:46 -05:00
|
|
|
if (!DBM::is_result($items)) {
|
2014-03-16 13:19:19 -04:00
|
|
|
return;
|
2017-04-04 13:48:25 -04:00
|
|
|
}
|
2014-03-16 13:19:19 -04:00
|
|
|
|
|
|
|
$item = $items[0];
|
|
|
|
|
2017-04-04 13:48:25 -04:00
|
|
|
if ($setmention) {
|
2014-03-16 13:19:19 -04:00
|
|
|
$item["mention"] = 1;
|
2017-04-04 13:48:25 -04:00
|
|
|
}
|
2014-03-16 13:19:19 -04:00
|
|
|
|
|
|
|
$sql = "";
|
|
|
|
|
2014-12-21 05:02:31 -05:00
|
|
|
foreach ($item AS $field => $data)
|
2018-01-15 08:05:12 -05:00
|
|
|
if (!in_array($field, ["guid", "title", "body", "rendered-html", "rendered-hash"])) {
|
2017-04-04 13:48:25 -04:00
|
|
|
if ($sql != "") {
|
2014-12-21 05:02:31 -05:00
|
|
|
$sql .= ", ";
|
2017-04-04 13:48:25 -04:00
|
|
|
}
|
2014-03-16 13:19:19 -04:00
|
|
|
|
2014-12-21 05:02:31 -05:00
|
|
|
$sql .= "`".$field."` = '".dbesc($data)."'";
|
|
|
|
}
|
2014-03-16 13:19:19 -04:00
|
|
|
|
2014-12-21 05:02:31 -05:00
|
|
|
$result = q("UPDATE `thread` SET ".$sql." WHERE `iid` = %d", intval($itemid));
|
2014-03-16 13:19:19 -04:00
|
|
|
|
2015-05-03 15:25:03 -04:00
|
|
|
logger("Update thread for item ".$itemid." - guid ".$item["guid"]." - ".print_r($result, true)." ".print_r($item, true), LOGGER_DEBUG);
|
2014-12-21 05:02:31 -05:00
|
|
|
|
|
|
|
// Updating a shadow item entry
|
2015-05-03 15:25:03 -04:00
|
|
|
$items = q("SELECT `id` FROM `item` WHERE `guid` = '%s' AND `uid` = 0 LIMIT 1", dbesc($item["guid"]));
|
2014-12-21 05:02:31 -05:00
|
|
|
|
2017-11-07 22:57:46 -05:00
|
|
|
if (!DBM::is_result($items)) {
|
2014-12-21 05:02:31 -05:00
|
|
|
return;
|
2017-04-04 13:48:25 -04:00
|
|
|
}
|
2014-12-21 05:02:31 -05:00
|
|
|
|
2016-01-14 15:56:37 -05:00
|
|
|
$result = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `rendered-html` = '%s', `rendered-hash` = '%s' WHERE `id` = %d",
|
2014-12-21 05:02:31 -05:00
|
|
|
dbesc($item["title"]),
|
|
|
|
dbesc($item["body"]),
|
2016-01-14 15:56:37 -05:00
|
|
|
dbesc($item["rendered-html"]),
|
|
|
|
dbesc($item["rendered-hash"]),
|
2015-05-03 15:25:03 -04:00
|
|
|
intval($items[0]["id"])
|
2014-12-21 05:02:31 -05:00
|
|
|
);
|
2015-05-03 15:25:03 -04:00
|
|
|
logger("Updating public shadow for post ".$items[0]["id"]." - guid ".$item["guid"]." Result: ".print_r($result, true), LOGGER_DEBUG);
|
2014-03-16 13:19:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function delete_thread_uri($itemuri, $uid) {
|
|
|
|
$messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
|
|
|
|
|
2017-11-07 22:57:46 -05:00
|
|
|
if (DBM::is_result($messages)) {
|
2017-04-04 13:48:25 -04:00
|
|
|
foreach ($messages as $message) {
|
2015-01-06 19:46:13 -05:00
|
|
|
delete_thread($message["id"], $itemuri);
|
2017-04-04 13:48:25 -04:00
|
|
|
}
|
|
|
|
}
|
2014-03-16 13:19:19 -04:00
|
|
|
}
|
|
|
|
|
2015-01-06 19:46:13 -05:00
|
|
|
function delete_thread($itemid, $itemuri = "") {
|
|
|
|
$item = q("SELECT `uid` FROM `thread` WHERE `iid` = %d", intval($itemid));
|
2014-12-21 05:02:31 -05:00
|
|
|
|
2017-11-07 22:57:46 -05:00
|
|
|
if (!DBM::is_result($item)) {
|
2017-09-24 08:02:32 -04:00
|
|
|
logger('No thread found for id '.$itemid, LOGGER_DEBUG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-17 04:01:22 -04:00
|
|
|
// Using dba::delete at this time could delete the associated item entries
|
2017-11-22 02:21:19 -05:00
|
|
|
$result = dba::e("DELETE FROM `thread` WHERE `iid` = ?", $itemid);
|
2014-03-16 13:19:19 -04:00
|
|
|
|
|
|
|
logger("delete_thread: Deleted thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
|
2014-12-21 05:02:31 -05:00
|
|
|
|
2015-01-06 19:46:13 -05:00
|
|
|
if ($itemuri != "") {
|
2017-09-17 04:01:22 -04:00
|
|
|
$r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND NOT `deleted` AND NOT (`uid` IN (%d, 0))",
|
2015-01-06 19:46:13 -05:00
|
|
|
dbesc($itemuri),
|
2014-12-21 05:02:31 -05:00
|
|
|
intval($item["uid"])
|
|
|
|
);
|
2017-11-07 22:57:46 -05:00
|
|
|
if (!DBM::is_result($r)) {
|
2018-01-15 08:05:12 -05:00
|
|
|
dba::delete('item', ['uri' => $itemuri, 'uid' => 0]);
|
2017-09-17 04:01:22 -04:00
|
|
|
logger("delete_thread: Deleted shadow for item ".$itemuri, LOGGER_DEBUG);
|
2014-12-21 05:02:31 -05:00
|
|
|
}
|
|
|
|
}
|
2014-03-16 13:19:19 -04:00
|
|
|
}
|