]> git.mxchange.org Git - friendica.git/blob - src/Model/Post.php
d29728e4901c6fa75dc4f802b32d7077bebe49ad
[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                 $stmt = self::select(['id'], $condition, ['limit' => 1]);
119
120                 if (is_bool($stmt)) {
121                         $retval = $stmt;
122                 } else {
123                         $retval = (DBA::numRows($stmt) > 0);
124                 }
125
126                 DBA::close($stmt);
127
128                 return $retval;
129         }
130
131         /**
132          * Retrieve a single record from the post table and returns it in an associative array
133          *
134          * @param array $fields
135          * @param array $condition
136          * @param array $params
137          * @return bool|array
138          * @throws \Exception
139          * @see   DBA::select
140          */
141         public static function selectFirst(array $fields = [], array $condition = [], $params = [])
142         {
143                 $params['limit'] = 1;
144
145                 $result = self::select($fields, $condition, $params);
146
147                 if (is_bool($result)) {
148                         return $result;
149                 } else {
150                         $row = self::fetch($result);
151                         DBA::close($result);
152                         return $row;
153                 }
154         }
155
156         /**
157          * Select rows from the post table and returns them as an array
158          *
159          * @param array $selected  Array of selected fields, empty for all
160          * @param array $condition Array of fields for condition
161          * @param array $params    Array of several parameters
162          *
163          * @return array
164          * @throws \Exception
165          */
166         public static function selectToArray(array $fields = [], array $condition = [], $params = [])
167         {
168                 $result = self::select($fields, $condition, $params);
169
170                 if (is_bool($result)) {
171                         return [];
172                 }
173
174                 $data = [];
175                 while ($row = self::fetch($result)) {
176                         $data[] = $row;
177                 }
178                 DBA::close($result);
179
180                 return $data;
181         }
182
183         /**
184          * Select rows from the post table
185          *
186          * @param array $selected  Array of selected fields, empty for all
187          * @param array $condition Array of fields for condition
188          * @param array $params    Array of several parameters
189          *
190          * @return boolean|object
191          * @throws \Exception
192          */
193         public static function select(array $selected = [], array $condition = [], $params = [])
194         {
195                 if (empty($selected)) {
196                         $selected = Item::DISPLAY_FIELDLIST;
197                 }
198
199                 $selected = array_merge($selected, ['internal-uri-id', 'internal-uid', 'internal-file-count']);
200                 $selected = array_unique($selected);
201
202                 return DBA::select('post-view', $selected, $condition, $params);
203         }
204 }