]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/tag.php
move opening brace of class declaration to next line
[quix0rs-gnu-social.git] / actions / tag.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/actions/showstream.php');
23 define('TAGS_PER_PAGE', 100);
24
25 class TagAction extends StreamAction
26 {
27
28     function handle($args)
29     {
30
31         parent::handle($args);
32
33         # Looks like we're good; show the header
34
35         if (isset($args['tag']) && $args['tag']) {
36             $tag = $args['tag'];
37             common_show_header(sprintf(_("Notices tagged with %s"), $tag),
38                                array($this, 'show_header'), $tag,
39                                array($this, 'show_top'));
40             $this->show_notices($tag);
41         } else {
42             common_show_header(_("Tags"),
43                                array($this, 'show_header'), '',
44                                array($this, 'show_top'));
45             $this->show_tags();
46         }
47
48         common_show_footer();
49     }
50
51     function show_header($tag = false)
52     {
53         if ($tag) {
54             common_element('link', array('rel' => 'alternate',
55                                          'href' => common_local_url('tagrss', array('tag' => $tag)),
56                                          'type' => 'application/rss+xml',
57                                          'title' => sprintf(_('Feed for tag %s'), $tag)));
58         }
59     }
60
61     function get_instructions()
62     {
63         return _('Showing most popular tags from the last week');
64     }
65
66     function show_top($tag = false)
67     {
68         if (!$tag) {
69             $instr = $this->get_instructions();
70             $output = common_markup_to_html($instr);
71             common_element_start('div', 'instructions');
72             common_raw($output);
73             common_element_end('div');
74             $this->public_views_menu();
75         }
76         else {
77             $this->show_feeds_list(array(0=>array('href'=>common_local_url('tagrss'),
78                                                   'type' => 'rss',
79                                                   'version' => 'RSS 1.0',
80                                                   'item' => 'tagrss')));
81         }
82     }
83
84     function show_tags()
85     {
86         # This should probably be cached rather than recalculated
87         $tags = DB_DataObject::factory('Notice_tag');
88
89         #Need to clear the selection and then only re-add the field
90         #we are grouping by, otherwise it's not a valid 'group by'
91         #even though MySQL seems to let it slide...
92         $tags->selectAdd();
93         $tags->selectAdd('tag');
94
95         #Add the aggregated columns...
96         $tags->selectAdd('max(notice_id) as last_notice_id');
97         if(common_config('db','type')=='pgsql') {
98             $calc='sum(exp(-extract(epoch from (now()-created))/%s)) as weight';
99         } else {
100             $calc='sum(exp(-(now() - created)/%s)) as weight';
101         }
102         $tags->selectAdd(sprintf($calc, common_config('tag', 'dropoff')));
103         $tags->groupBy('tag');
104         $tags->orderBy('weight DESC');
105
106         # $tags->whereAdd('created > "' . strftime('%Y-%m-%d %H:%M:%S', strtotime('-1 MONTH')) . '"');
107
108         $tags->limit(TAGS_PER_PAGE);
109
110         $cnt = $tags->find();
111
112         if ($cnt > 0) {
113             common_element_start('p', 'tagcloud');
114
115             $tw = array();
116             $sum = 0;
117             while ($tags->fetch()) {
118                 $tw[$tags->tag] = $tags->weight;
119                 $sum += $tags->weight;
120             }
121
122             ksort($tw);
123
124             foreach ($tw as $tag => $weight) {
125                 $this->show_tag($tag, $weight, $weight/$sum);
126             }
127
128             common_element_end('p');
129         }
130     }
131
132     function show_tag($tag, $weight, $relative)
133     {
134
135         # XXX: these should probably tune to the size of the site
136         if ($relative > 0.1) {
137             $cls =  'largest';
138         } else if ($relative > 0.05) {
139             $cls = 'verylarge';
140         } else if ($relative > 0.02) {
141             $cls = 'large';
142         } else if ($relative > 0.01) {
143             $cls = 'medium';
144         } else if ($relative > 0.005) {
145             $cls = 'small';
146         } else if ($relative > 0.002) {
147             $cls = 'verysmall';
148         } else {
149             $cls = 'smallest';
150         }
151
152         common_element('a', array('class' => "$cls weight-$weight relative-$relative",
153                                   'href' => common_local_url('tag', array('tag' => $tag))),
154                        $tag);
155         common_text(' ');
156     }
157
158     function show_notices($tag)
159     {
160
161         $cnt = 0;
162
163         $page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
164
165         $notice = Notice_tag::getStream($tag, (($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
166
167         $cnt = $this->show_notice_list($notice);
168
169         common_pagination($page > 1, $cnt > NOTICES_PER_PAGE,
170                           $page, 'tag', array('tag' => $tag));
171     }
172 }