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