]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/profileaction.php
Added more checked type-hints.
[quix0rs-gnu-social.git] / lib / profileaction.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Common parent of Personal and Profile actions
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  Personal
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Sarven Capadisli <csarven@status.net>
26  * @copyright 2008-2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/profileminilist.php';
36 require_once INSTALLDIR.'/lib/groupminilist.php';
37
38 /**
39  * Profile action common superclass
40  *
41  * Abstracts out common code from profile and personal tabs
42  *
43  * @category Personal
44  * @package  StatusNet
45  * @author   Evan Prodromou <evan@status.net>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://status.net/
48  */
49 class ProfileAction extends ManagedAction
50 {
51     var $page    = null;
52     var $tag     = null;
53
54     protected $target  = null;    // Profile that we're showing
55
56     protected function prepare(array $args=array())
57     {
58         parent::prepare($args);
59
60         try {
61             $nickname_arg = $this->arg('nickname');
62             $nickname     = common_canonical_nickname($nickname_arg);
63
64             // Permanent redirect on non-canonical nickname
65
66             if ($nickname_arg != $nickname) {
67                 $args = array('nickname' => $nickname);
68                 if ($this->arg('page') && $this->arg('page') != 1) {
69                     $args['page'] = $this->arg['page'];
70                 }
71                 common_redirect(common_local_url($this->getActionName(), $args), 301);
72             }
73             $this->user = User::getKV('nickname', $nickname);
74
75             if (!$this->user) {
76                 // TRANS: Client error displayed when calling a profile action without specifying a user.
77                 $this->clientError(_('No such user.'), 404);
78             }
79
80             $this->target = $this->user->getProfile();
81         } catch (NicknameException $e) {
82             $id = (int)$this->arg('id');
83             $this->target = Profile::getKV('id', $id);
84
85             if (!$this->target instanceof Profile) {
86                 // TRANS: Error message displayed when referring to a user without a profile.
87                 $this->serverError(_m('Profile ID does not exist.'));
88             }
89
90             if ($this->target->isLocal()) {
91                 // For local users when accessed by id number, redirect to
92                 // the same action but using the nickname as argument.
93                 common_redirect(common_local_url($this->getActionName(),
94                                                 array('nickname'=>$user->getNickname())));
95             }
96         }
97
98         if ($this->target->hasRole(Profile_role::SILENCED) &&
99             (empty($this->scoped) || !$this->scoped->hasRight(Right::SILENCEUSER))) {
100             throw new ClientException(_('This profile has been silenced by site moderators'), 403);
101         }
102
103         // backwards compatibility until all actions are fixed to use $this->target
104         $this->profile = $this->target;
105
106         $this->tag = $this->trimmed('tag');
107         $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
108         common_set_returnto($this->selfUrl());
109         return true;
110     }
111
112     function isReadOnly(array $args=array())
113     {
114         return true;
115     }
116
117     function showSections()
118     {
119         $this->showSubscriptions();
120         $this->showSubscribers();
121         $this->showGroups();
122         $this->showLists();
123         $this->showStatistics();
124     }
125
126     /**
127      * Convenience function for common pattern of links to subscription/groups sections.
128      *
129      * @param string $actionClass
130      * @param string $title
131      * @param string $cssClass
132      */
133     private function statsSectionLink($actionClass, $title, $cssClass='')
134     {
135         $this->element('a', array('href' => common_local_url($actionClass,
136                                                              array('nickname' => $this->target->getNickname())),
137                                   'class' => $cssClass),
138                        $title);
139     }
140
141     function showSubscriptions()
142     {
143         $this->elementStart('div', array('id' => 'entity_subscriptions',
144                                          'class' => 'section'));
145         if (Event::handle('StartShowSubscriptionsMiniList', array($this))) {
146             $this->elementStart('h2');
147             // TRANS: H2 text for user subscription statistics.
148             $this->statsSectionLink('subscriptions', _('Following'));
149             $this->text(' ');
150             $this->text($this->target->subscriptionCount());
151             $this->elementEnd('h2');
152         
153             try {
154                 $profile = $this->target->getSubscribed(0, PROFILES_PER_MINILIST + 1);
155                 $pml = new ProfileMiniList($profile, $this);
156                 $pml->show();
157             } catch (NoResultException $e) {
158                 // TRANS: Text for user subscription statistics if the user has no subscription
159                 $this->element('p', null, _('(None)'));
160             }
161
162             Event::handle('EndShowSubscriptionsMiniList', array($this));
163         }
164         $this->elementEnd('div');
165     }
166
167     function showSubscribers()
168     {
169         $this->elementStart('div', array('id' => 'entity_subscribers',
170                                          'class' => 'section'));
171
172         if (Event::handle('StartShowSubscribersMiniList', array($this))) {
173
174             $this->elementStart('h2');
175             // TRANS: H2 text for user subscriber statistics.
176             $this->statsSectionLink('subscribers', _('Followers'));
177             $this->text(' ');
178             $this->text($this->target->subscriberCount());
179             $this->elementEnd('h2');
180
181             try {
182                 $profile = $this->target->getSubscribers(0, PROFILES_PER_MINILIST + 1);
183                 $sml = new SubscribersMiniList($profile, $this);
184                 $sml->show();
185             } catch (NoResultException $e) {
186                 // TRANS: Text for user subscriber statistics if user has no subscribers.
187                 $this->element('p', null, _('(None)'));
188             }
189
190             Event::handle('EndShowSubscribersMiniList', array($this));
191         }
192
193         $this->elementEnd('div');
194     }
195
196     function showStatistics()
197     {
198         $notice_count = $this->target->noticeCount();
199         $age_days     = (time() - strtotime($this->target->created)) / 86400;
200         if ($age_days < 1) {
201             // Rather than extrapolating out to a bajillion...
202             $age_days = 1;
203         }
204         $daily_count = round($notice_count / $age_days);
205
206         $this->elementStart('div', array('id' => 'entity_statistics',
207                                          'class' => 'section'));
208
209         // TRANS: H2 text for user statistics.
210         $this->element('h2', null, _('Statistics'));
211
212         $profile = $this->target;
213         $actionParams = array('nickname' => $profile->nickname);
214         $stats = array(
215             array(
216                 'id' => 'user-id',
217                 // TRANS: Label for user statistics.
218                 'label' => _('User ID'),
219                 'value' => $profile->id,
220             ),
221             array(
222                 'id' => 'member-since',
223                 // TRANS: Label for user statistics.
224                 'label' => _('Member since'),
225                 'value' => date('j M Y', strtotime($profile->created))
226             ),
227             array(
228                 'id' => 'notices',
229                 // TRANS: Label for user statistics.
230                 'label' => _('Notices'),
231                 'value' => $notice_count,
232             ),
233             array(
234                 'id' => 'daily_notices',
235                 // TRANS: Label for user statistics.
236                 // TRANS: Average count of posts made per day since account registration.
237                 'label' => _('Daily average'),
238                 'value' => $daily_count
239             )
240         );
241
242         // Give plugins a chance to add stats entries
243         Event::handle('ProfileStats', array($profile, &$stats));
244
245         foreach ($stats as $row) {
246             $this->showStatsRow($row);
247         }
248         $this->elementEnd('div');
249     }
250
251     private function showStatsRow($row)
252     {
253         $this->elementStart('dl', 'entity_' . $row['id']);
254         $this->elementStart('dt');
255         if (!empty($row['link'])) {
256             $this->element('a', array('href' => $row['link']), $row['label']);
257         } else {
258             $this->text($row['label']);
259         }
260         $this->elementEnd('dt');
261         $this->element('dd', null, $row['value']);
262         $this->elementEnd('dl');
263     }
264
265     function showGroups()
266     {
267         $groups = $this->target->getGroups(0, GROUPS_PER_MINILIST + 1);
268
269         $this->elementStart('div', array('id' => 'entity_groups',
270                                          'class' => 'section'));
271         if (Event::handle('StartShowGroupsMiniList', array($this))) {
272             $this->elementStart('h2');
273             // TRANS: H2 text for user group membership statistics.
274             $this->statsSectionLink('usergroups', _('Groups'));
275             $this->text(' ');
276             $this->text($this->target->getGroupCount());
277             $this->elementEnd('h2');
278
279             if ($groups instanceof User_group) {
280                 $gml = new GroupMiniList($groups, $this->target, $this);
281                 $cnt = $gml->show();
282             } else {
283                 // TRANS: Text for user user group membership statistics if user is not a member of any group.
284                 $this->element('p', null, _('(None)'));
285             }
286
287             Event::handle('EndShowGroupsMiniList', array($this));
288         }
289             $this->elementEnd('div');
290     }
291
292     function showLists()
293     {
294         $lists = $this->target->getLists($this->scoped);
295
296         if ($lists->N > 0) {
297             $this->elementStart('div', array('id' => 'entity_lists',
298                                              'class' => 'section'));
299
300             if (Event::handle('StartShowListsMiniList', array($this))) {
301
302                 $url = common_local_url('peopletagsbyuser',
303                                         array('nickname' => $this->target->getNickname()));
304
305                 $this->elementStart('h2');
306                 $this->element('a',
307                                array('href' => $url),
308                                // TRANS: H2 text for user list membership statistics.
309                                _('Lists'));
310                 $this->text(' ');
311                 $this->text($lists->N);
312                 $this->elementEnd('h2');
313
314                 $this->elementStart('ul');
315
316
317                 $first = true;
318
319                 while ($lists->fetch()) {
320                     if (!empty($lists->mainpage)) {
321                         $url = $lists->mainpage;
322                     } else {
323                         $url = common_local_url('showprofiletag',
324                                                 array('tagger' => $this->target->getNickname(),
325                                                       'tag'    => $lists->tag));
326                     }
327                     if (!$first) {
328                         $this->text(', ');
329                     } else {
330                         $first = false;
331                     }
332
333                     $this->element('a', array('href' => $url),
334                                    $lists->tag);
335                 }
336
337                 $this->elementEnd('ul');
338
339                 Event::handle('EndShowListsMiniList', array($this));
340             }
341             $this->elementEnd('div');
342         }
343     }
344 }
345
346 class SubscribersMiniList extends ProfileMiniList
347 {
348     function newListItem(Profile $profile)
349     {
350         return new SubscribersMiniListItem($profile, $this->action);
351     }
352 }
353
354 class SubscribersMiniListItem extends ProfileMiniListItem
355 {
356     function linkAttributes()
357     {
358         $aAttrs = parent::linkAttributes();
359         if (common_config('nofollow', 'subscribers')) {
360             $aAttrs['rel'] .= ' nofollow';
361         }
362         return $aAttrs;
363     }
364 }