]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Merge pull request #9826 from annando/remove-icid
[friendica.git] / src / Model / Item.php
index f9a27b1964af0b384c78db512e3c7cad32979b42..70bd8b5c293ae8eb401f59e3cdacc7e4555b8ac9 100644 (file)
@@ -31,6 +31,7 @@ use Friendica\Core\Session;
 use Friendica\Core\System;
 use Friendica\Model\Tag;
 use Friendica\Core\Worker;
+use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
 use Friendica\DI;
@@ -43,7 +44,6 @@ use Friendica\Util\Map;
 use Friendica\Util\Network;
 use Friendica\Util\Strings;
 use Friendica\Worker\Delivery;
-use Friendica\Repository\PermissionSet as RepPermissionSet;
 use LanguageDetection\Language;
 
 class Item
@@ -109,7 +109,7 @@ class Item
        // All fields in the item table
        const ITEM_FIELDLIST = ['id', 'uid', 'parent', 'uri', 'parent-uri', 'thr-parent',
                        'guid', 'uri-id', 'parent-uri-id', 'thr-parent-id', 'vid',
-                       'contact-id', 'type', 'wall', 'gravity', 'extid', 'icid', 'psid',
+                       'contact-id', 'type', 'wall', 'gravity', 'extid', 'psid',
                        'created', 'edited', 'commented', 'received', 'changed', 'verb',
                        'postopts', 'plink', 'resource-id', 'event-id', 'inform',
                        'file', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'post-type',
@@ -134,8 +134,6 @@ class Item
 
        const TABLES = ['item', 'user-item', 'item-content', 'post-delivery-data', 'diaspora-interaction'];
 
-       private static $legacy_mode = null;
-
        private static function getItemFields()
        {
                $definition = DBStructure::definition('', false);
@@ -148,15 +146,6 @@ class Item
                return $postfields;
        }
 
-       public static function isLegacyMode()
-       {
-               if (is_null(self::$legacy_mode)) {
-                       self::$legacy_mode = (DI::config()->get("system", "post_update_version") < 1279);
-               }
-
-               return self::$legacy_mode;
-       }
-
        /**
         * Set the pinned state of an item
         *
@@ -186,690 +175,6 @@ class Item
                return (bool)$useritem['pinned'];
        }
 
-       /**
-        * Select pinned 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
-        * @throws \Exception
-        */
-       public static function selectPinned(int $uid, array $selected = [], array $condition = [], $params = [])
-       {
-               $useritems = DBA::select('user-item', ['iid'], ['uid' => $uid, 'pinned' => true]);
-               if (!DBA::isResult($useritems)) {
-                       return $useritems;
-               }
-
-               $pinned = [];
-               while ($useritem = DBA::fetch($useritems)) {
-                       $pinned[] = $useritem['iid'];
-               }
-               DBA::close($useritems);
-
-               if (empty($pinned)) {
-                       return [];
-               }
-
-               $condition = DBA::mergeConditions(['iid' => $pinned], $condition);
-
-               return self::selectThreadForUser($uid, $selected, $condition, $params);
-       }
-
-       /**
-        * Fetch a single item row
-        *
-        * @param mixed $stmt statement object
-        * @return array|false current row or false
-        * @throws \Exception
-        */
-       public static function fetch($stmt)
-       {
-               $row = DBA::fetch($stmt);
-
-               if (!is_array($row)) {
-                       return $row;
-               }
-
-               $row = DBA::castFields('item', $row);
-
-               // ---------------------- Transform item structure data ----------------------
-
-               // We prefer the data from the user's contact over the public one
-               if (!empty($row['author-link']) && !empty($row['contact-link']) &&
-                       ($row['author-link'] == $row['contact-link'])) {
-                       if (isset($row['author-avatar']) && !empty($row['contact-avatar'])) {
-                               $row['author-avatar'] = $row['contact-avatar'];
-                       }
-                       if (isset($row['author-name']) && !empty($row['contact-name'])) {
-                               $row['author-name'] = $row['contact-name'];
-                       }
-               }
-
-               if (!empty($row['owner-link']) && !empty($row['contact-link']) &&
-                       ($row['owner-link'] == $row['contact-link'])) {
-                       if (isset($row['owner-avatar']) && !empty($row['contact-avatar'])) {
-                               $row['owner-avatar'] = $row['contact-avatar'];
-                       }
-                       if (isset($row['owner-name']) && !empty($row['contact-name'])) {
-                               $row['owner-name'] = $row['contact-name'];
-                       }
-               }
-
-               // We can always comment on posts from these networks
-               if (array_key_exists('writable', $row) &&
-                       in_array($row['internal-network'], Protocol::FEDERATED)) {
-                       $row['writable'] = 1;
-               }
-
-               // ---------------------- Transform item content data ----------------------
-
-               // Fetch data from the item-content table whenever there is content there
-               if (self::isLegacyMode()) {
-                       $legacy_fields = array_merge(Post\DeliveryData::LEGACY_FIELD_LIST, self::MIXED_CONTENT_FIELDLIST);
-                       foreach ($legacy_fields as $field) {
-                               if (empty($row[$field]) && !empty($row['internal-item-' . $field])) {
-                                       $row[$field] = $row['internal-item-' . $field];
-                               }
-                               unset($row['internal-item-' . $field]);
-                       }
-               }
-
-               if (array_key_exists('verb', $row)) {
-                       if (!is_null($row['internal-verb'])) {
-                               $row['verb'] = $row['internal-verb'];
-                       }
-
-                       if (in_array($row['verb'], self::ACTIVITIES)) {
-                               if (array_key_exists('title', $row)) {
-                                       $row['title'] = '';
-                               }
-                               if (array_key_exists('body', $row)) {
-                                       $row['body'] = $row['verb'];
-                               }
-                               if (array_key_exists('object', $row)) {
-                                       $row['object'] = '';
-                               }
-                               if (array_key_exists('object-type', $row)) {
-                                       $row['object-type'] = Activity\ObjectType::NOTE;
-                               }
-                       } elseif (in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
-                               // Posts don't have a target - but having tags or files.
-                               if (array_key_exists('target', $row)) {
-                                       $row['target'] = '';
-                               }
-                       }
-               }
-
-               if (array_key_exists('vid', $row) && is_null($row['vid']) && !empty($row['verb'])) {
-                       $row['vid'] = Verb::getID($row['verb']);
-               }
-                       
-               if (!array_key_exists('verb', $row) || in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
-                       // Build the file string out of the term entries
-                       if (array_key_exists('file', $row) && empty($row['file'])) {
-                               $row['file'] = Post\Category::getTextByURIId($row['internal-uri-id'], $row['internal-uid']);
-                       }
-               }
-
-               if ($row['internal-psid'] == RepPermissionSet::PUBLIC) {
-                       if (array_key_exists('allow_cid', $row)) {
-                               $row['allow_cid'] = '';
-                       }
-                       if (array_key_exists('allow_gid', $row)) {
-                               $row['allow_gid'] = '';
-                       }
-                       if (array_key_exists('deny_cid', $row)) {
-                               $row['deny_cid'] = '';
-                       }
-                       if (array_key_exists('deny_gid', $row)) {
-                               $row['deny_gid'] = '';
-                       }
-               }
-
-               if (array_key_exists('ignored', $row) && array_key_exists('internal-user-ignored', $row) && !is_null($row['internal-user-ignored'])) {
-                       $row['ignored'] = $row['internal-user-ignored'];
-               }
-
-               // Remove internal fields
-               unset($row['internal-network']);
-               unset($row['internal-uri-id']);
-               unset($row['internal-uid']);
-               unset($row['internal-psid']);
-               unset($row['internal-verb']);
-               unset($row['internal-user-ignored']);
-               unset($row['interaction']);
-
-               return $row;
-       }
-
-       /**
-        * Fills an array with data from an item query
-        *
-        * @param object $stmt statement object
-        * @param bool   $do_close
-        * @return array Data array
-        */
-       public static function toArray($stmt, $do_close = true) {
-               if (is_bool($stmt)) {
-                       return $stmt;
-               }
-
-               $data = [];
-               while ($row = self::fetch($stmt)) {
-                       $data[] = $row;
-               }
-               if ($do_close) {
-                       DBA::close($stmt);
-               }
-               return $data;
-       }
-
-       /**
-        * Check if item data exists
-        *
-        * @param array $condition array of fields for condition
-        *
-        * @return boolean Are there rows for that condition?
-        * @throws \Exception
-        */
-       public static function exists($condition) {
-               $stmt = self::select(['id'], $condition, ['limit' => 1]);
-
-               if (is_bool($stmt)) {
-                       $retval = $stmt;
-               } else {
-                       $retval = (DBA::numRows($stmt) > 0);
-               }
-
-               DBA::close($stmt);
-
-               return $retval;
-       }
-
-       /**
-        * 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
-        * @throws \Exception
-        */
-       public static function selectForUser($uid, array $selected = [], array $condition = [], $params = [])
-       {
-               $params['uid'] = $uid;
-
-               if (empty($selected)) {
-                       $selected = self::DISPLAY_FIELDLIST;
-               }
-
-               return self::select($selected, $condition, $params);
-       }
-
-       /**
-        * Retrieve a single record from the item table and returns it in an associative array
-        *
-        * @param array $fields
-        * @param array $condition
-        * @param array $params
-        * @return bool|array
-        * @throws \Exception
-        * @see   DBA::select
-        */
-       public static function selectFirst(array $fields = [], array $condition = [], $params = [])
-       {
-               $params['limit'] = 1;
-
-               $result = self::select($fields, $condition, $params);
-
-               if (is_bool($result)) {
-                       return $result;
-               } else {
-                       $row = self::fetch($result);
-                       DBA::close($result);
-                       return $row;
-               }
-       }
-
-       /**
-        * Select rows from the item table and returns them as an array
-        *
-        * @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 array
-        * @throws \Exception
-        */
-       public static function selectToArray(array $fields = [], array $condition = [], $params = [])
-       {
-               $result = self::select($fields, $condition, $params);
-
-               if (is_bool($result)) {
-                       return [];
-               }
-
-               $data = [];
-               while ($row = self::fetch($result)) {
-                       $data[] = $row;
-               }
-               DBA::close($result);
-
-               return $data;
-       }
-
-       /**
-        * Select rows from the item table
-        *
-        * @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
-        * @throws \Exception
-        */
-       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($usermode);
-
-               $select_fields = self::constructSelectFields($fields, $selected);
-
-               $condition_string = DBA::buildCondition($condition);
-
-               $condition_string = self::addTablesToFields($condition_string, $fields);
-
-               if ($usermode) {
-                       $condition_string = $condition_string . ' AND ' . self::condition(false);
-               }
-
-               $param_string = self::addTablesToFields(DBA::buildParameter($params), $fields);
-
-               $table = "`item` " . self::constructJoins($uid, $select_fields . $condition_string . $param_string, false, $usermode);
-
-               $sql = "SELECT " . $select_fields . " FROM " . $table . $condition_string . $param_string;
-
-               return DBA::p($sql, $condition);
-       }
-
-       /**
-        * Select rows from the starting post in the item table
-        *
-        * @param integer $uid       User ID
-        * @param array   $selected
-        * @param array   $condition Array of fields for condition
-        * @param array   $params    Array of several parameters
-        *
-        * @return boolean|object
-        * @throws \Exception
-        */
-       public static function selectThreadForUser($uid, array $selected = [], array $condition = [], $params = [])
-       {
-               $params['uid'] = $uid;
-
-               if (empty($selected)) {
-                       $selected = self::DISPLAY_FIELDLIST;
-               }
-
-               return self::selectThread($selected, $condition, $params);
-       }
-
-       /**
-        * Retrieve a single record from the starting post in the item table and returns it in an associative array
-        *
-        * @param integer $uid User ID
-        * @param array   $selected
-        * @param array   $condition
-        * @param array   $params
-        * @return bool|array
-        * @throws \Exception
-        * @see   DBA::select
-        */
-       public static function selectFirstThreadForUser($uid, array $selected = [], array $condition = [], $params = [])
-       {
-               $params['uid'] = $uid;
-
-               if (empty($selected)) {
-                       $selected = self::DISPLAY_FIELDLIST;
-               }
-
-               return self::selectFirstThread($selected, $condition, $params);
-       }
-
-       /**
-        * Retrieve a single record from the starting post in the item table and returns it in an associative array
-        *
-        * @param array $fields
-        * @param array $condition
-        * @param array $params
-        * @return bool|array
-        * @throws \Exception
-        * @see   DBA::select
-        */
-       public static function selectFirstThread(array $fields = [], array $condition = [], $params = [])
-       {
-               $params['limit'] = 1;
-               $result = self::selectThread($fields, $condition, $params);
-
-               if (is_bool($result)) {
-                       return $result;
-               } else {
-                       $row = self::fetch($result);
-                       DBA::close($result);
-                       return $row;
-               }
-       }
-
-       /**
-        * Select rows from the starting post in the item table
-        *
-        * @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
-        * @throws \Exception
-        */
-       public static function selectThread(array $selected = [], array $condition = [], $params = [])
-       {
-               $uid = 0;
-               $usermode = false;
-
-               if (isset($params['uid'])) {
-                       $uid = $params['uid'];
-                       $usermode = true;
-               }
-
-               $fields = self::fieldlist($usermode);
-
-               $fields['thread'] = ['mention', 'ignored', 'iid'];
-
-               $threadfields = ['thread' => ['iid', 'uid', 'contact-id', 'owner-id', 'author-id',
-                       'created', 'edited', 'commented', 'received', 'changed', 'wall', 'private',
-                       'pubmail', 'moderated', 'visible', 'starred', 'ignored', 'post-type',
-                       'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'network']];
-
-               $select_fields = self::constructSelectFields($fields, $selected);
-
-               $condition_string = DBA::buildCondition($condition);
-
-               $condition_string = self::addTablesToFields($condition_string, $threadfields);
-               $condition_string = self::addTablesToFields($condition_string, $fields);
-
-               if ($usermode) {
-                       $condition_string = $condition_string . ' AND ' . self::condition(true);
-               }
-
-               $param_string = DBA::buildParameter($params);
-               $param_string = self::addTablesToFields($param_string, $threadfields);
-               $param_string = self::addTablesToFields($param_string, $fields);
-
-               $table = "`thread` " . self::constructJoins($uid, $select_fields . $condition_string . $param_string, true, $usermode);
-
-               $sql = "SELECT " . $select_fields . " FROM " . $table . $condition_string . $param_string;
-
-               return DBA::p($sql, $condition);
-       }
-
-       /**
-        * Returns a list of fields that are associated with the item table
-        *
-        * @param $usermode
-        * @return array field list
-        */
-       private static function fieldlist($usermode)
-       {
-               $fields = [];
-
-               $fields['item'] = ['id', 'uid', 'parent', 'uri', 'parent-uri', 'thr-parent',
-                       'guid', 'uri-id', 'parent-uri-id', 'thr-parent-id', 'vid', 'causer-id',
-                       'contact-id', 'owner-id', 'author-id', 'type', 'wall', 'gravity', 'extid',
-                       'created', 'edited', 'commented', 'received', 'changed', 'psid',
-                       'resource-id', 'event-id', 'post-type', 'file',
-                       'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark',
-                       'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'global',
-                       'id' => 'item_id', 'network', 'icid', 'event-id',
-                       'uri-id' => 'internal-uri-id', 'uid' => 'internal-uid',
-                       'network' => 'internal-network', 'psid' => 'internal-psid'];
-
-               if ($usermode) {
-                       $fields['user-item'] = ['pinned', 'notification-type', 'ignored' => 'internal-user-ignored'];
-               }
-
-               $fields['item-content'] = array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST);
-
-               $fields['post-delivery-data'] = array_merge(Post\DeliveryData::LEGACY_FIELD_LIST, Post\DeliveryData::FIELD_LIST);
-
-               $fields['verb'] = ['name' => 'internal-verb'];
-
-               $fields['permissionset'] = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'];
-
-               $fields['author'] = ['url' => 'author-link', 'name' => 'author-name', 'addr' => 'author-addr',
-                       'thumb' => 'author-avatar', 'nick' => 'author-nick', 'network' => 'author-network'];
-
-               $fields['owner'] = ['url' => 'owner-link', 'name' => 'owner-name', 'addr' => 'owner-addr',
-                       'thumb' => 'owner-avatar', 'nick' => 'owner-nick', 'network' => 'owner-network'];
-
-               $fields['causer'] = ['url' => 'causer-link', 'name' => 'causer-name', 'addr' => 'causer-addr',
-                       'thumb' => 'causer-avatar', 'nick' => 'causer-nick', 'network' => 'causer-network',
-                       'contact-type' => 'causer-contact-type'];
-
-               $fields['contact'] = ['url' => 'contact-link', 'name' => 'contact-name', 'thumb' => 'contact-avatar',
-                       'writable', 'self', 'id' => 'cid', 'alias', 'uid' => 'contact-uid',
-                       'photo', 'name-date', 'uri-date', 'avatar-date', 'thumb', 'dfrn-id'];
-
-               $fields['parent-item'] = ['guid' => 'parent-guid', 'network' => 'parent-network', 'author-id' => 'parent-author-id'];
-
-               $fields['parent-item-author'] = ['url' => 'parent-author-link', 'name' => 'parent-author-name',
-                       'network' => 'parent-author-network'];
-
-               $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'];
-
-               $fields['diaspora-interaction'] = ['interaction', 'interaction' => 'signed_text'];
-
-               return $fields;
-       }
-
-       /**
-        * Returns SQL condition for the "select" functions
-        *
-        * @param boolean $thread_mode Called for the items (false) or for the threads (true)
-        *
-        * @return string SQL condition
-        */
-       private static function condition($thread_mode)
-       {
-               if ($thread_mode) {
-                       $master_table = "`thread`";
-               } else {
-                       $master_table = "`item`";
-               }
-               return sprintf("$master_table.`visible` AND NOT $master_table.`deleted` AND NOT $master_table.`moderated`
-                       AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
-                       AND (`user-author`.`blocked` IS NULL OR NOT `user-author`.`blocked`)
-                       AND (`user-author`.`ignored` IS NULL OR NOT `user-author`.`ignored` OR `item`.`gravity` != %d)
-                       AND (`user-owner`.`blocked` IS NULL OR NOT `user-owner`.`blocked`)
-                       AND (`user-owner`.`ignored` IS NULL OR NOT `user-owner`.`ignored` OR `item`.`gravity` != %d) ",
-                       GRAVITY_PARENT, GRAVITY_PARENT);
-       }
-
-       /**
-        * Returns all needed "JOIN" commands for the "select" functions
-        *
-        * @param integer $uid          User ID
-        * @param string  $sql_commands The parts of the built SQL commands in the "select" functions
-        * @param boolean $thread_mode  Called for the items (false) or for the threads (true)
-        *
-        * @param         $user_mode
-        * @return string The SQL joins for the "select" functions
-        */
-       private static function constructJoins($uid, $sql_commands, $thread_mode, $user_mode)
-       {
-               if ($thread_mode) {
-                       $master_table = "`thread`";
-                       $master_table_key = "`thread`.`iid`";
-                       $joins = "STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid` ";
-               } else {
-                       $master_table = "`item`";
-                       $master_table_key = "`item`.`id`";
-                       $joins = '';
-               }
-
-               if ($user_mode) {
-                       $joins .= sprintf("STRAIGHT_JOIN `contact` ON `contact`.`id` = $master_table.`contact-id`
-                               AND NOT `contact`.`blocked`
-                               AND ((NOT `contact`.`readonly` AND NOT `contact`.`pending` AND (`contact`.`rel` IN (%s, %s)))
-                               OR `contact`.`self` OR `item`.`gravity` != %d OR `contact`.`uid` = 0)
-                               STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = $master_table.`author-id` AND NOT `author`.`blocked`
-                               STRAIGHT_JOIN `contact` AS `owner` ON `owner`.`id` = $master_table.`owner-id` AND NOT `owner`.`blocked`
-                               LEFT JOIN `user-item` ON `user-item`.`iid` = $master_table_key AND `user-item`.`uid` = %d
-                               LEFT JOIN `user-contact` AS `user-author` ON `user-author`.`cid` = $master_table.`author-id` AND `user-author`.`uid` = %d
-                               LEFT JOIN `user-contact` AS `user-owner` ON `user-owner`.`cid` = $master_table.`owner-id` AND `user-owner`.`uid` = %d",
-                               Contact::SHARING, Contact::FRIEND, GRAVITY_PARENT, intval($uid), intval($uid), intval($uid));
-               } else {
-                       if (strpos($sql_commands, "`contact`.") !== false) {
-                               $joins .= "LEFT JOIN `contact` ON `contact`.`id` = $master_table.`contact-id`";
-                       }
-                       if (strpos($sql_commands, "`author`.") !== false) {
-                               $joins .= " LEFT JOIN `contact` AS `author` ON `author`.`id` = $master_table.`author-id`";
-                       }
-                       if (strpos($sql_commands, "`owner`.") !== false) {
-                               $joins .= " LEFT JOIN `contact` AS `owner` ON `owner`.`id` = $master_table.`owner-id`";
-                       }
-               }
-               if (strpos($sql_commands, "`causer`.") !== false) {
-                       $joins .= " LEFT JOIN `contact` AS `causer` ON `causer`.`id` = `item`.`causer-id`";
-               }
-
-               if (strpos($sql_commands, "`group_member`.") !== false) {
-                       $joins .= " STRAIGHT_JOIN `group_member` ON `group_member`.`contact-id` = $master_table.`contact-id`";
-               }
-
-               if (strpos($sql_commands, "`user`.") !== false) {
-                       $joins .= " STRAIGHT_JOIN `user` ON `user`.`uid` = $master_table.`uid`";
-               }
-
-               if (strpos($sql_commands, "`event`.") !== false) {
-                       $joins .= " LEFT JOIN `event` ON `event-id` = `event`.`id`";
-               }
-
-               if (strpos($sql_commands, "`diaspora-interaction`.") !== false) {
-                       $joins .= " LEFT JOIN `diaspora-interaction` ON `diaspora-interaction`.`uri-id` = `item`.`uri-id`";
-               }
-
-               if (strpos($sql_commands, "`item-content`.") !== false) {
-                       $joins .= " LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`";
-               }
-
-               if (strpos($sql_commands, "`post-delivery-data`.") !== false) {
-                       $joins .= " LEFT JOIN `post-delivery-data` ON `post-delivery-data`.`uri-id` = `item`.`uri-id` AND `item`.`origin`";
-               }
-
-               if (strpos($sql_commands, "`verb`.") !== false) {
-                       $joins .= " LEFT JOIN `verb` ON `verb`.`id` = `item`.`vid`";
-               }
-
-               if (strpos($sql_commands, "`permissionset`.") !== false) {
-                       $joins .= " LEFT JOIN `permissionset` ON `permissionset`.`id` = `item`.`psid`";
-               }
-
-               if ((strpos($sql_commands, "`parent-item`.") !== false) || (strpos($sql_commands, "`parent-item-author`.") !== false)) {
-                       $joins .= " STRAIGHT_JOIN `item` AS `parent-item` ON `parent-item`.`id` = `item`.`parent`";
-
-                       if (strpos($sql_commands, "`parent-item-author`.") !== false) {
-                               $joins .= " STRAIGHT_JOIN `contact` AS `parent-item-author` ON `parent-item-author`.`id` = `parent-item`.`author-id`";
-                       }
-               }
-
-               return $joins;
-       }
-
-       /**
-        * Add the field list for the "select" functions
-        *
-        * @param array $fields The field definition array
-        * @param array $selected The array with the selected fields from the "select" functions
-        *
-        * @return string The field list
-        */
-       private static function constructSelectFields(array $fields, array $selected)
-       {
-               if (!empty($selected)) {
-                       $selected = array_merge($selected, ['internal-uri-id', 'internal-uid', 'internal-psid', 'internal-network']);
-               }
-
-               if (in_array('verb', $selected)) {
-                       $selected = array_merge($selected, ['internal-verb']);
-               }
-
-               if (in_array('ignored', $selected)) {
-                       $selected[] = 'internal-user-ignored';
-               }
-
-               $legacy_fields = array_merge(Post\DeliveryData::LEGACY_FIELD_LIST, self::MIXED_CONTENT_FIELDLIST);
-
-               $selection = [];
-               foreach ($fields as $table => $table_fields) {
-                       foreach ($table_fields as $field => $select) {
-                               if (empty($selected) || in_array($select, $selected)) {
-                                       if (self::isLegacyMode() && in_array($select, $legacy_fields)) {
-                                               $selection[] = "`item`.`".$select."` AS `internal-item-" . $select . "`";
-                                       }
-                                       if (is_int($field)) {
-                                               $selection[] = "`" . $table . "`.`" . $select . "`";
-                                       } else {
-                                               $selection[] = "`" . $table . "`.`" . $field . "` AS `" . $select . "`";
-                                       }
-                               }
-                       }
-               }
-               return implode(", ", $selection);
-       }
-
-       /**
-        * add table definition to fields in an SQL query
-        *
-        * @param string $query SQL query
-        * @param array $fields The field definition array
-        *
-        * @return string the changed SQL query
-        */
-       private static function addTablesToFields($query, $fields)
-       {
-               foreach ($fields as $table => $table_fields) {
-                       foreach ($table_fields as $alias => $field) {
-                               if (is_int($alias)) {
-                                       $replace_field = $field;
-                               } else {
-                                       $replace_field = $alias;
-                               }
-
-                               $search = "/([^\.])`" . $field . "`/i";
-                               $replace = "$1`" . $table . "`.`" . $replace_field . "`";
-                               $query = preg_replace($search, $replace, $query);
-                       }
-               }
-               return $query;
-       }
-
        /**
         * Update existing item entries
         *
@@ -898,13 +203,13 @@ class Item
                // We cannot simply expand the condition to check for origin entries
                // The condition needn't to be a simple array but could be a complex condition.
                // And we have to execute this query before the update to ensure to fetch the same data.
-               $items = DBA::select('item', ['id', 'origin', 'uri', 'uri-id', 'icid', 'uid', 'file'], $condition);
+               $items = DBA::select('item', ['id', 'origin', 'uri', 'uri-id', 'uid', 'file'], $condition);
 
                $content_fields = [];
                foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) {
                        if (isset($fields[$field])) {
                                $content_fields[$field] = $fields[$field];
-                               if (in_array($field, self::CONTENT_FIELDLIST) || !self::isLegacyMode()) {
+                               if (in_array($field, self::CONTENT_FIELDLIST)) {
                                        unset($fields[$field]);
                                } else {
                                        $fields[$field] = null;
@@ -960,22 +265,6 @@ class Item
                                }
                
                                self::updateContent($content_fields, ['uri-id' => $item['uri-id']]);
-
-                               if (empty($item['icid'])) {
-                                       $item_content = DBA::selectFirst('item-content', [], ['uri-id' => $item['uri-id']]);
-                                       if (DBA::isResult($item_content)) {
-                                               $item_fields = ['icid' => $item_content['id']];
-                                               // Clear all fields in the item table that have a content in the item-content table
-                                               if (self::isLegacyMode()) {
-                                                       foreach ($item_content as $field => $content) {
-                                                               if (in_array($field, self::MIXED_CONTENT_FIELDLIST) && !empty($content)) {
-                                                                       $item_fields[$field] = null;
-                                                               }
-                                                       }
-                                               }
-                                               DBA::update('item', $item_fields, ['id' => $item['id']]);
-                                       }
-                               }
                        }
 
                        if (!is_null($files)) {
@@ -1070,8 +359,7 @@ class Item
                // locate item to be deleted
                $fields = ['id', 'uri', 'uri-id', 'uid', 'parent', 'parent-uri', 'origin',
                        'deleted', 'file', 'resource-id', 'event-id',
-                       'verb', 'object-type', 'object', 'target', 'contact-id',
-                       'icid', 'psid', 'gravity'];
+                       'verb', 'object-type', 'object', 'target', 'contact-id', 'psid', 'gravity'];
                $item = Post::selectFirst($fields, ['id' => $item_id]);
                if (!DBA::isResult($item)) {
                        Logger::info('Item not found.', ['id' => $item_id]);
@@ -1256,21 +544,6 @@ class Item
                return $item['author-id'];
        }
 
-       // This function will finally cover most of the preparation functionality in mod/item.php
-       public static function prepare(&$item)
-       {
-               /*
-                * @TODO: Unused code triggering inspection errors
-                *
-               $data = BBCode::getAttachmentData($item['body']);
-               if ((preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", $item['body'], $match, PREG_SET_ORDER) || isset($data["type"]))
-                       && ($posttype != self::PT_PERSONAL_NOTE)) {
-                       $posttype = self::PT_PAGE;
-                       $objecttype = ACTIVITY_OBJ_BOOKMARK;
-               }
-                */
-       }
-
        /**
         * Write an item array into a spool file to be inserted later.
         * This command is called whenever there are issues storing an item.
@@ -1842,13 +1115,10 @@ class Item
                        $notify_type = Delivery::POST;
                }
 
-               if (!in_array($item['verb'], self::ACTIVITIES)) {
-                       $item['icid'] = self::insertContent($item);
-                       if (empty($item['icid'])) {
-                               // This shouldn't happen
-                               Logger::warning('No content stored, quitting', ['guid' => $item['guid'], 'uri-id' => $item['uri-id'], 'causer-id' => ($item['causer-id'] ?? 0), 'post-type' => $item['post-type'], 'network' => $item['network']]);
-                               return 0;
-                       }
+               if (!in_array($item['verb'], self::ACTIVITIES) && !self::insertContent($item)) {
+                       // This shouldn't happen
+                       Logger::warning('No content stored, quitting', ['guid' => $item['guid'], 'uri-id' => $item['uri-id'], 'causer-id' => ($item['causer-id'] ?? 0), 'post-type' => $item['post-type'], 'network' => $item['network']]);
+                       return 0;
                }
 
                $body = $item['body'];
@@ -2091,6 +1361,7 @@ class Item
         * Insert a new item content entry
         *
         * @param array $item The item fields that are to be inserted
+        * @return bool "true" if content was inserted or already existed
         * @throws \Exception
         */
        private static function insertContent(array $item)
