3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model;
24 use Friendica\Database\DBA;
25 use Friendica\Protocol\Activity;
30 * Fetch a single post row
32 * @param mixed $stmt statement object
33 * @return array|false current row or false
36 public static function fetch($stmt)
38 $row = DBA::fetch($stmt);
40 if (!is_array($row)) {
44 if (array_key_exists('verb', $row)) {
45 if (in_array($row['verb'], Item::ACTIVITIES)) {
46 if (array_key_exists('title', $row)) {
49 if (array_key_exists('body', $row)) {
50 $row['body'] = $row['verb'];
52 if (array_key_exists('object', $row)) {
55 if (array_key_exists('object-type', $row)) {
56 $row['object-type'] = Activity\ObjectType::NOTE;
58 } elseif (in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
59 // Posts don't have a target - but having tags or files.
60 if (array_key_exists('target', $row)) {
66 if (!array_key_exists('verb', $row) || in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
67 // Build the file string out of the term entries
68 if (array_key_exists('file', $row)) {
69 if ($row['internal-file-count'] > 0) {
70 $row['file'] = Post\Category::getTextByURIId($row['internal-uri-id'], $row['internal-uid']);
77 // Remove internal fields
78 unset($row['internal-file-count']);
79 unset($row['internal-uri-id']);
80 unset($row['internal-uid']);
86 * Fills an array with data from an post query
88 * @param object $stmt statement object
89 * @param bool $do_close
90 * @return array Data array
92 public static function toArray($stmt, $do_close = true) {
98 while ($row = self::fetch($stmt)) {
108 * Check if post data exists
110 * @param array $condition array of fields for condition
112 * @return boolean Are there rows for that condition?
115 public static function exists($condition) {
116 return DBA::exists('post-view', $condition);
120 * Counts the posts satisfying the provided condition
122 * @param array $condition array of fields for condition
123 * @param array $params Array of several parameters
128 * $condition = ["uid" => 1, "network" => 'dspr'];
130 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
132 * $count = Post::count($condition);
135 public static function count(array $condition = [], array $params = [])
137 return DBA::count('post-view', $condition, $params);
141 * Retrieve a single record from the post table and returns it in an associative array
143 * @param array $fields
144 * @param array $condition
145 * @param array $params
150 public static function selectFirst(array $fields = [], array $condition = [], $params = [])
152 $params['limit'] = 1;
154 $result = self::select($fields, $condition, $params);
156 if (is_bool($result)) {
159 $row = self::fetch($result);
166 * Select rows from the post table and returns them as an array
168 * @param array $selected Array of selected fields, empty for all
169 * @param array $condition Array of fields for condition
170 * @param array $params Array of several parameters
175 public static function selectToArray(array $fields = [], array $condition = [], $params = [])
177 $result = self::select($fields, $condition, $params);
179 if (is_bool($result)) {
184 while ($row = self::fetch($result)) {
193 * Select rows from the post table
195 * @param array $selected Array of selected fields, empty for all
196 * @param array $condition Array of fields for condition
197 * @param array $params Array of several parameters
199 * @return boolean|object
202 public static function select(array $selected = [], array $condition = [], $params = [])
204 if (empty($selected)) {
205 $selected = array_merge(['author-addr', 'author-nick', 'owner-addr', 'owner-nick', 'causer-addr', 'causer-nick',
206 'causer-network', 'photo', 'name-date', 'uri-date', 'avatar-date', 'thumb', 'dfrn-id',
207 'parent-guid', 'parent-network', 'parent-author-id', 'parent-author-link', 'parent-author-name',
208 'parent-author-network', 'signed_text'], Item::DISPLAY_FIELDLIST, Item::ITEM_FIELDLIST, Item::CONTENT_FIELDLIST);
211 $selected = array_merge($selected, ['internal-uri-id', 'internal-uid', 'internal-file-count']);
212 $selected = array_unique($selected);
214 return DBA::select('post-view', $selected, $condition, $params);
218 * Select rows from the post view for a given user
220 * @param integer $uid User ID
221 * @param array $selected Array of selected fields, empty for all
222 * @param array $condition Array of fields for condition
223 * @param array $params Array of several parameters
225 * @return boolean|object
228 public static function selectForUser($uid, array $selected = [], array $condition = [], $params = [])
230 if (empty($selected)) {
231 $selected = Item::DISPLAY_FIELDLIST;
234 $selected = array_unique(array_merge($selected, ['internal-uri-id', 'internal-uid', 'internal-file-count']));
236 $condition = DBA::mergeConditions($condition,
237 ["`visible` AND NOT `deleted` AND NOT `moderated`
238 AND NOT `author-blocked` AND NOT `owner-blocked`
239 AND (NOT `causer-blocked` OR `causer-id` = ?) AND NOT `contact-blocked`
240 AND ((NOT `contact-readonly` AND NOT `contact-pending` AND (`contact-rel` IN (?, ?)))
241 OR `self` OR `gravity` != ? OR `contact-uid` = ?)
242 AND NOT EXISTS (SELECT `iid` FROM `user-item` WHERE `hidden` AND `iid` = `id` AND `uid` = ?)
243 AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `author-id` AND `blocked`)
244 AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `owner-id` AND `blocked`)
245 AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `author-id` AND `ignored` AND `gravity` = ?)
246 AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `owner-id` AND `ignored` AND `gravity` = ?)",
247 0, Contact::SHARING, Contact::FRIEND, GRAVITY_PARENT, 0, $uid, $uid, $uid, $uid, GRAVITY_PARENT, $uid, GRAVITY_PARENT]);
251 if (in_array('pinned', $selected)) {
252 $selected = array_flip($selected);
253 unset($selected['pinned']);
254 $selected = array_flip($selected);
256 $select_string = '(SELECT `pinned` FROM `user-item` WHERE `iid` = `post-view`.`id` AND uid=`post-view`.`uid`) AS `pinned`, ';
259 $select_string .= implode(', ', array_map([DBA::class, 'quoteIdentifier'], $selected));
261 $condition_string = DBA::buildCondition($condition);
262 $param_string = DBA::buildParameter($params);
264 $sql = "SELECT " . $select_string . " FROM `post-view` " . $condition_string . $param_string;
265 $sql = DBA::cleanQuery($sql);
267 return DBA::p($sql, $condition);
271 * Retrieve a single record from the post view for a given user and returns it in an associative array
273 * @param integer $uid User ID
274 * @param array $selected
275 * @param array $condition
276 * @param array $params
281 public static function selectFirstForUser($uid, array $selected = [], array $condition = [], $params = [])
283 $params['limit'] = 1;
285 $result = self::selectForUser($uid, $selected, $condition, $params);
287 if (is_bool($result)) {
290 $row = self::fetch($result);