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\BBCode;
25 use Friendica\Core\Logger;
26 use Friendica\Core\System;
27 use Friendica\Database\DBA;
29 use Friendica\Util\Strings;
34 * This Model class handles tag table interactions.
35 * This tables stores relevant tags related to posts, like hashtags and mentions.
45 * An implicit mention is a mention in a comment body that is redundant with the threading information.
47 const IMPLICIT_MENTION = 8;
49 * An exclusive mention transfers the ownership of the post to the target account, usually a forum.
51 const EXCLUSIVE_MENTION = 9;
53 const TAG_CHARACTER = [
56 self::IMPLICIT_MENTION => '%',
57 self::EXCLUSIVE_MENTION => '!',
61 * Store tag/mention elements
63 * @param integer $uriid
64 * @param integer $type
67 * @param boolean $probing
69 public static function store(int $uriid, int $type, string $name, string $url = '', $probing = true)
71 if ($type = self::HASHTAG) {
72 // Remove some common "garbarge" from tags
73 $name = trim($name, "\x00..\x20\xFF#!@,;.:'/?!^°$%".'"');
75 $tags = explode(self::TAG_CHARACTER[self::HASHTAG], $name);
76 if (count($tags) > 1) {
77 foreach ($tags as $tag) {
78 self::store($uriid, $type, $tag, $url, $probing);
91 if (in_array($type, [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION])) {
93 // No mention without a contact url
98 $condition = ['nurl' => Strings::normaliseLink($url), 'uid' => 0, 'deleted' => false];
99 $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
100 if (DBA::isResult($contact)) {
101 $cid = $contact['id'];
102 Logger::info('Got id for contact url', ['cid' => $cid, 'url' => $url]);
106 $ssl_url = str_replace('http://', 'https://', $url);
107 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, 0];
108 $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
109 if (DBA::isResult($contact)) {
110 $cid = $contact['id'];
111 Logger::info('Got id for contact alias', ['cid' => $cid, 'url' => $url]);
115 $cid = Contact::getIdForURL($url, 0, true);
116 Logger::info('Got id by probing', ['cid' => $cid, 'url' => $url]);
120 // The contact wasn't found in the system (most likely some dead account)
121 // We ensure that we only store a single entry by overwriting the previous name
122 Logger::info('Contact not found, updating tag', ['url' => $url, 'name' => $name]);
123 DBA::update('tag', ['name' => substr($name, 0, 96)], ['url' => $url]);
128 $fields = ['name' => substr($name, 0, 96), 'url' => ''];
130 if (($type != self::HASHTAG) && !empty($url) && ($url != $name)) {
131 $fields['url'] = strtolower($url);
134 $tag = DBA::selectFirst('tag', ['id'], $fields);
135 if (!DBA::isResult($tag)) {
136 DBA::insert('tag', $fields, true);
137 $tagid = DBA::lastInsertId();
143 Logger::error('No tag id created', $fields);
148 $fields = ['uri-id' => $uriid, 'type' => $type, 'tid' => $tagid, 'cid' => $cid];
150 if (in_array($type, [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION])) {
151 $condition = $fields;
152 $condition['type'] = [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION];
153 if (DBA::exists('post-tag', $condition)) {
154 Logger::info('Tag already exists', $fields);
159 DBA::insert('post-tag', $fields, true);
161 Logger::info('Stored tag/mention', ['uri-id' => $uriid, 'tag-id' => $tagid, 'contact-id' => $cid, 'name' => $name, 'type' => $type, 'callstack' => System::callstack(8)]);
165 * Store tag/mention elements
167 * @param integer $uriid
168 * @param string $hash
169 * @param string $name
171 * @param boolean $probing
173 public static function storeByHash(int $uriid, string $hash, string $name, string $url = '', $probing = true)
175 $type = self::getTypeForHash($hash);
176 if ($type == self::UNKNOWN) {
180 self::store($uriid, $type, $name, $url, $probing);
184 * Store tags and mentions from the body
186 * @param integer $uriid URI-Id
187 * @param string $body Body of the post
188 * @param string $tags Accepted tags
189 * @param boolean $probing Perform a probing for contacts, adding them if needed
191 public static function storeFromBody(int $uriid, string $body, string $tags = null, $probing = true)
193 if (is_null($tags)) {
194 $tags = self::TAG_CHARACTER[self::HASHTAG] . self::TAG_CHARACTER[self::MENTION] . self::TAG_CHARACTER[self::EXCLUSIVE_MENTION];
197 Logger::info('Check for tags', ['uri-id' => $uriid, 'hash' => $tags, 'callstack' => System::callstack()]);
199 if (!preg_match_all("/([" . $tags . "])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism", $body, $result, PREG_SET_ORDER)) {
203 Logger::info('Found tags', ['uri-id' => $uriid, 'hash' => $tags, 'result' => $result]);
205 foreach ($result as $tag) {
206 self::storeByHash($uriid, $tag[1], $tag[3], $tag[2], $probing);
211 * Store raw tags (not encapsulated in links) from the body
212 * This function is needed in the intermediate phase.
213 * Later we can call item::setHashtags in advance to have all tags converted.
215 * @param integer $uriid URI-Id
216 * @param string $body Body of the post
218 public static function storeRawTagsFromBody(int $uriid, string $body)
220 Logger::info('Check for tags', ['uri-id' => $uriid, 'callstack' => System::callstack()]);
222 $result = BBCode::getTags($body);
223 if (empty($result)) {
227 Logger::info('Found tags', ['uri-id' => $uriid, 'result' => $result]);
229 foreach ($result as $tag) {
230 if (substr($tag, 0, 1) != self::TAG_CHARACTER[self::HASHTAG]) {
233 self::storeByHash($uriid, substr($tag, 0, 1), substr($tag, 1));
238 * Checks for stored hashtags and mentions for the given post
240 * @param integer $uriid
243 public static function existsForPost(int $uriid)
245 return DBA::exists('post-tag', ['uri-id' => $uriid, 'type' => [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION, self::EXCLUSIVE_MENTION]]);
251 * @param integer $uriid
252 * @param integer $type
253 * @param string $name
256 public static function remove(int $uriid, int $type, string $name, string $url = '')
258 $condition = ['uri-id' => $uriid, 'type' => $type, 'url' => $url];
259 if ($type == self::HASHTAG) {
260 $condition['name'] = $name;
263 $tag = DBA::selectFirst('tag-view', ['tid', 'cid'], $condition);
264 if (!DBA::isResult($tag)) {
268 Logger::info('Removing tag/mention', ['uri-id' => $uriid, 'tid' => $tag['tid'], 'name' => $name, 'url' => $url, 'callstack' => System::callstack(8)]);
269 DBA::delete('post-tag', ['uri-id' => $uriid, 'type' => $type, 'tid' => $tag['tid'], 'cid' => $tag['cid']]);
275 * @param integer $uriid
276 * @param string $hash
277 * @param string $name
280 public static function removeByHash(int $uriid, string $hash, string $name, string $url = '')
282 $type = self::getTypeForHash($hash);
283 if ($type == self::UNKNOWN) {
287 self::remove($uriid, $type, $name, $url);
291 * Get the type for the given hash
293 * @param string $hash
294 * @return integer type
296 private static function getTypeForHash(string $hash)
298 if ($hash == self::TAG_CHARACTER[self::MENTION]) {
299 return self::MENTION;
300 } elseif ($hash == self::TAG_CHARACTER[self::EXCLUSIVE_MENTION]) {
301 return self::EXCLUSIVE_MENTION;
302 } elseif ($hash == self::TAG_CHARACTER[self::IMPLICIT_MENTION]) {
303 return self::IMPLICIT_MENTION;
304 } elseif ($hash == self::TAG_CHARACTER[self::HASHTAG]) {
305 return self::HASHTAG;
307 return self::UNKNOWN;
312 * Retrieves the terms from the provided type(s) associated with the provided item ID.
314 * @param int $item_id
315 * @param int|array $type
319 public static function ArrayFromURIId(int $uri_id, array $type = [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION, self::EXCLUSIVE_MENTION])
321 $condition = ['uri-id' => $uri_id, 'type' => $type];
322 $tags = DBA::select('tag-view', ['type', 'name', 'url'], $condition);
323 if (!DBA::isResult($tags)) {
328 while ($tag = DBA::fetch($tags)) {
329 $tag['term'] = $tag['name']; /// @todo Remove this line when all occurrences of "term" had been replaced with "name"
337 * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
338 * provided item's body with them.
342 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
343 * @throws \ImagickException
345 public static function populateTagsFromItem(&$item)
351 'implicit_mentions' => [],
354 $searchpath = DI::baseUrl() . "/search?tag=";
356 $taglist = DBA::select('tag-view', ['type', 'name', 'url'],
357 ['uri-id' => $item['uri-id'], 'type' => [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]]);
358 while ($tag = DBA::fetch($taglist)) {
359 if ($tag['url'] == '') {
360 $tag['url'] = $searchpath . rawurlencode($tag['name']);
363 $orig_tag = $tag['url'];
365 $prefix = self::TAG_CHARACTER[$tag['type']];
366 switch($tag['type']) {
368 if ($orig_tag != $tag['url']) {
369 $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
372 $return['hashtags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
373 $return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
376 case self::EXCLUSIVE_MENTION:
377 $tag['url'] = Contact::magicLink($tag['url']);
378 $return['mentions'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
379 $return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
381 case self::IMPLICIT_MENTION:
382 $return['implicit_mentions'][] = $prefix . $tag['name'];
386 DBA::close($taglist);
392 * Search posts for given tag
394 * @param string $search
395 * @param integer $uid
396 * @param integer $start
397 * @param integer $limit
398 * @return array with URI-ID
400 public static function getURIIdListForTag(string $search, int $uid = 0, int $start = 0, int $limit = 100)
402 $condition = ["`name` = ? AND (NOT `private` OR (`private` AND `uid` = ?))", $search, $uid];
404 'order' => ['uri-id' => true],
405 'group_by' => ['uri-id'],
406 'limit' => [$start, $limit]
409 $tags = DBA::select('tag-search-view', ['uri-id'], $condition, $params);
412 while ($tag = DBA::fetch($tags)) {
413 $uriids[] = $tag['uri-id'];