use Friendica\Content\Text\BBCode;
use Friendica\Core\Cache\Duration;
use Friendica\Core\Logger;
+use Friendica\Core\Protocol;
use Friendica\Core\System;
+use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Util\Strings;
public static function store(int $uriid, int $type, string $name, string $url = '', $probing = true)
{
if ($type == self::HASHTAG) {
- // Remove some common "garbarge" from tags
- $name = trim($name, "\x00..\x20\xFF#!@,;.:'/?!^°$%".'"');
+ // Trim Unicode non-word characters
+ $name = preg_replace('/(^\W+)|(\W+$)/us', '', $name);
$tags = explode(self::TAG_CHARACTER[self::HASHTAG], $name);
if (count($tags) > 1) {
// 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]);
+ }
}
}
}
}
- DBA::insert('post-tag', $fields, true);
+ DBA::insert('post-tag', $fields, Database::INSERT_IGNORE);
Logger::info('Stored tag/mention', ['uri-id' => $uriid, 'tag-id' => $tagid, 'contact-id' => $cid, 'name' => $name, 'type' => $type, 'callstack' => System::callstack(8)]);
}
return $tag['id'];
}
- DBA::insert('tag', $fields, true);
+ DBA::insert('tag', $fields, Database::INSERT_IGNORE);
$tid = DBA::lastInsertId();
if (!empty($tid)) {
return $tid;
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'] == '') {
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>';
break;
*/
public static function countByTag(string $search, int $uid = 0)
{
- $condition = ["`name` = ? AND (NOT `private` OR (`private` AND `uid` = ?))", $search, $uid];
- $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` = ?))", $search, $uid];
+ $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]
];
* @return array
* @throws \Exception
*/
- public static function setGlobalTrendingHashtags(int $period, $limit = 10)
+ public static function setGlobalTrendingHashtags(int $period, int $limit = 10)
{
$tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
FROM `tag-search-view`
if (DBA::isResult($tagsStmt)) {
$tags = DBA::toArray($tagsStmt);
- DI::cache()->set('global_trending_tags-' . $period . '-' . $limit, $tags, Duration::HOUR);
+ DI::cache()->set('global_trending_tags-' . $period . '-' . $limit, $tags, Duration::DAY);
return $tags;
}
* @return array
* @throws \Exception
*/
- public static function setLocalTrendingHashtags(int $period, $limit = 10)
+ public static function setLocalTrendingHashtags(int $period, int $limit = 10)
{
$tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
FROM `tag-search-view`
if (DBA::isResult($tagsStmt)) {
$tags = DBA::toArray($tagsStmt);
- DI::cache()->set('local_trending_tags-' . $period . '-' . $limit, $tags, Duration::HOUR);
+ DI::cache()->set('local_trending_tags-' . $period . '-' . $limit, $tags, Duration::DAY);
return $tags;
}