- /**
- * 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 current row
- */
- public static function fetch($stmt)
- {
- $row = DBA::fetch($stmt);
-
- if (is_bool($row)) {
- return $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'] = true;
- }
-
- // ---------------------- 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 inArray($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;
- }
-
- /**
- * Retrieve a single record from the item table for a given user 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 selectFirstForUser($uid, array $selected = [], array $condition = [], $params = [])
- {
- $params['uid'] = $uid;
-
- if (empty($selected)) {
- $selected = Item::DISPLAY_FIELDLIST;
- }
-
- return self::selectFirst($selected, $condition, $params);
- }
-
- /**
- * 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 = Item::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 = Item::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 = Item::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',
- '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', 'id' => 'event-id'];
-
- $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;
- }
-