]> git.mxchange.org Git - friendica.git/blob - src/Model/Post.php
85f69826c43602196b5480bacf8c9c672afebcad
[friendica.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\Core\Protocol;
25 use Friendica\Database\DBA;
26 use Friendica\Protocol\Activity;
27 use Friendica\Repository\PermissionSet;
28
29 class Post
30 {
31         /**
32          * Fetch a single post row
33          *
34          * @param mixed $stmt statement object
35          * @return array|false current row or false
36          * @throws \Exception
37          */
38         public static function fetch($stmt)
39         {
40                 $row = DBA::fetch($stmt);
41
42                 if (!is_array($row)) {
43                         return $row;
44                 }
45
46                 if (array_key_exists('verb', $row)) {
47                         if (in_array($row['verb'], Item::ACTIVITIES)) {
48                                 if (array_key_exists('title', $row)) {
49                                         $row['title'] = '';
50                                 }
51                                 if (array_key_exists('body', $row)) {
52                                         $row['body'] = $row['verb'];
53                                 }
54                                 if (array_key_exists('object', $row)) {
55                                         $row['object'] = '';
56                                 }
57                                 if (array_key_exists('object-type', $row)) {
58                                         $row['object-type'] = Activity\ObjectType::NOTE;
59                                 }
60                         } elseif (in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
61                                 // Posts don't have a target - but having tags or files.
62                                 if (array_key_exists('target', $row)) {
63                                         $row['target'] = '';
64                                 }
65                         }
66                 }
67
68                 if (!array_key_exists('verb', $row) || in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
69                         // Build the file string out of the term entries
70                         if (array_key_exists('file', $row)) {
71                                 if ($row['internal-file-count'] > 0) {
72                                         $row['file'] = Post\Category::getTextByURIId($row['internal-uri-id'], $row['internal-uid']);
73                                 } else {
74                                         $row['file'] = '';
75                                 }
76                         }
77                 }
78
79                 // Remove internal fields
80                 unset($row['internal-file-count']);
81                 unset($row['internal-uri-id']);
82                 unset($row['internal-uid']);
83
84                 return $row;
85         }
86
87         /**
88          * Fills an array with data from an post query
89          *
90          * @param object $stmt statement object
91          * @param bool   $do_close
92          * @return array Data array
93          */
94         public static function inArray($stmt, $do_close = true) {
95                 if (is_bool($stmt)) {
96                         return $stmt;
97                 }
98
99                 $data = [];
100                 while ($row = self::fetch($stmt)) {
101                         $data[] = $row;
102                 }
103                 if ($do_close) {
104                         DBA::close($stmt);
105                 }
106                 return $data;
107         }
108
109         /**
110          * Check if post data exists
111          *
112          * @param array $condition array of fields for condition
113          *
114          * @return boolean Are there rows for that condition?
115          * @throws \Exception
116          */
117         public static function exists($condition) {
118                 return DBA::exists('post-view', $condition);
119         }
120
121         /**
122          * Counts the posts satisfying the provided condition
123          *
124          * @param array        $condition array of fields for condition
125          * @param array        $params    Array of several parameters
126          *
127          * @return int
128          *
129          * Example:
130          * $condition = ["uid" => 1, "network" => 'dspr'];
131          * or:
132          * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
133          *
134          * $count = Post::count($condition);
135          * @throws \Exception
136          */
137         public static function count(array $condition = [], array $params = [])
138         {
139                 return DBA::count('post-view', $condition, $params);
140         }
141
142         /**
143          * Retrieve a single record from the post table and returns it in an associative array
144          *
145          * @param array $fields
146          * @param array $condition
147          * @param array $params
148          * @return bool|array
149          * @throws \Exception
150          * @see   DBA::select
151          */
152         public static function selectFirst(array $fields = [], array $condition = [], $params = [])
153         {
154                 $params['limit'] = 1;
155
156                 $result = self::select($fields, $condition, $params);
157
158                 if (is_bool($result)) {
159                         return $result;
160                 } else {
161                         $row = self::fetch($result);
162                         DBA::close($result);
163                         return $row;
164                 }
165         }
166
167         /**
168          * Select rows from the post table and returns them as an array
169          *
170          * @param array $selected  Array of selected fields, empty for all
171          * @param array $condition Array of fields for condition
172          * @param array $params    Array of several parameters
173          *
174          * @return array
175          * @throws \Exception
176          */
177         public static function selectToArray(array $fields = [], array $condition = [], $params = [])
178         {
179                 $result = self::select($fields, $condition, $params);
180
181                 if (is_bool($result)) {
182                         return [];
183                 }
184
185                 $data = [];
186                 while ($row = self::fetch($result)) {
187                         $data[] = $row;
188                 }
189                 DBA::close($result);
190
191                 return $data;
192         }
193
194         /**
195          * Select rows from the post table
196          *
197          * @param array $selected  Array of selected fields, empty for all
198          * @param array $condition Array of fields for condition
199          * @param array $params    Array of several parameters
200          *
201          * @return boolean|object
202          * @throws \Exception
203          */
204         public static function select(array $selected = [], array $condition = [], $params = [])
205         {
206                 if (empty($selected)) {
207                         $selected = Item::DISPLAY_FIELDLIST;
208                 }
209
210                 $selected = array_merge($selected, ['internal-uri-id', 'internal-uid', 'internal-file-count']);
211                 $selected = array_unique($selected);
212
213                 return DBA::select('post-view', $selected, $condition, $params);
214         }
215 }