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 $created; // datetime() not_null
41 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
44 function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Profile',$k,$v); }
46 /* the code above is auto generated do not remove the tag below */
51 return User::staticGet('id', $this->id);
54 function getAvatar($width, $height=null)
56 if (is_null($height)) {
59 return Avatar::pkeyGet(array('profile_id' => $this->id,
61 'height' => $height));
64 function getOriginalAvatar()
66 $avatar = DB_DataObject::factory('avatar');
67 $avatar->profile_id = $this->id;
68 $avatar->original = true;
69 if ($avatar->find(true)) {
76 function setOriginal($filename)
78 $imagefile = new ImageFile($this->id, Avatar::path($filename));
80 $avatar = new Avatar();
81 $avatar->profile_id = $this->id;
82 $avatar->width = $imagefile->width;
83 $avatar->height = $imagefile->height;
84 $avatar->mediatype = image_type_to_mime_type($imagefile->type);
85 $avatar->filename = $filename;
86 $avatar->original = true;
87 $avatar->url = Avatar::url($filename);
88 $avatar->created = DB_DataObject_Cast::dateTime(); # current time
90 # XXX: start a transaction here
92 if (!$this->delete_avatars() || !$avatar->insert()) {
93 @unlink(Avatar::path($filename));
97 foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
98 # We don't do a scaled one if original is our scaled size
99 if (!($avatar->width == $size && $avatar->height == $size)) {
101 $scaled_filename = $imagefile->resize($size);
103 //$scaled = DB_DataObject::factory('avatar');
104 $scaled = new Avatar();
105 $scaled->profile_id = $this->id;
106 $scaled->width = $size;
107 $scaled->height = $size;
108 $scaled->original = false;
109 $scaled->mediatype = image_type_to_mime_type($imagefile->type);
110 $scaled->filename = $scaled_filename;
111 $scaled->url = Avatar::url($scaled_filename);
112 $scaled->created = DB_DataObject_Cast::dateTime(); # current time
114 if (!$scaled->insert()) {
123 function delete_avatars($original=true)
125 $avatar = new Avatar();
126 $avatar->profile_id = $this->id;
128 while ($avatar->fetch()) {
129 if ($avatar->original) {
130 if ($original == false) {
139 function getBestName()
141 return ($this->fullname) ? $this->fullname : $this->nickname;
144 # Get latest notice on or before date; default now
145 function getCurrentNotice($dt=null)
147 $notice = new Notice();
148 $notice->profile_id = $this->id;
150 $notice->whereAdd('created < "' . $dt . '"');
152 $notice->orderBy('created DESC, notice.id DESC');
154 if ($notice->find(true)) {
160 function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0, $since=null)
162 $ids = Notice::stream(array($this, '_streamTaggedDirect'),
164 'profile:notice_ids_tagged:' . $this->id . ':' . $tag,
165 $offset, $limit, $since_id, $max_id, $since);
166 return Notice::getStreamByIds($ids);
169 function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0, $since=null)
171 // XXX: I'm not sure this is going to be any faster. It probably isn't.
172 $ids = Notice::stream(array($this, '_streamDirect'),
174 'profile:notice_ids:' . $this->id,
175 $offset, $limit, $since_id, $max_id, $since);
177 return Notice::getStreamByIds($ids);
180 function _streamTaggedDirect($tag, $offset, $limit, $since_id, $max_id, $since)
182 // XXX It would be nice to do this without a join
184 $notice = new Notice();
187 "select id from notice join notice_tag on id=notice_id where tag='".
188 $notice->escape($tag) .
189 "' and profile_id=" . $notice->escape($this->id);
191 if ($since_id != 0) {
192 $query .= " and id > $since_id";
196 $query .= " and id < $max_id";
199 if (!is_null($since)) {
200 $query .= " and created > '" . date('Y-m-d H:i:s', $since) . "'";
203 $query .= ' order by id DESC';
205 if (!is_null($offset)) {
206 $query .= " LIMIT $limit OFFSET $offset";
209 $notice->query($query);
213 while ($notice->fetch()) {
214 $ids[] = $notice->id;
220 function _streamDirect($offset, $limit, $since_id, $max_id, $since = null)
222 $notice = new Notice();
224 $notice->profile_id = $this->id;
226 $notice->selectAdd();
227 $notice->selectAdd('id');
229 if ($since_id != 0) {
230 $notice->whereAdd('id > ' . $since_id);
234 $notice->whereAdd('id <= ' . $max_id);
237 if (!is_null($since)) {
238 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
241 $notice->orderBy('id DESC');
243 if (!is_null($offset)) {
244 $notice->limit($offset, $limit);
249 if ($notice->find()) {
250 while ($notice->fetch()) {
251 $ids[] = $notice->id;
258 function isMember($group)
260 $mem = new Group_member();
262 $mem->group_id = $group->id;
263 $mem->profile_id = $this->id;
272 function isAdmin($group)
274 $mem = new Group_member();
276 $mem->group_id = $group->id;
277 $mem->profile_id = $this->id;
287 function avatarUrl($size=AVATAR_PROFILE_SIZE)
289 $avatar = $this->getAvatar($size);
291 return $avatar->displayUrl();
293 return Avatar::defaultImage($size);
297 function getSubscriptions($offset=0, $limit=null)
300 'SELECT profile.* ' .
301 'FROM profile JOIN subscription ' .
302 'ON profile.id = subscription.subscribed ' .
303 'WHERE subscription.subscriber = %d ' .
304 'AND subscription.subscribed != subscription.subscriber ' .
305 'ORDER BY subscription.created DESC ';
307 if (common_config('db','type') == 'pgsql') {
308 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
310 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
313 $profile = new Profile();
315 $profile->query(sprintf($qry, $this->id));
320 function getSubscribers($offset=0, $limit=null)
323 'SELECT profile.* ' .
324 'FROM profile JOIN subscription ' .
325 'ON profile.id = subscription.subscriber ' .
326 'WHERE subscription.subscribed = %d ' .
327 'AND subscription.subscribed != subscription.subscriber ' .
328 'ORDER BY subscription.created DESC ';
331 if (common_config('db','type') == 'pgsql') {
332 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
334 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
338 $profile = new Profile();
340 $cnt = $profile->query(sprintf($qry, $this->id));
345 function subscriptionCount()
347 $c = common_memcache();
350 $cnt = $c->get(common_cache_key('profile:subscription_count:'.$this->id));
351 if (is_integer($cnt)) {
356 $sub = new Subscription();
357 $sub->subscriber = $this->id;
359 $cnt = (int) $sub->count('distinct subscribed');
361 $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
364 $c->set(common_cache_key('profile:subscription_count:'.$this->id), $cnt);
370 function subscriberCount()
372 $c = common_memcache();
374 $cnt = $c->get(common_cache_key('profile:subscriber_count:'.$this->id));
375 if (is_integer($cnt)) {
380 $sub = new Subscription();
381 $sub->subscribed = $this->id;
383 $cnt = (int) $sub->count('distinct subscriber');
385 $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
388 $c->set(common_cache_key('profile:subscriber_count:'.$this->id), $cnt);
396 $c = common_memcache();
398 $cnt = $c->get(common_cache_key('profile:fave_count:'.$this->id));
399 if (is_integer($cnt)) {
405 $faves->user_id = $this->id;
406 $cnt = (int) $faves->count('distinct notice_id');
409 $c->set(common_cache_key('profile:fave_count:'.$this->id), $cnt);
415 function noticeCount()
417 $c = common_memcache();
420 $cnt = $c->get(common_cache_key('profile:notice_count:'.$this->id));
421 if (is_integer($cnt)) {
426 $notices = new Notice();
427 $notices->profile_id = $this->id;
428 $cnt = (int) $notices->count('distinct id');
431 $c->set(common_cache_key('profile:notice_count:'.$this->id), $cnt);
437 function blowSubscriberCount()
439 $c = common_memcache();
441 $c->delete(common_cache_key('profile:subscriber_count:'.$this->id));
445 function blowSubscriptionCount()
447 $c = common_memcache();
449 $c->delete(common_cache_key('profile:subscription_count:'.$this->id));
453 function blowFaveCount()
455 $c = common_memcache();
457 $c->delete(common_cache_key('profile:fave_count:'.$this->id));
461 function blowNoticeCount()
463 $c = common_memcache();
465 $c->delete(common_cache_key('profile:notice_count:'.$this->id));
469 static function maxBio()
471 $biolimit = common_config('profile', 'biolimit');
472 // null => use global limit (distinct from 0!)
473 if (is_null($biolimit)) {
474 $biolimit = common_config('site', 'textlimit');
479 static function bioTooLong($bio)
481 $biolimit = self::maxBio();
482 return ($biolimit > 0 && !empty($bio) && (mb_strlen($bio) > $biolimit));
487 $this->_deleteNotices();
488 $this->_deleteSubscriptions();
489 $this->_deleteMessages();
490 $this->_deleteTags();
491 $this->_deleteBlocks();
493 $related = array('Avatar',
498 foreach ($related as $cls) {
500 $inst->profile_id = $this->id;
507 function _deleteNotices()
509 $notice = new Notice();
510 $notice->profile_id = $this->id;
512 if ($notice->find()) {
513 while ($notice->fetch()) {
514 $other = clone($notice);
520 function _deleteSubscriptions()
522 $sub = new Subscription();
523 $sub->subscriber = $this->id;
526 $subd = new Subscription();
527 $subd->subscribed = $this->id;
531 function _deleteMessages()
533 $msg = new Message();
534 $msg->from_profile = $this->id;
537 $msg = new Message();
538 $msg->to_profile = $this->id;
542 function _deleteTags()
544 $tag = new Profile_tag();
545 $tag->tagged = $this->id;
549 function _deleteBlocks()
551 $block = new Profile_block();
552 $block->blocked = $this->id;
555 $block = new Group_block();
556 $block->blocked = $this->id;