X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FTag.php;h=3020a2f2934839e0108779bda564314752e09bcb;hb=6dbbd081795fa1c8fe57db2248ac162efeeada88;hp=7eb475d727009e8642524895ed2b8a90597ead1c;hpb=fc2340d4afe6466c58e35afd64fe9010bf185b04;p=friendica.git diff --git a/src/Model/Tag.php b/src/Model/Tag.php index 7eb475d727..3020a2f293 100644 --- a/src/Model/Tag.php +++ b/src/Model/Tag.php @@ -1,6 +1,6 @@ 1) { @@ -123,7 +126,9 @@ class Tag // 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]); + } } } @@ -151,7 +156,7 @@ class Tag } } - 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)]); } @@ -172,7 +177,7 @@ class Tag return $tag['id']; } - DBA::insert('tag', $fields, true); + DBA::insert('tag', $fields, Database::INSERT_IGNORE); $tid = DBA::lastInsertId(); if (!empty($tid)) { return $tid; @@ -338,7 +343,7 @@ class Tag 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')) { @@ -405,7 +410,7 @@ class Tag $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'] == '') { @@ -421,14 +426,18 @@ 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: + if (!empty($tag['cid'])) { + $tag['url'] = Contact::magicLinkById($tag['cid']); + } 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']; @@ -440,6 +449,22 @@ class Tag return $return; } + /** + * Counts posts for given tag + * + * @param string $search + * @param integer $uid + * @return integer number of posts + */ + public static function countByTag(string $search, int $uid = 0) + { + $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); + } + /** * Search posts for given tag * @@ -447,14 +472,21 @@ class Tag * @param integer $uid * @param integer $start * @param integer $limit + * @param integer $last_uriid * @return array with URI-ID */ - public static function getURIIdListByTag(string $search, int $uid = 0, int $start = 0, int $limit = 100) + 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] ]; @@ -473,54 +505,123 @@ class Tag * Returns a list of the most frequent global hashtags over the given period * * @param int $period Period in hours to consider posts + * @param int $limit Number of returned tags * @return array * @throws \Exception */ public static function getGlobalTrendingHashtags(int $period, $limit = 10) { - $tags = DI::cache()->get('global_trending_tags'); - - if (empty($tags)) { - $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score` - FROM `tag-search-view` - WHERE `private` = ? AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR) - GROUP BY `term` ORDER BY `score` DESC LIMIT ?", - Item::PUBLIC, $period, $limit); - - if (DBA::isResult($tagsStmt)) { - $tags = DBA::toArray($tagsStmt); - DI::cache()->set('global_trending_tags', $tags, Duration::HOUR); - } + $tags = DI::cache()->get('global_trending_tags-' . $period . '-' . $limit); + if (!empty($tags)) { + return $tags; + } else { + return self::setGlobalTrendingHashtags($period, $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 ''; } - return $tags ?: []; + $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 + * + * @param int $period Period in hours to consider posts + * @param int $limit Number of returned tags + * @return array + * @throws \Exception + */ + public static function setGlobalTrendingHashtags(int $period, int $limit = 10) + { + // 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 `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); + DI::cache()->set('global_trending_tags-' . $period . '-' . $limit, $tags, Duration::DAY); + return $tags; + } + + return []; } /** * Returns a list of the most frequent local hashtags over the given period * * @param int $period Period in hours to consider posts + * @param int $limit Number of returned tags * @return array * @throws \Exception */ public static function getLocalTrendingHashtags(int $period, $limit = 10) { - $tags = DI::cache()->get('local_trending_tags'); - - if (empty($tags)) { - $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score` - 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); - - if (DBA::isResult($tagsStmt)) { - $tags = DBA::toArray($tagsStmt); - DI::cache()->set('local_trending_tags', $tags, Duration::HOUR); - } + $tags = DI::cache()->get('local_trending_tags-' . $period . '-' . $limit); + if (!empty($tags)) { + return $tags; + } else { + return self::setLocalTrendingHashtags($period, $limit); + } + } + + /** + * Returns a list of the most frequent local hashtags over the given period + * + * @param int $period Period in hours to consider posts + * @param int $limit Number of returned tags + * @return array + * @throws \Exception + */ + public static function setLocalTrendingHashtags(int $period, int $limit = 10) + { + // 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 `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); + DI::cache()->set('local_trending_tags-' . $period . '-' . $limit, $tags, Duration::DAY); + return $tags; } - return $tags ?: []; + return []; } /**