3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Model;
24 use Friendica\Core\Protocol;
25 use Friendica\Database\DBA;
26 use Friendica\Protocol\Activity;
27 use Friendica\Repository\PermissionSet;
32 * Fetch a single post row
34 * @param mixed $stmt statement object
35 * @return array|false current row or false
38 public static function fetch($stmt)
40 $row = DBA::fetch($stmt);
42 if (!is_array($row)) {
46 if (array_key_exists('verb', $row)) {
47 if (in_array($row['verb'], Item::ACTIVITIES)) {
48 if (array_key_exists('title', $row)) {
51 if (array_key_exists('body', $row)) {
52 $row['body'] = $row['verb'];
54 if (array_key_exists('object', $row)) {
57 if (array_key_exists('object-type', $row)) {
58 $row['object-type'] = Activity\ObjectType::NOTE;
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)) {
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']);
79 // Remove internal fields
80 unset($row['internal-file-count']);
81 unset($row['internal-uri-id']);
82 unset($row['internal-uid']);
88 * Fills an array with data from an post query
90 * @param object $stmt statement object
91 * @param bool $do_close
92 * @return array Data array
94 public static function inArray($stmt, $do_close = true) {
100 while ($row = self::fetch($stmt)) {
110 * Check if post data exists
112 * @param array $condition array of fields for condition
114 * @return boolean Are there rows for that condition?
117 public static function exists($condition) {
118 return DBA::exists('post-view', $condition);
122 * Counts the posts satisfying the provided condition
124 * @param array $condition array of fields for condition
125 * @param array $params Array of several parameters
130 * $condition = ["uid" => 1, "network" => 'dspr'];
132 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
134 * $count = Post::count($condition);
137 public static function count(array $condition = [], array $params = [])
139 return DBA::count('post-view', $condition, $params);
143 * Retrieve a single record from the post table and returns it in an associative array
145 * @param array $fields
146 * @param array $condition
147 * @param array $params
152 public static function selectFirst(array $fields = [], array $condition = [], $params = [])
154 $params['limit'] = 1;
156 $result = self::select($fields, $condition, $params);
158 if (is_bool($result)) {
161 $row = self::fetch($result);
168 * Select rows from the post table and returns them as an array
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
177 public static function selectToArray(array $fields = [], array $condition = [], $params = [])
179 $result = self::select($fields, $condition, $params);
181 if (is_bool($result)) {
186 while ($row = self::fetch($result)) {
195 * Select rows from the post table
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
201 * @return boolean|object
204 public static function select(array $selected = [], array $condition = [], $params = [])
206 if (empty($selected)) {
207 $selected = array_merge(['author-addr', 'author-nick', 'owner-addr', 'owner-nick', 'causer-addr', 'causer-nick',
208 'causer-network', 'photo', 'name-date', 'uri-date', 'avatar-date', 'thumb', 'dfrn-id',
209 'parent-guid', 'parent-network', 'parent-author-id', 'parent-author-link', 'parent-author-name',
210 'parent-author-network', 'signed_text'], Item::DISPLAY_FIELDLIST, Item::ITEM_FIELDLIST, Item::CONTENT_FIELDLIST);
213 $selected = array_merge($selected, ['internal-uri-id', 'internal-uid', 'internal-file-count']);
214 $selected = array_unique($selected);
216 return DBA::select('post-view', $selected, $condition, $params);