<?php
/**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
// The contact wasn't found in the system (most likely some dead account)
// We ensure that we only store a single entry by overwriting the previous name
Logger::info('Contact not found, updating tag', ['url' => $url, 'name' => $name]);
- DBA::update('tag', ['name' => substr($name, 0, 96)], ['url' => $url]);
+ if (!DBA::exists('tag', ['name' => substr($name, 0, 96), 'url' => $url])) {
+ DBA::update('tag', ['name' => substr($name, 0, 96)], ['url' => $url]);
+ }
}
}
public static function createImplicitMentions(int $uri_id, int $parent_uri_id)
{
// Always mention the direct parent author
- $parent = Item::selectFirst(['author-link', 'author-name'], ['uri-id' => $parent_uri_id]);
+ $parent = Post::selectFirst(['author-link', 'author-name'], ['uri-id' => $parent_uri_id]);
self::store($uri_id, self::IMPLICIT_MENTION, $parent['author-name'], $parent['author-link']);
if (DI::config()->get('system', 'disable_implicit_mentions')) {
$searchpath = DI::baseUrl() . "/search?tag=";
- $taglist = DBA::select('tag-view', ['type', 'name', 'url'],
+ $taglist = DBA::select('tag-view', ['type', 'name', 'url', 'cid'],
['uri-id' => $item['uri-id'], 'type' => [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]]);
while ($tag = DBA::fetch($taglist)) {
if ($tag['url'] == '') {
$item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
}
- $return['hashtags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
- $return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
+ $return['hashtags'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
+ $return['tags'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
break;
case self::MENTION:
case self::EXCLUSIVE_MENTION:
+ if (!empty($tag['cid'])) {
+ $tag['url'] = Contact::magicLinkById($tag['cid']);
+ } else {
$tag['url'] = Contact::magicLink($tag['url']);
- $return['mentions'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
- $return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
+ }
+ $return['mentions'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
+ $return['tags'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
break;
case self::IMPLICIT_MENTION:
$return['implicit_mentions'][] = $prefix . $tag['name'];
*/
public static function countByTag(string $search, int $uid = 0)
{
- $condition = ["`name` = ? AND (NOT `private` OR (`private` AND `uid` = ?))
- AND `uri-id` IN (SELECT `uri-id` FROM `item` WHERE `network` IN (?, ?, ?, ?))",
- $search, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
- $params = ['group_by' => ['uri-id']];
+ $condition = ["`name` = ? AND (`uid` = ? OR (`uid` = ? AND NOT `global`))
+ AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))",
+ $search, 0, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0];
- return DBA::count('tag-search-view', $condition, $params);
+ return DBA::count('tag-search-view', $condition);
}
/**
*/
public static function getURIIdListByTag(string $search, int $uid = 0, int $start = 0, int $limit = 100, int $last_uriid = 0)
{
- $condition = ["`name` = ? AND (NOT `private` OR (`private` AND `uid` = ?))
- AND `uri-id` IN (SELECT `uri-id` FROM `item` WHERE `network` IN (?, ?, ?, ?))",
- $search, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
+ $condition = ["`name` = ? AND (`uid` = ? OR (`uid` = ? AND NOT `global`))
+ AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))",
+ $search, 0, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0];
if (!empty($last_uriid)) {
$condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $last_uriid]);
$params = [
'order' => ['uri-id' => true],
- 'group_by' => ['uri-id'],
'limit' => [$start, $limit]
];
}
}
+ /**
+ * Fetch the blocked tags as SQL
+ *
+ * @return string
+ */
+ private static function getBlockedSQL()
+ {
+ $blocked_txt = DI::config()->get('system', 'blocked_tags');
+ if (empty($blocked_txt)) {
+ return '';
+ }
+
+ $blocked = explode(',', $blocked_txt);
+ array_walk($blocked, function(&$value) { $value = "'" . DBA::escape(trim($value)) . "'";});
+ return " AND NOT `name` IN (" . implode(',', $blocked) . ")";
+ }
+
/**
* Creates a list of the most frequent global hashtags over the given period
*
*/
public static function setGlobalTrendingHashtags(int $period, int $limit = 10)
{
- $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
+ // Get a uri-id that is at least X hours old.
+ // We use the uri-id in the query for the hash tags since this is much faster
+ $post = Post::selectFirstThread(['uri-id'], ["`uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? HOUR", 0, $period],
+ ['order' => ['received' => true]]);
+ if (empty($post['uri-id'])) {
+ return [];
+ }
+
+ $block_sql = self::getBlockedSQL();
+
+ $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`, COUNT(DISTINCT(`author-id`)) as `authors`
FROM `tag-search-view`
- WHERE `private` = ? AND `uid` = ? AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
- GROUP BY `term` ORDER BY `score` DESC LIMIT ?",
- Item::PUBLIC, 0, $period, $limit);
+ WHERE `private` = ? AND `uid` = ? AND `uri-id` > ? $block_sql
+ GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?",
+ Item::PUBLIC, 0, $post['uri-id'], $limit);
if (DBA::isResult($tagsStmt)) {
$tags = DBA::toArray($tagsStmt);
*/
public static function setLocalTrendingHashtags(int $period, int $limit = 10)
{
- $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
+ // Get a uri-id that is at least X hours old.
+ // We use the uri-id in the query for the hash tags since this is much faster
+ $post = Post::selectFirstThread(['uri-id'], ["`uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? HOUR", 0, $period],
+ ['order' => ['received' => true]]);
+ if (empty($post['uri-id'])) {
+ return [];
+ }
+
+ $block_sql = self::getBlockedSQL();
+
+ $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`, COUNT(DISTINCT(`author-id`)) as `authors`
FROM `tag-search-view`
- WHERE `private` = ? AND `wall` AND `origin` AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
- GROUP BY `term` ORDER BY `score` DESC LIMIT ?",
- Item::PUBLIC, $period, $limit);
+ WHERE `private` = ? AND `wall` AND `origin` AND `uri-id` > ? $block_sql
+ GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?",
+ Item::PUBLIC, $post['uri-id'], $limit);
if (DBA::isResult($tagsStmt)) {
$tags = DBA::toArray($tagsStmt);