3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/actions/showstream.php');
23 define('TAGS_PER_PAGE', 100);
25 class TagAction extends StreamAction {
27 function handle($args) {
29 parent::handle($args);
31 # Looks like we're good; show the header
33 if (isset($args['tag']) && $args['tag']) {
35 common_show_header(sprintf(_("Notices tagged with %s"), $tag),
36 array($this, 'show_header'), $tag,
37 array($this, 'show_top'));
39 $this->show_notices($tag);
41 common_show_header(_("Tags"),
42 array($this, 'show_header'), '',
43 array($this, 'show_top'));
50 function show_header($tag = false) {
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)));
59 function get_instructions() {
60 return _('Showing most popular tags from the last week');
63 function show_top($tag = false) {
65 $instr = $this->get_instructions();
66 $output = common_markup_to_html($instr);
67 common_element_start('div', 'instructions');
69 common_element_end('div');
72 common_element_start('ul', array('id' => 'nav_views'));
74 common_menu_item(common_local_url('tags'),
79 common_menu_item(common_local_url('tag', array('tag' => $tag)),
81 sprintf(_("Notices tagged with %s"), $tag),
84 common_element_end('ul');
89 # This should probably be cached rather than recalculated
90 $tags = DB_DataObject::factory('Notice_tag');
92 #Need to clear the selection and then only re-add the field
93 #we are grouping by, otherwise it's not a valid 'group by'
94 #even though MySQL seems to let it slide...
96 $tags->selectAdd('tag');
98 #Add the aggregated columns...
99 $tags->selectAdd('max(notice_id) as last_notice_id');
100 if(common_config('db','type')=='pgsql') {
101 $calc='sum(exp(-extract(epoch from (now()-created))/%f)) as weight';
103 $calc='sum(exp(-(now() - created)/%f)) as weight';
105 $tags->selectAdd(sprintf($calc, common_config('tag', 'dropoff')));
106 $tags->groupBy('tag');
107 $tags->orderBy('weight DESC');
109 # $tags->whereAdd('created > "' . strftime('%Y-%m-%d %H:%M:%S', strtotime('-1 MONTH')) . '"');
111 $tags->limit(TAGS_PER_PAGE);
113 $cnt = $tags->find();
116 common_element_start('p', 'tagcloud');
120 while ($tags->fetch()) {
121 $tw[$tags->tag] = $tags->weight;
122 $sum += $tags->weight;
127 foreach ($tw as $tag => $weight) {
128 $this->show_tag($tag, $weight, $weight/$sum);
131 common_element_end('p');
135 function show_tag($tag, $weight, $relative) {
137 # XXX: these should probably tune to the size of the site
138 if ($relative > 0.1) {
140 } else if ($relative > 0.05) {
142 } else if ($relative > 0.02) {
144 } else if ($relative > 0.01) {
146 } else if ($relative > 0.005) {
148 } else if ($relative > 0.002) {
154 common_element('a', array('class' => "$cls weight-$weight relative-$relative",
155 'href' => common_local_url('tag', array('tag' => $tag))),
160 function show_notices($tag) {
162 $notices = Notice_tag::getStream($tag, (($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
165 common_element_start('ul', array('id' => 'notices'));
166 for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
167 if ($tags->fetch()) {
168 $notice = new Notice();
169 $notice->id = $tags->notice_id;
170 $result = $notice->find(true);
174 $this->show_notice($notice);
180 common_element_end('ul');
183 common_pagination($page > 1, $cnt > NOTICES_PER_PAGE,
184 $page, 'tag', array('tag' => $tag));