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', 'language', 'raw-body'], Item::DISPLAY_FIELDLIST, Item::ITEM_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 `uri-id` FROM `post-user` WHERE `hidden` AND `uri-id` = `" . $view . "`.`uri-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 `post-thread-user` WHERE `uri-id` = `" . $view . "`.`uri-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 * Select pinned rows from the item table for a given user
350 * @param integer $uid User ID
351 * @param array $selected Array of selected fields, empty for all
352 * @param array $condition Array of fields for condition
353 * @param array $params Array of several parameters
355 * @return boolean|object
358 public static function selectPinned(int $uid, array $selected = [], array $condition = [], $params = [])
360 $postthreaduser = DBA::select('post-thread-user', ['uri-id'], ['uid' => $uid, 'pinned' => true]);
361 if (!DBA::isResult($postthreaduser)) {
362 return $postthreaduser;
366 while ($useritem = DBA::fetch($postthreaduser)) {
367 $pinned[] = $useritem['uri-id'];
369 DBA::close($postthreaduser);
371 if (empty($pinned)) {
375 $condition = DBA::mergeConditions(['uri-id' => $pinned, 'uid' => $uid, 'gravity' => GRAVITY_PARENT], $condition);
377 return self::selectForUser($uid, $selected, $condition, $params);
381 * Update existing post entries
383 * @param array $fields The fields that are to be changed
384 * @param array $condition The condition for finding the item entries
386 * A return value of "0" doesn't mean an error - but that 0 rows had been changed.
388 * @return integer|boolean number of affected rows - or "false" if there was an error
389 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
391 public static function update(array $fields, array $condition)
395 Logger::info('Start Update', ['fields' => $fields, 'condition' => $condition]);
397 // Don't allow changes to fields that are responsible for the relation between the records
398 unset($fields['id']);
399 unset($fields['parent']);
400 unset($fields['uid']);
401 unset($fields['uri']);
402 unset($fields['uri-id']);
403 unset($fields['thr-parent']);
404 unset($fields['thr-parent-id']);
405 unset($fields['parent-uri']);
406 unset($fields['parent-uri-id']);
408 $thread_condition = DBA::mergeConditions($condition, ['gravity' => GRAVITY_PARENT]);
410 // To ensure the data integrity we do it in an transaction
413 $update_fields = DBStructure::getFieldsForTable('post-user', $fields);
414 if (!empty($update_fields)) {
415 $rows = DBA::selectToArray('post-view', ['post-user-id'], $condition);
416 $puids = array_column($rows, 'post-user-id');
417 if (!DBA::update('post-user', $update_fields, ['id' => $puids])) {
419 Logger::notice('Updating post-user failed', ['fields' => $update_fields, 'condition' => $condition]);
422 $affected = DBA::affectedRows();
425 $update_fields = DBStructure::getFieldsForTable('post-content', $fields);
426 if (!empty($update_fields)) {
427 $rows = DBA::selectToArray('post-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
428 $uriids = array_column($rows, 'uri-id');
429 if (!DBA::update('post-content', $update_fields, ['uri-id' => $uriids])) {
431 Logger::notice('Updating post-content failed', ['fields' => $update_fields, 'condition' => $condition]);
434 $affected = max($affected, DBA::affectedRows());
437 $update_fields = Post\DeliveryData::extractFields($fields);
438 if (!empty($update_fields)) {
439 if (empty($uriids)) {
440 $rows = DBA::selectToArray('post-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
441 $uriids = array_column($rows, 'uri-id');
443 if (!DBA::update('post-delivery-data', $update_fields, ['uri-id' => $uriids])) {
445 Logger::notice('Updating post-delivery-data failed', ['fields' => $update_fields, 'condition' => $condition]);
448 $affected = max($affected, DBA::affectedRows());
451 $update_fields = DBStructure::getFieldsForTable('post-thread-user', $fields);
452 if (!empty($update_fields)) {
453 $rows = DBA::selectToArray('post-view', ['post-user-id'], $thread_condition);
454 $thread_puids = array_column($rows, 'post-user-id');
456 $post_thread_condition = DBA::collapseCondition(['id' => $thread_puids]);
458 $post_thread_condition[0] = "EXISTS(SELECT `id` FROM `post-user` WHERE " .
459 $post_thread_condition[0] . " AND `uri-id` = `post-thread-user`.`uri-id` AND `uid` = `post-thread-user`.`uid`)";
460 if (!DBA::update('post-thread-user', $update_fields, $post_thread_condition)) {
462 Logger::notice('Updating post-thread-user failed', ['fields' => $update_fields, 'condition' => $condition]);
465 $affected = max($affected, DBA::affectedRows());
468 $update_fields = DBStructure::getFieldsForTable('thread', $fields);
469 if (!empty($update_fields)) {
470 $rows = DBA::selectToArray('post-view', ['id'], $thread_condition);
471 $ids = array_column($rows, 'id');
472 if (!DBA::update('thread', $update_fields, ['iid' => $ids])) {
474 Logger::notice('Updating thread failed', ['fields' => $update_fields, 'condition' => $thread_condition]);
477 $affected = max($affected, DBA::affectedRows());
481 foreach (Item::USED_FIELDLIST as $field) {
482 if (array_key_exists($field, $fields)) {
483 $update_fields[$field] = $fields[$field];
486 if (!empty($update_fields)) {
487 $rows = DBA::selectToArray('post-view', ['id'], $condition, []);
488 $ids = array_column($rows, 'id');
489 if (!DBA::update('item', $update_fields, ['id' => $ids])) {
491 Logger::notice('Updating item failed', ['fields' => $update_fields, 'condition' => $condition]);
494 $affected = max($affected, DBA::affectedRows());
499 Logger::info('Updated posts', ['rows' => $affected]);