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 */
49 function getAvatar($width, $height=null)
51 if (is_null($height)) {
54 return Avatar::pkeyGet(array('profile_id' => $this->id,
56 'height' => $height));
59 function getOriginalAvatar()
61 $avatar = DB_DataObject::factory('avatar');
62 $avatar->profile_id = $this->id;
63 $avatar->original = true;
64 if ($avatar->find(true)) {
71 function setOriginal($filename)
73 $imagefile = new ImageFile($this->id, Avatar::path($filename));
75 $avatar = new Avatar();
76 $avatar->profile_id = $this->id;
77 $avatar->width = $imagefile->width;
78 $avatar->height = $imagefile->height;
79 $avatar->mediatype = image_type_to_mime_type($imagefile->type);
80 $avatar->filename = $filename;
81 $avatar->original = true;
82 $avatar->url = Avatar::url($filename);
83 $avatar->created = DB_DataObject_Cast::dateTime(); # current time
85 # XXX: start a transaction here
87 if (!$this->delete_avatars() || !$avatar->insert()) {
88 @unlink(Avatar::path($filename));
92 foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
93 # We don't do a scaled one if original is our scaled size
94 if (!($avatar->width == $size && $avatar->height == $size)) {
96 $scaled_filename = $imagefile->resize($size);
98 //$scaled = DB_DataObject::factory('avatar');
99 $scaled = new Avatar();
100 $scaled->profile_id = $this->id;
101 $scaled->width = $size;
102 $scaled->height = $size;
103 $scaled->original = false;
104 $scaled->mediatype = image_type_to_mime_type($imagefile->type);
105 $scaled->filename = $scaled_filename;
106 $scaled->url = Avatar::url($scaled_filename);
107 $scaled->created = DB_DataObject_Cast::dateTime(); # current time
109 if (!$scaled->insert()) {
118 function delete_avatars($original=true)
120 $avatar = new Avatar();
121 $avatar->profile_id = $this->id;
123 while ($avatar->fetch()) {
124 if ($avatar->original) {
125 if ($original == false) {
134 function getBestName()
136 return ($this->fullname) ? $this->fullname : $this->nickname;
139 # Get latest notice on or before date; default now
140 function getCurrentNotice($dt=null)
142 $notice = new Notice();
143 $notice->profile_id = $this->id;
145 $notice->whereAdd('created < "' . $dt . '"');
147 $notice->orderBy('created DESC, notice.id DESC');
149 if ($notice->find(true)) {
155 function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0, $since=null)
157 $ids = Notice::stream(array($this, '_streamTaggedDirect'),
159 'profile:notice_ids_tagged:' . $this->id . ':' . $tag,
160 $offset, $limit, $since_id, $max_id, $since);
161 return Notice::getStreamByIds($ids);
164 function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0, $since=null)
166 // XXX: I'm not sure this is going to be any faster. It probably isn't.
167 $ids = Notice::stream(array($this, '_streamDirect'),
169 'profile:notice_ids:' . $this->id,
170 $offset, $limit, $since_id, $max_id, $since);
172 return Notice::getStreamByIds($ids);
175 function _streamTaggedDirect($tag, $offset, $limit, $since_id, $max_id, $since)
177 // XXX It would be nice to do this without a join
179 $notice = new Notice();
182 "select id from notice join notice_tag on id=notice_id where tag='".
183 $notice->escape($tag) .
184 "' and profile_id=" . $notice->escape($this->id);
186 if ($since_id != 0) {
187 $query .= " and id > $since_id";
191 $query .= " and id < $max_id";
194 if (!is_null($since)) {
195 $query .= " and created > '" . date('Y-m-d H:i:s', $since) . "'";
198 $query .= ' order by id DESC';
200 if (!is_null($offset)) {
201 $query .= " LIMIT $limit OFFSET $offset";
204 $notice->query($query);
208 while ($notice->fetch()) {
209 $ids[] = $notice->id;
215 function _streamDirect($offset, $limit, $since_id, $max_id, $since = null)
217 $notice = new Notice();
219 $notice->profile_id = $this->id;
221 $notice->selectAdd();
222 $notice->selectAdd('id');
224 if ($since_id != 0) {
225 $notice->whereAdd('id > ' . $since_id);
229 $notice->whereAdd('id <= ' . $max_id);
232 if (!is_null($since)) {
233 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
236 $notice->orderBy('id DESC');
238 if (!is_null($offset)) {
239 $notice->limit($offset, $limit);
244 if ($notice->find()) {
245 while ($notice->fetch()) {
246 $ids[] = $notice->id;
253 function isMember($group)
255 $mem = new Group_member();
257 $mem->group_id = $group->id;
258 $mem->profile_id = $this->id;
267 function isAdmin($group)
269 $mem = new Group_member();
271 $mem->group_id = $group->id;
272 $mem->profile_id = $this->id;
282 function avatarUrl($size=AVATAR_PROFILE_SIZE)
284 $avatar = $this->getAvatar($size);
286 return $avatar->displayUrl();
288 return Avatar::defaultImage($size);
292 function getSubscriptions($offset=0, $limit=null)
295 'SELECT profile.* ' .
296 'FROM profile JOIN subscription ' .
297 'ON profile.id = subscription.subscribed ' .
298 'WHERE subscription.subscriber = %d ' .
299 'AND subscription.subscribed != subscription.subscriber ' .
300 'ORDER BY subscription.created DESC ';
302 if (common_config('db','type') == 'pgsql') {
303 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
305 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
308 $profile = new Profile();
310 $profile->query(sprintf($qry, $this->id));
315 function getSubscribers($offset=0, $limit=null)
318 'SELECT profile.* ' .
319 'FROM profile JOIN subscription ' .
320 'ON profile.id = subscription.subscriber ' .
321 'WHERE subscription.subscribed = %d ' .
322 'AND subscription.subscribed != subscription.subscriber ' .
323 'ORDER BY subscription.created DESC ';
326 if (common_config('db','type') == 'pgsql') {
327 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
329 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
333 $profile = new Profile();
335 $cnt = $profile->query(sprintf($qry, $this->id));
340 function subscriptionCount()
342 $c = common_memcache();
345 $cnt = $c->get(common_cache_key('profile:subscription_count:'.$this->id));
346 if (is_integer($cnt)) {
351 $sub = new Subscription();
352 $sub->subscriber = $this->id;
354 $cnt = (int) $sub->count('distinct subscribed');
356 $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
359 $c->set(common_cache_key('profile:subscription_count:'.$this->id), $cnt);
365 function subscriberCount()
367 $c = common_memcache();
369 $cnt = $c->get(common_cache_key('profile:subscriber_count:'.$this->id));
370 if (is_integer($cnt)) {
375 $sub = new Subscription();
376 $sub->subscribed = $this->id;
378 $cnt = (int) $sub->count('distinct subscriber');
380 $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
383 $c->set(common_cache_key('profile:subscriber_count:'.$this->id), $cnt);
391 $c = common_memcache();
393 $cnt = $c->get(common_cache_key('profile:fave_count:'.$this->id));
394 if (is_integer($cnt)) {
400 $faves->user_id = $this->id;
401 $cnt = (int) $faves->count('distinct notice_id');
404 $c->set(common_cache_key('profile:fave_count:'.$this->id), $cnt);
410 function noticeCount()
412 $c = common_memcache();
415 $cnt = $c->get(common_cache_key('profile:notice_count:'.$this->id));
416 if (is_integer($cnt)) {
421 $notices = new Notice();
422 $notices->profile_id = $this->id;
423 $cnt = (int) $notices->count('distinct id');
426 $c->set(common_cache_key('profile:notice_count:'.$this->id), $cnt);
432 function blowSubscriberCount()
434 $c = common_memcache();
436 $c->delete(common_cache_key('profile:subscriber_count:'.$this->id));
440 function blowSubscriptionCount()
442 $c = common_memcache();
444 $c->delete(common_cache_key('profile:subscription_count:'.$this->id));
448 function blowFaveCount()
450 $c = common_memcache();
452 $c->delete(common_cache_key('profile:fave_count:'.$this->id));
456 function blowNoticeCount()
458 $c = common_memcache();
460 $c->delete(common_cache_key('profile:notice_count:'.$this->id));
464 static function maxBio()
466 $biolimit = common_config('profile', 'biolimit');
467 // null => use global limit (distinct from 0!)
468 if (is_null($biolimit)) {
469 $biolimit = common_config('site', 'textlimit');
474 static function bioTooLong($bio)
476 $biolimit = self::maxBio();
477 return ($biolimit > 0 && !empty($bio) && (mb_strlen($bio) > $biolimit));
482 $this->_deleteNotices();
483 $this->_deleteSubscriptions();
484 $this->_deleteMessages();
485 $this->_deleteTags();
486 $this->_deleteBlocks();
488 $related = array('Avatar',
493 foreach ($related as $cls) {
495 $inst->profile_id = $this->id;
502 function _deleteNotices()
504 $notice = new Notice();
505 $notice->profile_id = $this->id;
507 if ($notice->find()) {
508 while ($notice->fetch()) {
509 $other = clone($notice);
515 function _deleteSubscriptions()
517 $sub = new Subscription();
518 $sub->subscriber = $this->id;
521 $subd = new Subscription();
522 $subd->subscribed = $this->id;
526 function _deleteMessages()
528 $msg = new Message();
529 $msg->from_profile = $this->id;
532 $msg = new Message();
533 $msg->to_profile = $this->id;
537 function _deleteTags()
539 $tag = new Profile_tag();
540 $tag->tagged = $this->id;
544 function _deleteBlocks()
546 $block = new Profile_block();
547 $block->blocked = $this->id;
550 $block = new Group_block();
551 $block->blocked = $this->id;