@@ -2103,25 +1374,23 @@ class Item
                        }
                }
 
-               $item_content = DBA::selectFirst('item-content', ['id'], ['uri-id' => $item['uri-id']]);
-               if (DBA::isResult($item_content)) {
-                       $icid = $item_content['id'];
-                       Logger::info('Existing content found', ['icid' => $icid, 'uri' => $item['uri']]);
-                       return $icid;
+               $found = DBA::exists('item-content', ['uri-id' => $item['uri-id']]);
+               if ($found) {
+                       Logger::info('Existing content found', ['uri-id' => $item['uri-id'], 'uri' => $item['uri']]);
+                       return true;
                }
 
-               DBA::replace('item-content', $fields);
+               DBA::insert('item-content', $fields, Database::INSERT_IGNORE);
 
-               $item_content = DBA::selectFirst('item-content', ['id'], ['uri-id' => $item['uri-id']]);
-               if (DBA::isResult($item_content)) {
-                       $icid = $item_content['id'];
-                       Logger::notice('Content inserted', ['icid' => $icid, 'uri' => $item['uri']]);
-                       return $icid;
+               $found = DBA::exists('item-content', ['uri-id' => $item['uri-id']]);
+               if ($found) {
+                       Logger::notice('Content inserted', ['uri-id' => $item['uri-id'], 'uri' => $item['uri']]);
+                       return true;
                }
 
                // This shouldn't happen.
                Logger::error("Content wasn't inserted", $item);
-               return null;
+               return false;
        }
 
        /**
@@ -2158,7 +1427,7 @@ class Item
         */
        public static function distribute($itemid, $signed_text = '')
        {
-               $condition = ["`id` IN (SELECT `parent` FROM `item` WHERE `id` = ?)", $itemid];
+               $condition = ["`id` IN (SELECT `parent` FROM `post-view` WHERE `id` = ?)", $itemid];
                $parent = Post::selectFirst(['owner-id'], $condition);
                if (!DBA::isResult($parent)) {
                        return;
@@ -3460,17 +2729,28 @@ class Item
                return $condition;
        }
 
-       public static function getPermissionsSQLByUserId($owner_id)
+       /**
+        * Get a permission SQL string for the given user
+        * 
+        * @param int $owner_id 
+        * @param string $table 
+        * @return string 
+        */
+       public static function getPermissionsSQLByUserId(int $owner_id, string $table = '')
        {
                $local_user = local_user();
                $remote_user = Session::getRemoteContactID($owner_id);
 
+               if (!empty($table)) {
+                       $table = DBA::quoteIdentifier($table) . '.';
+               }
+
                /*
                 * Construct permissions
                 *
                 * default permissions - anonymous user
                 */
-               $sql = sprintf(" AND `private` != %d", self::PRIVATE);
+               $sql = sprintf(" AND " . $table . "`private` != %d", self::PRIVATE);
 
                // Profile owner - everything is visible
                if ($local_user && ($local_user == $owner_id)) {
@@ -3486,12 +2766,12 @@ class Item
                        $set = PermissionSet::get($owner_id, $remote_user);
 
                        if (!empty($set)) {
-                               $sql_set = sprintf(" OR (`private` = %d AND `wall` AND `psid` IN (", self::PRIVATE) . implode(',', $set) . "))";
+                               $sql_set = sprintf(" OR (" . $table . "`private` = %d AND " . $table . "`wall` AND " . $table . "`psid` IN (", self::PRIVATE) . implode(',', $set) . "))";
                        } else {
                                $sql_set = '';
                        }
 
-                       $sql = sprintf(" AND (`private` != %d", self::PRIVATE) . $sql_set . ")";
+                       $sql = sprintf(" AND (" . $table . "`private` != %d", self::PRIVATE) . $sql_set . ")";
                }
 
                return $sql;