]> git.mxchange.org Git - friendica.git/blob - src/Model/Post.php
Merge pull request #13238 from annando/issue-13221
[friendica.git] / src / Model / Post.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use BadMethodCallException;
25 use Friendica\Core\Logger;
26 use Friendica\Core\System;
27 use Friendica\Database\Database;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Protocol\Activity;
31
32 class Post
33 {
34         /**
35          * Insert a new post entry
36          *
37          * @param integer $uri_id
38          * @param array   $fields
39          * @return int    ID of inserted post
40          * @throws \Exception
41          */
42         public static function insert(int $uri_id, array $data = []): int
43         {
44                 if (empty($uri_id)) {
45                         throw new BadMethodCallException('Empty URI_id');
46                 }
47
48                 $fields = DI::dbaDefinition()->truncateFieldsForTable('post', $data);
49
50                 // Additionally assign the key fields
51                 $fields['uri-id'] = $uri_id;
52
53                 if (!DBA::insert('post', $fields, Database::INSERT_IGNORE)) {
54                         return 0;
55                 }
56
57                 return DBA::lastInsertId();
58         }
59
60         /**
61          * Fetch a single post row
62          *
63          * @param mixed $stmt statement object
64          * @return array|false current row or false
65          * @throws \Exception
66          */
67         public static function fetch($stmt)
68         {
69                 $row = DBA::fetch($stmt);
70
71                 if (!is_array($row)) {
72                         return $row;
73                 }
74
75                 if (array_key_exists('verb', $row)) {
76                         if (in_array($row['verb'], Item::ACTIVITIES)) {
77                                 if (array_key_exists('title', $row)) {
78                                         $row['title'] = '';
79                                 }
80                                 if (array_key_exists('body', $row)) {
81                                         $row['body'] = $row['verb'];
82                                 }
83                                 if (array_key_exists('object', $row)) {
84                                         $row['object'] = '';
85                                 }
86                                 if (array_key_exists('object-type', $row)) {
87                                         $row['object-type'] = Activity\ObjectType::NOTE;
88                                 }
89                         } elseif (in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
90                                 // Posts don't have a target - but having tags or files.
91                                 if (array_key_exists('target', $row)) {
92                                         $row['target'] = '';
93                                 }
94                         }
95                 }
96
97                 if (array_key_exists('extid', $row) && is_null($row['extid'])) {
98                         $row['extid'] = '';
99                 }
100
101                 return $row;
102         }
103
104         /**
105          * Fills an array with data from a post query
106          *
107          * @param object|bool $stmt Return value from Database->select
108          * @return array Data array
109          * @throws \Exception
110          */
111         public static function toArray($stmt): array
112         {
113                 if (is_bool($stmt)) {
114                         return [];
115                 }
116
117                 $data = [];
118                 while ($row = self::fetch($stmt)) {
119                         $data[] = $row;
120                 }
121
122                 DBA::close($stmt);
123
124                 return $data;
125         }
126
127         /**
128          * Check if post-user-view records exists
129          *
130          * @param array $condition array of fields for condition
131          *
132          * @return boolean Are there rows for that condition?
133          * @throws \Exception
134          */
135         public static function exists(array $condition): bool
136         {
137                 return DBA::exists('post-user-view', $condition);
138         }
139
140         /**
141          * Counts the post-user-view records satisfying the provided condition
142          *
143          * @param array        $condition array of fields for condition
144          * @param array        $params    Array of several parameters
145          *
146          * @return int
147          *
148          * Example:
149          * $condition = ["uid" => 1, "network" => 'dspr'];
150          * or:
151          * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
152          *
153          * $count = Post::count($condition);
154          * @throws \Exception
155          */
156         public static function count(array $condition = [], array $params = []): int
157         {
158                 return DBA::count('post-user-view', $condition, $params);
159         }
160
161         /**
162          * Counts the post-thread-user-view records satisfying the provided condition
163          *
164          * @param array        $condition array of fields for condition
165          * @param array        $params    Array of several parameters
166          *
167          * @return int
168          *
169          * Example:
170          * $condition = ["uid" => 1, "network" => 'dspr'];
171          * or:
172          * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
173          *
174          * $count = Post::count($condition);
175          * @throws \Exception
176          */
177         public static function countThread(array $condition = [], array $params = []): int
178         {
179                 return DBA::count('post-thread-user-view', $condition, $params);
180         }
181
182         /**
183          * Counts the post-view records satisfying the provided condition
184          *
185          * @param array        $condition array of fields for condition
186          * @param array        $params    Array of several parameters
187          *
188          * @return int
189          *
190          * Example:
191          * $condition = ["network" => 'dspr'];
192          * or:
193          * $condition = ["`network` IN (?, ?)", 1, 'dfrn', 'dspr'];
194          *
195          * $count = Post::count($condition);
196          * @throws \Exception
197          */
198         public static function countPosts(array $condition = [], array $params = []): int
199         {
200                 return DBA::count('post-view', $condition, $params);
201         }
202
203         /**
204          * Retrieve a single record from the post-user-view view and returns it in an associative array
205          *
206          * @param array $fields
207          * @param array $condition
208          * @param array $params
209          * @param bool  $user_mode true = post-user-view, false = post-view
210          * @return bool|array
211          * @throws \Exception
212          * @see   DBA::select
213          */
214         public static function selectFirst(array $fields = [], array $condition = [], array $params = [])
215         {
216                 $params['limit'] = 1;
217
218                 $result = self::select($fields, $condition, $params);
219
220                 if (is_bool($result)) {
221                         return $result;
222                 } else {
223                         $row = self::fetch($result);
224                         DBA::close($result);
225                         return $row;
226                 }
227         }
228
229         /**
230          * Retrieve a single record from the post-user-view view and returns it in an associative array
231          * When the requested record is a reshare activity, the system fetches the reshared original post.
232          * Otherwise the function reacts similar to selectFirst
233          *
234          * @param array $fields
235          * @param array $condition
236          * @param array $params
237          * @param bool  $user_mode true = post-user-view, false = post-view
238          * @return bool|array
239          * @throws \Exception
240          * @see   DBA::select
241          */
242         public static function selectOriginal(array $fields = [], array $condition = [], array $params = [])
243         {
244                 $original_fields = $fields;
245                 $remove = [];
246                 if (!empty($fields)) {
247                         foreach (['gravity', 'verb', 'thr-parent-id', 'uid'] as $field) {
248                                 if (!in_array($field, $fields)) {
249                                         $fields[] = $field;
250                                         $remove[] = $field;
251                                 }
252                         }
253                 }
254                 $result = self::selectFirst($fields, $condition, $params);
255                 if (empty($result)) {
256                         return $result;
257                 }
258
259                 if (($result['gravity'] != Item::GRAVITY_ACTIVITY) || ($result['verb'] != Activity::ANNOUNCE)) {
260                         foreach ($remove as $field) {
261                                 unset($result[$field]);
262                         }
263                         return $result;
264                 }
265
266                 return self::selectFirst($original_fields, ['uri-id' => $result['thr-parent-id'], 'uid' => [0, $result['uid']]], $params);
267         }
268
269         /**
270          * Retrieve a single record from the post-view view and returns it in an associative array
271          *
272          * @param array $fields
273          * @param array $condition
274          * @param array $params
275          * @return bool|array
276          * @throws \Exception
277          * @see   DBA::select
278          */
279         public static function selectFirstPost(array $fields = [], array $condition = [], array $params = [])
280         {
281                 $params['limit'] = 1;
282
283                 $result = self::selectPosts($fields, $condition, $params);
284
285                 if (is_bool($result)) {
286                         return $result;
287                 } else {
288                         $row = self::fetch($result);
289                         DBA::close($result);
290                         return $row;
291                 }
292         }
293
294         /**
295          * Retrieve a single record from the post-thread-user-view view and returns it in an associative array
296          *
297          * @param array $fields
298          * @param array $condition
299          * @param array $params
300          * @return bool|array
301          * @throws \Exception
302          * @see   DBA::select
303          */
304         public static function selectFirstThread(array $fields = [], array $condition = [], array $params = [])
305         {
306                 $params['limit'] = 1;
307
308                 $result = self::selectThread($fields, $condition, $params);
309
310                 if (is_bool($result)) {
311                         return $result;
312                 } else {
313                         $row = self::fetch($result);
314                         DBA::close($result);
315                         return $row;
316                 }
317         }
318
319         /**
320          * Select rows from the post-user-view view and returns them as an array
321          *
322          * @param array $selected  Array of selected fields, empty for all
323          * @param array $condition Array of fields for condition
324          * @param array $params    Array of several parameters
325          *
326          * @return array
327          * @throws \Exception
328          */
329         public static function selectToArray(array $fields = [], array $condition = [], array $params = [])
330         {
331                 $result = self::select($fields, $condition, $params);
332
333                 if (is_bool($result)) {
334                         return [];
335                 }
336
337                 $data = [];
338                 while ($row = self::fetch($result)) {
339                         $data[] = $row;
340                 }
341                 DBA::close($result);
342
343                 return $data;
344         }
345
346         /**
347          * Select rows from the given view
348          *
349          * @param string $view      View (post-user-view or post-thread-user-view)
350          * @param array  $selected  Array of selected fields, empty for all
351          * @param array  $condition Array of fields for condition
352          * @param array  $params    Array of several parameters
353          *
354          * @return boolean|object
355          * @throws \Exception
356          */
357         private static function selectView(string $view, array $selected = [], array $condition = [], array $params = [])
358         {
359                 if (empty($selected)) {
360                         $selected = array_merge(Item::DISPLAY_FIELDLIST, Item::ITEM_FIELDLIST);
361
362                         if ($view == 'post-thread-user-view') {
363                                 $selected = array_merge($selected, ['ignored']);
364                         }
365                 }
366
367                 $selected = array_unique($selected);
368
369                 return DBA::select($view, $selected, $condition, $params);
370         }
371
372         /**
373          * Select rows from the post-user-view view
374          *
375          * @param array $selected  Array of selected fields, empty for all
376          * @param array $condition Array of fields for condition
377          * @param array $params    Array of several parameters
378          *
379          * @return boolean|object
380          * @throws \Exception
381          */
382         public static function select(array $selected = [], array $condition = [], array $params = [])
383         {
384                 return self::selectView('post-user-view', $selected, $condition, $params);
385         }
386
387         /**
388          * Select rows from the post-view view
389          *
390          * @param array $selected  Array of selected fields, empty for all
391          * @param array $condition Array of fields for condition
392          * @param array $params    Array of several parameters
393          *
394          * @return boolean|object
395          * @throws \Exception
396          */
397         public static function selectPosts(array $selected = [], array $condition = [], array $params = [])
398         {
399                 return self::selectView('post-view', $selected, $condition, $params);
400         }
401
402         /**
403          * Select rows from the post-thread-user-view view
404          *
405          * @param array $selected  Array of selected fields, empty for all
406          * @param array $condition Array of fields for condition
407          * @param array $params    Array of several parameters
408          *
409          * @return boolean|object
410          * @throws \Exception
411          */
412         public static function selectThread(array $selected = [], array $condition = [], array $params = [])
413         {
414                 return self::selectView('post-thread-user-view', $selected, $condition, $params);
415         }
416
417         /**
418          * Select rows from the post-thread-view view
419          *
420          * @param array $selected  Array of selected fields, empty for all
421          * @param array $condition Array of fields for condition
422          * @param array $params    Array of several parameters
423          *
424          * @return boolean|object
425          * @throws \Exception
426          */
427         public static function selectPostThread(array $selected = [], array $condition = [], array $params = [])
428         {
429                 return self::selectView('post-thread-view', $selected, $condition, $params);
430         }
431
432         /**
433          * Select rows from the given view for a given user
434          *
435          * @param string  $view      View (post-user-view or post-thread-user-view)
436          * @param integer $uid       User ID
437          * @param array   $selected  Array of selected fields, empty for all
438          * @param array   $condition Array of fields for condition
439          * @param array   $params    Array of several parameters
440          *
441          * @return boolean|object
442          * @throws \Exception
443          */
444         private static function selectViewForUser(string $view, int $uid, array $selected = [], array $condition = [], array $params = [])
445         {
446                 if (empty($selected)) {
447                         $selected = Item::DISPLAY_FIELDLIST;
448                 }
449
450                 $condition = DBA::mergeConditions($condition,
451                         ["`visible` AND NOT `deleted`
452                         AND NOT `author-blocked` AND NOT `owner-blocked`
453                         AND (NOT `causer-blocked` OR `causer-id` = ? OR `causer-id` IS NULL) AND NOT `contact-blocked`
454                         AND ((NOT `contact-readonly` AND NOT `contact-pending` AND (`contact-rel` IN (?, ?)))
455                                 OR `self` OR `contact-uid` = ?)
456                         AND NOT `" . $view . "`.`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `uid` = ? AND `hidden`)
457                         AND NOT `author-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `blocked` AND `cid` = `author-id`)
458                         AND NOT `owner-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `blocked` AND `cid` = `owner-id`)
459                         AND NOT `author-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `ignored` AND `cid` = `author-id`)
460                         AND NOT `owner-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `ignored` AND `cid` = `owner-id`)",
461                                 0, Contact::SHARING, Contact::FRIEND, 0, $uid, $uid, $uid, $uid, $uid]);
462
463                 $select_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], $selected));
464
465                 $condition_string = DBA::buildCondition($condition);
466                 $param_string     = DBA::buildParameter($params);
467
468                 $sql = "SELECT " . $select_string . " FROM `" . $view . "` " . $condition_string . $param_string;
469                 $sql = DBA::cleanQuery($sql);
470
471                 return DBA::p($sql, $condition);
472         }
473
474         /**
475          * Select rows from the post-user-view view for a given user
476          *
477          * @param integer $uid       User ID
478          * @param array   $selected  Array of selected fields, empty for all
479          * @param array   $condition Array of fields for condition
480          * @param array   $params    Array of several parameters
481          *
482          * @return boolean|object
483          * @throws \Exception
484          */
485         public static function selectForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
486         {
487                 return self::selectViewForUser('post-user-view', $uid, $selected, $condition, $params);
488         }
489
490         /**
491          * Select rows from the post-view view for a given user
492          *
493          * @param integer $uid       User ID
494          * @param array   $selected  Array of selected fields, empty for all
495          * @param array   $condition Array of fields for condition
496          * @param array   $params    Array of several parameters
497          *
498          * @return boolean|object
499          * @throws \Exception
500          */
501         public static function selectPostsForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
502         {
503                 return self::selectViewForUser('post-view', $uid, $selected, $condition, $params);
504         }
505
506         /**
507          * Select rows from the post-thread-user-view view for a given user
508          *
509          * @param integer $uid       User ID
510          * @param array   $selected  Array of selected fields, empty for all
511          * @param array   $condition Array of fields for condition
512          * @param array   $params    Array of several parameters
513          *
514          * @return boolean|object
515          * @throws \Exception
516          */
517         public static function selectThreadForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
518         {
519                 return self::selectViewForUser('post-thread-user-view', $uid, $selected, $condition, $params);
520         }
521
522         /**
523          * Retrieve a single record from the post-user-view view for a given user and returns it in an associative array
524          *
525          * @param integer $uid User ID
526          * @param array   $selected
527          * @param array   $condition
528          * @param array   $params
529          * @return bool|array
530          * @throws \Exception
531          * @see   DBA::select
532          */
533         public static function selectFirstForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
534         {
535                 $params['limit'] = 1;
536
537                 $result = self::selectForUser($uid, $selected, $condition, $params);
538
539                 if (is_bool($result)) {
540                         return $result;
541                 } else {
542                         $row = self::fetch($result);
543                         DBA::close($result);
544                         return $row;
545                 }
546         }
547
548         /**
549          * Retrieve a single record from the post-user-view view for a given user and returns it in an associative array
550          * When the requested record is a reshare activity, the system fetches the reshared original post.
551          * Otherwise the function reacts similar to selectFirstForUser
552          *
553          * @param integer $uid User ID
554          * @param array   $selected
555          * @param array   $condition
556          * @param array   $params
557          * @return bool|array
558          * @throws \Exception
559          * @see   DBA::select
560          */
561         public static function selectOriginalForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
562         {
563                 $original_selected = $selected;
564                 $remove = [];
565                 if (!empty($selected)) {
566                         foreach (['gravity', 'verb', 'thr-parent-id'] as $field) {
567                                 if (!in_array($field, $selected)) {
568                                         $selected[] = $field;
569                                         $remove[]   = $field;
570                                 }
571                         }
572                 }
573                 $result = self::selectFirstForUser($uid, $selected, $condition, $params);
574                 if (empty($result)) {
575                         return $result;
576                 }
577
578                 if (($result['gravity'] != Item::GRAVITY_ACTIVITY) || ($result['verb'] != Activity::ANNOUNCE)) {
579                         foreach ($remove as $field) {
580                                 unset($result[$field]);
581                         }
582                         return $result;
583                 }
584
585                 return self::selectFirstForUser($uid, $original_selected, ['uri-id' => $result['thr-parent-id'], 'uid' => [0, $uid]], $params);
586         }
587
588         /**
589          * Update existing post entries
590          *
591          * @param array $fields    The fields that are to be changed
592          * @param array $condition The condition for finding the item entries
593          *
594          * A return value of "0" doesn't mean an error - but that 0 rows had been changed.
595          *
596          * @return integer|boolean number of affected rows - or "false" if there was an error
597          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
598          */
599         public static function update(array $fields, array $condition)
600         {
601                 $affected = 0;
602
603                 Logger::info('Start Update', ['fields' => $fields, 'condition' => $condition, 'uid' => DI::userSession()->getLocalUserId(),'callstack' => System::callstack(10)]);
604
605                 // Don't allow changes to fields that are responsible for the relation between the records
606                 unset($fields['id']);
607                 unset($fields['parent']);
608                 unset($fields['uid']);
609                 unset($fields['uri']);
610                 unset($fields['uri-id']);
611                 unset($fields['thr-parent']);
612                 unset($fields['thr-parent-id']);
613                 unset($fields['parent-uri']);
614                 unset($fields['parent-uri-id']);
615
616                 $thread_condition = DBA::mergeConditions($condition, ['gravity' => Item::GRAVITY_PARENT]);
617
618                 // To ensure the data integrity we do it in an transaction
619                 DBA::transaction();
620
621                 $update_fields = DI::dbaDefinition()->truncateFieldsForTable('post-user', $fields);
622                 if (!empty($update_fields)) {
623                         $affected_count = 0;
624                         $posts          = DBA::select('post-user-view', ['post-user-id'], $condition);
625                         while ($rows = DBA::toArray($posts, false, 100)) {
626                                 $puids = array_column($rows, 'post-user-id');
627                                 if (!DBA::update('post-user', $update_fields, ['id' => $puids])) {
628                                         DBA::rollback();
629                                         Logger::warning('Updating post-user failed', ['fields' => $update_fields, 'condition' => $condition]);
630                                         return false;
631                                 }
632                                 $affected_count += DBA::affectedRows();
633                         }
634                         DBA::close($posts);
635                         $affected = $affected_count;
636                 }
637
638                 $update_fields = DI::dbaDefinition()->truncateFieldsForTable('post-content', $fields);
639                 if (!empty($update_fields)) {
640                         $affected_count = 0;
641                         $posts          = DBA::select('post-user-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
642                         while ($rows = DBA::toArray($posts, false, 100)) {
643                                 $uriids = array_column($rows, 'uri-id');
644                                 if (!DBA::update('post-content', $update_fields, ['uri-id' => $uriids])) {
645                                         DBA::rollback();
646                                         Logger::warning('Updating post-content failed', ['fields' => $update_fields, 'condition' => $condition]);
647                                         return false;
648                                 }
649                                 $affected_count += DBA::affectedRows();
650                         }
651                         DBA::close($posts);
652                         $affected = max($affected, $affected_count);
653                 }
654
655                 $update_fields = DI::dbaDefinition()->truncateFieldsForTable('post', $fields);
656                 if (!empty($update_fields)) {
657                         $affected_count = 0;
658                         $posts          = DBA::select('post-user-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
659                         while ($rows = DBA::toArray($posts, false, 100)) {
660                                 $uriids = array_column($rows, 'uri-id');
661
662                                 // Only delete the "post" entry when all "post-user" entries are deleted
663                                 if (!empty($update_fields['deleted']) && DBA::exists('post-user', ['uri-id' => $uriids, 'deleted' => false])) {
664                                         unset($update_fields['deleted']);
665                                 }
666
667                                 if (!DBA::update('post', $update_fields, ['uri-id' => $uriids])) {
668                                         DBA::rollback();
669                                         Logger::warning('Updating post failed', ['fields' => $update_fields, 'condition' => $condition]);
670                                         return false;
671                                 }
672                                 $affected_count += DBA::affectedRows();
673                         }
674                         DBA::close($posts);
675                         $affected = max($affected, $affected_count);
676                 }
677
678                 $update_fields = Post\DeliveryData::extractFields($fields);
679                 if (!empty($update_fields)) {
680                         $affected_count = 0;
681                         $posts          = DBA::select('post-user-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
682                         while ($rows = DBA::toArray($posts, false, 100)) {
683                                 $uriids = array_column($rows, 'uri-id');
684                                 if (!DBA::update('post-delivery-data', $update_fields, ['uri-id' => $uriids])) {
685                                         DBA::rollback();
686                                         Logger::warning('Updating post-delivery-data failed', ['fields' => $update_fields, 'condition' => $condition]);
687                                         return false;
688                                 }
689                                 $affected_count += DBA::affectedRows();
690                         }
691                         DBA::close($posts);
692                         $affected = max($affected, $affected_count);
693                 }
694
695                 $update_fields = DI::dbaDefinition()->truncateFieldsForTable('post-thread', $fields);
696                 if (!empty($update_fields)) {
697                         $affected_count = 0;
698                         $posts          = DBA::select('post-user-view', ['uri-id'], $thread_condition, ['group_by' => ['uri-id']]);
699                         while ($rows = DBA::toArray($posts, false, 100)) {
700                                 $uriids = array_column($rows, 'uri-id');
701                                 if (!DBA::update('post-thread', $update_fields, ['uri-id' => $uriids])) {
702                                         DBA::rollback();
703                                         Logger::warning('Updating post-thread failed', ['fields' => $update_fields, 'condition' => $condition]);
704                                         return false;
705                                 }
706                                 $affected_count += DBA::affectedRows();
707                         }
708                         DBA::close($posts);
709                         $affected = max($affected, $affected_count);
710                 }
711
712                 $update_fields = DI::dbaDefinition()->truncateFieldsForTable('post-thread-user', $fields);
713                 if (!empty($update_fields)) {
714                         $affected_count = 0;
715                         $posts          = DBA::select('post-user-view', ['post-user-id'], $thread_condition);
716                         while ($rows = DBA::toArray($posts, false, 100)) {
717                                 $thread_puids = array_column($rows, 'post-user-id');
718                                 if (!DBA::update('post-thread-user', $update_fields, ['post-user-id' => $thread_puids])) {
719                                         DBA::rollback();
720                                         Logger::warning('Updating post-thread-user failed', ['fields' => $update_fields, 'condition' => $condition]);
721                                         return false;
722                                 }
723                                 $affected_count += DBA::affectedRows();
724                         }
725                         DBA::close($posts);
726                         $affected = max($affected, $affected_count);
727                 }
728
729                 DBA::commit();
730
731                 Logger::info('Updated posts', ['rows' => $affected]);
732                 return $affected;
733         }
734
735         /**
736          * Delete a row from the post table
737          *
738          * @param array        $conditions Field condition(s)
739          * @param array        $options
740          *                           - cascade: If true we delete records in other tables that depend on the one we're deleting through
741          *                           relations (default: true)
742          *
743          * @return boolean was the delete successful?
744          * @throws \Exception
745          */
746         public static function delete(array $conditions, array $options = []): bool
747         {
748                 return DBA::delete('post', $conditions, $options);
749         }
750 }