]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/publictagcloud.php
Merge branch '0.8.x' into deleteuser
[quix0rs-gnu-social.git] / actions / publictagcloud.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Public tag cloud for notices
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Public
23  * @package   StatusNet
24  * @author    Mike Cochrane <mikec@mikenz.geek.nz>
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2008 Mike Cochrane
27  * @copyright 2008-2009 StatusNet, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29  * @link      http://status.net/
30  */
31
32 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
33
34 define('TAGS_PER_PAGE', 100);
35
36 /**
37  * Public tag cloud for notices
38  *
39  * @category Personal
40  * @package  StatusNet
41  * @author    Mike Cochrane <mikec@mikenz.geek.nz>
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2008 Mike Cochrane
44  * @copyright 2008-2009 StatusNet, Inc.
45  * @link     http://status.net/
46  */
47
48 class PublictagcloudAction extends Action
49 {
50     function isReadOnly($args)
51     {
52         return true;
53     }
54
55     function title()
56     {
57         return _('Public tag cloud');
58     }
59
60     function showPageNotice()
61     {
62         $this->element('p', 'instructions',
63                        sprintf(_('These are most popular recent tags on %s '),
64                                common_config('site', 'name')));
65     }
66
67     function showEmptyList()
68     {
69         $message = _('No one has posted a notice with a [hashtag](%%doc.tags%%) yet.') . ' ';
70
71         if (common_logged_in()) {
72             $message .= _('Be the first to post one!');
73         }
74         else {
75             $message .= sprintf(_('Why not [register an account](%%%%action.%s%%%%) and be the first to post one!'),
76                                 (!common_config('site','openidonly')) ? 'register' : 'openidlogin');
77         }
78
79         $this->elementStart('div', 'guide');
80         $this->raw(common_markup_to_html($message));
81         $this->elementEnd('div');
82     }
83
84     function showLocalNav()
85     {
86         $nav = new PublicGroupNav($this);
87         $nav->show();
88     }
89
90     function handle($args)
91     {
92         parent::handle($args);
93         $this->showPage();
94     }
95
96     function showContent()
97     {
98         # This should probably be cached rather than recalculated
99         $tags = new Notice_tag();
100
101         #Need to clear the selection and then only re-add the field
102         #we are grouping by, otherwise it's not a valid 'group by'
103         #even though MySQL seems to let it slide...
104         $tags->selectAdd();
105         $tags->selectAdd('tag');
106
107         #Add the aggregated columns...
108         $tags->selectAdd('max(notice_id) as last_notice_id');
109         if(common_config('db','type')=='pgsql') {
110             $calc='sum(exp(-extract(epoch from (now()-created))/%s)) as weight';
111         } else {
112             $calc='sum(exp(-(now() - created)/%s)) as weight';
113         }
114         $tags->selectAdd(sprintf($calc, common_config('tag', 'dropoff')));
115         $tags->groupBy('tag');
116         $tags->orderBy('weight DESC');
117
118         $tags->limit(TAGS_PER_PAGE);
119
120         $cnt = $tags->find();
121
122         if ($cnt > 0) {
123             $this->elementStart('div', array('id' => 'tagcloud',
124                                              'class' => 'section'));
125
126             $tw = array();
127             $sum = 0;
128             while ($tags->fetch()) {
129                 $tw[$tags->tag] = $tags->weight;
130                 $sum += $tags->weight;
131             }
132
133             ksort($tw);
134
135             $this->elementStart('dl');
136             $this->element('dt', null, _('Tag cloud'));
137             $this->elementStart('dd');
138             $this->elementStart('ul', 'tags xoxo tag-cloud');
139             foreach ($tw as $tag => $weight) {
140                 $this->showTag($tag, $weight, $weight/$sum);
141             }
142             $this->elementEnd('ul');
143             $this->elementEnd('dd');
144             $this->elementEnd('dl');
145             $this->elementEnd('div');
146         } else {
147             $this->showEmptyList();
148         }
149     }
150
151     function showTag($tag, $weight, $relative)
152     {
153         if ($relative > 0.1) {
154             $rel =  'tag-cloud-7';
155         } else if ($relative > 0.05) {
156             $rel = 'tag-cloud-6';
157         } else if ($relative > 0.02) {
158             $rel = 'tag-cloud-5';
159         } else if ($relative > 0.01) {
160             $rel = 'tag-cloud-4';
161         } else if ($relative > 0.005) {
162             $rel = 'tag-cloud-3';
163         } else if ($relative > 0.002) {
164             $rel = 'tag-cloud-2';
165         } else {
166             $rel = 'tag-cloud-1';
167         }
168
169         $this->elementStart('li', $rel);
170         $this->element('a', array('href' => common_local_url('tag', array('tag' => $tag))),
171                        $tag);
172         $this->elementEnd('li');
173     }
174 }