]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/tag.php
replace all tabs with four spaces
[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     function handle($args) {
28
29         parent::handle($args);
30
31         # Looks like we're good; show the header
32
33         if (isset($args['tag']) && $args['tag']) {
34             $tag = $args['tag'];
35             common_show_header(sprintf(_("Notices tagged with %s"), $tag),
36                                array($this, 'show_header'), $tag,
37                                array($this, 'show_top'));
38             $this->show_notices($tag);
39         } else {
40             common_show_header(_("Tags"),
41                                array($this, 'show_header'), '',
42                                array($this, 'show_top'));
43             $this->show_tags();
44         }
45
46         common_show_footer();
47     }
48
49     function show_header($tag = false) {
50         if ($tag) {
51             common_element('link', array('rel' => 'alternate',
52                                          'href' => common_local_url('tagrss', array('tag' => $tag)),
53                                          'type' => 'application/rss+xml',
54                                          'title' => sprintf(_('Feed for tag %s'), $tag)));
55         }
56     }
57
58     function get_instructions() {
59         return _('Showing most popular tags from the last week');
60     }
61
62     function show_top($tag = false) {
63         if (!$tag) {
64             $instr = $this->get_instructions();
65             $output = common_markup_to_html($instr);
66             common_element_start('div', 'instructions');
67             common_raw($output);
68             common_element_end('div');
69             $this->public_views_menu();
70         }
71         else {
72             $this->show_feeds_list(array(0=>array('href'=>common_local_url('tagrss'),
73                                                   'type' => 'rss',
74                                                   'version' => 'RSS 1.0',
75                                                   'item' => 'tagrss')));
76         }
77     }
78
79     function show_tags()
80     {
81         # This should probably be cached rather than recalculated
82         $tags = DB_DataObject::factory('Notice_tag');
83
84         #Need to clear the selection and then only re-add the field
85         #we are grouping by, otherwise it's not a valid 'group by'
86         #even though MySQL seems to let it slide...
87         $tags->selectAdd();
88         $tags->selectAdd('tag');
89
90         #Add the aggregated columns...
91         $tags->selectAdd('max(notice_id) as last_notice_id');
92         if(common_config('db','type')=='pgsql') {
93             $calc='sum(exp(-extract(epoch from (now()-created))/%s)) as weight';
94         } else {
95             $calc='sum(exp(-(now() - created)/%s)) as weight';
96         }
97         $tags->selectAdd(sprintf($calc, common_config('tag', 'dropoff')));
98         $tags->groupBy('tag');
99         $tags->orderBy('weight DESC');
100
101         # $tags->whereAdd('created > "' . strftime('%Y-%m-%d %H:%M:%S', strtotime('-1 MONTH')) . '"');
102
103         $tags->limit(TAGS_PER_PAGE);
104
105         $cnt = $tags->find();
106
107         if ($cnt > 0) {
108             common_element_start('p', 'tagcloud');
109
110             $tw = array();
111             $sum = 0;
112             while ($tags->fetch()) {
113                 $tw[$tags->tag] = $tags->weight;
114                 $sum += $tags->weight;
115             }
116
117             ksort($tw);
118
119             foreach ($tw as $tag => $weight) {
120                 $this->show_tag($tag, $weight, $weight/$sum);
121             }
122
123             common_element_end('p');
124         }
125     }
126
127     function show_tag($tag, $weight, $relative) {
128
129         # XXX: these should probably tune to the size of the site
130         if ($relative > 0.1) {
131             $cls =  'largest';
132         } else if ($relative > 0.05) {
133             $cls = 'verylarge';
134         } else if ($relative > 0.02) {
135             $cls = 'large';
136         } else if ($relative > 0.01) {
137             $cls = 'medium';
138         } else if ($relative > 0.005) {
139             $cls = 'small';
140         } else if ($relative > 0.002) {
141             $cls = 'verysmall';
142         } else {
143             $cls = 'smallest';
144         }
145
146         common_element('a', array('class' => "$cls weight-$weight relative-$relative",
147                                   'href' => common_local_url('tag', array('tag' => $tag))),
148                        $tag);
149         common_text(' ');
150     }
151
152     function show_notices($tag) {
153
154         $cnt = 0;
155
156         $page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
157
158         $notice = Notice_tag::getStream($tag, (($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
159
160         $cnt = $this->show_notice_list($notice);
161
162         common_pagination($page > 1, $cnt > NOTICES_PER_PAGE,
163                           $page, 'tag', array('tag' => $tag));
164     }
165 }