// 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 countByTag(string $search, int $uid = 0)
{
- $condition = ["`name` = ? AND (NOT `private` OR (`private` AND `uid` = ?))
- AND `uri-id` IN (SELECT `uri-id` FROM `post-view` 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 `post-view` 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`
+ $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 ?",
+ WHERE `private` = ? AND `uid` = ? AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR) $block_sql
+ GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?",
Item::PUBLIC, 0, $period, $limit);
if (DBA::isResult($tagsStmt)) {
*/
public static function setLocalTrendingHashtags(int $period, int $limit = 10)
{
- $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
+ $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 ?",
+ WHERE `private` = ? AND `wall` AND `origin` AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR) $block_sql
+ GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?",
Item::PUBLIC, $period, $limit);
if (DBA::isResult($tagsStmt)) {