3 * @copyright Copyright (C) 2010-2021, 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\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\Util\Strings;
37 * This Model class handles tag table interactions.
38 * This tables stores relevant tags related to posts, like hashtags and mentions.
46 * An implicit mention is a mention in a comment body that is redundant with the threading information.
48 const IMPLICIT_MENTION = 8;
50 * An exclusive mention transfers the ownership of the post to the target account, usually a forum.
52 const EXCLUSIVE_MENTION = 9;
54 const TAG_CHARACTER = [
57 self::IMPLICIT_MENTION => '%',
58 self::EXCLUSIVE_MENTION => '!',
62 * Store tag/mention elements
64 * @param integer $uriid
65 * @param integer $type
68 * @param boolean $probing
70 public static function store(int $uriid, int $type, string $name, string $url = '', $probing = true)
72 if ($type == self::HASHTAG) {
73 // Trim Unicode non-word characters
74 $name = preg_replace('/(^\W+)|(\W+$)/us', '', $name);
76 $tags = explode(self::TAG_CHARACTER[self::HASHTAG], $name);
77 if (count($tags) > 1) {
78 foreach ($tags as $tag) {
79 self::store($uriid, $type, $tag, $url, $probing);
92 if (in_array($type, [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION])) {
94 // No mention without a contact url
98 if ((substr($url, 0, 7) == 'https//') || (substr($url, 0, 6) == 'http//')) {
99 Logger::notice('Wrong scheme in url', ['url' => $url, 'callstack' => System::callstack(20)]);
103 $condition = ['nurl' => Strings::normaliseLink($url), 'uid' => 0, 'deleted' => false];
104 $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
105 if (DBA::isResult($contact)) {
106 $cid = $contact['id'];
107 Logger::info('Got id for contact url', ['cid' => $cid, 'url' => $url]);
111 $ssl_url = str_replace('http://', 'https://', $url);
112 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, 0];
113 $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
114 if (DBA::isResult($contact)) {
115 $cid = $contact['id'];
116 Logger::info('Got id for contact alias', ['cid' => $cid, 'url' => $url]);
120 $cid = Contact::getIdForURL($url, 0, false);
121 Logger::info('Got id by probing', ['cid' => $cid, 'url' => $url]);
125 // The contact wasn't found in the system (most likely some dead account)
126 // We ensure that we only store a single entry by overwriting the previous name
127 Logger::info('Contact not found, updating tag', ['url' => $url, 'name' => $name]);
128 if (!DBA::exists('tag', ['name' => substr($name, 0, 96), 'url' => $url])) {
129 DBA::update('tag', ['name' => substr($name, 0, 96)], ['url' => $url]);
135 if (($type != self::HASHTAG) && !empty($url) && ($url != $name)) {
136 $url = strtolower($url);
141 $tagid = self::getID($name, $url);
147 $fields = ['uri-id' => $uriid, 'type' => $type, 'tid' => $tagid, 'cid' => $cid];
149 if (in_array($type, [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION])) {
150 $condition = $fields;
151 $condition['type'] = [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION];
152 if (DBA::exists('post-tag', $condition)) {
153 Logger::info('Tag already exists', $fields);
158 DBA::insert('post-tag', $fields, Database::INSERT_IGNORE);
160 Logger::info('Stored tag/mention', ['uri-id' => $uriid, 'tag-id' => $tagid, 'contact-id' => $cid, 'name' => $name, 'type' => $type, 'callstack' => System::callstack(8)]);
164 * Get a tag id for a given tag name and url
166 * @param string $name
170 public static function getID(string $name, string $url = '')
172 $fields = ['name' => substr($name, 0, 96), 'url' => $url];
174 $tag = DBA::selectFirst('tag', ['id'], $fields);
175 if (DBA::isResult($tag)) {
179 DBA::insert('tag', $fields, Database::INSERT_IGNORE);
180 $tid = DBA::lastInsertId();
185 Logger::error('No tag id created', $fields);
190 * Store tag/mention elements
192 * @param integer $uriid
193 * @param string $hash
194 * @param string $name
196 * @param boolean $probing
198 public static function storeByHash(int $uriid, string $hash, string $name, string $url = '', $probing = true)
200 $type = self::getTypeForHash($hash);
201 if ($type == self::UNKNOWN) {
205 self::store($uriid, $type, $name, $url, $probing);
209 * Store tags and mentions from the body
211 * @param integer $uriid URI-Id
212 * @param string $body Body of the post
213 * @param string $tags Accepted tags
214 * @param boolean $probing Perform a probing for contacts, adding them if needed
216 public static function storeFromBody(int $uriid, string $body, string $tags = null, $probing = true)
218 if (is_null($tags)) {
219 $tags = self::TAG_CHARACTER[self::HASHTAG] . self::TAG_CHARACTER[self::MENTION] . self::TAG_CHARACTER[self::EXCLUSIVE_MENTION];
222 Logger::info('Check for tags', ['uri-id' => $uriid, 'hash' => $tags, 'callstack' => System::callstack()]);
224 if (!preg_match_all("/([" . $tags . "])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism", $body, $result, PREG_SET_ORDER)) {
228 Logger::info('Found tags', ['uri-id' => $uriid, 'hash' => $tags, 'result' => $result]);
230 foreach ($result as $tag) {
231 self::storeByHash($uriid, $tag[1], $tag[3], $tag[2], $probing);
236 * Store raw tags (not encapsulated in links) from the body
237 * This function is needed in the intermediate phase.
238 * Later we can call item::setHashtags in advance to have all tags converted.
240 * @param integer $uriid URI-Id
241 * @param string $body Body of the post
243 public static function storeRawTagsFromBody(int $uriid, string $body)
245 Logger::info('Check for tags', ['uri-id' => $uriid, 'callstack' => System::callstack()]);
247 $result = BBCode::getTags($body);
248 if (empty($result)) {
252 Logger::info('Found tags', ['uri-id' => $uriid, 'result' => $result]);
254 foreach ($result as $tag) {
255 if (substr($tag, 0, 1) != self::TAG_CHARACTER[self::HASHTAG]) {
258 self::storeByHash($uriid, substr($tag, 0, 1), substr($tag, 1));
263 * Checks for stored hashtags and mentions for the given post
265 * @param integer $uriid
268 public static function existsForPost(int $uriid)
270 return DBA::exists('post-tag', ['uri-id' => $uriid, 'type' => [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION, self::EXCLUSIVE_MENTION]]);
276 * @param integer $uriid
277 * @param integer $type
278 * @param string $name
281 public static function remove(int $uriid, int $type, string $name, string $url = '')
283 $condition = ['uri-id' => $uriid, 'type' => $type, 'url' => $url];
284 if ($type == self::HASHTAG) {
285 $condition['name'] = $name;
288 $tag = DBA::selectFirst('tag-view', ['tid', 'cid'], $condition);
289 if (!DBA::isResult($tag)) {
293 Logger::info('Removing tag/mention', ['uri-id' => $uriid, 'tid' => $tag['tid'], 'name' => $name, 'url' => $url, 'callstack' => System::callstack(8)]);
294 DBA::delete('post-tag', ['uri-id' => $uriid, 'type' => $type, 'tid' => $tag['tid'], 'cid' => $tag['cid']]);
300 * @param integer $uriid
301 * @param string $hash
302 * @param string $name
305 public static function removeByHash(int $uriid, string $hash, string $name, string $url = '')
307 $type = self::getTypeForHash($hash);
308 if ($type == self::UNKNOWN) {
312 self::remove($uriid, $type, $name, $url);
316 * Get the type for the given hash
318 * @param string $hash
319 * @return integer type
321 private static function getTypeForHash(string $hash)
323 if ($hash == self::TAG_CHARACTER[self::MENTION]) {
324 return self::MENTION;
325 } elseif ($hash == self::TAG_CHARACTER[self::EXCLUSIVE_MENTION]) {
326 return self::EXCLUSIVE_MENTION;
327 } elseif ($hash == self::TAG_CHARACTER[self::IMPLICIT_MENTION]) {
328 return self::IMPLICIT_MENTION;
329 } elseif ($hash == self::TAG_CHARACTER[self::HASHTAG]) {
330 return self::HASHTAG;
332 return self::UNKNOWN;
337 * Create implicit mentions for a given post
339 * @param integer $uri_id
340 * @param integer $parent_uri_id
342 public static function createImplicitMentions(int $uri_id, int $parent_uri_id)
344 // Always mention the direct parent author
345 $parent = Post::selectFirst(['author-link', 'author-name'], ['uri-id' => $parent_uri_id]);
346 self::store($uri_id, self::IMPLICIT_MENTION, $parent['author-name'], $parent['author-link']);
348 if (DI::config()->get('system', 'disable_implicit_mentions')) {
352 $tags = DBA::select('tag-view', ['name', 'url'], ['uri-id' => $parent_uri_id]);
353 while ($tag = DBA::fetch($tags)) {
354 self::store($uri_id, self::IMPLICIT_MENTION, $tag['name'], $tag['url']);
360 * Retrieves the terms from the provided type(s) associated with the provided item ID.
362 * @param int $item_id
363 * @param int|array $type
367 public static function getByURIId(int $uri_id, array $type = [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION, self::EXCLUSIVE_MENTION])
369 $condition = ['uri-id' => $uri_id, 'type' => $type];
370 return DBA::selectToArray('tag-view', ['type', 'name', 'url'], $condition);
374 * Return a string with all tags and mentions
376 * @param integer $uri_id
378 * @return string tags and mentions
381 public static function getCSVByURIId(int $uri_id, array $type = [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION, self::EXCLUSIVE_MENTION])
384 $tags = self::getByURIId($uri_id, $type);
385 foreach ($tags as $tag) {
386 $tag_list[] = self::TAG_CHARACTER[$tag['type']] . '[url=' . $tag['url'] . ']' . $tag['name'] . '[/url]';
389 return implode(',', $tag_list);
393 * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
394 * provided item's body with them.
398 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
399 * @throws \ImagickException
401 public static function populateFromItem(&$item)
407 'implicit_mentions' => [],
410 $searchpath = DI::baseUrl() . "/search?tag=";
412 $taglist = DBA::select('tag-view', ['type', 'name', 'url', 'cid'],
413 ['uri-id' => $item['uri-id'], 'type' => [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]]);
414 while ($tag = DBA::fetch($taglist)) {
415 if ($tag['url'] == '') {
416 $tag['url'] = $searchpath . rawurlencode($tag['name']);
419 $orig_tag = $tag['url'];
421 $prefix = self::TAG_CHARACTER[$tag['type']];
422 switch($tag['type']) {
424 if ($orig_tag != $tag['url']) {
425 $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
428 $return['hashtags'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
429 $return['tags'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
432 case self::EXCLUSIVE_MENTION:
433 if (!empty($tag['cid'])) {
434 $tag['url'] = Contact::magicLinkById($tag['cid']);
436 $tag['url'] = Contact::magicLink($tag['url']);
438 $return['mentions'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
439 $return['tags'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
441 case self::IMPLICIT_MENTION:
442 $return['implicit_mentions'][] = $prefix . $tag['name'];
446 DBA::close($taglist);
452 * Counts posts for given tag
454 * @param string $search
455 * @param integer $uid
456 * @return integer number of posts
458 public static function countByTag(string $search, int $uid = 0)
460 $condition = ["`name` = ? AND (`uid` = ? OR (`uid` = ? AND NOT `global`))
461 AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))",
462 $search, 0, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0];
464 return DBA::count('tag-search-view', $condition);
468 * Search posts for given tag
470 * @param string $search
471 * @param integer $uid
472 * @param integer $start
473 * @param integer $limit
474 * @param integer $last_uriid
475 * @return array with URI-ID
477 public static function getURIIdListByTag(string $search, int $uid = 0, int $start = 0, int $limit = 100, int $last_uriid = 0)
479 $condition = ["`name` = ? AND (`uid` = ? OR (`uid` = ? AND NOT `global`))
480 AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))",
481 $search, 0, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0];
483 if (!empty($last_uriid)) {
484 $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $last_uriid]);
488 'order' => ['uri-id' => true],
489 'limit' => [$start, $limit]
492 $tags = DBA::select('tag-search-view', ['uri-id'], $condition, $params);
495 while ($tag = DBA::fetch($tags)) {
496 $uriids[] = $tag['uri-id'];
504 * Returns a list of the most frequent global hashtags over the given period
506 * @param int $period Period in hours to consider posts
507 * @param int $limit Number of returned tags
511 public static function getGlobalTrendingHashtags(int $period, $limit = 10)
513 $tags = DI::cache()->get('global_trending_tags-' . $period . '-' . $limit);
517 return self::setGlobalTrendingHashtags($period, $limit);
522 * Fetch the blocked tags as SQL
526 private static function getBlockedSQL()
528 $blocked_txt = DI::config()->get('system', 'blocked_tags');
529 if (empty($blocked_txt)) {
533 $blocked = explode(',', $blocked_txt);
534 array_walk($blocked, function(&$value) { $value = "'" . DBA::escape(trim($value)) . "'";});
535 return " AND NOT `name` IN (" . implode(',', $blocked) . ")";
539 * Creates a list of the most frequent global hashtags over the given period
541 * @param int $period Period in hours to consider posts
542 * @param int $limit Number of returned tags
546 public static function setGlobalTrendingHashtags(int $period, int $limit = 10)
548 $block_sql = self::getBlockedSQL();
550 $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`, COUNT(DISTINCT(`author-id`)) as `authors`
551 FROM `tag-search-view`
552 WHERE `private` = ? AND `uid` = ? AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR) $block_sql
553 GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?",
554 Item::PUBLIC, 0, $period, $limit);
556 if (DBA::isResult($tagsStmt)) {
557 $tags = DBA::toArray($tagsStmt);
558 DI::cache()->set('global_trending_tags-' . $period . '-' . $limit, $tags, Duration::DAY);
566 * Returns a list of the most frequent local hashtags over the given period
568 * @param int $period Period in hours to consider posts
569 * @param int $limit Number of returned tags
573 public static function getLocalTrendingHashtags(int $period, $limit = 10)
575 $tags = DI::cache()->get('local_trending_tags-' . $period . '-' . $limit);
579 return self::setLocalTrendingHashtags($period, $limit);
584 * Returns a list of the most frequent local hashtags over the given period
586 * @param int $period Period in hours to consider posts
587 * @param int $limit Number of returned tags
591 public static function setLocalTrendingHashtags(int $period, int $limit = 10)
593 $block_sql = self::getBlockedSQL();
595 $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`, COUNT(DISTINCT(`author-id`)) as `authors`
596 FROM `tag-search-view`
597 WHERE `private` = ? AND `wall` AND `origin` AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR) $block_sql
598 GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?",
599 Item::PUBLIC, $period, $limit);
601 if (DBA::isResult($tagsStmt)) {
602 $tags = DBA::toArray($tagsStmt);
603 DI::cache()->set('local_trending_tags-' . $period . '-' . $limit, $tags, Duration::DAY);
611 * Check if the provided tag is of one of the provided term types.
614 * @param int ...$types
617 public static function isType($tag, ...$types)
620 foreach ($types as $type) {
621 if (array_key_exists($type, self::TAG_CHARACTER)) {
622 $tag_chars[] = self::TAG_CHARACTER[$type];
626 return Strings::startsWithChars($tag, $tag_chars);
630 * Fetch user who subscribed to the given tag
633 * @return array User list
635 private static function getUIDListByTag(string $tag)
638 $searches = DBA::select('search', ['uid'], ['term' => $tag]);
639 while ($search = DBA::fetch($searches)) {
640 $uids[] = $search['uid'];
642 DBA::close($searches);
648 * Fetch user who subscribed to the tags of the given item
650 * @param integer $uri_id
651 * @return array User list
653 public static function getUIDListByURIId(int $uri_id)
656 $tags = self::getByURIId($uri_id, [self::HASHTAG]);
658 foreach ($tags as $tag) {
659 $uids = array_merge($uids, self::getUIDListByTag(self::TAG_CHARACTER[self::HASHTAG] . $tag['name']));
662 return array_unique($uids);