X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FTag.php;h=17a68f120f4b5ab643277ac387571410b6d93944;hb=f329d5c067f0b981fb100fafdf5ad484e5b828f7;hp=8f132e82f3614168b91812480bc21c16f6a17173;hpb=1792046a4f270f15d904d591c1e9eb0444ff2349;p=friendica.git diff --git a/src/Model/Tag.php b/src/Model/Tag.php index 8f132e82f3..17a68f120f 100644 --- a/src/Model/Tag.php +++ b/src/Model/Tag.php @@ -1,6 +1,6 @@ $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]); + } } } @@ -204,22 +207,40 @@ class Tag } /** - * Store tags and mentions from the body + * Get tags and mentions from the body * - * @param integer $uriid URI-Id * @param string $body Body of the post * @param string $tags Accepted tags - * @param boolean $probing Perform a probing for contacts, adding them if needed + * + * @return array Tag list */ - public static function storeFromBody(int $uriid, string $body, string $tags = null, $probing = true) + public static function getFromBody(string $body, string $tags = null) { if (is_null($tags)) { $tags = self::TAG_CHARACTER[self::HASHTAG] . self::TAG_CHARACTER[self::MENTION] . self::TAG_CHARACTER[self::EXCLUSIVE_MENTION]; } + if (!preg_match_all("/([" . $tags . "])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism", $body, $result, PREG_SET_ORDER)) { + return []; + } + + return $result; + } + + /** + * Store tags and mentions from the body + * + * @param integer $uriid URI-Id + * @param string $body Body of the post + * @param string $tags Accepted tags + * @param boolean $probing Perform a probing for contacts, adding them if needed + */ + public static function storeFromBody(int $uriid, string $body, string $tags = null, $probing = true) + { Logger::info('Check for tags', ['uri-id' => $uriid, 'hash' => $tags, 'callstack' => System::callstack()]); - if (!preg_match_all("/([" . $tags . "])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism", $body, $result, PREG_SET_ORDER)) { + $result = self::getFromBody($body, $tags); + if (empty($result)) { return; } @@ -423,8 +444,8 @@ class Tag $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']); } - $return['hashtags'][] = $prefix . '' . htmlspecialchars($tag['name']) . ''; - $return['tags'][] = $prefix . '' . htmlspecialchars($tag['name']) . ''; + $return['hashtags'][] = '' . $prefix . '' . htmlspecialchars($tag['name']) . ''; + $return['tags'][] = '' . $prefix . '' . htmlspecialchars($tag['name']) . ''; break; case self::MENTION: case self::EXCLUSIVE_MENTION: @@ -433,8 +454,8 @@ class Tag } else { $tag['url'] = Contact::magicLink($tag['url']); } - $return['mentions'][] = $prefix . '' . htmlspecialchars($tag['name']) . ''; - $return['tags'][] = $prefix . '' . htmlspecialchars($tag['name']) . ''; + $return['mentions'][] = '' . $prefix . '' . htmlspecialchars($tag['name']) . ''; + $return['tags'][] = '' . $prefix . '' . htmlspecialchars($tag['name']) . ''; break; case self::IMPLICIT_MENTION: $return['implicit_mentions'][] = $prefix . $tag['name']; @@ -516,6 +537,23 @@ class Tag } } + /** + * 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 * @@ -526,11 +564,21 @@ class Tag */ 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` < ?", 0, DateTimeFormat::utc('now - ' . $period . ' hour')], + ['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); @@ -569,11 +617,21 @@ class Tag */ 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` < ?", 0, DateTimeFormat::utc('now - ' . $period . ' hour')], + ['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);