3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
23 * Table Definition for profile
25 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
27 class Profile extends Memcached_DataObject
30 /* the code below is auto generated do not remove the above tag */
32 public $__table = 'profile'; // table name
33 public $id; // int(4) primary_key not_null
34 public $nickname; // varchar(64) multiple_key not_null
35 public $fullname; // varchar(255) multiple_key
36 public $profileurl; // varchar(255)
37 public $homepage; // varchar(255) multiple_key
38 public $bio; // text() multiple_key
39 public $location; // varchar(255) multiple_key
40 public $lat; // decimal(10,7)
41 public $lon; // decimal(10,7)
42 public $location_id; // int(4)
43 public $location_ns; // int(4)
44 public $created; // datetime() not_null
45 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
48 function staticGet($k,$v=NULL) {
49 return Memcached_DataObject::staticGet('Profile',$k,$v);
52 /* the code above is auto generated do not remove the tag below */
57 return User::staticGet('id', $this->id);
60 function getAvatar($width, $height=null)
62 if (is_null($height)) {
65 return Avatar::pkeyGet(array('profile_id' => $this->id,
67 'height' => $height));
70 function getOriginalAvatar()
72 $avatar = DB_DataObject::factory('avatar');
73 $avatar->profile_id = $this->id;
74 $avatar->original = true;
75 if ($avatar->find(true)) {
82 function setOriginal($filename)
84 $imagefile = new ImageFile($this->id, Avatar::path($filename));
86 $avatar = new Avatar();
87 $avatar->profile_id = $this->id;
88 $avatar->width = $imagefile->width;
89 $avatar->height = $imagefile->height;
90 $avatar->mediatype = image_type_to_mime_type($imagefile->type);
91 $avatar->filename = $filename;
92 $avatar->original = true;
93 $avatar->url = Avatar::url($filename);
94 $avatar->created = DB_DataObject_Cast::dateTime(); # current time
96 # XXX: start a transaction here
98 if (!$this->delete_avatars() || !$avatar->insert()) {
99 @unlink(Avatar::path($filename));
103 foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
104 # We don't do a scaled one if original is our scaled size
105 if (!($avatar->width == $size && $avatar->height == $size)) {
106 $scaled_filename = $imagefile->resize($size);
108 //$scaled = DB_DataObject::factory('avatar');
109 $scaled = new Avatar();
110 $scaled->profile_id = $this->id;
111 $scaled->width = $size;
112 $scaled->height = $size;
113 $scaled->original = false;
114 $scaled->mediatype = image_type_to_mime_type($imagefile->type);
115 $scaled->filename = $scaled_filename;
116 $scaled->url = Avatar::url($scaled_filename);
117 $scaled->created = DB_DataObject_Cast::dateTime(); # current time
119 if (!$scaled->insert()) {
129 * Delete attached avatars for this user from the database and filesystem.
130 * This should be used instead of a batch delete() to ensure that files
131 * get removed correctly.
133 * @param boolean $original true to delete only the original-size file
136 function delete_avatars($original=true)
138 $avatar = new Avatar();
139 $avatar->profile_id = $this->id;
141 while ($avatar->fetch()) {
142 if ($avatar->original) {
143 if ($original == false) {
153 * Gets either the full name (if filled) or the nickname.
157 function getBestName()
159 return ($this->fullname) ? $this->fullname : $this->nickname;
163 * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
164 * if no fullname is provided.
168 function getFancyName()
170 if ($this->fullname) {
171 // TRANS: Full name of a profile or group followed by nickname in parens
172 return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
174 return $this->nickname;
179 * Get the most recent notice posted by this user, if any.
181 * @return mixed Notice or null
184 function getCurrentNotice()
186 $notice = $this->getNotices(0, 1);
188 if ($notice->fetch()) {
189 if ($notice instanceof ArrayWrapper) {
190 // hack for things trying to work with single notices
191 return $notice->_items[0];
199 function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
201 $ids = Notice::stream(array($this, '_streamTaggedDirect'),
203 'profile:notice_ids_tagged:' . $this->id . ':' . $tag,
204 $offset, $limit, $since_id, $max_id);
205 return Notice::getStreamByIds($ids);
208 function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
210 // XXX: I'm not sure this is going to be any faster. It probably isn't.
211 $ids = Notice::stream(array($this, '_streamDirect'),
213 'profile:notice_ids:' . $this->id,
214 $offset, $limit, $since_id, $max_id);
216 return Notice::getStreamByIds($ids);
219 function _streamTaggedDirect($tag, $offset, $limit, $since_id, $max_id)
221 // XXX It would be nice to do this without a join
222 // (necessary to do it efficiently on accounts with long history)
224 $notice = new Notice();
227 "select id from notice join notice_tag on id=notice_id where tag='".
228 $notice->escape($tag) .
229 "' and profile_id=" . intval($this->id);
231 $since = Notice::whereSinceId($since_id, 'id', 'notice.created');
233 $query .= " and ($since)";
236 $max = Notice::whereMaxId($max_id, 'id', 'notice.created');
238 $query .= " and ($max)";
241 $query .= ' order by notice.created DESC, id DESC';
243 if (!is_null($offset)) {
244 $query .= " LIMIT " . intval($limit) . " OFFSET " . intval($offset);
247 $notice->query($query);
251 while ($notice->fetch()) {
252 $ids[] = $notice->id;
258 function _streamDirect($offset, $limit, $since_id, $max_id)
260 $notice = new Notice();
262 $notice->profile_id = $this->id;
264 $notice->selectAdd();
265 $notice->selectAdd('id');
267 Notice::addWhereSinceId($notice, $since_id);
268 Notice::addWhereMaxId($notice, $max_id);
270 $notice->orderBy('created DESC, id DESC');
272 if (!is_null($offset)) {
273 $notice->limit($offset, $limit);
280 while ($notice->fetch()) {
281 $ids[] = $notice->id;
287 function isMember($group)
289 $mem = new Group_member();
291 $mem->group_id = $group->id;
292 $mem->profile_id = $this->id;
301 function isAdmin($group)
303 $mem = new Group_member();
305 $mem->group_id = $group->id;
306 $mem->profile_id = $this->id;
316 function getGroups($offset=0, $limit=null)
319 'SELECT user_group.* ' .
320 'FROM user_group JOIN group_member '.
321 'ON user_group.id = group_member.group_id ' .
322 'WHERE group_member.profile_id = %d ' .
323 'ORDER BY group_member.created DESC ';
325 if ($offset>0 && !is_null($limit)) {
327 if (common_config('db','type') == 'pgsql') {
328 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
330 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
335 $groups = new User_group();
337 $cnt = $groups->query(sprintf($qry, $this->id));
342 function isTagged($peopletag)
344 $tag = Profile_tag::pkeyGet(array('tagger' => $peopletag->tagger,
345 'tagged' => $this->id,
346 'tag' => $peopletag->tag));
350 function canTag($tagged)
352 if (empty($tagged)) {
356 if ($tagged->id == $this->id) {
360 $all = common_config('peopletag', 'allow_tagging', 'all');
361 $local = common_config('peopletag', 'allow_tagging', 'local');
362 $remote = common_config('peopletag', 'allow_tagging', 'remote');
363 $subs = common_config('peopletag', 'allow_tagging', 'subs');
369 $tagged_user = $tagged->getUser();
370 if (!empty($tagged_user)) {
375 return (Subscription::exists($this, $tagged) ||
376 Subscription::exists($tagged, $this));
377 } else if ($remote) {
383 function getOwnedTags($auth_user, $offset=0, $limit=null, $since_id=0, $max_id=0)
385 $tags = new Profile_list();
386 $tags->tagger = $this->id;
388 if (($auth_user instanceof User || $auth_user instanceof Profile) &&
389 $auth_user->id === $this->id) {
390 // no condition, get both private and public tags
392 $tags->private = false;
395 $tags->selectAdd('id as "cursor"');
398 $tags->whereAdd('id > '.$since_id);
402 $tags->whereAdd('id <= '.$max_id);
405 if($offset>=0 && !is_null($limit)) {
406 $tags->limit($offset, $limit);
409 $tags->orderBy('id DESC');
415 function getOtherTags($auth_user=null, $offset=0, $limit=null, $since_id=0, $max_id=0)
417 $lists = new Profile_list();
419 $tags = new Profile_tag();
420 $tags->tagged = $this->id;
422 $lists->joinAdd($tags);
423 #@fixme: postgres (round(date_part('epoch', my_date)))
424 $lists->selectAdd('unix_timestamp(profile_tag.modified) as "cursor"');
426 if ($auth_user instanceof User || $auth_user instanceof Profile) {
427 $lists->whereAdd('( ( profile_list.private = false ) ' .
428 'OR ( profile_list.tagger = ' . $auth_user->id . ' AND ' .
429 'profile_list.private = true ) )');
431 $lists->private = false;
435 $lists->whereAdd('cursor > '.$since_id);
439 $lists->whereAdd('cursor <= '.$max_id);
442 if($offset>=0 && !is_null($limit)) {
443 $lists->limit($offset, $limit);
446 $lists->orderBy('profile_tag.modified DESC');
452 function getPrivateTags($offset=0, $limit=null, $since_id=0, $max_id=0)
454 $tags = new Profile_list();
455 $tags->private = true;
456 $tags->tagger = $this->id;
459 $tags->whereAdd('id > '.$since_id);
463 $tags->whereAdd('id <= '.$max_id);
466 if($offset>=0 && !is_null($limit)) {
467 $tags->limit($offset, $limit);
470 $tags->orderBy('id DESC');
476 function hasLocalTags()
478 $tags = new Profile_tag();
480 $tags->joinAdd(array('tagger', 'user:id'));
481 $tags->whereAdd('tagged = '.$this->id);
482 $tags->whereAdd('tagger != '.$this->id);
487 return ($tags->N == 0) ? false : true;
490 function getTagSubscriptions($offset=0, $limit=null, $since_id=0, $max_id=0)
492 $lists = new Profile_list();
493 $subs = new Profile_tag_subscription();
495 $lists->joinAdd($subs);
496 #@fixme: postgres (round(date_part('epoch', my_date)))
497 $lists->selectAdd('unix_timestamp(profile_tag_subscription.created) as "cursor"');
499 $lists->whereAdd('profile_tag_subscription.profile_id = '.$this->id);
502 $lists->whereAdd('cursor > '.$since_id);
506 $lists->whereAdd('cursor <= '.$max_id);
509 if($offset>=0 && !is_null($limit)) {
510 $lists->limit($offset, $limit);
513 $lists->orderBy('"cursor" DESC');
519 function avatarUrl($size=AVATAR_PROFILE_SIZE)
521 $avatar = $this->getAvatar($size);
523 return $avatar->displayUrl();
525 return Avatar::defaultImage($size);
529 function getSubscriptions($offset=0, $limit=null)
531 $subs = Subscription::bySubscriber($this->id,
537 while ($subs->fetch()) {
538 $profile = Profile::staticGet($subs->subscribed);
540 $profiles[] = $profile;
544 return new ArrayWrapper($profiles);
547 function getSubscribers($offset=0, $limit=null)
549 $subs = Subscription::bySubscribed($this->id,
555 while ($subs->fetch()) {
556 $profile = Profile::staticGet($subs->subscriber);
558 $profiles[] = $profile;
562 return new ArrayWrapper($profiles);
566 function getTaggedSubscribers($tag)
569 'SELECT profile.* ' .
570 'FROM profile JOIN (subscription, profile_tag, profile_list) ' .
571 'ON profile.id = subscription.subscriber ' .
572 'AND profile.id = profile_tag.tagged ' .
573 'AND profile_tag.tagger = profile_list.tagger AND profile_tag.tag = profile_list.tag ' .
574 'WHERE subscription.subscribed = %d ' .
575 'AND subscription.subscribed != subscription.subscriber ' .
576 'AND profile_tag.tagger = %d AND profile_tag.tag = "%s" ' .
577 'AND profile_list.private = false ' .
578 'ORDER BY subscription.created DESC';
580 $profile = new Profile();
583 $cnt = $profile->query(sprintf($qry, $this->id, $this->id, $tag));
585 while ($profile->fetch()) {
586 $tagged[] = clone($profile);
591 function subscriptionCount()
593 $c = Cache::instance();
596 $cnt = $c->get(Cache::key('profile:subscription_count:'.$this->id));
597 if (is_integer($cnt)) {
602 $sub = new Subscription();
603 $sub->subscriber = $this->id;
605 $cnt = (int) $sub->count('distinct subscribed');
607 $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
610 $c->set(Cache::key('profile:subscription_count:'.$this->id), $cnt);
616 function subscriberCount()
618 $c = Cache::instance();
620 $cnt = $c->get(Cache::key('profile:subscriber_count:'.$this->id));
621 if (is_integer($cnt)) {
626 $sub = new Subscription();
627 $sub->subscribed = $this->id;
628 $sub->whereAdd('subscriber != subscribed');
629 $cnt = (int) $sub->count('distinct subscriber');
632 $c->set(Cache::key('profile:subscriber_count:'.$this->id), $cnt);
639 * Is this profile subscribed to another profile?
641 * @param Profile $other
644 function isSubscribed($other)
646 return Subscription::exists($this, $other);
650 * Are these two profiles subscribed to each other?
652 * @param Profile $other
655 function mutuallySubscribed($other)
657 return $this->isSubscribed($other) &&
658 $other->isSubscribed($this);
661 function hasFave($notice)
663 $cache = Cache::instance();
665 // XXX: Kind of a hack.
667 if (!empty($cache)) {
668 // This is the stream of favorite notices, in rev chron
669 // order. This forces it into cache.
671 $ids = Fave::stream($this->id, 0, NOTICE_CACHE_WINDOW);
673 // If it's in the list, then it's a fave
675 if (in_array($notice->id, $ids)) {
679 // If we're not past the end of the cache window,
680 // then the cache has all available faves, so this one
683 if (count($ids) < NOTICE_CACHE_WINDOW) {
687 // Otherwise, cache doesn't have all faves;
688 // fall through to the default
691 $fave = Fave::pkeyGet(array('user_id' => $this->id,
692 'notice_id' => $notice->id));
693 return ((is_null($fave)) ? false : true);
698 $c = Cache::instance();
700 $cnt = $c->get(Cache::key('profile:fave_count:'.$this->id));
701 if (is_integer($cnt)) {
707 $faves->user_id = $this->id;
708 $cnt = (int) $faves->count('distinct notice_id');
711 $c->set(Cache::key('profile:fave_count:'.$this->id), $cnt);
717 function noticeCount()
719 $c = Cache::instance();
722 $cnt = $c->get(Cache::key('profile:notice_count:'.$this->id));
723 if (is_integer($cnt)) {
728 $notices = new Notice();
729 $notices->profile_id = $this->id;
730 $cnt = (int) $notices->count('distinct id');
733 $c->set(Cache::key('profile:notice_count:'.$this->id), $cnt);
739 function blowFavesCache()
741 $cache = Cache::instance();
743 // Faves don't happen chronologically, so we need to blow
745 $cache->delete(Cache::key('fave:ids_by_user:'.$this->id));
746 $cache->delete(Cache::key('fave:ids_by_user:'.$this->id.';last'));
747 $cache->delete(Cache::key('fave:ids_by_user_own:'.$this->id));
748 $cache->delete(Cache::key('fave:ids_by_user_own:'.$this->id.';last'));
750 $this->blowFaveCount();
753 function blowSubscriberCount()
755 $c = Cache::instance();
757 $c->delete(Cache::key('profile:subscriber_count:'.$this->id));
761 function blowSubscriptionCount()
763 $c = Cache::instance();
765 $c->delete(Cache::key('profile:subscription_count:'.$this->id));
769 function blowFaveCount()
771 $c = Cache::instance();
773 $c->delete(Cache::key('profile:fave_count:'.$this->id));
777 function blowNoticeCount()
779 $c = Cache::instance();
781 $c->delete(Cache::key('profile:notice_count:'.$this->id));
785 static function maxBio()
787 $biolimit = common_config('profile', 'biolimit');
788 // null => use global limit (distinct from 0!)
789 if (is_null($biolimit)) {
790 $biolimit = common_config('site', 'textlimit');
795 static function bioTooLong($bio)
797 $biolimit = self::maxBio();
798 return ($biolimit > 0 && !empty($bio) && (mb_strlen($bio) > $biolimit));
803 $this->_deleteNotices();
804 $this->_deleteSubscriptions();
805 $this->_deleteMessages();
806 $this->_deleteTags();
807 $this->_deleteBlocks();
808 $this->delete_avatars();
810 // Warning: delete() will run on the batch objects,
811 // not on individual objects.
812 $related = array('Reply',
815 Event::handle('ProfileDeleteRelated', array($this, &$related));
817 foreach ($related as $cls) {
819 $inst->profile_id = $this->id;
826 function _deleteNotices()
828 $notice = new Notice();
829 $notice->profile_id = $this->id;
831 if ($notice->find()) {
832 while ($notice->fetch()) {
833 $other = clone($notice);
839 function _deleteSubscriptions()
841 $sub = new Subscription();
842 $sub->subscriber = $this->id;
846 while ($sub->fetch()) {
847 $other = Profile::staticGet('id', $sub->subscribed);
851 if ($other->id == $this->id) {
854 Subscription::cancel($this, $other);
857 $subd = new Subscription();
858 $subd->subscribed = $this->id;
861 while ($subd->fetch()) {
862 $other = Profile::staticGet('id', $subd->subscriber);
866 if ($other->id == $this->id) {
869 Subscription::cancel($other, $this);
872 $self = new Subscription();
874 $self->subscriber = $this->id;
875 $self->subscribed = $this->id;
880 function _deleteMessages()
882 $msg = new Message();
883 $msg->from_profile = $this->id;
886 $msg = new Message();
887 $msg->to_profile = $this->id;
891 function _deleteTags()
893 $tag = new Profile_tag();
894 $tag->tagged = $this->id;
898 function _deleteBlocks()
900 $block = new Profile_block();
901 $block->blocked = $this->id;
904 $block = new Group_block();
905 $block->blocked = $this->id;
909 // XXX: identical to Notice::getLocation.
911 function getLocation()
915 if (!empty($this->location_id) && !empty($this->location_ns)) {
916 $location = Location::fromId($this->location_id, $this->location_ns);
919 if (is_null($location)) { // no ID, or Location::fromId() failed
920 if (!empty($this->lat) && !empty($this->lon)) {
921 $location = Location::fromLatLon($this->lat, $this->lon);
925 if (is_null($location)) { // still haven't found it!
926 if (!empty($this->location)) {
927 $location = Location::fromName($this->location);
934 function hasRole($name)
937 if (Event::handle('StartHasRole', array($this, $name, &$has_role))) {
938 $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
940 $has_role = !empty($role);
941 Event::handle('EndHasRole', array($this, $name, $has_role));
946 function grantRole($name)
948 if (Event::handle('StartGrantRole', array($this, $name))) {
950 $role = new Profile_role();
952 $role->profile_id = $this->id;
954 $role->created = common_sql_now();
956 $result = $role->insert();
959 throw new Exception("Can't save role '$name' for profile '{$this->id}'");
962 if ($name == 'owner') {
963 User::blow('user:site_owner');
966 Event::handle('EndGrantRole', array($this, $name));
972 function revokeRole($name)
974 if (Event::handle('StartRevokeRole', array($this, $name))) {
976 $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
980 // TRANS: Exception thrown when trying to revoke an existing role for a user that does not exist.
981 // TRANS: %1$s is the role name, %2$s is the user ID (number).
982 throw new Exception(sprintf(_('Cannot revoke role "%1$s" for user #%2$d; does not exist.'),$name, $this->id));
985 $result = $role->delete();
988 common_log_db_error($role, 'DELETE', __FILE__);
989 // TRANS: Exception thrown when trying to revoke a role for a user with a failing database query.
990 // TRANS: %1$s is the role name, %2$s is the user ID (number).
991 throw new Exception(sprintf(_('Cannot revoke role "%1$s" for user #%2$d; database error.'),$name, $this->id));
994 if ($name == 'owner') {
995 User::blow('user:site_owner');
998 Event::handle('EndRevokeRole', array($this, $name));
1004 function isSandboxed()
1006 return $this->hasRole(Profile_role::SANDBOXED);
1009 function isSilenced()
1011 return $this->hasRole(Profile_role::SILENCED);
1016 $this->grantRole(Profile_role::SANDBOXED);
1019 function unsandbox()
1021 $this->revokeRole(Profile_role::SANDBOXED);
1026 $this->grantRole(Profile_role::SILENCED);
1029 function unsilence()
1031 $this->revokeRole(Profile_role::SILENCED);
1035 * Does this user have the right to do X?
1037 * With our role-based authorization, this is merely a lookup for whether the user
1038 * has a particular role. The implementation currently uses a switch statement
1039 * to determine if the user has the pre-defined role to exercise the right. Future
1040 * implementations may allow per-site roles, and different mappings of roles to rights.
1042 * @param $right string Name of the right, usually a constant in class Right
1043 * @return boolean whether the user has the right in question
1045 function hasRight($right)
1049 if ($this->hasRole(Profile_role::DELETED)) {
1053 if (Event::handle('UserRightsCheck', array($this, $right, &$result))) {
1056 case Right::DELETEOTHERSNOTICE:
1057 case Right::MAKEGROUPADMIN:
1058 case Right::SANDBOXUSER:
1059 case Right::SILENCEUSER:
1060 case Right::DELETEUSER:
1061 case Right::DELETEGROUP:
1062 $result = $this->hasRole(Profile_role::MODERATOR);
1064 case Right::CONFIGURESITE:
1065 $result = $this->hasRole(Profile_role::ADMINISTRATOR);
1067 case Right::GRANTROLE:
1068 case Right::REVOKEROLE:
1069 $result = $this->hasRole(Profile_role::OWNER);
1071 case Right::NEWNOTICE:
1072 case Right::NEWMESSAGE:
1073 case Right::SUBSCRIBE:
1074 case Right::CREATEGROUP:
1075 $result = !$this->isSilenced();
1077 case Right::PUBLICNOTICE:
1078 case Right::EMAILONREPLY:
1079 case Right::EMAILONSUBSCRIBE:
1080 case Right::EMAILONFAVE:
1081 $result = !$this->isSandboxed();
1083 case Right::WEBLOGIN:
1084 $result = !$this->isSilenced();
1087 $result = !$this->isSilenced();
1089 case Right::BACKUPACCOUNT:
1090 $result = common_config('profile', 'backup');
1092 case Right::RESTOREACCOUNT:
1093 $result = common_config('profile', 'restore');
1095 case Right::DELETEACCOUNT:
1096 $result = common_config('profile', 'delete');
1098 case Right::MOVEACCOUNT:
1099 $result = common_config('profile', 'move');
1109 function hasRepeated($notice_id)
1111 // XXX: not really a pkey, but should work
1113 $notice = Memcached_DataObject::pkeyGet('Notice',
1114 array('profile_id' => $this->id,
1115 'repeat_of' => $notice_id));
1117 return !empty($notice);
1121 * Returns an XML string fragment with limited profile information
1122 * as an Atom <author> element.
1124 * Assumes that Atom has been previously set up as the base namespace.
1126 * @param Profile $cur the current authenticated user
1130 function asAtomAuthor($cur = null)
1132 $xs = new XMLStringer(true);
1134 $xs->elementStart('author');
1135 $xs->element('name', null, $this->nickname);
1136 $xs->element('uri', null, $this->getUri());
1139 $attrs['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
1140 $attrs['blocking'] = $cur->hasBlocked($this) ? 'true' : 'false';
1141 $xs->element('statusnet:profile_info', $attrs, null);
1143 $xs->elementEnd('author');
1145 return $xs->getString();
1149 * Extra profile info for atom entries
1151 * Clients use some extra profile info in the atom stream.
1152 * This gives it to them.
1154 * @param User $cur Current user
1156 * @return array representation of <statusnet:profile_info> element or null
1159 function profileInfo($cur)
1161 $profileInfoAttr = array('local_id' => $this->id);
1164 // Whether the current user is a subscribed to this profile
1165 $profileInfoAttr['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
1166 // Whether the current user is has blocked this profile
1167 $profileInfoAttr['blocking'] = $cur->hasBlocked($this) ? 'true' : 'false';
1170 return array('statusnet:profile_info', $profileInfoAttr, null);
1174 * Returns an XML string fragment with profile information as an
1175 * Activity Streams <activity:actor> element.
1177 * Assumes that 'activity' namespace has been previously defined.
1181 function asActivityActor()
1183 return $this->asActivityNoun('actor');
1187 * Returns an XML string fragment with profile information as an
1188 * Activity Streams noun object with the given element type.
1190 * Assumes that 'activity', 'georss', and 'poco' namespace has been
1191 * previously defined.
1193 * @param string $element one of 'actor', 'subject', 'object', 'target'
1197 function asActivityNoun($element)
1199 $noun = ActivityObject::fromProfile($this);
1200 return $noun->asString('activity:' . $element);
1204 * Returns the best URI for a profile. Plugins may override.
1206 * @return string $uri
1212 // give plugins a chance to set the URI
1213 if (Event::handle('StartGetProfileUri', array($this, &$uri))) {
1215 // check for a local user first
1216 $user = User::staticGet('id', $this->id);
1218 if (!empty($user)) {
1221 // return OMB profile if any
1222 $remote = Remote_profile::staticGet('id', $this->id);
1223 if (!empty($remote)) {
1224 $uri = $remote->uri;
1227 Event::handle('EndGetProfileUri', array($this, &$uri));
1233 function hasBlocked($other)
1235 $block = Profile_block::get($this->id, $other->id);
1237 if (empty($block)) {
1246 function getAtomFeed()
1250 if (Event::handle('StartProfileGetAtomFeed', array($this, &$feed))) {
1251 $user = User::staticGet('id', $this->id);
1252 if (!empty($user)) {
1253 $feed = common_local_url('ApiTimelineUser', array('id' => $user->id,
1254 'format' => 'atom'));
1256 Event::handle('EndProfileGetAtomFeed', array($this, $feed));
1262 static function fromURI($uri)
1266 if (Event::handle('StartGetProfileFromURI', array($uri, &$profile))) {
1267 // Get a local user or remote (OMB 0.1) profile
1268 $user = User::staticGet('uri', $uri);
1269 if (!empty($user)) {
1270 $profile = $user->getProfile();
1272 $remote_profile = Remote_profile::staticGet('uri', $uri);
1273 if (!empty($remote_profile)) {
1274 $profile = Profile::staticGet('id', $remote_profile->profile_id);
1277 Event::handle('EndGetProfileFromURI', array($uri, $profile));