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