]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget/TrendingTags.php
15a06e029c974b16d9305bbc25bfd3e9ba1f0768
[friendica.git] / src / Content / Widget / TrendingTags.php
1 <?php
2
3 namespace Friendica\Content\Widget;
4
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;
10
11 class TrendingTags
12 {
13         /**
14          * @param string $content 'global' (all posts) or 'local' (this node's posts only)
15          * @param int    $period  Period in hours to consider posts
16          * @return string
17          * @throws \Exception
18          */
19         public static function getHTML($content = 'global', int $period = 24)
20         {
21                 if ($content == 'local') {
22                         $tags = self::getLocalTrendingTags($period);
23                 } else {
24                         $tags = self::getGlobalTrendingTags($period);
25                 }
26
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'),
31                         '$tags' => $tags,
32                 ]);
33
34                 return $o;
35         }
36
37         /**
38          * Returns a list of the most frequent global tags over the given period
39          *
40          * @param int $period Period in hours to consider posts
41          * @return array
42          * @throws \Exception
43          */
44         private static function getGlobalTrendingTags(int $period)
45         {
46                 $tags = Cache::get('global_trending_tags');
47
48                 if (!$tags) {
49                         $tagsStmt = DBA::p("SELECT t.`term`, COUNT(*) AS `score`
50 FROM `term` t
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`
57   AND t.`uid` = 0
58   AND t.`otype` = ?
59   AND t.`type` = ?
60   AND t.`term` != ''
61   AND i.`received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
62 GROUP BY `term`
63 ORDER BY `score` DESC
64 LIMIT 20", Term::OBJECT_TYPE_POST, Term::HASHTAG, $period);
65
66                         if (DBA::isResult($tags)) {
67                                 $tags = DBA::toArray($tagsStmt);
68                                 Cache::set('global_trending_tags', $tags, Cache::HOUR);
69                         }
70                 }
71
72                 return $tags ?: [];
73         }
74
75         /**
76          * Returns a list of the most frequent local tags over the given period
77          *
78          * @param int $period Period in hours to consider posts
79          * @return array
80          * @throws \Exception
81          */
82         private static function getLocalTrendingTags(int $period)
83         {
84                 $tags = Cache::get('local_trending_tags');
85
86                 if (!$tags) {
87                         $tagsStmt = DBA::p("SELECT t.`term`, COUNT(*) AS `score`
88 FROM `term` t
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`
96   AND `thread`.`wall`
97   AND `thread`.`origin`
98   AND t.`otype` = ?
99   AND t.`type` = ?
100   AND t.`term` != ''
101   AND i.`received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
102 GROUP BY `term`
103 ORDER BY `score` DESC
104 LIMIT 20", Term::OBJECT_TYPE_POST, Term::HASHTAG, $period);
105
106                         if (DBA::isResult($tags)) {
107                                 $tags = DBA::toArray($tagsStmt);
108                                 Cache::set('local_trending_tags', $tags, Cache::HOUR);
109                         }
110                 }
111
112                 return $tags ?: [];
113         }
114 }