Replace legacy file/category handling
[friendica.git/.git] / src / Model / Post.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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 Friendica\Database\DBA;
25 use Friendica\Protocol\Activity;
26
27 class Post
28 {
29         /**
30          * Fetch a single post row
31          *
32          * @param mixed $stmt statement object
33          * @return array|false current row or false
34          * @throws \Exception
35          */
36         public static function fetch($stmt)
37         {
38                 $row = DBA::fetch($stmt);
39
40                 if (!is_array($row)) {
41                         return $row;
42                 }
43
44                 if (array_key_exists('verb', $row)) {
45                         if (in_array($row['verb'], Item::ACTIVITIES)) {
46                                 if (array_key_exists('title', $row)) {
47                                         $row['title'] = '';
48                                 }
49                                 if (array_key_exists('body', $row)) {
50                                         $row['body'] = $row['verb'];
51                                 }
52                                 if (array_key_exists('object', $row)) {
53                                         $row['object'] = '';
54                                 }
55                                 if (array_key_exists('object-type', $row)) {
56                                         $row['object-type'] = Activity\ObjectType::NOTE;
57                                 }
58                         } elseif (in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
59                                 // Posts don't have a target - but having tags or files.
60                                 if (array_key_exists('target', $row)) {
61                                         $row['target'] = '';
62                                 }
63                         }
64                 }
65
66                 return $row;
67         }
68
69         /**
70          * Fills an array with data from an post query
71          *
72          * @param object $stmt statement object
73          * @param bool   $do_close
74          * @return array Data array
75          */
76         public static function toArray($stmt, $do_close = true) {
77                 if (is_bool($stmt)) {
78                         return $stmt;
79                 }
80
81                 $data = [];
82                 while ($row = self::fetch($stmt)) {
83                         $data[] = $row;
84                 }
85                 if ($do_close) {
86                         DBA::close($stmt);
87                 }
88                 return $data;
89         }
90
91         /**
92          * Check if post data exists
93          *
94          * @param array $condition array of fields for condition
95          *
96          * @return boolean Are there rows for that condition?
97          * @throws \Exception
98          */
99         public static function exists($condition) {
100                 return DBA::exists('post-view', $condition);
101         }
102
103         /**
104          * Counts the posts satisfying the provided condition
105          *
106          * @param array        $condition array of fields for condition
107          * @param array        $params    Array of several parameters
108          *
109          * @return int
110          *
111          * Example:
112          * $condition = ["uid" => 1, "network" => 'dspr'];
113          * or:
114          * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
115          *
116          * $count = Post::count($condition);
117          * @throws \Exception
118          */
119         public static function count(array $condition = [], array $params = [])
120         {
121                 return DBA::count('post-view', $condition, $params);
122         }
123
124         /**
125          * Retrieve a single record from the post table and returns it in an associative array
126          *
127          * @param array $fields
128          * @param array $condition
129          * @param array $params
130          * @return bool|array
131          * @throws \Exception
132          * @see   DBA::select
133          */
134         public static function selectFirst(array $fields = [], array $condition = [], $params = [])
135         {
136                 $params['limit'] = 1;
137
138                 $result = self::select($fields, $condition, $params);
139
140                 if (is_bool($result)) {
141                         return $result;
142                 } else {
143                         $row = self::fetch($result);
144                         DBA::close($result);
145                         return $row;
146                 }
147         }
148
149         /**
150          * Select rows from the post table and returns them as an array
151          *
152          * @param array $selected  Array of selected fields, empty for all
153          * @param array $condition Array of fields for condition
154          * @param array $params    Array of several parameters
155          *
156          * @return array
157          * @throws \Exception
158          */
159         public static function selectToArray(array $fields = [], array $condition = [], $params = [])
160         {
161                 $result = self::select($fields, $condition, $params);
162
163                 if (is_bool($result)) {
164                         return [];
165                 }
166
167                 $data = [];
168                 while ($row = self::fetch($result)) {
169                         $data[] = $row;
170                 }
171                 DBA::close($result);
172
173                 return $data;
174         }
175
176         /**
177          * Select rows from the given view
178          *
179          * @param string $view      View (post-view or post-thread-view)
180          * @param array  $selected  Array of selected fields, empty for all
181          * @param array  $condition Array of fields for condition
182          * @param array  $params    Array of several parameters
183          *
184          * @return boolean|object
185          * @throws \Exception
186          */
187         private static function selectView(string $view, array $selected = [], array $condition = [], $params = [])
188         {
189                 if (empty($selected)) {
190                         $selected = array_merge(['author-addr', 'author-nick', 'owner-addr', 'owner-nick', 'causer-addr', 'causer-nick',
191                                 'causer-network', 'photo', 'name-date', 'uri-date', 'avatar-date', 'thumb', 'dfrn-id',
192                                 'parent-guid', 'parent-network', 'parent-author-id', 'parent-author-link', 'parent-author-name',
193                                 'parent-author-network', 'signed_text'], Item::DISPLAY_FIELDLIST, Item::ITEM_FIELDLIST, Item::CONTENT_FIELDLIST);
194                         
195                         if ($view == 'post-thread-view') {
196                                 $selected = array_merge($selected, ['ignored', 'iid']);
197                         }
198                 }
199
200                 $selected = array_unique($selected);
201
202                 return DBA::select($view, $selected, $condition, $params);
203         }
204
205         /**
206          * Select rows from the post table
207          *
208          * @param array $selected  Array of selected fields, empty for all
209          * @param array $condition Array of fields for condition
210          * @param array $params    Array of several parameters
211          *
212          * @return boolean|object
213          * @throws \Exception
214          */
215         public static function select(array $selected = [], array $condition = [], $params = [])
216         {
217                 return self::selectView('post-view', $selected, $condition, $params);
218         }
219
220         /**
221          * Select rows from the post table
222          *
223          * @param array $selected  Array of selected fields, empty for all
224          * @param array $condition Array of fields for condition
225          * @param array $params    Array of several parameters
226          *
227          * @return boolean|object
228          * @throws \Exception
229          */
230         public static function selectThread(array $selected = [], array $condition = [], $params = [])
231         {
232                 return self::selectView('post-thread-view', $selected, $condition, $params);
233         }
234
235         /**
236          * Select rows from the given view for a given user
237          *
238          * @param string  $view      View (post-view or post-thread-view)
239          * @param integer $uid       User ID
240          * @param array   $selected  Array of selected fields, empty for all
241          * @param array   $condition Array of fields for condition
242          * @param array   $params    Array of several parameters
243          *
244          * @return boolean|object
245          * @throws \Exception
246          */
247         private static function selectViewForUser(string $view, $uid, array $selected = [], array $condition = [], $params = [])
248         {
249                 if (empty($selected)) {
250                         $selected = Item::DISPLAY_FIELDLIST;
251                 }
252
253                 $condition = DBA::mergeConditions($condition,
254                         ["`visible` AND NOT `deleted` AND NOT `moderated`
255                         AND NOT `author-blocked` AND NOT `owner-blocked`
256                         AND (NOT `causer-blocked` OR `causer-id` = ?) AND NOT `contact-blocked`
257                         AND ((NOT `contact-readonly` AND NOT `contact-pending` AND (`contact-rel` IN (?, ?)))
258                                 OR `self` OR `gravity` != ? OR `contact-uid` = ?)
259                         AND NOT EXISTS (SELECT `iid` FROM `user-item` WHERE `hidden` AND `iid` = `id` AND `uid` = ?)
260                         AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `author-id` AND `blocked`)
261                         AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `owner-id` AND `blocked`)
262                         AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `author-id` AND `ignored` AND `gravity` = ?)
263                         AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `owner-id` AND `ignored` AND `gravity` = ?)",
264                         0, Contact::SHARING, Contact::FRIEND, GRAVITY_PARENT, 0, $uid, $uid, $uid, $uid, GRAVITY_PARENT, $uid, GRAVITY_PARENT]);
265
266                 $select_string = '';
267
268                 if (in_array('pinned', $selected)) {
269                         $selected = array_flip($selected);
270                         unset($selected['pinned']);
271                         $selected = array_flip($selected);      
272
273                         $select_string = "(SELECT `pinned` FROM `user-item` WHERE `iid` = `" . $view . "`.`id` AND uid=`" . $view . "`.`uid`) AS `pinned`, ";
274                 }
275
276                 $select_string .= implode(', ', array_map([DBA::class, 'quoteIdentifier'], $selected));
277
278                 $condition_string = DBA::buildCondition($condition);
279                 $param_string = DBA::buildParameter($params);
280
281                 $sql = "SELECT " . $select_string . " FROM `" . $view . "` " . $condition_string . $param_string;
282                 $sql = DBA::cleanQuery($sql);
283
284                 return DBA::p($sql, $condition);
285         }
286
287         /**
288          * Select rows from the post view for a given user
289          *
290          * @param integer $uid       User ID
291          * @param array   $selected  Array of selected fields, empty for all
292          * @param array   $condition Array of fields for condition
293          * @param array   $params    Array of several parameters
294          *
295          * @return boolean|object
296          * @throws \Exception
297          */
298         public static function selectForUser($uid, array $selected = [], array $condition = [], $params = [])
299         {
300                 return self::selectViewForUser('post-view', $uid, $selected, $condition, $params);
301         }
302
303                 /**
304          * Select rows from the post view for a given user
305          *
306          * @param integer $uid       User ID
307          * @param array   $selected  Array of selected fields, empty for all
308          * @param array   $condition Array of fields for condition
309          * @param array   $params    Array of several parameters
310          *
311          * @return boolean|object
312          * @throws \Exception
313          */
314         public static function selectThreadForUser($uid, array $selected = [], array $condition = [], $params = [])
315         {
316                 return self::selectViewForUser('post-thread-view', $uid, $selected, $condition, $params);
317         }
318
319         /**
320          * Retrieve a single record from the post view for a given user and returns it in an associative array
321          *
322          * @param integer $uid User ID
323          * @param array   $selected
324          * @param array   $condition
325          * @param array   $params
326          * @return bool|array
327          * @throws \Exception
328          * @see   DBA::select
329          */
330         public static function selectFirstForUser($uid, array $selected = [], array $condition = [], $params = [])
331         {
332                 $params['limit'] = 1;
333
334                 $result = self::selectForUser($uid, $selected, $condition, $params);
335
336                 if (is_bool($result)) {
337                         return $result;
338                 } else {
339                         $row = self::fetch($result);
340                         DBA::close($result);
341                         return $row;
342                 }
343         }
344
345         /**
346          * Retrieve a single record from the starting post in the item table and returns it in an associative array
347          *
348          * @param integer $uid User ID
349          * @param array   $selected
350          * @param array   $condition
351          * @param array   $params
352          * @return bool|array
353          * @throws \Exception
354          * @see   DBA::select
355          */
356         public static function selectFirstThreadForUser($uid, array $selected = [], array $condition = [], $params = [])
357         {
358                 $params['limit'] = 1;
359
360                 $result = self::selectThreadForUser($uid, $selected, $condition, $params);
361
362                 if (is_bool($result)) {
363                         return $result;
364                 } else {
365                         $row = self::fetch($result);
366                         DBA::close($result);
367                         return $row;
368                 }
369         }
370
371         /**
372          * Select pinned rows from the item table for a given user
373          *
374          * @param integer $uid       User ID
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 selectPinned(int $uid, array $selected = [], array $condition = [], $params = [])
383         {
384                 $useritems = DBA::select('user-item', ['iid'], ['uid' => $uid, 'pinned' => true]);
385                 if (!DBA::isResult($useritems)) {
386                         return $useritems;
387                 }
388
389                 $pinned = [];
390                 while ($useritem = DBA::fetch($useritems)) {
391                         $pinned[] = $useritem['iid'];
392                 }
393                 DBA::close($useritems);
394
395                 if (empty($pinned)) {
396                         return [];
397                 }
398
399                 $condition = DBA::mergeConditions(['iid' => $pinned], $condition);
400
401                 return self::selectThreadForUser($uid, $selected, $condition, $params);
402         }
403 }