3 namespace Friendica\Content\Widget;
5 use Friendica\Core\Cache;
6 use Friendica\Core\L10n;
7 use Friendica\Core\Renderer;
8 use Friendica\Database\DBA;
9 use Friendica\Model\Term;
14 * @param string $content 'global' (all posts) or 'local' (this node's posts only)
15 * @param int $period Period in hours to consider posts
19 public static function getHTML($content = 'global', int $period = 24)
21 if ($content == 'local') {
22 $tags = self::getLocalTrendingTags($period);
24 $tags = self::getGlobalTrendingTags($period);
27 $tpl = Renderer::getMarkupTemplate('widget/trending_tags.tpl');
28 $o = Renderer::replaceMacros($tpl, [
29 '$title' => L10n::tt('Trending Tags (last %d hour)', 'Trending Tags (last %d hours)', $period),
30 '$more' => L10n::t('More Trending Tags'),
38 * Returns a list of the most frequent global tags over the given period
40 * @param int $period Period in hours to consider posts
44 private static function getGlobalTrendingTags(int $period)
46 $tags = Cache::get('global_trending_tags');
49 $tagsStmt = DBA::p("SELECT t.`term`, COUNT(*) AS `score`
51 JOIN `item` i ON i.`id` = t.`oid` AND i.`uid` = t.`uid`
52 JOIN `thread` ON `thread`.`iid` = i.`id`
53 WHERE `thread`.`visible`
54 AND NOT `thread`.`deleted`
55 AND NOT `thread`.`moderated`
56 AND NOT `thread`.`private`
61 AND i.`received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
64 LIMIT 20", Term::OBJECT_TYPE_POST, Term::HASHTAG, $period);
66 if (DBA::isResult($tags)) {
67 $tags = DBA::toArray($tagsStmt);
68 Cache::set('global_trending_tags', $tags, Cache::HOUR);
76 * Returns a list of the most frequent local tags over the given period
78 * @param int $period Period in hours to consider posts
82 private static function getLocalTrendingTags(int $period)
84 $tags = Cache::get('local_trending_tags');
87 $tagsStmt = DBA::p("SELECT t.`term`, COUNT(*) AS `score`
89 JOIN `item` i ON i.`id` = t.`oid` AND i.`uid` = t.`uid`
90 JOIN `thread` ON `thread`.`iid` = i.`id`
91 JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
92 WHERE `thread`.`visible`
93 AND NOT `thread`.`deleted`
94 AND NOT `thread`.`moderated`
95 AND NOT `thread`.`private`
101 AND i.`received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
103 ORDER BY `score` DESC
104 LIMIT 20", Term::OBJECT_TYPE_POST, Term::HASHTAG, $period);
106 if (DBA::isResult($tags)) {
107 $tags = DBA::toArray($tagsStmt);
108 Cache::set('local_trending_tags', $tags, Cache::HOUR);