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