From abef0cd46c601fa66c06068a4c41c8e556681f19 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 16 Jun 2018 15:35:30 +0000 Subject: [PATCH 1/7] Don't use the author-link anymore - just author-id --- include/enotify.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/include/enotify.php b/include/enotify.php index b1cf044b5..3e01736f1 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -724,23 +724,24 @@ function check_item_notification($itemid, $uid, $defaulttype = "") { $profiles = $profiles2; - $profile_list = ""; + $ret = dba::select('contact', ['id'], ['uid' => 0, 'nurl' => $profiles]); - foreach ($profiles AS $profile) { - if ($profile_list != "") - $profile_list .= "', '"; + $contacts = []; - $profile_list .= dbesc($profile); + while ($contact = dba::fetch($ret)) { + $contacts[] = $contact['id']; } - $profile_list = "'".$profile_list."'"; + $contact_list = implode(',', $contacts); + + dba::close($ret); // Only act if it is a "real" post // We need the additional check for the "local_profile" because of mixed situations on connector networks $item = q("SELECT `id`, `mention`, `tag`,`parent`, `title`, `body`, `author-id`, `guid`, `parent-uri`, `uri`, `contact-id`, `network` FROM `item` WHERE `id` = %d AND `verb` IN ('%s', '') AND `type` != 'activity' AND - NOT (`author-link` IN ($profile_list)) LIMIT 1", + NOT (`author-id` IN ($contact_list)) LIMIT 1", intval($itemid), dbesc(ACTIVITY_POST)); if (!$item) return false; @@ -807,7 +808,7 @@ function check_item_notification($itemid, $uid, $defaulttype = "") { // Is it a post that the user had started or where he interacted? $parent = q("SELECT `thread`.`iid` FROM `thread` INNER JOIN `item` ON `item`.`parent` = `thread`.`iid` WHERE `thread`.`iid` = %d AND NOT `thread`.`ignored` AND - (`thread`.`mention` OR `item`.`author-link` IN ($profile_list)) + (`thread`.`mention` OR `item`.`author-id` IN ($contact_list)) LIMIT 1", intval($item[0]["parent"])); From 1aec8f620d4ab5073f4f194da2f934faa3a0eb40 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 16 Jun 2018 22:32:57 +0000 Subject: [PATCH 2/7] More item abstractions for DFRN and Diaspora --- src/Model/Item.php | 23 +++++-- src/Protocol/DFRN.php | 89 ++++++++++++------------- src/Protocol/Diaspora.php | 137 +++++++++++++------------------------- 3 files changed, 108 insertions(+), 141 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index a30ed3739..e2b771d27 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -162,9 +162,6 @@ class Item extends BaseObject { /* 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', @@ -172,13 +169,24 @@ class Item extends BaseObject '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', @@ -193,6 +201,7 @@ class Item extends BaseObject 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; @@ -257,6 +266,10 @@ class Item extends BaseObject $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`"; } diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 6b9ff3de5..7f5e0aa4d 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -228,17 +228,10 @@ class DFRN $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), @@ -246,13 +239,26 @@ class DFRN 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; @@ -321,33 +327,24 @@ class DFRN public static function itemFeed($item_id, $conversation = false) { if ($conversation) { - $condition = '`item`.`parent`'; + $condition = ['parent' => $item_id]; } else { - $condition = '`item`.`id`'; + $condition = ['id' => $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)) { + $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']); @@ -2253,6 +2250,8 @@ class DFRN } 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( [ @@ -2264,10 +2263,9 @@ class DFRN "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, @@ -2334,9 +2332,9 @@ class DFRN // 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"]) ); @@ -2345,9 +2343,9 @@ class DFRN } $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"]) ); @@ -2469,16 +2467,14 @@ class DFRN // 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; @@ -2736,9 +2732,8 @@ class DFRN * 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))) { diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 20d1d74fe..cb41ba19e 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -69,7 +69,7 @@ class Diaspora 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; } @@ -1170,15 +1170,10 @@ class Diaspora */ 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; @@ -1385,16 +1380,13 @@ class Diaspora */ 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) { @@ -1405,23 +1397,16 @@ class Diaspora 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; } } @@ -1602,7 +1587,7 @@ class Diaspora */ 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) { @@ -1632,9 +1617,9 @@ class Diaspora */ 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; } @@ -1649,11 +1634,10 @@ class Diaspora */ 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; } @@ -1721,13 +1705,11 @@ class Diaspora $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); @@ -2056,13 +2038,11 @@ class Diaspora $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); @@ -2079,7 +2059,7 @@ class Diaspora // 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"]; @@ -2216,7 +2196,7 @@ class Diaspora 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; @@ -2237,11 +2217,11 @@ class Diaspora } // 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 { @@ -2599,7 +2579,7 @@ class Diaspora $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."); @@ -2643,7 +2623,7 @@ class Diaspora $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 @@ -2701,13 +2681,11 @@ class Diaspora $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); @@ -2787,7 +2765,7 @@ class Diaspora } 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; @@ -2795,7 +2773,7 @@ class Diaspora 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"])) { @@ -2921,13 +2899,11 @@ class Diaspora $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); @@ -3433,7 +3409,7 @@ class Diaspora 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"]); @@ -3700,16 +3676,11 @@ class Diaspora */ 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) { @@ -3736,16 +3707,11 @@ class Diaspora */ 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'; @@ -3785,18 +3751,11 @@ class Diaspora 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); @@ -4265,16 +4224,16 @@ class Diaspora $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; } From 0280a46ab44c4cc1f1b294345a048f412a448989 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 17 Jun 2018 06:27:52 +0000 Subject: [PATCH 3/7] Use predefined field lists --- include/api.php | 28 +++++++++++------------ include/conversation.php | 2 +- mod/display.php | 2 +- mod/network.php | 2 +- mod/notes.php | 2 +- mod/profile.php | 2 +- mod/search.php | 4 ++-- src/Model/Contact.php | 4 ++-- src/Model/Item.php | 48 ++++++++++++++++++++++++++-------------- 9 files changed, 54 insertions(+), 40 deletions(-) diff --git a/include/api.php b/include/api.php index af71e2f1c..9848862d5 100644 --- a/include/api.php +++ b/include/api.php @@ -1626,7 +1626,7 @@ function api_search($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Item::select(api_user(), [], $condition, $params); + $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); $data['status'] = api_format_items(dba::inArray($statuses), $user_info); @@ -1693,7 +1693,7 @@ function api_statuses_home_timeline($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Item::select(api_user(), [], $condition, $params); + $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); $items = dba::inArray($statuses); @@ -1770,7 +1770,7 @@ function api_statuses_public_timeline($type) } $params = ['order' => ['iid' => true], 'limit' => [$start, $count]]; - $statuses = Item::selectThread(api_user(), [], $condition, $params); + $statuses = Item::selectThread(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); $r = dba::inArray($statuses); } else { @@ -1787,7 +1787,7 @@ function api_statuses_public_timeline($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Item::select(api_user(), [], $condition, $params); + $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); $r = dba::inArray($statuses); } @@ -1846,7 +1846,7 @@ function api_statuses_networkpublic_timeline($type) } $params = ['order' => ['iid' => true], 'limit' => [$start, $count]]; - $statuses = Item::selectThread(api_user(), [], $condition, $params); + $statuses = Item::selectThread(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); $ret = api_format_items(dba::inArray($statuses), $user_info, false, $type); @@ -1917,7 +1917,7 @@ function api_statuses_show($type) $params = []; } - $statuses = Item::select(api_user(), [], $condition, $params); + $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); /// @TODO How about copying this to above methods which don't check $r ? if (!DBM::is_result($statuses)) { @@ -1998,7 +1998,7 @@ function api_conversation_show($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Item::select(api_user(), [], $condition, $params); + $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); if (!DBM::is_result($statuses)) { throw new BadRequestException("There is no status with id $id."); @@ -2168,7 +2168,7 @@ function api_statuses_mentions($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Item::select(api_user(), [], $condition, $params); + $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); $ret = api_format_items(dba::inArray($statuses), $user_info, false, $type); @@ -2248,7 +2248,7 @@ function api_statuses_user_timeline($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Item::select(api_user(), [], $condition, $params); + $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); $ret = api_format_items(dba::inArray($statuses), $user_info, true, $type); @@ -2300,7 +2300,7 @@ function api_favorites_create_destroy($type) $itemid = intval($_REQUEST['id']); } - $item = Item::selectFirst(api_user(), [], ['id' => $itemid, 'uid' => api_user()]); + $item = Item::selectFirst(api_user(), Item::DISPLAY_FIELDLIST, ['id' => $itemid, 'uid' => api_user()]); if (!DBM::is_result($item)) { throw new BadRequestException("Invalid item."); @@ -2390,7 +2390,7 @@ function api_favorites($type) $condition[] = $max_id; } - $statuses = Item::select(api_user(), [], $condition, $params); + $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); $ret = api_format_items(dba::inArray($statuses), $user_info, false, $type); } @@ -3198,7 +3198,7 @@ function api_lists_statuses($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Item::select(api_user(), [], $condition, $params); + $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); $items = api_format_items(dba::inArray($statuses), $user_info, false, $type); @@ -4723,7 +4723,7 @@ function prepare_photo_data($type, $scale, $photo_id) $condition = ["`parent` = ? AND `uid` = ? AND (`verb` = ? OR `type`='photo')", $item[0]['parent'], api_user(), ACTIVITY_POST]; - $statuses = Item::select(api_user(), [], $condition); + $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition); // prepare output of comments $commentData = api_format_items(dba::inArray($statuses), $user_info, false, $type); @@ -5716,7 +5716,7 @@ function api_friendica_notification_seen($type) $nm->setSeen($note); if ($note['otype']=='item') { // would be really better with an ItemsManager and $im->getByID() :-P - $item = Item::selectFirst(api_user(), [], ['id' => $note['iid'], 'uid' => api_user()]); + $item = Item::selectFirst(api_user(), Item::DISPLAY_FIELDLIST, ['id' => $note['iid'], 'uid' => api_user()]); if (DBM::is_result($$item)) { // we found the item, return it to the user $ret = api_format_items([$item], $user_info, false, $type); diff --git a/include/conversation.php b/include/conversation.php index fd4814a36..9730c4af9 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -788,7 +788,7 @@ function conversation_add_children($parents, $block_authors, $order, $uid) { if ($block_authors) { $condition[0] .= "AND NOT `author`.`hidden`"; } - $thread_items = Item::select(local_user(), [], $condition, $params); + $thread_items = Item::select(local_user(), Item::DISPLAY_FIELDLIST, $condition, $params); $comments = dba::inArray($thread_items); diff --git a/mod/display.php b/mod/display.php index 816af820a..ad71f3b1c 100644 --- a/mod/display.php +++ b/mod/display.php @@ -349,7 +349,7 @@ function display_content(App $a, $update = false, $update_uid = 0) { $condition = ["`item`.`parent-uri` = (SELECT `parent-uri` FROM `item` WHERE `id` = ?) AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, local_user()]; $params = ['order' => ['uid', 'parent' => true, 'gravity', 'id']]; - $r = Item::select(local_user(), [], $condition, $params); + $r = Item::select(local_user(), Item::DISPLAY_FIELDLIST, $condition, $params); if (!DBM::is_result($r)) { notice(L10n::t('Item not found.') . EOL); diff --git a/mod/network.php b/mod/network.php index 995b1da55..6921f54d8 100644 --- a/mod/network.php +++ b/mod/network.php @@ -455,7 +455,7 @@ function networkFlatView(App $a, $update = 0) } $params = ['order' => ['id' => true], 'limit' => [$a->pager['start'], $a->pager['itemspage']]]; - $result = Item::select(local_user(), [], $condition, $params); + $result = Item::select(local_user(), Item::DISPLAY_FIELDLIST, $condition, $params); $items = dba::inArray($result); $condition = ['unseen' => true, 'uid' => local_user()]; diff --git a/mod/notes.php b/mod/notes.php index fb42408c6..0001a035f 100644 --- a/mod/notes.php +++ b/mod/notes.php @@ -79,7 +79,7 @@ function notes_content(App $a, $update = false) dba::close($r); $condition = ['uid' => local_user(), 'parent' => $parents_arr]; - $result = Item::select(local_user(), [], $condition); + $result = Item::select(local_user(), Item::DISPLAY_FIELDLIST, $condition); if (DBM::is_result($result)) { $items = conv_sort(dba::inArray($result), 'commented'); $o .= conversation($a, $items, 'notes', $update); diff --git a/mod/profile.php b/mod/profile.php index 91d04b2de..0efc9d5be 100644 --- a/mod/profile.php +++ b/mod/profile.php @@ -338,7 +338,7 @@ function profile_content(App $a, $update = 0) } $condition = ['uid' => $a->profile['profile_uid'], 'parent' => $parents_arr]; - $result = Item::select($a->profile['profile_uid'], [], $condition); + $result = Item::select($a->profile['profile_uid'], Item::DISPLAY_FIELDLIST, $condition); $items = conv_sort(dba::inArray($result), 'created'); } else { $items = []; diff --git a/mod/search.php b/mod/search.php index c42bcacfa..3c421984a 100644 --- a/mod/search.php +++ b/mod/search.php @@ -211,7 +211,7 @@ function search_content(App $a) { } dba::close($terms); - $items = Item::select(local_user(), [], ['id' => array_reverse($itemids)]); + $items = Item::select(local_user(), Item::DISPLAY_FIELDLIST, ['id' => array_reverse($itemids)]); $r = dba::inArray($items); } else { logger("Start fulltext search for '".$search."'", LOGGER_DEBUG); @@ -221,7 +221,7 @@ function search_content(App $a) { local_user(), $search]; $params = ['order' => ['id' => true], 'limit' => [$a->pager['start'], $a->pager['itemspage']]]; - $items = Item::select(local_user(), [], $condition, $params); + $items = Item::select(local_user(), Item::DISPLAY_FIELDLIST, $condition, $params); $r = dba::inArray($items); } diff --git a/src/Model/Contact.php b/src/Model/Contact.php index f354f89c7..4e46b49fd 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1068,10 +1068,10 @@ class Contact extends BaseObject $author_id, ACTIVITY_POST, local_user()]; $params = ['order' => ['created' => true], 'limit' => [$a->pager['start'], $a->pager['itemspage']]]; - $r = Item::select(local_user(), [], $condition, $params); + $r = Item::select(local_user(), Item::DISPLAY_FIELDLIST, $condition, $params); $items = dba::inArray($r); - + $o = conversation($a, $items, 'contact-posts', false); $o .= alt_pager($a, count($items)); diff --git a/src/Model/Item.php b/src/Model/Item.php index e2b771d27..647c37d89 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -33,6 +33,29 @@ require_once 'include/text.php'; class Item extends BaseObject { + // Field list that is used to display the items + const DISPLAY_FIELDLIST = ['uid', 'id', 'parent', 'uri', 'thr-parent', 'parent-uri', 'guid', + 'commented', 'created', 'edited', 'received', 'verb', 'object-type', 'postopts', 'plink', + 'wall', 'private', 'starred', 'origin', 'title', 'body', 'file', 'attach', + 'content-warning', 'location', 'coord', 'app', 'rendered-hash', 'rendered-html', 'object', + 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'item_id', 'item_network', + 'author-id', 'author-link', 'author-name', 'author-avatar', + 'owner-id', 'owner-link', 'owner-name', 'owner-avatar', + 'contact-id', 'contact-link', 'contact-name', 'contact-avatar', + 'network', 'url', 'name', 'writable', 'self', 'cid', 'alias', + 'event-id', 'event-created', 'event-edited', 'event-start', 'event-finish', + 'event-summary', 'event-desc', 'event-location', 'event-type', + 'event-nofinish', 'event-adjust', 'event-ignore', 'event-id']; + + // Field list that is used to deliver items via the protocols + const DELIVER_FIELDLIST = ['uid', 'id', 'parent', 'uri', 'thr-parent', 'parent-uri', 'guid', + 'created', 'edited', 'verb', 'object-type', 'object', 'target', + 'private', 'title', 'body', 'location', 'coord', 'app', + 'attach', 'tag', 'bookmark', 'deleted', 'extid', + 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', + 'author-id', 'author-link', 'owner-link', 'contact-uid', + 'signed_text', 'signature', 'signer']; + /** * Retrieve a single record from the item table and returns it in an associative array * @@ -170,22 +193,15 @@ class Item extends BaseObject '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', - '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); - } + 'id' => 'item_id', 'network' => 'item_network', + 'type', 'extid', 'changed', 'moderated', 'target-type', 'target', + 'resource-id', 'tag', 'inform', 'pubmail', 'visible', 'bookmark', 'unseen', 'deleted', + 'forum_mode', 'mention', 'global', 'shadow']; $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', 'uid' => 'contact-uid', 'photo', 'name-date', 'uri-date', 'avatar-date', 'thumb', 'dfrn-id']; $event_fields = ['created' => 'event-created', 'edited' => 'event-edited', @@ -198,11 +214,9 @@ class Item extends BaseObject $fields = ['item' => $item_fields, 'author' => $author_fields, 'owner' => $owner_fields, 'contact' => $contact_fields, 'event' => $event_fields]; - 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']; - } + $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; } From 6e10de92847dd7ef1bf20f8eac05316fbe31e1e7 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 17 Jun 2018 17:05:17 +0000 Subject: [PATCH 4/7] New function to fetch item data especially for users --- include/api.php | 30 ++++++------ include/conversation.php | 2 +- include/enotify.php | 2 +- mod/acl.php | 2 +- mod/display.php | 2 +- mod/network.php | 2 +- mod/notes.php | 4 +- mod/ping.php | 2 +- mod/poke.php | 2 +- mod/profile.php | 2 +- mod/search.php | 4 +- mod/share.php | 2 +- mod/subthread.php | 2 +- mod/tagger.php | 2 +- src/Core/NotificationsManager.php | 6 +-- src/Model/Contact.php | 2 +- src/Model/Item.php | 80 +++++++++++++++++++++++++------ src/Protocol/DFRN.php | 24 ++++++---- src/Protocol/Diaspora.php | 38 +++++++-------- 19 files changed, 133 insertions(+), 77 deletions(-) diff --git a/include/api.php b/include/api.php index 9848862d5..d66446cb6 100644 --- a/include/api.php +++ b/include/api.php @@ -1626,7 +1626,7 @@ function api_search($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $statuses = Item::selectForUser(api_user(), [], $condition, $params); $data['status'] = api_format_items(dba::inArray($statuses), $user_info); @@ -1693,7 +1693,7 @@ function api_statuses_home_timeline($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $statuses = Item::selectForUser(api_user(), [], $condition, $params); $items = dba::inArray($statuses); @@ -1770,7 +1770,7 @@ function api_statuses_public_timeline($type) } $params = ['order' => ['iid' => true], 'limit' => [$start, $count]]; - $statuses = Item::selectThread(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $statuses = Item::selectThreadForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); $r = dba::inArray($statuses); } else { @@ -1787,7 +1787,7 @@ function api_statuses_public_timeline($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $statuses = Item::selectForUser(api_user(), [], $condition, $params); $r = dba::inArray($statuses); } @@ -1846,7 +1846,7 @@ function api_statuses_networkpublic_timeline($type) } $params = ['order' => ['iid' => true], 'limit' => [$start, $count]]; - $statuses = Item::selectThread(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $statuses = Item::selectThreadForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); $ret = api_format_items(dba::inArray($statuses), $user_info, false, $type); @@ -1917,7 +1917,7 @@ function api_statuses_show($type) $params = []; } - $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $statuses = Item::selectForUser(api_user(), [], $condition, $params); /// @TODO How about copying this to above methods which don't check $r ? if (!DBM::is_result($statuses)) { @@ -1998,7 +1998,7 @@ function api_conversation_show($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $statuses = Item::selectForUser(api_user(), [], $condition, $params); if (!DBM::is_result($statuses)) { throw new BadRequestException("There is no status with id $id."); @@ -2048,7 +2048,7 @@ function api_statuses_repeat($type) logger('API: api_statuses_repeat: '.$id); $fields = ['body', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink']; - $item = Item::selectFirst(api_user(), $fields, ['id' => $id, 'private' => false]); + $item = Item::selectFirst($fields, ['id' => $id, 'private' => false]); if (DBM::is_result($item) && $item['body'] != "") { if (strpos($item['body'], "[/share]") !== false) { @@ -2168,7 +2168,7 @@ function api_statuses_mentions($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $statuses = Item::selectForUser(api_user(), [], $condition, $params); $ret = api_format_items(dba::inArray($statuses), $user_info, false, $type); @@ -2248,7 +2248,7 @@ function api_statuses_user_timeline($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $statuses = Item::selectForUser(api_user(), [], $condition, $params); $ret = api_format_items(dba::inArray($statuses), $user_info, true, $type); @@ -2300,7 +2300,7 @@ function api_favorites_create_destroy($type) $itemid = intval($_REQUEST['id']); } - $item = Item::selectFirst(api_user(), Item::DISPLAY_FIELDLIST, ['id' => $itemid, 'uid' => api_user()]); + $item = Item::selectFirstForUser(api_user(), [], ['id' => $itemid, 'uid' => api_user()]); if (!DBM::is_result($item)) { throw new BadRequestException("Invalid item."); @@ -2390,7 +2390,7 @@ function api_favorites($type) $condition[] = $max_id; } - $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $statuses = Item::selectForUser(api_user(), [], $condition, $params); $ret = api_format_items(dba::inArray($statuses), $user_info, false, $type); } @@ -3198,7 +3198,7 @@ function api_lists_statuses($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $statuses = Item::selectForUser(api_user(), [], $condition, $params); $items = api_format_items(dba::inArray($statuses), $user_info, false, $type); @@ -4723,7 +4723,7 @@ function prepare_photo_data($type, $scale, $photo_id) $condition = ["`parent` = ? AND `uid` = ? AND (`verb` = ? OR `type`='photo')", $item[0]['parent'], api_user(), ACTIVITY_POST]; - $statuses = Item::select(api_user(), Item::DISPLAY_FIELDLIST, $condition); + $statuses = Item::selectForUser(api_user(), [], $condition); // prepare output of comments $commentData = api_format_items(dba::inArray($statuses), $user_info, false, $type); @@ -5716,7 +5716,7 @@ function api_friendica_notification_seen($type) $nm->setSeen($note); if ($note['otype']=='item') { // would be really better with an ItemsManager and $im->getByID() :-P - $item = Item::selectFirst(api_user(), Item::DISPLAY_FIELDLIST, ['id' => $note['iid'], 'uid' => api_user()]); + $item = Item::selectFirstForUser(api_user(), [], ['id' => $note['iid'], 'uid' => api_user()]); if (DBM::is_result($$item)) { // we found the item, return it to the user $ret = api_format_items([$item], $user_info, false, $type); diff --git a/include/conversation.php b/include/conversation.php index 9730c4af9..97be7e9aa 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -788,7 +788,7 @@ function conversation_add_children($parents, $block_authors, $order, $uid) { if ($block_authors) { $condition[0] .= "AND NOT `author`.`hidden`"; } - $thread_items = Item::select(local_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $thread_items = Item::selectForUser(local_user(), [], $condition, $params); $comments = dba::inArray($thread_items); diff --git a/include/enotify.php b/include/enotify.php index 3e01736f1..00459ac5c 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -130,7 +130,7 @@ function notification($params) $item = null; if ($params['otype'] === 'item' && $parent_id) { - $item = Item::selectFirst($params['uid'], [], ['id' => $parent_id]); + $item = Item::selectFirstForUser($params['uid'], [], ['id' => $parent_id]); } $item_post_type = item_post_type($item); diff --git a/mod/acl.php b/mod/acl.php index 7f1715689..ede694032 100644 --- a/mod/acl.php +++ b/mod/acl.php @@ -258,7 +258,7 @@ function acl_content(App $a) $condition = ["`parent` = ?", $conv_id]; $params = ['order' => ['author-name' => true]]; - $authors = Item::select(local_user(), ['author-link'], $condition, $params); + $authors = Item::selectForUser(local_user(), ['author-link'], $condition, $params); $item_authors = []; while ($author = dba::fetch($authors)) { $item_authors[$author['author-link']] = $author['author-link']; diff --git a/mod/display.php b/mod/display.php index ad71f3b1c..7198d4bb5 100644 --- a/mod/display.php +++ b/mod/display.php @@ -349,7 +349,7 @@ function display_content(App $a, $update = false, $update_uid = 0) { $condition = ["`item`.`parent-uri` = (SELECT `parent-uri` FROM `item` WHERE `id` = ?) AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, local_user()]; $params = ['order' => ['uid', 'parent' => true, 'gravity', 'id']]; - $r = Item::select(local_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $r = Item::selectForUser(local_user(), [], $condition, $params); if (!DBM::is_result($r)) { notice(L10n::t('Item not found.') . EOL); diff --git a/mod/network.php b/mod/network.php index 6921f54d8..61d957e1a 100644 --- a/mod/network.php +++ b/mod/network.php @@ -455,7 +455,7 @@ function networkFlatView(App $a, $update = 0) } $params = ['order' => ['id' => true], 'limit' => [$a->pager['start'], $a->pager['itemspage']]]; - $result = Item::select(local_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $result = Item::selectForUser(local_user(), [], $condition, $params); $items = dba::inArray($result); $condition = ['unseen' => true, 'uid' => local_user()]; diff --git a/mod/notes.php b/mod/notes.php index 0001a035f..69b544472 100644 --- a/mod/notes.php +++ b/mod/notes.php @@ -68,7 +68,7 @@ function notes_content(App $a, $update = false) $params = ['order' => ['created' => true], 'limit' => [$a->pager['start'], $a->pager['itemspage']]]; - $r = Item::select(local_user(), ['item_id'], $condition, $params); + $r = Item::selectForUser(local_user(), ['item_id'], $condition, $params); if (DBM::is_result($r)) { $parents_arr = []; @@ -79,7 +79,7 @@ function notes_content(App $a, $update = false) dba::close($r); $condition = ['uid' => local_user(), 'parent' => $parents_arr]; - $result = Item::select(local_user(), Item::DISPLAY_FIELDLIST, $condition); + $result = Item::selectForUser(local_user(), [], $condition); if (DBM::is_result($result)) { $items = conv_sort(dba::inArray($result), 'commented'); $o .= conversation($a, $items, 'notes', $update); diff --git a/mod/ping.php b/mod/ping.php index 0256500df..06d83a1d9 100644 --- a/mod/ping.php +++ b/mod/ping.php @@ -132,7 +132,7 @@ function ping_init(App $a) $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar', 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid']; $params = ['order' => ['created' => true]]; - $items = Item::select(local_user(), $fields, $condition, $params); + $items = Item::selectForUser(local_user(), $fields, $condition, $params); if (DBM::is_result($items)) { $items_unseen = dba::inArray($items); diff --git a/mod/poke.php b/mod/poke.php index 470f1eb2d..55bb799d1 100644 --- a/mod/poke.php +++ b/mod/poke.php @@ -71,7 +71,7 @@ function poke_init(App $a) { if ($parent) { $fields = ['uri', 'private', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid']; $condition = ['id' => $parent, 'parent' => $parent, 'uid' => $uid]; - $item = Item::selectFirst(local_user(), $fields, $condition); + $item = Item::selectFirst($fields, $condition); if (DBM::is_result($item)) { $parent_uri = $item['uri']; diff --git a/mod/profile.php b/mod/profile.php index 0efc9d5be..49d6fe261 100644 --- a/mod/profile.php +++ b/mod/profile.php @@ -338,7 +338,7 @@ function profile_content(App $a, $update = 0) } $condition = ['uid' => $a->profile['profile_uid'], 'parent' => $parents_arr]; - $result = Item::select($a->profile['profile_uid'], Item::DISPLAY_FIELDLIST, $condition); + $result = Item::selectForUser($a->profile['profile_uid'], [], $condition); $items = conv_sort(dba::inArray($result), 'created'); } else { $items = []; diff --git a/mod/search.php b/mod/search.php index 3c421984a..6580246e4 100644 --- a/mod/search.php +++ b/mod/search.php @@ -211,7 +211,7 @@ function search_content(App $a) { } dba::close($terms); - $items = Item::select(local_user(), Item::DISPLAY_FIELDLIST, ['id' => array_reverse($itemids)]); + $items = Item::selectForUser(local_user(), [], ['id' => array_reverse($itemids)]); $r = dba::inArray($items); } else { logger("Start fulltext search for '".$search."'", LOGGER_DEBUG); @@ -221,7 +221,7 @@ function search_content(App $a) { local_user(), $search]; $params = ['order' => ['id' => true], 'limit' => [$a->pager['start'], $a->pager['itemspage']]]; - $items = Item::select(local_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $items = Item::selectForUser(local_user(), [], $condition, $params); $r = dba::inArray($items); } diff --git a/mod/share.php b/mod/share.php index 8a4028885..fb4495019 100644 --- a/mod/share.php +++ b/mod/share.php @@ -13,7 +13,7 @@ function share_init(App $a) { $fields = ['private', 'body', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'title']; - $item = Item::selectFirst(local_user(), $fields, ['id' => $post_id]); + $item = Item::selectFirst($fields, ['id' => $post_id]); if (!DBM::is_result($item) || $item['private'] == 1) { killme(); diff --git a/mod/subthread.php b/mod/subthread.php index e2c11d0a3..0b6f4ecaa 100644 --- a/mod/subthread.php +++ b/mod/subthread.php @@ -23,7 +23,7 @@ function subthread_content(App $a) { $item_id = (($a->argc > 1) ? notags(trim($a->argv[1])) : 0); $condition = ["`parent` = ? OR `parent-uri` = ? AND `parent` = `id`", $item_id, $item_id]; - $item = Item::selectFirst(local_user(), [], $condition); + $item = Item::selectFirst([], $condition); if (empty($item_id) || !DBM::is_result($item)) { logger('subthread: no item ' . $item_id); diff --git a/mod/tagger.php b/mod/tagger.php index e72061b59..400c7787f 100644 --- a/mod/tagger.php +++ b/mod/tagger.php @@ -32,7 +32,7 @@ function tagger_content(App $a) { logger('tagger: tag ' . $term . ' item ' . $item_id); - $item = Item::selectFirst(local_user(), [], ['id' => $item_id]); + $item = Item::selectFirst([], ['id' => $item_id]); if (!$item_id || !DBM::is_result($item)) { logger('tagger: no item ' . $item_id); diff --git a/src/Core/NotificationsManager.php b/src/Core/NotificationsManager.php index 609eafc43..8020a281b 100644 --- a/src/Core/NotificationsManager.php +++ b/src/Core/NotificationsManager.php @@ -407,7 +407,7 @@ class NotificationsManager extends BaseObject $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar', 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid']; $params = ['order' => ['created' => true], 'limit' => [$start, $limit]]; - $items = Item::select(local_user(), $fields, $condition, $params); + $items = Item::selectForUser(local_user(), $fields, $condition, $params); if (DBM::is_result($items)) { $notifs = $this->formatNotifs(dba::inArray($items), $ident); @@ -492,7 +492,7 @@ class NotificationsManager extends BaseObject $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar', 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid']; $params = ['order' => ['created' => true], 'limit' => [$start, $limit]]; - $items = Item::select(local_user(), $fields, $condition, $params); + $items = Item::selectForUser(local_user(), $fields, $condition, $params); if (DBM::is_result($items)) { $notifs = $this->formatNotifs(dba::inArray($items), $ident); @@ -532,7 +532,7 @@ class NotificationsManager extends BaseObject $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar', 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid']; $params = ['order' => ['created' => true], 'limit' => [$start, $limit]]; - $items = Item::select(local_user(), $fields, $condition, $params); + $items = Item::selectForUser(local_user(), $fields, $condition, $params); if (DBM::is_result($items)) { $notifs = $this->formatNotifs(dba::inArray($items), $ident); diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 4e46b49fd..6a24b7da9 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1068,7 +1068,7 @@ class Contact extends BaseObject $author_id, ACTIVITY_POST, local_user()]; $params = ['order' => ['created' => true], 'limit' => [$a->pager['start'], $a->pager['itemspage']]]; - $r = Item::select(local_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $r = Item::selectForUser(local_user(), [], $condition, $params); $items = dba::inArray($r); diff --git a/src/Model/Item.php b/src/Model/Item.php index 647c37d89..4018db9ff 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -57,7 +57,7 @@ class Item extends BaseObject 'signed_text', 'signature', 'signer']; /** - * Retrieve a single record from the item table and returns it in an associative array + * Retrieve a single record from the item table for a given user and returns it in an associative array * * @brief Retrieve a single record from a table * @param integer $uid User ID @@ -67,10 +67,53 @@ class Item extends BaseObject * @return bool|array * @see dba::select */ - public static function selectFirst($uid, array $fields = [], array $condition = [], $params = []) + public static function selectFirstForUser($uid, array $selected = [], array $condition = [], $params = []) + { + $params['uid'] = $uid; + + if (empty($selected)) { + $selected = Item::DISPLAY_FIELDLIST; + } + + return self::selectFirst($selected, $condition, $params); + } + + /** + * @brief Select rows from the item table for a given user + * + * @param integer $uid User ID + * @param array $selected Array of selected fields, empty for all + * @param array $condition Array of fields for condition + * @param array $params Array of several parameters + * + * @return boolean|object + */ + public static function selectForUser($uid, array $selected = [], array $condition = [], $params = []) + { + $params['uid'] = $uid; + + if (empty($selected)) { + $selected = Item::DISPLAY_FIELDLIST; + } + + return self::select($selected, $condition, $params); + } + + /** + * Retrieve a single record from the item table and returns it in an associative array + * + * @brief Retrieve a single record from a table + * @param array $fields + * @param array $condition + * @param array $params + * @return bool|array + * @see dba::select + */ + public static function selectFirst(array $fields = [], array $condition = [], $params = []) { $params['limit'] = 1; - $result = self::select($uid, $fields, $condition, $params); + + $result = self::select($fields, $condition, $params); if (is_bool($result)) { return $result; @@ -84,15 +127,22 @@ class Item extends BaseObject /** * @brief Select rows from the item table * - * @param integer $uid User ID - * @param array $fields Array of selected fields, empty for all + * @param array $selected Array of selected fields, empty for all * @param array $condition Array of fields for condition * @param array $params Array of several parameters * * @return boolean|object */ - public static function select($uid, array $selected = [], array $condition = [], $params = []) + public static function select(array $selected = [], array $condition = [], $params = []) { + $uid = 0; + $usermode = false; + + if (isset($params['uid'])) { + $uid = $params['uid']; + $usermode = true; + } + $fields = self::fieldlist($selected); $select_fields = self::constructSelectFields($fields, $selected); @@ -101,7 +151,9 @@ class Item extends BaseObject $condition_string = self::addTablesToFields($condition_string, $fields); - $condition_string = $condition_string . ' AND ' . self::condition(false); + if ($usermode) { + $condition_string = $condition_string . ' AND ' . self::condition(false); + } $param_string = self::addTablesToFields(dba::buildParameter($params), $fields); @@ -123,10 +175,10 @@ class Item extends BaseObject * @return bool|array * @see dba::select */ - public static function selectFirstThread($uid, array $fields = [], array $condition = [], $params = []) + public static function selectFirstThreadForUser($uid, array $fields = [], array $condition = [], $params = []) { $params['limit'] = 1; - $result = self::selectThread($uid, $fields, $condition, $params); + $result = self::selectThreadForUser($uid, $fields, $condition, $params); if (is_bool($result)) { return $result; @@ -147,8 +199,12 @@ class Item extends BaseObject * * @return boolean|object */ - public static function selectThread($uid, array $selected = [], array $condition = [], $params = []) + public static function selectThreadForUser($uid, array $selected = [], array $condition = [], $params = []) { + if (empty($selected)) { + $selected = Item::DISPLAY_FIELDLIST; + } + $fields = self::fieldlist($selected); $threadfields = ['thread' => ['iid', 'uid', 'contact-id', 'owner-id', 'author-id', @@ -183,10 +239,6 @@ class Item extends BaseObject */ private static function fieldlist($selected) { - /* - These Fields are not added below. They are here to for bug search. - */ - $item_fields = ['author-id', 'owner-id', 'contact-id', 'uid', 'id', 'parent', 'uri', 'thr-parent', 'parent-uri', 'content-warning', 'commented', 'created', 'edited', 'received', 'verb', 'object-type', 'postopts', 'plink', diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 7f5e0aa4d..2487b3ca5 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -244,15 +244,19 @@ class DFRN $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); + if (!empty($ids)) { + $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($fields, $condition); + $items = dba::inArray($ret); + } else { + $items = []; + } /* * Will check further below if this actually returned results. @@ -338,7 +342,7 @@ class DFRN '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); + $ret = Item::select($fields, $condition); $items = dba::inArray($ret); if (!DBM::is_result($items)) { killme(); diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index cb41ba19e..c42e8c248 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -69,7 +69,7 @@ class Diaspora if (Config::get("system", "relay_directly", false)) { // We distribute our stuff based on the parent to ensure that the thread will be complete - $parent = Item::selectFirst(0, ['parent'], ['id' => $item_id]); + $parent = Item::selectFirst(['parent'], ['id' => $item_id]); if (!DBM::is_result($parent)) { return; } @@ -1170,7 +1170,7 @@ class Diaspora */ private static function messageExists($uid, $guid) { - $item = Item::selectFirst($uid, ['id'], ['uid' => $uid, 'guid' => $guid]); + $item = Item::selectFirst(['id'], ['uid' => $uid, 'guid' => $guid]); if (DBM::is_result($item)) { logger("message ".$guid." already exists for user ".$uid); return $item["id"]; @@ -1384,7 +1384,7 @@ class Diaspora 'author-name', 'author-link', 'author-avatar', 'owner-name', 'owner-link', 'owner-avatar']; $condition = ['uid' => $uid, 'guid' => $guid]; - $item = Item::selectFirst($uid, $fields, $condition); + $item = Item::selectFirst($fields, $condition); if (!DBM::is_result($item)) { $result = self::storeByGuid($guid, $contact["url"], $uid); @@ -1397,7 +1397,7 @@ class Diaspora if ($result) { logger("Fetched missing item ".$guid." - result: ".$result, LOGGER_DEBUG); - $item = Item::selectFirst($uid, $fields, $condition); + $item = Item::selectFirst($fields, $condition); } } @@ -1587,7 +1587,7 @@ class Diaspora */ private static function getUriFromGuid($author, $guid, $onlyfound = false) { - $item = Item::selectFirst(0, ['uri'], ['guid' => $guid]); + $item = Item::selectFirst(['uri'], ['guid' => $guid]); if (DBM::is_result($item)) { return $item["uri"]; } elseif (!$onlyfound) { @@ -1617,7 +1617,7 @@ class Diaspora */ private static function getGuidFromUri($uri, $uid) { - $item = Item::selectFirst($uid, ['guid'], ['uri' => $uri, 'uid' => $uid]); + $item = Item::selectFirst(['guid'], ['uri' => $uri, 'uid' => $uid]); if (DBM::is_result($item)) { return $item["guid"]; } else { @@ -1634,7 +1634,7 @@ class Diaspora */ private static function importerForGuid($guid) { - $item = Item::selectFirst(0, ['uid'], ['origin' => true, 'guid' => $guid]); + $item = Item::selectFirst(['uid'], ['origin' => true, 'guid' => $guid]); if (DBM::is_result($item)) { logger("Found user ".$item['uid']." as owner of item ".$guid, LOGGER_DEBUG); $contact = dba::selectFirst('contact', [], ['self' => true, 'uid' => $item['uid']]); @@ -2059,7 +2059,7 @@ class Diaspora // like on comments have the comment as parent. So we need to fetch the toplevel parent if ($parent_item["id"] != $parent_item["parent"]) { - $toplevel = Item::selectFirst($importer["uid"], ['origin'], ['id' => $parent_item["parent"]]); + $toplevel = Item::selectFirst(['origin'], ['id' => $parent_item["parent"]]); $origin = $toplevel["origin"]; } else { $origin = $parent_item["origin"]; @@ -2196,7 +2196,7 @@ class Diaspora return false; } - $item = Item::selectFirst(0, ['id'], ['guid' => $parent_guid, 'origin' => true, 'private' => false]); + $item = Item::selectFirst(['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; @@ -2217,7 +2217,7 @@ class Diaspora } // Send all existing comments and likes to the requesting server - $comments = Item::select(0, ['id', 'verb', 'self'], ['parent' => $item['id']]); + $comments = Item::select(['id', 'verb', 'self'], ['parent' => $item['id']]); while ($comment = dba::fetch($comments)) { if ($comment['id'] == $comment['parent']) { continue; @@ -2579,7 +2579,7 @@ class Diaspora $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 = Item::selectFirst(0, $fields, $condition); + $item = Item::selectFirst($fields, $condition); if (DBM::is_result($item)) { logger("reshared message ".$guid." already exists on system."); @@ -2623,7 +2623,7 @@ class Diaspora $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 = Item::selectFirst(0, $fields, $condition); + $item = Item::selectFirst($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 @@ -2765,7 +2765,7 @@ class Diaspora } else { $condition = ["`guid` = ? AND `uid` = ? AND NOT `file` LIKE '%%[%%' AND NOT `deleted`", $target_guid, $importer['uid']]; } - $r = Item::select($importer['uid'], $fields, $condition); + $r = Item::select($fields, $condition); if (!DBM::is_result($r)) { logger("Target guid ".$target_guid." was not found on this system for user ".$importer['uid']."."); return false; @@ -2773,7 +2773,7 @@ class Diaspora while ($item = dba::fetch($r)) { // Fetch the parent item - $parent = Item::selectFirst(0, ['author-link'], ['id' => $item["parent"]]); + $parent = Item::selectFirst(['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"])) { @@ -3409,7 +3409,7 @@ class Diaspora if (($guid != "") && $complete) { $condition = ['guid' => $guid, 'network' => [NETWORK_DFRN, NETWORK_DIASPORA]]; - $item = Item::selectFirst(0, ['contact-id'], $condition); + $item = Item::selectFirst(['contact-id'], $condition); if (DBM::is_result($item)) { $ret= []; $ret["root_handle"] = self::handleFromContact($item["contact-id"]); @@ -3676,7 +3676,7 @@ class Diaspora */ private static function constructLike($item, $owner) { - $parent = Item::selectFirst(0, ['guid', 'uri', 'parent-uri'], ['uri' => $item["thr-parent"]]); + $parent = Item::selectFirst(['guid', 'uri', 'parent-uri'], ['uri' => $item["thr-parent"]]); if (!DBM::is_result($parent)) { return false; } @@ -3707,7 +3707,7 @@ class Diaspora */ private static function constructAttend($item, $owner) { - $parent = Item::selectFirst(0, ['guid', 'uri', 'parent-uri'], ['uri' => $item["thr-parent"]]); + $parent = Item::selectFirst(['guid', 'uri', 'parent-uri'], ['uri' => $item["thr-parent"]]); if (!DBM::is_result($parent)) { return false; } @@ -3751,7 +3751,7 @@ class Diaspora return $result; } - $parent = Item::selectFirst(0, ['guid'], ['id' => $item["parent"], 'parent' => $item["parent"]]); + $parent = Item::selectFirst(['guid'], ['id' => $item["parent"], 'parent' => $item["parent"]]); if (!DBM::is_result($parent)) { return false; } @@ -4224,7 +4224,7 @@ class Diaspora $contact["uprvkey"] = $r[0]['prvkey']; - $item = Item::selectFirst(0, [], ['id' => $post_id]); + $item = Item::selectFirst([], ['id' => $post_id]); if (!DBM::is_result($item)) { return false; } From e42b9348072b54ae50e53f94bd8e5a1eb9698eb7 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 17 Jun 2018 17:14:52 +0000 Subject: [PATCH 5/7] Item field list structure is unified --- src/Model/Item.php | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index 4018db9ff..0dede482a 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -239,7 +239,9 @@ class Item extends BaseObject */ private static function fieldlist($selected) { - $item_fields = ['author-id', 'owner-id', 'contact-id', 'uid', 'id', 'parent', + $fields = []; + + $fields['item'] = ['author-id', 'owner-id', 'contact-id', 'uid', 'id', 'parent', 'uri', 'thr-parent', 'parent-uri', 'content-warning', 'commented', 'created', 'edited', 'received', 'verb', 'object-type', 'postopts', 'plink', 'guid', 'wall', 'private', 'starred', 'origin', 'title', 'body', 'file', 'event-id', @@ -250,24 +252,25 @@ class Item extends BaseObject 'resource-id', 'tag', 'inform', 'pubmail', 'visible', 'bookmark', 'unseen', 'deleted', 'forum_mode', 'mention', 'global', 'shadow']; - $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', + $fields['author'] = ['url' => 'author-link', 'name' => 'author-name', 'thumb' => 'author-avatar']; + + $fields['owner'] = ['url' => 'owner-link', 'name' => 'owner-name', 'thumb' => 'owner-avatar']; + + $fields['contact'] = ['url' => 'contact-link', 'name' => 'contact-name', 'thumb' => 'contact-avatar', 'network', 'url', 'name', 'writable', 'self', 'id' => 'cid', 'alias', 'uid' => 'contact-uid', 'photo', 'name-date', 'uri-date', 'avatar-date', 'thumb', 'dfrn-id']; - $event_fields = ['created' => 'event-created', 'edited' => 'event-edited', + $fields['parent-item'] = ['guid' => 'parent-guid']; + + $fields['parent-item-author'] = ['url' => 'parent-author-link', 'name' => 'parent-author-name']; + + $fields['event'] = ['created' => 'event-created', 'edited' => 'event-edited', 'start' => 'event-start','finish' => 'event-finish', 'summary' => 'event-summary','desc' => 'event-desc', 'location' => 'event-location', 'type' => 'event-type', 'nofinish' => 'event-nofinish','adjust' => 'event-adjust', 'ignore' => 'event-ignore', 'id' => 'event-id']; - $fields = ['item' => $item_fields, 'author' => $author_fields, 'owner' => $owner_fields, - 'contact' => $contact_fields, 'event' => $event_fields]; - - $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; From 23aa9dffa061309026f5717a6ed18a387e5d4d23 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 17 Jun 2018 21:35:33 +0000 Subject: [PATCH 6/7] New item functions are now used in the delivery process as well --- src/Model/Item.php | 20 ++++++++++---------- src/Worker/Delivery.php | 14 +++++--------- src/Worker/Notifier.php | 19 ++++++++++--------- 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index 0dede482a..dcf944cc4 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -241,16 +241,16 @@ class Item extends BaseObject { $fields = []; - $fields['item'] = ['author-id', 'owner-id', 'contact-id', 'uid', 'id', 'parent', - 'uri', 'thr-parent', 'parent-uri', 'content-warning', - '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', - 'id' => 'item_id', 'network' => 'item_network', - 'type', 'extid', 'changed', 'moderated', 'target-type', 'target', - 'resource-id', 'tag', 'inform', 'pubmail', 'visible', 'bookmark', 'unseen', 'deleted', - 'forum_mode', 'mention', 'global', 'shadow']; + $fields['item'] = ['id', 'uid', 'parent', 'uri', 'parent-uri', 'thr-parent', 'guid', + 'contact-id', 'owner-id', 'author-id', 'type', 'wall', 'gravity', 'extid', + 'created', 'edited', 'commented', 'received', 'changed', + 'title', 'body', 'app', 'verb', 'object-type', 'object', 'target-type', 'target', + 'postopts', 'plink', 'resource-id', 'event-id', 'tag', 'attach', 'inform', + 'file', 'location', 'coord', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', + 'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark', + 'unseen', 'deleted', 'origin', 'forum_mode', 'mention', + 'rendered-hash', 'rendered-html', 'global', 'shadow', 'content-warning', + 'id' => 'item_id', 'network' => 'item_network']; $fields['author'] = ['url' => 'author-link', 'name' => 'author-name', 'thumb' => 'author-avatar']; diff --git a/src/Worker/Delivery.php b/src/Worker/Delivery.php index f91d0c198..15273762a 100644 --- a/src/Worker/Delivery.php +++ b/src/Worker/Delivery.php @@ -59,14 +59,10 @@ class Delivery extends BaseObject } $parent_id = intval($item['parent']); - $itemdata = dba::p("SELECT `item`.*, `contact`.`uid` AS `cuid`, - `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer` - FROM `item` - INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` - LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` - WHERE `item`.`id` IN (?, ?) AND `visible` AND NOT `moderated` - ORDER BY `item`.`id`", - $item_id, $parent_id); + $condition = ['id' => [$item_id, $parent_id], 'visible' => true, 'moderated' => false]; + $params = ['order' => ['id']]; + $itemdata = Item::select([], $condition, $params); + $items = []; while ($item = dba::fetch($itemdata)) { if ($item['id'] == $parent_id) { @@ -79,7 +75,7 @@ class Delivery extends BaseObject } dba::close($itemdata); - $uid = $target_item['cuid']; + $uid = $target_item['contact-uid']; // avoid race condition with deleting entries if ($items[0]['deleted']) { diff --git a/src/Worker/Notifier.php b/src/Worker/Notifier.php index 6ad6827f7..61b296a26 100644 --- a/src/Worker/Notifier.php +++ b/src/Worker/Notifier.php @@ -10,6 +10,7 @@ use Friendica\Core\Worker; use Friendica\Database\DBM; use Friendica\Model\Contact; use Friendica\Model\Group; +use Friendica\Model\Item; use Friendica\Model\User; use Friendica\Model\PushSubscriber; use Friendica\Network\Probe; @@ -104,27 +105,27 @@ class Notifier { intval($uid), NETWORK_DFRN, NETWORK_DIASPORA); } else { // find ancestors - $target_item = dba::fetch_first("SELECT `item`.*, `contact`.`uid` AS `cuid` FROM `item` - INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` - WHERE `item`.`id` = ? AND `visible` AND NOT `moderated`", $item_id); + $condition = ['id' => $item_id, 'visible' => true, 'moderated' => false]; + $target_item = Item::selectFirst([], $condition); if (!DBM::is_result($target_item) || !intval($target_item['parent'])) { return; } $parent_id = intval($target_item['parent']); - $uid = $target_item['cuid']; + $uid = $target_item['contact-uid']; $updated = $target_item['edited']; - $items = q("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer` - FROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d AND visible AND NOT moderated ORDER BY `id` ASC", - intval($parent_id) - ); + $condition = ['parent' => $parent_id, 'visible' => true, 'moderated' => false]; + $params = ['order' => ['id']]; + $ret = Item::select([], $condition, $params); - if (!count($items)) { + if (!DBM::is_result($ret)) { return; } + $items = dba::inArray($ret); + // avoid race condition with deleting entries if ($items[0]['deleted']) { foreach ($items as $item) { From 2a76290d19adb072fcf90a143ab7e6eb8a2d3ced Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 17 Jun 2018 21:55:01 +0000 Subject: [PATCH 7/7] Using the constants --- src/Protocol/DFRN.php | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 2487b3ca5..9a24c75e4 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -245,14 +245,7 @@ class DFRN } if (!empty($ids)) { - $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($fields, $condition); + $ret = Item::select(Item::DELIVER_FIELDLIST, ['id' => $ids]); $items = dba::inArray($ret); } else { $items = []; @@ -336,13 +329,7 @@ class DFRN $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($fields, $condition); + $ret = Item::select(Item::DELIVER_FIELDLIST, $condition); $items = dba::inArray($ret); if (!DBM::is_result($items)) { killme();