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);
24 define('AGE_FACTOR', 864000.0);
26 class TagAction extends StreamAction {
28 function handle($args) {
30 parent::handle($args);
32 # Looks like we're good; show the header
34 if (isset($args['tag']) && $args['tag']) {
36 common_show_header(sprintf(_("Notices tagged with %s"), $tag),
37 array($this, 'show_header'), $tag,
38 array($this, 'show_top'));
40 $this->show_notices($tag);
42 common_show_header(_("Tags"),
43 array($this, 'show_header'), '',
44 array($this, 'show_top'));
51 function show_header($tag = false) {
53 common_element('link', array('rel' => 'alternate',
54 'href' => common_local_url('tagrss', array('tag' => $tag)),
55 'type' => 'application/rss+xml',
56 'title' => sprintf(_('Feed for tag %s'), $tag)));
60 function get_instructions() {
61 return _('Showing most popular tags from the last week');
64 function show_top($tag = false) {
66 $instr = $this->get_instructions();
67 $output = common_markup_to_html($instr);
68 common_element_start('div', 'instructions');
70 common_element_end('div');
73 common_element_start('ul', array('id' => 'nav_views'));
75 common_menu_item(common_local_url('tags'),
80 common_menu_item(common_local_url('tag', array('tag' => $tag)),
82 sprintf(_("Notices tagged with %s"), $tag),
85 common_element_end('ul');
90 # This should probably be cached rather than recalculated
91 $tags = DB_DataObject::factory('Notice_tag');
92 $tags->selectAdd('max(notice_id) as last_notice_id');
93 $tags->selectAdd(sprintf('sum(exp(-(now() - created)/%f)) as weight', AGE_FACTOR));
94 $tags->groupBy('tag');
95 $tags->orderBy('weight DESC');
97 # $tags->whereAdd('created > "' . strftime('%Y-%m-%d %H:%M:%S', strtotime('-1 MONTH')) . '"');
99 $tags->limit(TAGS_PER_PAGE);
101 $cnt = $tags->find();
104 common_element_start('p', 'tagcloud');
108 while ($tags->fetch()) {
109 $tw[$tags->tag] = $tags->weight;
110 $sum += $tags->weight;
113 foreach ($tw as $tag => $weight) {
114 $this->show_tag($tag, $weight, $weight/$sum);
117 common_element_end('p');
121 function show_tag($tag, $weight, $relative) {
123 # XXX: these should probably tune to the size of the site
124 $cls = ($relative > 0.1) ? 'largest' :
125 ($relative > 0.05) ? 'verylarge' :
126 ($relative > 0.02) ? 'large' :
127 ($relative > 0.01) ? 'medium' :
128 ($relative > 0.005) ? 'small' :
129 ($relative > 0.002) ? 'verysmall' :
132 common_element('a', array('class' => "$cls weight-$weight relative-$relative",
133 'href' => common_local_url('tag', array('tag' => $tag))),
138 function show_notices($tag) {
140 $tags = DB_DataObject::factory('Notice_tag');
144 $tags->orderBy('created DESC');
146 $page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
148 $tags->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
150 $cnt = $tags->find();
153 common_element_start('ul', array('id' => 'notices'));
154 for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
155 if ($tags->fetch()) {
156 $notice = new Notice();
157 $notice->id = $tags->notice_id;
158 $result = $notice->find(true);
162 $this->show_notice($notice);
168 common_element_end('ul');
171 common_pagination($page > 1, $cnt > NOTICES_PER_PAGE,
172 $page, 'tag', array('tag' => $tag));