]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/Content.php
New table "post-content"
[friendica.git] / src / Model / Post / Content.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\Post;
23
24 use \BadMethodCallException;
25 use Friendica\Core\Protocol;
26 use Friendica\Database\Database;
27 use Friendica\Database\DBA;
28 use Friendica\Database\DBStructure;
29 use Friendica\Model\Post;
30
31 class Content
32 {
33         /**
34          * Insert a new post-content entry
35          *
36          * @param integer $uri_id
37          * @param array   $fields
38          * @return bool   success
39          * @throws \Exception
40          */
41         public static function insert(int $uri_id, array $data = [])
42         {
43                 if (empty($uri_id)) {
44                         throw new BadMethodCallException('Empty URI_id');
45                 }
46
47                 if (DBA::exists('post-content', ['uri-id' => $uri_id])) {
48                         return false;
49                 }
50
51                 $fields = DBStructure::getFieldsForTable('post-content', $data);
52
53                 // Additionally assign the key fields
54                 $fields['uri-id'] = $uri_id;
55
56                 return DBA::insert('post-content', $fields, Database::INSERT_IGNORE);
57         }
58
59         /**
60          * Update a post content entry
61          *
62          * @param integer $uri_id
63          * @param array   $data
64          * @param bool    $insert_if_missing
65          * @return bool
66          * @throws \Exception
67          */
68         public static function update(int $uri_id, array $data = [], bool $insert_if_missing = false)
69         {
70                 if (empty($uri_id)) {
71                         throw new BadMethodCallException('Empty URI_id');
72                 }
73
74                 $fields = DBStructure::getFieldsForTable('post-content', $data);
75
76                 // Remove the key fields
77                 unset($fields['uri-id']);
78
79                 if (empty($fields)) {
80                         return true;
81                 }
82
83                 return DBA::update('post-content', $fields, ['uri-id' => $uri_id], $insert_if_missing ? true : []);
84         }
85
86         /**
87          * Delete a row from the post-content table
88          *
89          * @param array        $conditions Field condition(s)
90          * @param array        $options
91          *                           - cascade: If true we delete records in other tables that depend on the one we're deleting through
92          *                           relations (default: true)
93          *
94          * @return boolean was the delete successful?
95          * @throws \Exception
96          */
97         public static function delete(array $conditions, array $options = [])
98         {
99                 return DBA::delete('post-content', $conditions, $options);
100         }
101
102
103         /**
104          * Search posts for given content
105          *
106          * @param string $search
107          * @param integer $uid
108          * @param integer $start
109          * @param integer $limit
110          * @param integer $last_uriid
111          * @return array
112          */
113         public static function getURIIdListBySearch(string $search, int $uid = 0, int $start = 0, int $limit = 100, int $last_uriid = 0)
114         {
115                 $condition = ["`uri-id` IN (SELECT `uri-id` FROM `post-content` WHERE MATCH (`title`, `content-warning`, `body`) AGAINST (? IN BOOLEAN MODE))
116                         AND (NOT `private` OR (`private` AND `uid` = ?)) AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))",
117                         $search, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0];
118
119                 if (!empty($last_uriid)) {
120                         $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $last_uriid]);
121                 }
122
123                 $params = [
124                         'order' => ['uri-id' => true],
125                         'group_by' => ['uri-id'],
126                         'limit' => [$start, $limit]
127                 ];
128
129                 $tags = Post::select(['uri-id'], $condition, $params);
130
131                 $uriids = [];
132                 while ($tag = DBA::fetch($tags)) {
133                         $uriids[] = $tag['uri-id'];
134                 }
135                 DBA::close($tags);
136
137                 return $uriids;
138         }
139
140         public static function countBySearch(string $search, int $uid = 0)
141         {
142                 $condition = ["`uri-id` IN (SELECT `uri-id` FROM `post-content` WHERE MATCH (`title`, `content-warning`, `body`) AGAINST (? IN BOOLEAN MODE))
143                         AND (NOT `private` OR (`private` AND `uid` = ?)) AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))",
144                         $search, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0];
145                 return Post::count($condition);
146         }
147 }