{
/*
These Fields are not added below. They are here to for bug search.
- 'type', 'extid', 'changed', 'moderated', 'target-type', 'target', 'resource-id',
- 'tag', 'inform', 'pubmail', 'visible', 'bookmark', 'unseen', 'deleted',
- 'forum_mode', 'mention', 'global', 'shadow',
*/
$item_fields = ['author-id', 'owner-id', 'contact-id', 'uid', 'id', 'parent',
'commented', 'created', 'edited', 'received', 'verb', 'object-type', 'postopts', 'plink',
'guid', 'wall', 'private', 'starred', 'origin', 'title', 'body', 'file', 'event-id',
'location', 'coord', 'app', 'attach', 'rendered-hash', 'rendered-html', 'object',
- 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'unseen',
+ 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
'id' => 'item_id', 'network' => 'item_network'];
+ // The additional fields aren't needed to be selected by default.
+ // We need them to select the correct tables. To see the difference we split the arrays
+ if (!empty($selected)) {
+ $additional_item_fields = ['type', 'extid', 'changed', 'moderated', 'target-type', 'target',
+ 'resource-id', 'tag', 'inform', 'pubmail', 'visible', 'bookmark', 'unseen', 'deleted',
+ 'forum_mode', 'mention', 'global', 'shadow'];
+
+ $item_fields = array_merge($item_fields, $additional_item_fields);
+ }
+
$author_fields = ['url' => 'author-link', 'name' => 'author-name', 'thumb' => 'author-avatar'];
$owner_fields = ['url' => 'owner-link', 'name' => 'owner-name', 'thumb' => 'owner-avatar'];
$contact_fields = ['url' => 'contact-link', 'name' => 'contact-name', 'thumb' => 'contact-avatar',
- 'network', 'url', 'name', 'writable', 'self', 'id' => 'cid', 'alias'];
+ 'network', 'url', 'name', 'writable', 'self', 'id' => 'cid', 'alias',
+ 'photo', 'name-date', 'uri-date', 'avatar-date', 'thumb', 'dfrn-id'];
$event_fields = ['created' => 'event-created', 'edited' => 'event-edited',
'start' => 'event-start','finish' => 'event-finish',
if (!empty($selected)) {
$fields['parent-item'] = ['guid' => 'parent-guid'];
$fields['parent-item-author'] = ['url' => 'parent-author-link', 'name' => 'parent-author-name'];
+ $fields['sign'] = ['signed_text', 'signature', 'signer'];
}
return $fields;
$joins .= " LEFT JOIN `event` ON `event-id` = `event`.`id`";
}
+ if (strpos($sql_commands, "`sign`.") !== false) {
+ $joins .= " LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`";
+ }
+
if ((strpos($sql_commands, "`parent-item`.") !== false) || (strpos($sql_commands, "`parent-author`.") !== false)) {
$joins .= " STRAIGHT_JOIN `item` AS `parent-item` ON `parent-item`.`id` = `item`.`parent`";
}
$check_date = DateTimeFormat::utc($last_update);
$r = q(
- "SELECT `item`.*, `item`.`id` AS `item_id`,
- `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`,
- `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
- `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
- `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
+ "SELECT `item`.`id`
FROM `item` USE INDEX (`uid_wall_changed`) $sql_post_table
STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
- AND (NOT `contact`.`blocked` OR `contact`.`pending`)
- LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
- WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`moderated` AND `item`.`parent` != 0
- AND `item`.`wall` AND `item`.`changed` > '%s'
+ WHERE `item`.`uid` = %d AND `item`.`wall` AND `item`.`changed` > '%s'
$sql_extra
ORDER BY `item`.`parent` ".$sort.", `item`.`created` ASC LIMIT 0, 300",
intval($owner_id),
dbesc($sort)
);
+ $ids = [];
+ foreach ($r as $item) {
+ $ids[] = $item['id'];
+ }
+
+ $condition = ['id' => $ids];
+ $fields = ['author-id', 'uid', 'id', 'parent', 'uri', 'thr-parent',
+ 'parent-uri', 'created', 'edited', 'verb', 'object-type',
+ 'guid', 'private', 'title', 'body', 'location', 'coord', 'app',
+ 'attach', 'object', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
+ 'extid', 'target', 'tag', 'bookmark', 'deleted',
+ 'author-link', 'owner-link', 'signed_text', 'signature', 'signer'];
+ $ret = Item::select($owner_id, $fields, $condition);
+ $items = dba::inArray($ret);
+
/*
* Will check further below if this actually returned results.
* We will provide an empty feed if that is the case.
*/
- $items = $r;
-
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
public static function itemFeed($item_id, $conversation = false)
{
if ($conversation) {
- $condition = '`item`.`parent`';
+ $condition = ['parent' => $item_id];
} else {
- $condition = '`item`.`id`';
- }
-
- $r = q(
- "SELECT `item`.*, `item`.`id` AS `item_id`,
- `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`,
- `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
- `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
- `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
- FROM `item`
- STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
- AND (NOT `contact`.`blocked` OR `contact`.`pending`)
- LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
- WHERE %s = %d AND `item`.`visible` AND NOT `item`.`moderated` AND `item`.`parent` != 0
- AND NOT `item`.`private`",
- $condition,
- intval($item_id)
- );
-
- if (!DBM::is_result($r)) {
+ $condition = ['id' => $item_id];
+ }
+
+ $fields = ['author-id', 'uid', 'id', 'parent', 'uri', 'thr-parent',
+ 'parent-uri', 'created', 'edited', 'verb', 'object-type',
+ 'guid', 'private', 'title', 'body', 'location', 'coord', 'app',
+ 'attach', 'object', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
+ 'extid', 'target', 'tag', 'bookmark', 'deleted',
+ 'author-link', 'owner-link', 'signed_text', 'signature', 'signer'];
+ $ret = Item::select(0, $fields, $condition);
+ $items = dba::inArray($ret);
+ if (!DBM::is_result($items)) {
killme();
}
- $items = $r;
- $item = $r[0];
+ $item = $items[0];
if ($item['uid'] != 0) {
$owner = User::getOwnerDataById($item['uid']);
}
if ($Blink && link_compare($Blink, System::baseUrl() . "/profile/" . $importer["nickname"])) {
+ $author = dba::selectFirst('contact', ['name', 'thumb', 'url'], ['id' => $item['author-id']]);
+
// send a notification
notification(
[
"uid" => $importer["importer_uid"],
"item" => $item,
"link" => System::baseUrl()."/display/".urlencode(Item::getGuidById($posted_id)),
- "source_name" => stripslashes($item["author-name"]),
- "source_link" => $item["author-link"],
- "source_photo" => ((link_compare($item["author-link"], $importer["url"]))
- ? $importer["thumb"] : $item["author-avatar"]),
+ "source_name" => $author["name"],
+ "source_link" => $author["url"],
+ "source_photo" => $author["thumb"],
"verb" => $item["verb"],
"otype" => "person",
"activity" => $verb,
// only one like or dislike per person
// splitted into two queries for performance issues
$r = q(
- "SELECT `id` FROM `item` WHERE `uid` = %d AND `author-link` = '%s' AND `verb` = '%s' AND `parent-uri` = '%s' AND NOT `deleted` LIMIT 1",
+ "SELECT `id` FROM `item` WHERE `uid` = %d AND `author-id` = %d AND `verb` = '%s' AND `parent-uri` = '%s' AND NOT `deleted` LIMIT 1",
intval($item["uid"]),
- dbesc($item["author-link"]),
+ intval($item["author-id"]),
dbesc($item["verb"]),
dbesc($item["parent-uri"])
);
}
$r = q(
- "SELECT `id` FROM `item` WHERE `uid` = %d AND `author-link` = '%s' AND `verb` = '%s' AND `thr-parent` = '%s' AND NOT `deleted` LIMIT 1",
+ "SELECT `id` FROM `item` WHERE `uid` = %d AND `author-id` = %d AND `verb` = '%s' AND `thr-parent` = '%s' AND NOT `deleted` LIMIT 1",
intval($item["uid"]),
- dbesc($item["author-link"]),
+ intval($item["author-id"]),
dbesc($item["verb"]),
dbesc($item["parent-uri"])
);
// Fetch the owner
$owner = self::fetchauthor($xpath, $entry, $importer, "dfrn:owner", true);
- $item["owner-name"] = $owner["name"];
$item["owner-link"] = $owner["link"];
- $item["owner-avatar"] = $owner["avatar"];
+ $item["owner-id"] = Contact::getIdForURL($owner["link"], 0);
// fetch the author
$author = self::fetchauthor($xpath, $entry, $importer, "atom:author", true);
- $item["author-name"] = $author["name"];
$item["author-link"] = $author["link"];
- $item["author-avatar"] = $author["avatar"];
+ $item["author-id"] = Contact::getIdForURL($author["link"], 0);
$item["title"] = $xpath->query("atom:title/text()", $entry)->item(0)->nodeValue;
* but we're going to unconditionally correct it here so that the post will always be owned by our contact.
*/
logger('Correcting item owner.', LOGGER_DEBUG);
- $item["owner-name"] = $importer["senderName"];
- $item["owner-link"] = $importer["url"];
- $item["owner-avatar"] = $importer["thumb"];
+ $item["owner-link"] = $importer["url"];
+ $item["owner-id"] = Contact::getIdForURL($importer["url"], 0);
}
if (($importer["rel"] == CONTACT_IS_FOLLOWER) && (!self::tgroupCheck($importer["importer_uid"], $item))) {
if (Config::get("system", "relay_directly", false)) {
// We distribute our stuff based on the parent to ensure that the thread will be complete
- $parent = dba::selectFirst('item', ['parent'], ['id' => $item_id]);
+ $parent = Item::selectFirst(0, ['parent'], ['id' => $item_id]);
if (!DBM::is_result($parent)) {
return;
}
*/
private static function messageExists($uid, $guid)
{
- $r = q(
- "SELECT `id` FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
- intval($uid),
- dbesc($guid)
- );
-
- if (DBM::is_result($r)) {
+ $item = Item::selectFirst($uid, ['id'], ['uid' => $uid, 'guid' => $guid]);
+ if (DBM::is_result($item)) {
logger("message ".$guid." already exists for user ".$uid);
- return $r[0]["id"];
+ return $item["id"];
}
return false;
*/
private static function parentItem($uid, $guid, $author, $contact)
{
- $r = q(
- "SELECT `id`, `parent`, `body`, `wall`, `uri`, `guid`, `private`, `origin`,
- `author-name`, `author-link`, `author-avatar`,
- `owner-name`, `owner-link`, `owner-avatar`
- FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
- intval($uid),
- dbesc($guid)
- );
+ $fields = ['id', 'parent', 'body', 'wall', 'uri', 'guid', 'private', 'origin',
+ 'author-name', 'author-link', 'author-avatar',
+ 'owner-name', 'owner-link', 'owner-avatar'];
+ $condition = ['uid' => $uid, 'guid' => $guid];
+ $item = Item::selectFirst($uid, $fields, $condition);
- if (!$r) {
+ if (!DBM::is_result($item)) {
$result = self::storeByGuid($guid, $contact["url"], $uid);
if (!$result) {
if ($result) {
logger("Fetched missing item ".$guid." - result: ".$result, LOGGER_DEBUG);
- $r = q(
- "SELECT `id`, `body`, `wall`, `uri`, `private`, `origin`,
- `author-name`, `author-link`, `author-avatar`,
- `owner-name`, `owner-link`, `owner-avatar`
- FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
- intval($uid),
- dbesc($guid)
- );
+ $item = Item::selectFirst($uid, $fields, $condition);
}
}
- if (!$r) {
+ if (!DBM::is_result($item)) {
logger("parent item not found: parent: ".$guid." - user: ".$uid);
return false;
} else {
logger("parent item found: parent: ".$guid." - user: ".$uid);
- return $r[0];
+ return $item;
}
}
*/
private static function getUriFromGuid($author, $guid, $onlyfound = false)
{
- $item = dba::selectFirst('item', ['uri'], ['guid' => $guid]);
+ $item = Item::selectFirst(0, ['uri'], ['guid' => $guid]);
if (DBM::is_result($item)) {
return $item["uri"];
} elseif (!$onlyfound) {
*/
private static function getGuidFromUri($uri, $uid)
{
- $r = q("SELECT `guid` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($uri), intval($uid));
- if (DBM::is_result($r)) {
- return $r[0]["guid"];
+ $item = Item::selectFirst($uid, ['guid'], ['uri' => $uri, 'uid' => $uid]);
+ if (DBM::is_result($item)) {
+ return $item["guid"];
} else {
return false;
}
*/
private static function importerForGuid($guid)
{
- $item = dba::fetch_first("SELECT `uid` FROM `item` WHERE `origin` AND `guid` = ? LIMIT 1", $guid);
-
+ $item = Item::selectFirst(0, ['uid'], ['origin' => true, 'guid' => $guid]);
if (DBM::is_result($item)) {
logger("Found user ".$item['uid']." as owner of item ".$guid, LOGGER_DEBUG);
- $contact = dba::fetch_first("SELECT * FROM `contact` WHERE `self` AND `uid` = ?", $item['uid']);
+ $contact = dba::selectFirst('contact', [], ['self' => true, 'uid' => $item['uid']]);
if (DBM::is_result($contact)) {
return $contact;
}
$datarray["contact-id"] = $author_contact["cid"];
$datarray["network"] = $author_contact["network"];
- $datarray["author-name"] = $person["name"];
$datarray["author-link"] = $person["url"];
- $datarray["author-avatar"] = ((x($person, "thumb")) ? $person["thumb"] : $person["photo"]);
+ $datarray["author-id"] = Contact::getIdForURL($person["url"], 0);
- $datarray["owner-name"] = $contact["name"];
$datarray["owner-link"] = $contact["url"];
- $datarray["owner-avatar"] = ((x($contact, "thumb")) ? $contact["thumb"] : $contact["photo"]);
+ $datarray["owner-id"] = Contact::getIdForURL($contact["url"], 0);
$datarray["guid"] = $guid;
$datarray["uri"] = self::getUriFromGuid($author, $guid);
$datarray["contact-id"] = $author_contact["cid"];
$datarray["network"] = $author_contact["network"];
- $datarray["author-name"] = $person["name"];
$datarray["author-link"] = $person["url"];
- $datarray["author-avatar"] = ((x($person, "thumb")) ? $person["thumb"] : $person["photo"]);
+ $datarray["author-id"] = Contact::getIdForURL($person["url"], 0);
- $datarray["owner-name"] = $contact["name"];
$datarray["owner-link"] = $contact["url"];
- $datarray["owner-avatar"] = ((x($contact, "thumb")) ? $contact["thumb"] : $contact["photo"]);
+ $datarray["owner-id"] = Contact::getIdForURL($contact["url"], 0);
$datarray["guid"] = $guid;
$datarray["uri"] = self::getUriFromGuid($author, $guid);
// like on comments have the comment as parent. So we need to fetch the toplevel parent
if ($parent_item["id"] != $parent_item["parent"]) {
- $toplevel = dba::selectFirst('item', ['origin'], ['id' => $parent_item["parent"]]);
+ $toplevel = Item::selectFirst($importer["uid"], ['origin'], ['id' => $parent_item["parent"]]);
$origin = $toplevel["origin"];
} else {
$origin = $parent_item["origin"];
return false;
}
- $item = dba::selectFirst('item', ['id'], ['guid' => $parent_guid, 'origin' => true, 'private' => false]);
+ $item = Item::selectFirst(0, ['id'], ['guid' => $parent_guid, 'origin' => true, 'private' => false]);
if (!DBM::is_result($item)) {
logger('Item not found, no origin or private: '.$parent_guid);
return false;
}
// Send all existing comments and likes to the requesting server
- $comments = dba::p("SELECT `item`.`id`, `item`.`verb`, `contact`.`self`
- FROM `item`
- INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
- WHERE `item`.`parent` = ? AND `item`.`id` != `item`.`parent`", $item['id']);
+ $comments = Item::select(0, ['id', 'verb', 'self'], ['parent' => $item['id']]);
while ($comment = dba::fetch($comments)) {
+ if ($comment['id'] == $comment['parent']) {
+ continue;
+ }
if ($comment['verb'] == ACTIVITY_POST) {
$cmd = $comment['self'] ? 'comment-new' : 'comment-import';
} else {
$fields = ['body', 'tag', 'app', 'created', 'object-type', 'uri', 'guid',
'author-name', 'author-link', 'author-avatar'];
$condition = ['guid' => $guid, 'visible' => true, 'deleted' => false, 'private' => false];
- $item = dba::selectfirst('item', $fields, $condition);
+ $item = Item::selectFirst(0, $fields, $condition);
if (DBM::is_result($item)) {
logger("reshared message ".$guid." already exists on system.");
$fields = ['body', 'tag', 'app', 'created', 'object-type', 'uri', 'guid',
'author-name', 'author-link', 'author-avatar'];
$condition = ['guid' => $guid, 'visible' => true, 'deleted' => false, 'private' => false];
- $item = dba::selectfirst('item', $fields, $condition);
+ $item = Item::selectFirst(0, $fields, $condition);
if (DBM::is_result($item)) {
// If it is a reshared post from another network then reformat to avoid display problems with two share elements
$datarray["contact-id"] = $contact["id"];
$datarray["network"] = NETWORK_DIASPORA;
- $datarray["author-name"] = $contact["name"];
$datarray["author-link"] = $contact["url"];
- $datarray["author-avatar"] = ((x($contact, "thumb")) ? $contact["thumb"] : $contact["photo"]);
+ $datarray["author-id"] = Contact::getIdForURL($contact["url"], 0);
- $datarray["owner-name"] = $datarray["author-name"];
$datarray["owner-link"] = $datarray["author-link"];
- $datarray["owner-avatar"] = $datarray["author-avatar"];
+ $datarray["owner-id"] = $datarray["author-id"];
$datarray["guid"] = $guid;
$datarray["uri"] = $datarray["parent-uri"] = self::getUriFromGuid($author, $guid);
} else {
$condition = ["`guid` = ? AND `uid` = ? AND NOT `file` LIKE '%%[%%' AND NOT `deleted`", $target_guid, $importer['uid']];
}
- $r = dba::select('item', $fields, $condition);
+ $r = Item::select($importer['uid'], $fields, $condition);
if (!DBM::is_result($r)) {
logger("Target guid ".$target_guid." was not found on this system for user ".$importer['uid'].".");
return false;
while ($item = dba::fetch($r)) {
// Fetch the parent item
- $parent = dba::selectFirst('item', ['author-link'], ['id' => $item["parent"]]);
+ $parent = Item::selectFirst(0, ['author-link'], ['id' => $item["parent"]]);
// Only delete it if the parent author really fits
if (!link_compare($parent["author-link"], $contact["url"]) && !link_compare($item["author-link"], $contact["url"])) {
$datarray["contact-id"] = $contact["id"];
$datarray["network"] = NETWORK_DIASPORA;
- $datarray["author-name"] = $contact["name"];
$datarray["author-link"] = $contact["url"];
- $datarray["author-avatar"] = ((x($contact, "thumb")) ? $contact["thumb"] : $contact["photo"]);
+ $datarray["author-id"] = Contact::getIdForURL($contact["url"], 0);
- $datarray["owner-name"] = $datarray["author-name"];
$datarray["owner-link"] = $datarray["author-link"];
- $datarray["owner-avatar"] = $datarray["author-avatar"];
+ $datarray["owner-id"] = $datarray["author-id"];
$datarray["guid"] = $guid;
$datarray["uri"] = $datarray["parent-uri"] = self::getUriFromGuid($author, $guid);
if (($guid != "") && $complete) {
$condition = ['guid' => $guid, 'network' => [NETWORK_DFRN, NETWORK_DIASPORA]];
- $item = dba::selectFirst('item', ['contact-id'], $condition);
+ $item = Item::selectFirst(0, ['contact-id'], $condition);
if (DBM::is_result($item)) {
$ret= [];
$ret["root_handle"] = self::handleFromContact($item["contact-id"]);
*/
private static function constructLike($item, $owner)
{
- $p = q(
- "SELECT `guid`, `uri`, `parent-uri` FROM `item` WHERE `uri` = '%s' LIMIT 1",
- dbesc($item["thr-parent"])
- );
- if (!DBM::is_result($p)) {
+ $parent = Item::selectFirst(0, ['guid', 'uri', 'parent-uri'], ['uri' => $item["thr-parent"]]);
+ if (!DBM::is_result($parent)) {
return false;
}
- $parent = $p[0];
-
$target_type = ($parent["uri"] === $parent["parent-uri"] ? "Post" : "Comment");
$positive = null;
if ($item['verb'] === ACTIVITY_LIKE) {
*/
private static function constructAttend($item, $owner)
{
- $p = q(
- "SELECT `guid`, `uri`, `parent-uri` FROM `item` WHERE `uri` = '%s' LIMIT 1",
- dbesc($item["thr-parent"])
- );
- if (!DBM::is_result($p)) {
+ $parent = Item::selectFirst(0, ['guid', 'uri', 'parent-uri'], ['uri' => $item["thr-parent"]]);
+ if (!DBM::is_result($parent)) {
return false;
}
- $parent = $p[0];
-
switch ($item['verb']) {
case ACTIVITY_ATTEND:
$attend_answer = 'accepted';
return $result;
}
- $p = q(
- "SELECT `guid` FROM `item` WHERE `parent` = %d AND `id` = %d LIMIT 1",
- intval($item["parent"]),
- intval($item["parent"])
- );
-
- if (!DBM::is_result($p)) {
+ $parent = Item::selectFirst(0, ['guid'], ['id' => $item["parent"], 'parent' => $item["parent"]]);
+ if (!DBM::is_result($parent)) {
return false;
}
- $parent = $p[0];
-
$text = html_entity_decode(BBCode::toMarkdown($item["body"]));
$created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM);
$contact["uprvkey"] = $r[0]['prvkey'];
- $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1", intval($post_id));
- if (!DBM::is_result($r)) {
+ $item = Item::selectFirst(0, [], ['id' => $post_id]);
+ if (!DBM::is_result($item)) {
return false;
}
- if (!in_array($r[0]["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
+ if (!in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
return false;
}
- $message = self::constructLike($r[0], $contact);
+ $message = self::constructLike($item, $contact);
if ($message === false) {
return false;
}