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