profileaction.php 13 KB

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