]> git.mxchange.org Git - friendica.git/blob - src/Model/ItemContent.php
Merge pull request #9364 from annando/issue-9363
[friendica.git] / src / Model / ItemContent.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\Content\Text;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Core\Protocol;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29
30 class ItemContent
31 {
32         /**
33          * Search posts for given content
34          *
35          * @param string $search
36          * @param integer $uid
37          * @param integer $start
38          * @param integer $limit
39          * @param integer $last_uriid
40          * @return array
41          */
42         public static function getURIIdListBySearch(string $search, int $uid = 0, int $start = 0, int $limit = 100, int $last_uriid = 0)
43         {
44                 $condition = ["`uri-id` IN (SELECT `uri-id` FROM `item-content` WHERE MATCH (`title`, `content-warning`, `body`) AGAINST (? IN BOOLEAN MODE))
45                         AND (NOT `private` OR (`private` AND `uid` = ?))", $search, $uid];
46
47                 if (!empty($last_uriid)) {
48                         $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $last_uriid]);
49                 }
50
51                 $params = [
52                         'order' => ['uri-id' => true],
53                         'group_by' => ['uri-id'],
54                         'limit' => [$start, $limit]
55                 ];
56
57                 $tags = DBA::select('item', ['uri-id'], $condition, $params);
58
59                 $uriids = [];
60                 while ($tag = DBA::fetch($tags)) {
61                         $uriids[] = $tag['uri-id'];
62                 }
63                 DBA::close($tags);
64
65                 return $uriids;
66         }
67
68         public static function countBySearch(string $search, int $uid = 0)
69         {
70                 $condition = ["`uri-id` IN (SELECT `uri-id` FROM `item-content` WHERE MATCH (`title`, `content-warning`, `body`) AGAINST (? IN BOOLEAN MODE))
71                         AND (NOT `private` OR (`private` AND `uid` = ?))", $search, $uid];
72                 return DBA::count('item', $condition);
73         }
74
75         /**
76          * Convert a message into plaintext for connectors to other networks
77          *
78          * @param array  $item           The message array that is about to be posted
79          * @param int    $limit          The maximum number of characters when posting to that network
80          * @param bool   $includedlinks  Has an attached link to be included into the message?
81          * @param int    $htmlmode       This controls the behavior of the BBCode conversion
82          * @param string $target_network Name of the network where the post should go to.
83          *
84          * @return array Same array structure than \Friendica\Content\Text\BBCode::getAttachedData
85          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
86          * @see   \Friendica\Content\Text\BBCode::getAttachedData
87          *
88          */
89         public static function getPlaintextPost($item, $limit = 0, $includedlinks = false, $htmlmode = BBCode::API, $target_network = '')
90         {
91                 // Remove hashtags
92                 $URLSearchString = '^\[\]';
93                 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $item['body']);
94
95                 // Add an URL element if the text contains a raw link
96                 $body = preg_replace('/([^\]\=\'"]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism',
97                         '$1[url]$2[/url]', $body);
98
99                 // Remove the abstract
100                 $body = Text\BBCode::stripAbstract($body);
101
102                 // At first look at data that is attached via "type-..." stuff
103                 // This will hopefully replaced with a dedicated bbcode later
104                 //$post = self::getAttachedData($b['body']);
105                 $post = Text\BBCode::getAttachedData($body, $item);
106
107                 if (($item['title'] != '') && ($post['text'] != '')) {
108                         $post['text'] = trim($item['title'] . "\n\n" . $post['text']);
109                 } elseif ($item['title'] != '') {
110                         $post['text'] = trim($item['title']);
111                 }
112
113                 $abstract = '';
114
115                 // Fetch the abstract from the given target network
116                 if ($target_network != '') {
117                         $default_abstract = Text\BBCode::getAbstract($item['body']);
118                         $abstract = Text\BBCode::getAbstract($item['body'], $target_network);
119
120                         // If we post to a network with no limit we only fetch
121                         // an abstract exactly for this network
122                         if (($limit == 0) && ($abstract == $default_abstract)) {
123                                 $abstract = '';
124                         }
125                 } else {// Try to guess the correct target network
126                         switch ($htmlmode) {
127                                 case BBCode::TWITTER:
128                                         $abstract = Text\BBCode::getAbstract($item['body'], Protocol::TWITTER);
129                                         break;
130
131                                 case BBCode::OSTATUS:
132                                         $abstract = Text\BBCode::getAbstract($item['body'], Protocol::STATUSNET);
133                                         break;
134
135                                 default: // We don't know the exact target.
136                                         // We fetch an abstract since there is a posting limit.
137                                         if ($limit > 0) {
138                                                 $abstract = Text\BBCode::getAbstract($item['body']);
139                                         }
140                         }
141                 }
142
143                 if ($abstract != '') {
144                         $post['text'] = $abstract;
145
146                         if ($post['type'] == 'text') {
147                                 $post['type'] = 'link';
148                                 $post['url'] = $item['plink'];
149                         }
150                 }
151
152                 $html = Text\BBCode::convert($post['text'] . ($post['after'] ?? ''), false, $htmlmode);
153                 $msg = Text\HTML::toPlaintext($html, 0, true);
154                 $msg = trim(html_entity_decode($msg, ENT_QUOTES, 'UTF-8'));
155
156                 $link = '';
157                 if ($includedlinks) {
158                         if ($post['type'] == 'link') {
159                                 $link = $post['url'];
160                         } elseif ($post['type'] == 'text') {
161                                 $link = $post['url'] ?? '';
162                         } elseif ($post['type'] == 'video') {
163                                 $link = $post['url'];
164                         } elseif ($post['type'] == 'photo') {
165                                 $link = $post['image'];
166                         }
167
168                         if (($msg == '') && isset($post['title'])) {
169                                 $msg = trim($post['title']);
170                         }
171
172                         if (($msg == '') && isset($post['description'])) {
173                                 $msg = trim($post['description']);
174                         }
175
176                         // If the link is already contained in the post, then it neeedn't to be added again
177                         // But: if the link is beyond the limit, then it has to be added.
178                         if (($link != '') && strstr($msg, $link)) {
179                                 $pos = strpos($msg, $link);
180
181                                 // Will the text be shortened in the link?
182                                 // Or is the link the last item in the post?
183                                 if (($limit > 0) && ($pos < $limit) && (($pos + 23 > $limit) || ($pos + strlen($link) == strlen($msg)))) {
184                                         $msg = trim(str_replace($link, '', $msg));
185                                 } elseif (($limit == 0) || ($pos < $limit)) {
186                                         // The limit has to be increased since it will be shortened - but not now
187                                         // Only do it with Twitter
188                                         if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == BBCode::TWITTER)) {
189                                                 $limit = $limit - 23 + strlen($link);
190                                         }
191
192                                         $link = '';
193
194                                         if ($post['type'] == 'text') {
195                                                 unset($post['url']);
196                                         }
197                                 }
198                         }
199                 }
200
201                 if ($limit > 0) {
202                         // Reduce multiple spaces
203                         // When posted to a network with limited space, we try to gain space where possible
204                         while (strpos($msg, '  ') !== false) {
205                                 $msg = str_replace('  ', ' ', $msg);
206                         }
207
208                         // Twitter is using its own limiter, so we always assume that shortened links will have this length
209                         if (iconv_strlen($link, 'UTF-8') > 0) {
210                                 $limit = $limit - 23;
211                         }
212
213                         if (iconv_strlen($msg, 'UTF-8') > $limit) {
214                                 if (($post['type'] == 'text') && isset($post['url'])) {
215                                         $post['url'] = $item['plink'];
216                                 } elseif (!isset($post['url'])) {
217                                         $limit = $limit - 23;
218                                         $post['url'] = $item['plink'];
219                                 } elseif (strpos($item['body'], '[share') !== false) {
220                                         $post['url'] = $item['plink'];
221                                 } elseif (DI::pConfig()->get($item['uid'], 'system', 'no_intelligent_shortening')) {
222                                         $post['url'] = $item['plink'];
223                                 }
224                                 $msg = Text\Plaintext::shorten($msg, $limit);
225                         }
226                 }
227
228                 $post['text'] = trim($msg);
229
230                 return $post;
231         }
232 }