]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/publictagcloud.php
Grrr, not fixed. So adding debug lines.
[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 class PublictagcloudAction extends Action
48 {
49     function isReadOnly(array $args=array())
50     {
51         return true;
52     }
53
54     function title()
55     {
56         // TRANS: Title for public tag cloud.
57         return _('Public tag cloud');
58     }
59
60     function showPageNotice()
61     {
62         $this->element('p', 'instructions',
63                        // TRANS: Instructions (more used like an explanation/header).
64                        // TRANS: %s is the StatusNet sitename.
65                        sprintf(_('These are most popular recent tags on %s'),
66                                common_config('site', 'name')));
67     }
68
69     function showEmptyList()
70     {
71         // TRANS: This message contains a Markdown URL. The link description is between
72         // TRANS: square brackets, and the link between parentheses. Do not separate "]("
73         // TRANS: and do not change the URL part.
74         $message = _('No one has posted a notice with a [hashtag](%%doc.tags%%) yet.') . ' ';
75
76         if (common_logged_in()) {
77             // TRANS: Message shown to a logged in user for the public tag cloud
78             // TRANS: while no tags exist yet. "One" refers to the non-existing hashtag.
79             $message .= _('Be the first to post one!');
80         }
81         else {
82             // TRANS: Message shown to a anonymous user for the public tag cloud
83             // TRANS: while no tags exist yet. "One" refers to the non-existing hashtag.
84             // TRANS: This message contains a Markdown URL. The link description is between
85             // TRANS: square brackets, and the link between parentheses. Do not separate "]("
86             // TRANS: and do not change the URL part.
87             $message .= _('Why not [register an account](%%action.register%%) and be the first to post one!');
88         }
89
90         $this->elementStart('div', 'guide');
91         $this->raw(common_markup_to_html($message));
92         $this->elementEnd('div');
93     }
94
95     function handle(array $args=array())
96     {
97         parent::handle($args);
98         $this->showPage();
99     }
100
101     function showContent()
102     {
103         // This should probably be cached rather than recalculated
104         $tags = new Notice_tag();
105
106         /*
107          * Need to clear the selection and then only re-add the field
108          * we are grouping by, otherwise it's not a valid 'group by'
109          * even though MySQL seems to let it slide...
110          */
111         $tags->selectAdd();
112         $tags->selectAdd('tag');
113         $tags->selectAdd('notice_id');
114         $tags->selectAdd('scope');
115
116         // Add the aggregated columns...
117         $tags->selectAdd('max(notice_id) as last_notice_id');
118         $calc = common_sql_weight('created', common_config('tag', 'dropoff'));
119         $cutoff = sprintf("notice_tag.created > '%s'",
120                           common_sql_date(time() - common_config('tag', 'cutoff')));
121         $tags->selectAdd($calc . ' as weight');
122         $tags->joinAdd(array('notice_id', 'notice:id'));
123         $tags->whereAdd($cutoff);
124         $tags->groupBy('tag');
125         $tags->orderBy('weight DESC');
126
127         $tags->limit(TAGS_PER_PAGE);
128
129         $cnt = $tags->find();
130
131         if ($cnt > 0) {
132             $this->elementStart('div', array('id' => 'tagcloud',
133                                              'class' => 'section'));
134
135             $tw = array();
136             $sum = 0;
137             while ($tags->fetch()) {
138                 // Check scope:
139
140                 // 1) Get notice object and set id
141                 $notice = new Notice();
142                 $notice->id    = $tags->notice_id;
143                 $notice->scope = $tags->scope;
144                 /* NOISY-DEBUG: */ common_debug('[' . __METHOD__ . ':' . __LINE__ . '] tags->tag=' . $tags->tag . ',notice->id=' . $notice->id . ',notice->scope=' . $notice->scope);
145
146                 // Is it private scope?
147                 if ($notice->isPrivateScope()) {
148                     // 2) Get current profile
149                     $profile = Profile::current();
150
151                     // Is the profile not set?
152                     if (!$profile instanceof Profile) {
153                         // Public viewer shall not see a tag from a private dent (privacy leak)
154                         /* NOISY-DEBUG: */ common_debug('[' . __METHOD__ . ':' . __LINE__ . '] Not logged in, skipping ...');
155                         continue;
156                     } elseif (!$notice->inScope($profile)) {
157                         // Current profile is not in scope (not allowed to see) of notice
158                         /* NOISY-DEBUG: */ common_debug('[' . __METHOD__ . ':' . __LINE__ . '] profile->id=' . $profile->id . ' is not allowed to see this tag, skipping ...');
159                         continue;
160                     }
161                 }
162
163                 /* NOISY-DEBUG: */ common_debug('[' . __METHOD__ . ':' . __LINE__ . '] tags->tag=' . $tags->tag . ',tags->weight=' . $tags->weight . ' - Added!');
164                 $tw[$tags->tag] = $tags->weight;
165                 $sum += $tags->weight;
166             }
167
168             ksort($tw);
169
170             $this->elementStart('ul', 'tags xoxo tag-cloud');
171             foreach ($tw as $tag => $weight) {
172                 if ($sum) {
173                     $weightedSum = $weight/$sum;
174                 } else {
175                     $weightedSum = 0.5;
176                 }
177                 $this->showTag($tag, $weight, $weightedSum);
178             }
179             $this->elementEnd('ul');
180
181             $this->elementEnd('div');
182         } else {
183             $this->showEmptyList();
184         }
185     }
186
187     function showTag($tag, $weight, $relative)
188     {
189         if ($relative > 0.1) {
190             $rel =  'tag-cloud-7';
191         } else if ($relative > 0.05) {
192             $rel = 'tag-cloud-6';
193         } else if ($relative > 0.02) {
194             $rel = 'tag-cloud-5';
195         } else if ($relative > 0.01) {
196             $rel = 'tag-cloud-4';
197         } else if ($relative > 0.005) {
198             $rel = 'tag-cloud-3';
199         } else if ($relative > 0.002) {
200             $rel = 'tag-cloud-2';
201         } else {
202             $rel = 'tag-cloud-1';
203         }
204
205         $this->elementStart('li', $rel);
206         $this->element('a', array('href' => common_local_url('tag', array('tag' => $tag))),
207                        $tag);
208         $this->elementEnd('li');
209     }
210 }