3 * @copyright Copyright (C) 2010-2023, the Friendica project
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\Cache\Enum\Duration;
26 use Friendica\Core\Logger;
27 use Friendica\Core\Protocol;
28 use Friendica\Core\System;
29 use Friendica\Database\Database;
30 use Friendica\Database\DBA;
32 use Friendica\Protocol\ActivityPub;
33 use Friendica\Util\DateTimeFormat;
34 use Friendica\Util\Network;
35 use Friendica\Util\Strings;
40 * This Model class handles tag table interactions.
41 * This tables stores relevant tags related to posts, like hashtags and mentions.
49 * An implicit mention is a mention in a comment body that is redundant with the threading information.
51 const IMPLICIT_MENTION = 8;
53 * An exclusive mention transmits the post only to the target account without transmitting it to the followers, usually a forum.
55 const EXCLUSIVE_MENTION = 9;
62 const ATTRIBUTED = 15;
65 const GENERAL_COLLECTION = 2;
66 const FOLLOWER_COLLECTION = 3;
67 const PUBLIC_COLLECTION = 4;
69 const TAG_CHARACTER = [
72 self::EXCLUSIVE_MENTION => '!',
73 self::IMPLICIT_MENTION => '%',
77 * Store tag/mention elements
79 * @param integer $uriId
80 * @param integer $type Tag type
81 * @param string $name Tag name
82 * @param string $url Contact URL (optional)
83 * @param integer $target Target (default: null)
86 public static function store(int $uriId, int $type, string $name, string $url = '', int $target = null)
88 if ($type == self::HASHTAG) {
89 // Trim Unicode non-word characters
90 $name = preg_replace('/(^\W+)|(\W+$)/us', '', $name);
92 $tags = explode(self::TAG_CHARACTER[self::HASHTAG], $name);
93 if (count($tags) > 1) {
94 foreach ($tags as $tag) {
95 self::store($uriId, $type, $tag, $url);
108 if (in_array($type, [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION, self::TO, self::CC, self::BTO, self::BCC, self::AUDIENCE, self::ATTRIBUTED])) {
110 // No mention without a contact url
114 if ((substr($url, 0, 7) == 'https//') || (substr($url, 0, 6) == 'http//')) {
115 Logger::notice('Wrong scheme in url', ['url' => $url, 'callstack' => System::callstack(20)]);
118 $cid = Contact::getIdForURL($url, 0, false);
119 Logger::debug('Got id for contact', ['cid' => $cid, 'url' => $url]);
122 $tag = DBA::selectFirst('tag', ['name', 'type'], ['url' => $url]);
124 if ($tag['name'] != substr($name, 0, 96)) {
125 DBA::update('tag', ['name' => substr($name, 0, 96)], ['url' => $url, 'type' => $tag['type']]);
127 if (!empty($target) && ($tag['type'] != $target)) {
128 DBA::update('tag', ['type' => $target], ['url' => $url]);
135 if (!in_array($type, [self::TO, self::CC, self::BTO, self::BCC, self::AUDIENCE, self::ATTRIBUTED])) {
136 if (($type != self::HASHTAG) && !empty($url) && ($url != $name)) {
137 $url = strtolower($url);
143 $tagid = self::getID($name, $url, $target);
149 $fields = ['uri-id' => $uriId, 'type' => $type, 'tid' => $tagid, 'cid' => $cid];
151 if (in_array($type, [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION])) {
152 $condition = $fields;
153 $condition['type'] = [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION];
154 if (DBA::exists('post-tag', $condition)) {
155 Logger::info('Tag already exists', $fields);
160 DBA::insert('post-tag', $fields, Database::INSERT_IGNORE);
162 Logger::debug('Stored tag/mention', ['uri-id' => $uriId, 'tag-id' => $tagid, 'contact-id' => $cid, 'name' => $name, 'type' => $type, 'callstack' => System::callstack(8)]);
166 * Fetch the target type for the given url
169 * @param bool $fetch Fetch information via network operations
172 public static function getTargetType(string $url, bool $fetch = true)
180 $tag = DBA::selectFirst('tag', ['url', 'type'], ['url' => $url]);
181 if (!empty($tag['type'])) {
182 $target = $tag['type'];
183 if ($target != self::GENERAL_COLLECTION) {
184 Logger::debug('Found existing type', ['type' => $tag['type'], 'url' => $url]);
189 if ($url == ActivityPub::PUBLIC_COLLECTION) {
190 $target = self::PUBLIC_COLLECTION;
191 Logger::debug('Public collection', ['url' => $url]);
193 if (DBA::exists('apcontact', ['followers' => $url])) {
194 $target = self::FOLLOWER_COLLECTION;
195 Logger::debug('Found collection via existing apcontact', ['url' => $url]);
196 } elseif (Contact::getIdForURL($url, 0, $fetch ? null : false)) {
197 $target = self::ACCOUNT;
198 Logger::debug('URL is an account', ['url' => $url]);
199 } elseif ($fetch && ($target != self::GENERAL_COLLECTION)) {
200 $content = ActivityPub::fetchContent($url);
201 if (!empty($content['type']) && ($content['type'] == 'OrderedCollection')) {
202 $target = self::GENERAL_COLLECTION;
203 Logger::debug('URL is an ordered collection', ['url' => $url]);
208 if (!empty($target) && !empty($tag['url']) && ($tag['type'] != $target)) {
209 DBA::update('tag', ['type' => $target], ['url' => $url]);
212 if (empty($target)) {
213 Logger::debug('No type could be detected', ['url' => $url]);
220 * Get a tag id for a given tag name and URL
222 * @param string $name Name of tag
224 * @param int $type Type of tag
227 public static function getID(string $name, string $url = '', int $type = null): int
229 $fields = ['name' => substr($name, 0, 96), 'url' => $url];
231 $tag = DBA::selectFirst('tag', ['id', 'type'], $fields);
232 if (DBA::isResult($tag)) {
233 if (empty($tag['type']) && !empty($type)) {
234 DBA::update('tag', ['type' => $type], $fields);
240 $fields['type'] = $type;
243 DBA::insert('tag', $fields, Database::INSERT_IGNORE);
244 $tid = DBA::lastInsertId();
250 $fields['type'] = $type;
252 Logger::error('No tag id created', $fields);
257 * Store tag/mention elements
259 * @param integer $uriId
260 * @param string $hash
261 * @param string $name
265 public static function storeByHash(int $uriId, string $hash, string $name, string $url = '')
267 $type = self::getTypeForHash($hash);
268 if ($type == self::UNKNOWN) {
272 self::store($uriId, $type, $name, $url);
276 * Get tags and mentions from the body
278 * @param string $body Body of the post
279 * @param string $tags Accepted tags
281 * @return array Tag list
283 public static function getFromBody(string $body, string $tags = null): array
285 if (is_null($tags)) {
286 $tags = self::TAG_CHARACTER[self::HASHTAG] . self::TAG_CHARACTER[self::MENTION] . self::TAG_CHARACTER[self::EXCLUSIVE_MENTION];
289 if (!preg_match_all("/([" . $tags . "])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism", $body, $result, PREG_SET_ORDER)) {
297 * Store tags and mentions from the body
299 * @param integer $uriId URI-Id
300 * @param string $body Body of the post
301 * @param string $tags Accepted tags
304 public static function storeFromBody(int $uriId, string $body, string $tags = null)
306 $item = ['uri-id' => $uriId, 'body' => $body, 'quote-uri-id' => null];
307 self::storeFromArray($item, $tags);
311 * Store tags and mentions from the item array
313 * @param array $item Item array
314 * @param string $tags Accepted tags
317 public static function storeFromArray(array $item, string $tags = null)
319 Logger::info('Check for tags', ['uri-id' => $item['uri-id'], 'hash' => $tags, 'callstack' => System::callstack()]);
321 if (is_null($tags)) {
322 $tags = self::TAG_CHARACTER[self::HASHTAG] . self::TAG_CHARACTER[self::MENTION] . self::TAG_CHARACTER[self::EXCLUSIVE_MENTION];
325 foreach (self::getFromBody($item['body'], $tags) as $tag) {
326 self::storeByHash($item['uri-id'], $tag[1], $tag[3], $tag[2]);
329 $shared = DI::contentItem()->getSharedPost($item, ['uri-id']);
331 // Search for hashtags in the shared body (but only if hashtags are wanted)
332 if (!empty($shared) && (strpos($tags, self::TAG_CHARACTER[self::HASHTAG]) !== false)) {
333 foreach (self::getByURIId($shared['post']['uri-id'], [self::HASHTAG]) as $tag) {
334 self::store($item['uri-id'], $tag['type'], $tag['name'], $tag['url']);
340 * Store raw tags (not encapsulated in links) from the body
341 * This function is needed in the intermediate phase.
342 * Later we can call item::setHashtags in advance to have all tags converted.
344 * @param integer $uriId URI-Id
345 * @param string $body Body of the post
348 public static function storeRawTagsFromBody(int $uriId, string $body)
350 Logger::info('Check for tags', ['uri-id' => $uriId, 'callstack' => System::callstack()]);
352 $result = BBCode::getTags($body);
353 if (empty($result)) {
357 Logger::info('Found tags', ['uri-id' => $uriId, 'result' => $result]);
359 foreach ($result as $tag) {
360 if (substr($tag, 0, 1) != self::TAG_CHARACTER[self::HASHTAG]) {
363 self::storeByHash($uriId, substr($tag, 0, 1), substr($tag, 1));
368 * Checks for stored hashtags and mentions for the given post
370 * @param integer $uriId
373 public static function existsForPost(int $uriId): bool
375 return DBA::exists('post-tag', ['uri-id' => $uriId, 'type' => [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]]);
381 * @param integer $uriId
382 * @param integer $type Type
383 * @param string $name Name
384 * @param string $url URL
387 public static function remove(int $uriId, int $type, string $name, string $url = '')
389 $condition = ['uri-id' => $uriId, 'type' => $type, 'url' => $url];
390 if ($type == self::HASHTAG) {
391 $condition['name'] = $name;
394 $tag = DBA::selectFirst('tag-view', ['tid', 'cid'], $condition);
395 if (!DBA::isResult($tag)) {
399 Logger::debug('Removing tag/mention', ['uri-id' => $uriId, 'tid' => $tag['tid'], 'name' => $name, 'url' => $url, 'callstack' => System::callstack(8)]);
400 DBA::delete('post-tag', ['uri-id' => $uriId, 'type' => $type, 'tid' => $tag['tid'], 'cid' => $tag['cid']]);
406 * @param integer $uriId
407 * @param string $hash
408 * @param string $name
412 public static function removeByHash(int $uriId, string $hash, string $name, string $url = '')
414 $type = self::getTypeForHash($hash);
415 if ($type == self::UNKNOWN) {
419 self::remove($uriId, $type, $name, $url);
423 * Get the type for the given hash
425 * @param string $hash
426 * @return integer Tag type
428 private static function getTypeForHash(string $hash): int
430 if ($hash == self::TAG_CHARACTER[self::MENTION]) {
431 return self::MENTION;
432 } elseif ($hash == self::TAG_CHARACTER[self::EXCLUSIVE_MENTION]) {
433 return self::EXCLUSIVE_MENTION;
434 } elseif ($hash == self::TAG_CHARACTER[self::IMPLICIT_MENTION]) {
435 return self::IMPLICIT_MENTION;
436 } elseif ($hash == self::TAG_CHARACTER[self::HASHTAG]) {
437 return self::HASHTAG;
439 return self::UNKNOWN;
444 * Create implicit mentions for a given post
446 * @param integer $uriId
447 * @param integer $parentUriId
450 public static function createImplicitMentions(int $uriId, int $parentUriId)
452 // Always mention the direct parent author
453 $parent = Post::selectFirst(['author-link', 'author-name'], ['uri-id' => $parentUriId]);
454 self::store($uriId, self::IMPLICIT_MENTION, $parent['author-name'], $parent['author-link']);
456 if (DI::config()->get('system', 'disable_implicit_mentions')) {
460 $tags = DBA::select('tag-view', ['name', 'url'], ['uri-id' => $parentUriId, 'type' => [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]]);
461 while ($tag = DBA::fetch($tags)) {
462 self::store($uriId, self::IMPLICIT_MENTION, $tag['name'], $tag['url']);
468 * Retrieves the terms from the provided type(s) associated with the provided item ID.
471 * @param array $type Tag type(s)
472 * @return array|bool Array on success, false on error
475 public static function getByURIId(int $uriId, array $type = [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION])
477 $condition = ['uri-id' => $uriId, 'type' => $type];
478 return DBA::selectToArray('tag-view', ['type', 'name', 'url', 'tag-type'], $condition);
482 * Checks if the given url is mentioned in the post
484 * @param integer $uriId
490 public static function isMentioned(int $uriId, string $url, array $type = [self::MENTION, self::EXCLUSIVE_MENTION]): bool
492 $tags = self::getByURIId($uriId, $type);
493 foreach ($tags as $tag) {
494 if (Strings::compareLink($url, $tag['url'])) {
502 * Return a string with all tags and mentions
504 * @param integer $uriId
505 * @param array $type Tag type(s)
506 * @return string tags and mentions
509 public static function getCSVByURIId(int $uriId, array $type = [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]): string
512 foreach (self::getByURIId($uriId, $type) as $tag) {
513 $tag_list[] = self::TAG_CHARACTER[$tag['type']] . '[url=' . $tag['url'] . ']' . $tag['name'] . '[/url]';
516 return implode(',', $tag_list);
520 * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
521 * provided item's body with them.
525 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
526 * @throws \ImagickException
528 public static function populateFromItem(array &$item): array
534 'implicit_mentions' => [],
537 $searchpath = DI::baseUrl() . '/search?tag=';
539 $taglist = DBA::select(
541 ['type', 'name', 'url', 'cid'],
542 ['uri-id' => $item['uri-id'], 'type' => [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]]
544 while ($tag = DBA::fetch($taglist)) {
545 if ($tag['url'] == '') {
546 $tag['url'] = $searchpath . rawurlencode($tag['name']);
549 $orig_tag = $tag['url'];
551 $prefix = self::TAG_CHARACTER[$tag['type']];
552 switch ($tag['type']) {
554 if ($orig_tag != $tag['url']) {
555 $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
558 $return['hashtags'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
559 $return['tags'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
563 case self::EXCLUSIVE_MENTION:
564 if (!empty($tag['cid'])) {
565 $tag['url'] = Contact::magicLinkById($tag['cid']);
567 $tag['url'] = Contact::magicLink($tag['url']);
569 $return['mentions'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
570 $return['tags'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
573 case self::IMPLICIT_MENTION:
574 $return['implicit_mentions'][] = $prefix . $tag['name'];
578 Logger::warning('Unknown tag type found', $tag);
581 DBA::close($taglist);
587 * Counts posts for given tag
589 * @param string $search
590 * @param integer $uid
591 * @return integer number of posts
593 public static function countByTag(string $search, int $uid = 0): int
595 $condition = ["`name` = ? AND (`uid` = ? OR (`uid` = ? AND NOT `global`))
596 AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))",
598 Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0,
601 return DBA::count('tag-search-view', $condition);
605 * Search posts for given tag
607 * @param string $search Tag to search for
608 * @param integer $uid User Id
609 * @param integer $start Starting record
610 * @param integer $limit Maximum count of records
611 * @param integer $last_uriid
612 * @return array with URI-ID
614 public static function getURIIdListByTag(string $search, int $uid = 0, int $start = 0, int $limit = 100, int $last_uriid = 0): array
616 $condition = ["`name` = ? AND (`uid` = ? OR (`uid` = ? AND NOT `global`))
617 AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))",
619 Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0,
622 if (!empty($last_uriid)) {
623 $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $last_uriid]);
627 'order' => ['uri-id' => true],
628 'limit' => [$start, $limit]
631 $tags = DBA::select('tag-search-view', ['uri-id'], $condition, $params);
634 while ($tag = DBA::fetch($tags)) {
635 $uriIds[] = $tag['uri-id'];
643 * Returns a list of the most frequent global hashtags over the given period
645 * @param int $period Period in hours to consider posts
646 * @param int $limit Number of returned tags
647 * @param int $offset Page offset in results
651 public static function getGlobalTrendingHashtags(int $period, int $limit = 10, int $offset = 0): array
653 $tags = DI::cache()->get("global_trending_tags-$period");
655 $tags = self::setGlobalTrendingHashtags($period, 1000);
657 return array_slice($tags, $offset, $limit);
661 * Fetch the blocked tags as SQL
663 * @return string SQL for blocked tag names or empty string
665 private static function getBlockedSQL(): string
667 $blocked_txt = DI::config()->get('system', 'blocked_tags');
668 if (empty($blocked_txt)) {
672 $blocked = explode(',', $blocked_txt);
673 array_walk($blocked, function (&$value) {
674 $value = "'" . DBA::escape(trim($value)) . "'";
676 return ' AND NOT `name` IN (' . implode(',', $blocked) . ')';
680 * Creates a list of the most frequent global hashtags over the given period
682 * @param int $period Period in hours to consider posts
683 * @param int $limit Number of returned tags
687 public static function setGlobalTrendingHashtags(int $period, int $limit = 10): array
690 * Get a uri-id that is at least X hours old.
691 * We use the uri-id in the query for the hash tags since this is much faster
693 $post = Post::selectFirstThread(
695 ["`uid` = ? AND `received` < ?", 0, DateTimeFormat::utc('now - ' . $period . ' hour')],
696 ['order' => ['received' => true]]
699 if (empty($post['uri-id'])) {
703 $block_sql = self::getBlockedSQL();
706 "SELECT `name` AS `term`, COUNT(*) AS `score`, COUNT(DISTINCT(`author-id`)) as `authors`
707 FROM `tag-search-view`
708 WHERE `private` = ? AND `uid` = ? AND `uri-id` > ? $block_sql
709 GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?",
716 if (DBA::isResult($tagsStmt)) {
717 $tags = DBA::toArray($tagsStmt);
718 DI::cache()->set("global_trending_tags-$period", $tags, Duration::HOUR);
726 * Returns a list of the most frequent local hashtags over the given period
728 * @param int $period Period in hours to consider posts
729 * @param int $limit Number of returned tags
730 * @param int $offset Page offset in results
734 public static function getLocalTrendingHashtags(int $period, $limit = 10, int $offset = 0): array
736 $tags = DI::cache()->get("local_trending_tags-$period");
738 $tags = self::setLocalTrendingHashtags($period, 1000);
740 return array_slice($tags, $offset, $limit);
744 * Returns a list of the most frequent local hashtags over the given period
746 * @param int $period Period in hours to consider posts
747 * @param int $limit Number of returned tags
751 public static function setLocalTrendingHashtags(int $period, int $limit = 10): array
753 // Get a uri-id that is at least X hours old.
754 // We use the uri-id in the query for the hash tags since this is much faster
755 $post = Post::selectFirstThread(
757 ["`uid` = ? AND `received` < ?", 0, DateTimeFormat::utc('now - ' . $period . ' hour')],
758 ['order' => ['received' => true]]
760 if (empty($post['uri-id'])) {
764 $block_sql = self::getBlockedSQL();
767 "SELECT `name` AS `term`, COUNT(*) AS `score`, COUNT(DISTINCT(`author-id`)) as `authors`
768 FROM `tag-search-view`
769 WHERE `private` = ? AND `wall` AND `origin` AND `uri-id` > ? $block_sql
770 GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?",
776 if (DBA::isResult($tagsStmt)) {
777 $tags = DBA::toArray($tagsStmt);
778 DI::cache()->set("local_trending_tags-$period", $tags, Duration::HOUR);
786 * Check if the provided tag is of one of the provided term types.
788 * @param string $tag Tag name
789 * @param int ...$types
792 public static function isType(string $tag, ...$types): bool
795 foreach ($types as $type) {
796 if (array_key_exists($type, self::TAG_CHARACTER)) {
797 $tag_chars[] = self::TAG_CHARACTER[$type];
801 return Strings::startsWithChars($tag, $tag_chars);
805 * Fetch user who subscribed to the given tag
808 * @return array User list
810 private static function getUIDListByTag(string $tag): array
813 $searches = DBA::select('search', ['uid'], ['term' => $tag]);
814 while ($search = DBA::fetch($searches)) {
815 $uids[] = $search['uid'];
817 DBA::close($searches);
823 * Fetch user who subscribed to the tags of the given item
825 * @param integer $uriId
826 * @return array User list
828 public static function getUIDListByURIId(int $uriId): array
831 $tags = self::getByURIId($uriId, [self::HASHTAG]);
833 foreach ($tags as $tag) {
834 $uids = array_merge($uids, self::getUIDListByTag(self::TAG_CHARACTER[self::HASHTAG] . $tag['name']));
837 return array_unique($uids);