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\Core\Logger;
25 use Friendica\Database\DBA;
26 use Friendica\Database\DBStructure;
27 use Friendica\Protocol\Activity;
32 * Fetch a single post row
34 * @param mixed $stmt statement object
35 * @return array|false current row or false
38 public static function fetch($stmt)
40 $row = DBA::fetch($stmt);
42 if (!is_array($row)) {
46 if (array_key_exists('verb', $row)) {
47 if (in_array($row['verb'], Item::ACTIVITIES)) {
48 if (array_key_exists('title', $row)) {
51 if (array_key_exists('body', $row)) {
52 $row['body'] = $row['verb'];
54 if (array_key_exists('object', $row)) {
57 if (array_key_exists('object-type', $row)) {
58 $row['object-type'] = Activity\ObjectType::NOTE;
60 } elseif (in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
61 // Posts don't have a target - but having tags or files.
62 if (array_key_exists('target', $row)) {
72 * Fills an array with data from an post query
74 * @param object $stmt statement object
75 * @param bool $do_close
76 * @return array Data array
78 public static function toArray($stmt, $do_close = true) {
84 while ($row = self::fetch($stmt)) {
94 * Check if post data exists
96 * @param array $condition array of fields for condition
98 * @return boolean Are there rows for that condition?
101 public static function exists($condition) {
102 return DBA::exists('post-view', $condition);
106 * Counts the posts satisfying the provided condition
108 * @param array $condition array of fields for condition
109 * @param array $params Array of several parameters
114 * $condition = ["uid" => 1, "network" => 'dspr'];
116 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
118 * $count = Post::count($condition);
121 public static function count(array $condition = [], array $params = [])
123 return DBA::count('post-view', $condition, $params);
127 * Retrieve a single record from the post table and returns it in an associative array
129 * @param array $fields
130 * @param array $condition
131 * @param array $params
136 public static function selectFirst(array $fields = [], array $condition = [], $params = [])
138 $params['limit'] = 1;
140 $result = self::select($fields, $condition, $params);
142 if (is_bool($result)) {
145 $row = self::fetch($result);
152 * Select rows from the post table and returns them as an array
154 * @param array $selected Array of selected fields, empty for all
155 * @param array $condition Array of fields for condition
156 * @param array $params Array of several parameters
161 public static function selectToArray(array $fields = [], array $condition = [], $params = [])
163 $result = self::select($fields, $condition, $params);
165 if (is_bool($result)) {
170 while ($row = self::fetch($result)) {
179 * Select rows from the given view
181 * @param string $view View (post-view or post-thread-view)
182 * @param array $selected Array of selected fields, empty for all
183 * @param array $condition Array of fields for condition
184 * @param array $params Array of several parameters
186 * @return boolean|object
189 private static function selectView(string $view, array $selected = [], array $condition = [], $params = [])
191 if (empty($selected)) {
192 $selected = array_merge(['author-addr', 'author-nick', 'owner-addr', 'owner-nick', 'causer-addr', 'causer-nick',
193 'causer-network', 'photo', 'name-date', 'uri-date', 'avatar-date', 'thumb', 'dfrn-id',
194 'parent-guid', 'parent-network', 'parent-author-id', 'parent-author-link', 'parent-author-name',
195 'parent-author-network', 'signed_text'], Item::DISPLAY_FIELDLIST, Item::ITEM_FIELDLIST, Item::CONTENT_FIELDLIST);
197 if ($view == 'post-thread-view') {
198 $selected = array_merge($selected, ['ignored', 'iid']);
202 $selected = array_unique($selected);
204 return DBA::select($view, $selected, $condition, $params);
208 * Select rows from the post table
210 * @param array $selected Array of selected fields, empty for all
211 * @param array $condition Array of fields for condition
212 * @param array $params Array of several parameters
214 * @return boolean|object
217 public static function select(array $selected = [], array $condition = [], $params = [])
219 return self::selectView('post-view', $selected, $condition, $params);
223 * Select rows from the post table
225 * @param array $selected Array of selected fields, empty for all
226 * @param array $condition Array of fields for condition
227 * @param array $params Array of several parameters
229 * @return boolean|object
232 public static function selectThread(array $selected = [], array $condition = [], $params = [])
234 return self::selectView('post-thread-view', $selected, $condition, $params);
238 * Select rows from the given view for a given user
240 * @param string $view View (post-view or post-thread-view)
241 * @param integer $uid User ID
242 * @param array $selected Array of selected fields, empty for all
243 * @param array $condition Array of fields for condition
244 * @param array $params Array of several parameters
246 * @return boolean|object
249 private static function selectViewForUser(string $view, $uid, array $selected = [], array $condition = [], $params = [])
251 if (empty($selected)) {
252 $selected = Item::DISPLAY_FIELDLIST;
255 $condition = DBA::mergeConditions($condition,
256 ["`visible` AND NOT `deleted` AND NOT `moderated`
257 AND NOT `author-blocked` AND NOT `owner-blocked`
258 AND (NOT `causer-blocked` OR `causer-id` = ?) AND NOT `contact-blocked`
259 AND ((NOT `contact-readonly` AND NOT `contact-pending` AND (`contact-rel` IN (?, ?)))
260 OR `self` OR `gravity` != ? OR `contact-uid` = ?)
261 AND NOT EXISTS (SELECT `iid` FROM `user-item` WHERE `hidden` AND `iid` = `id` AND `uid` = ?)
262 AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `author-id` AND `blocked`)
263 AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `owner-id` AND `blocked`)
264 AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `author-id` AND `ignored` AND `gravity` = ?)
265 AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `owner-id` AND `ignored` AND `gravity` = ?)",
266 0, Contact::SHARING, Contact::FRIEND, GRAVITY_PARENT, 0, $uid, $uid, $uid, $uid, GRAVITY_PARENT, $uid, GRAVITY_PARENT]);
270 if (in_array('pinned', $selected)) {
271 $selected = array_flip($selected);
272 unset($selected['pinned']);
273 $selected = array_flip($selected);
275 $select_string = "(SELECT `pinned` FROM `user-item` WHERE `iid` = `" . $view . "`.`id` AND uid=`" . $view . "`.`uid`) AS `pinned`, ";
278 $select_string .= implode(', ', array_map([DBA::class, 'quoteIdentifier'], $selected));
280 $condition_string = DBA::buildCondition($condition);
281 $param_string = DBA::buildParameter($params);
283 $sql = "SELECT " . $select_string . " FROM `" . $view . "` " . $condition_string . $param_string;
284 $sql = DBA::cleanQuery($sql);
286 return DBA::p($sql, $condition);
290 * Select rows from the post view for a given user
292 * @param integer $uid User ID
293 * @param array $selected Array of selected fields, empty for all
294 * @param array $condition Array of fields for condition
295 * @param array $params Array of several parameters
297 * @return boolean|object
300 public static function selectForUser($uid, array $selected = [], array $condition = [], $params = [])
302 return self::selectViewForUser('post-view', $uid, $selected, $condition, $params);
306 * Select rows from the post view for a given user
308 * @param integer $uid User ID
309 * @param array $selected Array of selected fields, empty for all
310 * @param array $condition Array of fields for condition
311 * @param array $params Array of several parameters
313 * @return boolean|object
316 public static function selectThreadForUser($uid, array $selected = [], array $condition = [], $params = [])
318 return self::selectViewForUser('post-thread-view', $uid, $selected, $condition, $params);
322 * Retrieve a single record from the post view for a given user and returns it in an associative array
324 * @param integer $uid User ID
325 * @param array $selected
326 * @param array $condition
327 * @param array $params
332 public static function selectFirstForUser($uid, array $selected = [], array $condition = [], $params = [])
334 $params['limit'] = 1;
336 $result = self::selectForUser($uid, $selected, $condition, $params);
338 if (is_bool($result)) {
341 $row = self::fetch($result);
348 * Retrieve a single record from the starting post in the item table and returns it in an associative array
350 * @param integer $uid User ID
351 * @param array $selected
352 * @param array $condition
353 * @param array $params
358 public static function selectFirstThreadForUser($uid, array $selected = [], array $condition = [], $params = [])
360 $params['limit'] = 1;
362 $result = self::selectThreadForUser($uid, $selected, $condition, $params);
364 if (is_bool($result)) {
367 $row = self::fetch($result);
374 * Select pinned rows from the item table for a given user
376 * @param integer $uid User ID
377 * @param array $selected Array of selected fields, empty for all
378 * @param array $condition Array of fields for condition
379 * @param array $params Array of several parameters
381 * @return boolean|object
384 public static function selectPinned(int $uid, array $selected = [], array $condition = [], $params = [])
386 $useritems = DBA::select('user-item', ['iid'], ['uid' => $uid, 'pinned' => true]);
387 if (!DBA::isResult($useritems)) {
392 while ($useritem = DBA::fetch($useritems)) {
393 $pinned[] = $useritem['iid'];
395 DBA::close($useritems);
397 if (empty($pinned)) {
401 $condition = DBA::mergeConditions(['iid' => $pinned], $condition);
403 return self::selectThreadForUser($uid, $selected, $condition, $params);
407 * Update existing post entries
409 * @param array $fields The fields that are to be changed
410 * @param array $condition The condition for finding the item entries
412 * A return value of "0" doesn't mean an error - but that 0 rows had been changed.
414 * @return integer|boolean number of affected rows - or "false" if there was an error
415 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
417 public static function update(array $fields, array $condition)
421 Logger::info('Start Update', ['fields' => $fields, 'condition' => $condition]);
423 // Don't allow changes to fields that are responsible for the relation between the records
424 unset($fields['id']);
425 unset($fields['parent']);
426 unset($fields['uid']);
427 unset($fields['uri']);
428 unset($fields['uri-id']);
429 unset($fields['thr-parent']);
430 unset($fields['thr-parent-id']);
431 unset($fields['parent-uri']);
432 unset($fields['parent-uri-id']);
434 // To ensure the data integrity we do it in an transaction
437 $update_fields = DBStructure::getFieldsForTable('post-user', $fields);
438 if (!empty($update_fields)) {
439 $rows = DBA::selectToArray('post-view', ['post-user-id'], $condition);
440 $puids = array_column($rows, 'post-user-id');
441 if (!DBA::update('post-user', $update_fields, ['id' => $puids])) {
443 Logger::notice('Updating post-user failed', ['fields' => $update_fields, 'condition' => $condition]);
446 $affected = DBA::affectedRows();
449 $update_fields = DBStructure::getFieldsForTable('item-content', $fields);
450 if (!empty($update_fields)) {
451 $rows = DBA::selectToArray('post-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
452 $uriids = array_column($rows, 'uri-id');
453 if (!DBA::update('item-content', $update_fields, ['uri-id' => $uriids])) {
455 Logger::notice('Updating item-content failed', ['fields' => $update_fields, 'condition' => $condition]);
458 $affected = max($affected, DBA::affectedRows());
461 $update_fields = Post\DeliveryData::extractFields($fields);
462 if (!empty($update_fields)) {
463 if (empty($uriids)) {
464 $rows = DBA::selectToArray('post-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
465 $uriids = array_column($rows, 'uri-id');
467 if (!DBA::update('post-delivery-data', $update_fields, ['uri-id' => $uriids])) {
469 Logger::notice('Updating post-delivery-data failed', ['fields' => $update_fields, 'condition' => $condition]);
472 $affected = max($affected, DBA::affectedRows());
475 $update_fields = DBStructure::getFieldsForTable('thread', $fields);
476 if (!empty($update_fields)) {
477 $rows = DBA::selectToArray('post-view', ['id'], $condition);
478 $ids = array_column($rows, 'id');
479 if (!DBA::update('thread', $update_fields, ['iid' => $ids])) {
481 Logger::notice('Updating thread failed', ['fields' => $update_fields, 'condition' => $condition]);
484 $affected = max($affected, DBA::affectedRows());
487 $item_fields = ['guid', 'type', 'wall', 'gravity', 'extid', 'created', 'edited', 'commented', 'received', 'changed',
488 'resource-id', 'post-type', 'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark',
489 'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'global', 'network', 'vid', 'psid',
490 'contact-id', 'author-id', 'owner-id', 'causer-id', 'event-id'];
493 foreach ($item_fields as $field) {
494 if (array_key_exists($field, $fields)) {
495 $update_fields[$field] = $fields[$field];
498 if (!empty($update_fields)) {
500 $rows = DBA::selectToArray('post-view', ['id'], $condition, []);
501 $ids = array_column($rows, 'id');
503 if (!DBA::update('item', $update_fields, ['id' => $ids])) {
505 Logger::notice('Updating item failed', ['fields' => $update_fields, 'condition' => $condition]);
508 $affected = max($affected, DBA::affectedRows());
513 Logger::info('Updated posts', ['rows' => $affected]);