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\Content\Text;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Core\Protocol;
27 use Friendica\Database\DBA;
33 * Search posts for given content
35 * @param string $search
37 * @param integer $start
38 * @param integer $limit
39 * @param integer $last_uriid
42 public static function getURIIdListBySearch(string $search, int $uid = 0, int $start = 0, int $limit = 100, int $last_uriid = 0)
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` = ?))
46 AND `uri-id` IN (SELECT `uri-id` FROM `item` WHERE `network` IN (?, ?, ?, ?))",
47 $search, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
49 if (!empty($last_uriid)) {
50 $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $last_uriid]);
54 'order' => ['uri-id' => true],
55 'group_by' => ['uri-id'],
56 'limit' => [$start, $limit]
59 $tags = DBA::select('item', ['uri-id'], $condition, $params);
62 while ($tag = DBA::fetch($tags)) {
63 $uriids[] = $tag['uri-id'];
70 public static function countBySearch(string $search, int $uid = 0)
72 $condition = ["`uri-id` IN (SELECT `uri-id` FROM `item-content` WHERE MATCH (`title`, `content-warning`, `body`) AGAINST (? IN BOOLEAN MODE))
73 AND (NOT `private` OR (`private` AND `uid` = ?))
74 AND `uri-id` IN (SELECT `uri-id` FROM `item` WHERE `network` IN (?, ?, ?, ?))",
75 $search, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
76 return DBA::count('item', $condition);
80 * Convert a message into plaintext for connectors to other networks
82 * @param array $item The message array that is about to be posted
83 * @param int $limit The maximum number of characters when posting to that network
84 * @param bool $includedlinks Has an attached link to be included into the message?
85 * @param int $htmlmode This controls the behavior of the BBCode conversion
86 * @param string $target_network Name of the network where the post should go to.
88 * @return array Same array structure than \Friendica\Content\Text\BBCode::getAttachedData
89 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
90 * @see \Friendica\Content\Text\BBCode::getAttachedData
93 public static function getPlaintextPost($item, $limit = 0, $includedlinks = false, $htmlmode = BBCode::API, $target_network = '')
96 $URLSearchString = '^\[\]';
97 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $item['body']);
99 // Add an URL element if the text contains a raw link
100 $body = preg_replace('/([^\]\=\'"]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism',
101 '$1[url]$2[/url]', $body);
103 // Remove the abstract
104 $body = Text\BBCode::stripAbstract($body);
106 // At first look at data that is attached via "type-..." stuff
107 // This will hopefully replaced with a dedicated bbcode later
108 //$post = self::getAttachedData($b['body']);
109 $post = Text\BBCode::getAttachedData($body, $item);
111 if (($item['title'] != '') && ($post['text'] != '')) {
112 $post['text'] = trim($item['title'] . "\n\n" . $post['text']);
113 } elseif ($item['title'] != '') {
114 $post['text'] = trim($item['title']);
119 // Fetch the abstract from the given target network
120 if ($target_network != '') {
121 $default_abstract = Text\BBCode::getAbstract($item['body']);
122 $abstract = Text\BBCode::getAbstract($item['body'], $target_network);
124 // If we post to a network with no limit we only fetch
125 // an abstract exactly for this network
126 if (($limit == 0) && ($abstract == $default_abstract)) {
129 } else {// Try to guess the correct target network
131 case BBCode::TWITTER:
132 $abstract = Text\BBCode::getAbstract($item['body'], Protocol::TWITTER);
135 case BBCode::OSTATUS:
136 $abstract = Text\BBCode::getAbstract($item['body'], Protocol::STATUSNET);
139 default: // We don't know the exact target.
140 // We fetch an abstract since there is a posting limit.
142 $abstract = Text\BBCode::getAbstract($item['body']);
147 if ($abstract != '') {
148 $post['text'] = $abstract;
150 if ($post['type'] == 'text') {
151 $post['type'] = 'link';
152 $post['url'] = $item['plink'];
156 $html = Text\BBCode::convert($post['text'] . ($post['after'] ?? ''), false, $htmlmode);
157 $msg = Text\HTML::toPlaintext($html, 0, true);
158 $msg = trim(html_entity_decode($msg, ENT_QUOTES, 'UTF-8'));
161 if ($includedlinks) {
162 if ($post['type'] == 'link') {
163 $link = $post['url'];
164 } elseif ($post['type'] == 'text') {
165 $link = $post['url'] ?? '';
166 } elseif ($post['type'] == 'video') {
167 $link = $post['url'];
168 } elseif ($post['type'] == 'photo') {
169 $link = $post['image'];
172 if (($msg == '') && isset($post['title'])) {
173 $msg = trim($post['title']);
176 if (($msg == '') && isset($post['description'])) {
177 $msg = trim($post['description']);
180 // If the link is already contained in the post, then it neeedn't to be added again
181 // But: if the link is beyond the limit, then it has to be added.
182 if (($link != '') && strstr($msg, $link)) {
183 $pos = strpos($msg, $link);
185 // Will the text be shortened in the link?
186 // Or is the link the last item in the post?
187 if (($limit > 0) && ($pos < $limit) && (($pos + 23 > $limit) || ($pos + strlen($link) == strlen($msg)))) {
188 $msg = trim(str_replace($link, '', $msg));
189 } elseif (($limit == 0) || ($pos < $limit)) {
190 // The limit has to be increased since it will be shortened - but not now
191 // Only do it with Twitter
192 if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == BBCode::TWITTER)) {
193 $limit = $limit - 23 + strlen($link);
198 if ($post['type'] == 'text') {
206 // Reduce multiple spaces
207 // When posted to a network with limited space, we try to gain space where possible
208 while (strpos($msg, ' ') !== false) {
209 $msg = str_replace(' ', ' ', $msg);
212 // Twitter is using its own limiter, so we always assume that shortened links will have this length
213 if (iconv_strlen($link, 'UTF-8') > 0) {
214 $limit = $limit - 23;
217 if (iconv_strlen($msg, 'UTF-8') > $limit) {
218 if (($post['type'] == 'text') && isset($post['url'])) {
219 $post['url'] = $item['plink'];
220 } elseif (!isset($post['url'])) {
221 $limit = $limit - 23;
222 $post['url'] = $item['plink'];
223 } elseif (strpos($item['body'], '[share') !== false) {
224 $post['url'] = $item['plink'];
225 } elseif (DI::pConfig()->get($item['uid'], 'system', 'no_intelligent_shortening')) {
226 $post['url'] = $item['plink'];
228 $msg = Text\Plaintext::shorten($msg, $limit);
232 $post['text'] = trim($msg);