We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

profileaction.php 12.6 KB
Newer Older
1 2
<?php
/**
3
 * StatusNet, the distributed open-source microblogging tool
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Common parent of Personal and Profile actions
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  Personal
23
 * @package   StatusNet
24 25
 * @author    Evan Prodromou <evan@status.net>
 * @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('GNUSOCIAL')) { exit(1); }
32 33 34 35 36 37 38

/**
 * Profile action common superclass
 *
 * Abstracts out common code from profile and personal tabs
 *
 * @category Personal
39
 * @package  StatusNet
40
 * @author   Evan Prodromou <evan@status.net>
41
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
42
 * @link     http://status.net/
43
 */
44
abstract class ProfileAction extends ManagedAction
45
{
46 47
    var $page    = null;
    var $tag     = null;
48

49 50
    protected $target  = null;    // Profile that we're showing

51
    protected function doPreparation()
52
    {
53 54 55
        try {
            $nickname_arg = $this->arg('nickname');
            $nickname     = common_canonical_nickname($nickname_arg);
56

57
            // Permanent redirect on non-canonical nickname
58

59 60 61 62 63
            if ($nickname_arg != $nickname) {
                $args = array('nickname' => $nickname);
                if ($this->arg('page') && $this->arg('page') != 1) {
                    $args['page'] = $this->arg['page'];
                }
64
                common_redirect(common_local_url($this->getActionName(), $args), 301);
65
            }
66
            $this->user = User::getKV('nickname', $nickname);
67

68 69 70 71
            if (!$this->user) {
                // TRANS: Client error displayed when calling a profile action without specifying a user.
                $this->clientError(_('No such user.'), 404);
            }
72

73 74 75 76
            $this->target = $this->user->getProfile();
        } catch (NicknameException $e) {
            $id = (int)$this->arg('id');
            $this->target = Profile::getKV('id', $id);
77

78
            if (!$this->target instanceof Profile) {
79 80 81
                // TRANS: Error message displayed when referring to a user without a profile.
                $this->serverError(_m('Profile ID does not exist.'));
            }
82

83 84 85 86 87
            if ($this->target->isLocal()) {
                // For local users when accessed by id number, redirect to
                // the same action but using the nickname as argument.
                common_redirect(common_local_url($this->getActionName(),
                                                array('nickname'=>$user->getNickname())));
88
            }
89 90
        }

91 92
        if ($this->target->hasRole(Profile_role::SILENCED) &&
            (empty($this->scoped) || !$this->scoped->hasRight(Right::SILENCEUSER))) {
93 94 95
            throw new ClientException(_('This profile has been silenced by site moderators'), 403);
        }

96 97 98
        // backwards compatibility until all actions are fixed to use $this->target
        $this->profile = $this->target;

99
        $this->tag = $this->trimmed('tag');
100 101
        $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
        common_set_returnto($this->selfUrl());
102 103 104 105 106 107 108

        return $this->profileActionPreparation();
    }

    protected function profileActionPreparation()
    {
        // No-op by default.
109 110
    }

111 112 113 114 115
    function isReadOnly($args)
    {
        return true;
    }

116 117 118 119 120
    function showSections()
    {
        $this->showSubscriptions();
        $this->showSubscribers();
        $this->showGroups();
Evan Prodromou's avatar
Evan Prodromou committed
121
        $this->showLists();
122 123 124
        $this->showStatistics();
    }

125 126 127 128 129 130 131 132 133 134
    /**
     * Convenience function for common pattern of links to subscription/groups sections.
     *
     * @param string $actionClass
     * @param string $title
     * @param string $cssClass
     */
    private function statsSectionLink($actionClass, $title, $cssClass='')
    {
        $this->element('a', array('href' => common_local_url($actionClass,
135
                                                             array('nickname' => $this->target->getNickname())),
136 137 138 139
                                  'class' => $cssClass),
                       $title);
    }

140 141 142 143
    function showSubscriptions()
    {
        $this->elementStart('div', array('id' => 'entity_subscriptions',
                                         'class' => 'section'));
144
        if (Event::handle('StartShowSubscriptionsMiniList', array($this))) {
145
            $this->elementStart('h2');
146
            // TRANS: H2 text for user subscription statistics.
147 148
            $this->statsSectionLink('subscriptions', _('Following'));
            $this->text(' ');
149
            $this->text($this->target->subscriptionCount());
150
            $this->elementEnd('h2');
151 152
        
            try {
153
                $profile = $this->target->getSubscribed(0, PROFILES_PER_MINILIST + 1);
154
                $pml = new ProfileMiniList($profile, $this);
155 156 157 158
                $pml->show();
            } catch (NoResultException $e) {
                // TRANS: Text for user subscription statistics if the user has no subscription
                $this->element('p', null, _('(None)'));
159 160
            }

161 162
            Event::handle('EndShowSubscriptionsMiniList', array($this));
        }
163 164 165 166 167 168 169 170
        $this->elementEnd('div');
    }

    function showSubscribers()
    {
        $this->elementStart('div', array('id' => 'entity_subscribers',
                                         'class' => 'section'));

171
        if (Event::handle('StartShowSubscribersMiniList', array($this))) {
172

173
            $this->elementStart('h2');
174
            // TRANS: H2 text for user subscriber statistics.
175 176
            $this->statsSectionLink('subscribers', _('Followers'));
            $this->text(' ');
177
            $this->text($this->target->subscriberCount());
178
            $this->elementEnd('h2');
179

180
            try {
181
                $profile = $this->target->getSubscribers(0, PROFILES_PER_MINILIST + 1);
Evan Prodromou's avatar
Evan Prodromou committed
182
                $sml = new SubscribersMiniList($profile, $this);
183 184 185 186
                $sml->show();
            } catch (NoResultException $e) {
                // TRANS: Text for user subscriber statistics if user has no subscribers.
                $this->element('p', null, _('(None)'));
187 188
            }

189
            Event::handle('EndShowSubscribersMiniList', array($this));
190 191 192 193 194 195 196
        }

        $this->elementEnd('div');
    }

    function showStatistics()
    {
197 198
        $notice_count = $this->target->noticeCount();
        $age_days     = (time() - strtotime($this->target->created)) / 86400;
199 200 201 202 203
        if ($age_days < 1) {
            // Rather than extrapolating out to a bajillion...
            $age_days = 1;
        }
        $daily_count = round($notice_count / $age_days);
204 205 206 207

        $this->elementStart('div', array('id' => 'entity_statistics',
                                         'class' => 'section'));

208
        // TRANS: H2 text for user statistics.
209 210
        $this->element('h2', null, _('Statistics'));

211
        $profile = $this->target;
212 213 214 215
        $actionParams = array('nickname' => $profile->nickname);
        $stats = array(
            array(
                'id' => 'user-id',
216
                // TRANS: Label for user statistics.
217 218 219 220 221
                'label' => _('User ID'),
                'value' => $profile->id,
            ),
            array(
                'id' => 'member-since',
222
                // TRANS: Label for user statistics.
223 224 225 226 227
                'label' => _('Member since'),
                'value' => date('j M Y', strtotime($profile->created))
            ),
            array(
                'id' => 'notices',
228
                // TRANS: Label for user statistics.
229 230 231 232 233
                'label' => _('Notices'),
                'value' => $notice_count,
            ),
            array(
                'id' => 'daily_notices',
234 235
                // TRANS: Label for user statistics.
                // TRANS: Average count of posts made per day since account registration.
236 237 238 239 240 241 242 243 244 245 246
                'label' => _('Daily average'),
                'value' => $daily_count
            )
        );

        // Give plugins a chance to add stats entries
        Event::handle('ProfileStats', array($profile, &$stats));

        foreach ($stats as $row) {
            $this->showStatsRow($row);
        }
247 248 249
        $this->elementEnd('div');
    }

250
    private function showStatsRow($row)
251
    {
252
        $this->elementStart('dl', 'entity_' . $row['id']);
253
        $this->elementStart('dt');
254 255
        if (!empty($row['link'])) {
            $this->element('a', array('href' => $row['link']), $row['label']);
256
        } else {
257
            $this->text($row['label']);
258 259
        }
        $this->elementEnd('dt');
260
        $this->element('dd', null, $row['value']);
261 262 263
        $this->elementEnd('dl');
    }

264 265
    function showGroups()
    {
266
        $groups = $this->target->getGroups(0, GROUPS_PER_MINILIST + 1);
267 268 269

        $this->elementStart('div', array('id' => 'entity_groups',
                                         'class' => 'section'));
270
        if (Event::handle('StartShowGroupsMiniList', array($this))) {
271
            $this->elementStart('h2');
272
            // TRANS: H2 text for user group membership statistics.
273
            $this->statsSectionLink('usergroups', _('Groups'));
274
            $this->text(' ');
275
            $this->text($this->target->getGroupCount());
276
            $this->elementEnd('h2');
277

278
            if ($groups instanceof User_group) {
279
                $gml = new GroupMiniList($groups, $this->target, $this);
280
                $cnt = $gml->show();
281 282 283
            } else {
                // TRANS: Text for user user group membership statistics if user is not a member of any group.
                $this->element('p', null, _('(None)'));
284
            }
285

286
            Event::handle('EndShowGroupsMiniList', array($this));
287
        }
288
            $this->elementEnd('div');
289
    }
Evan Prodromou's avatar
Evan Prodromou committed
290 291 292

    function showLists()
    {
293
        $lists = $this->target->getLists($this->scoped);
Evan Prodromou's avatar
Evan Prodromou committed
294 295 296 297 298 299 300

        if ($lists->N > 0) {
            $this->elementStart('div', array('id' => 'entity_lists',
                                             'class' => 'section'));

            if (Event::handle('StartShowListsMiniList', array($this))) {

301
                $url = common_local_url('peopletagsbyuser',
302
                                        array('nickname' => $this->target->getNickname()));
303

Evan Prodromou's avatar
Evan Prodromou committed
304
                $this->elementStart('h2');
305 306
                $this->element('a',
                               array('href' => $url),
307
                               // TRANS: H2 text for user list membership statistics.
308
                               _('Lists'));
Evan Prodromou's avatar
Evan Prodromou committed
309 310 311 312 313 314 315
                $this->text(' ');
                $this->text($lists->N);
                $this->elementEnd('h2');

                $this->elementStart('ul');


316 317
                $first = true;

Evan Prodromou's avatar
Evan Prodromou committed
318
                while ($lists->fetch()) {
319 320 321 322
                    if (!empty($lists->mainpage)) {
                        $url = $lists->mainpage;
                    } else {
                        $url = common_local_url('showprofiletag',
323
                                                array('tagger' => $this->target->getNickname(),
324
                                                      'tag'    => $lists->tag));
Evan Prodromou's avatar
Evan Prodromou committed
325
                    }
326 327 328 329 330 331 332 333
                    if (!$first) {
                        $this->text(', ');
                    } else {
                        $first = false;
                    }

                    $this->element('a', array('href' => $url),
                                   $lists->tag);
Evan Prodromou's avatar
Evan Prodromou committed
334 335 336 337 338 339 340 341 342
                }

                $this->elementEnd('ul');

                Event::handle('EndShowListsMiniList', array($this));
            }
            $this->elementEnd('div');
        }
    }
343 344
}

Evan Prodromou's avatar
Evan Prodromou committed
345 346 347 348 349 350 351 352 353 354
class SubscribersMiniList extends ProfileMiniList
{
    function newListItem($profile)
    {
        return new SubscribersMiniListItem($profile, $this->action);
    }
}

class SubscribersMiniListItem extends ProfileMiniListItem
{
355
    function linkAttributes()
Evan Prodromou's avatar
Evan Prodromou committed
356
    {
357 358 359
        $aAttrs = parent::linkAttributes();
        if (common_config('nofollow', 'subscribers')) {
            $aAttrs['rel'] .= ' nofollow';
Evan Prodromou's avatar
Evan Prodromou committed
360
        }
361
        return $aAttrs;
Evan Prodromou's avatar
Evan Prodromou committed
362 363
    }
}