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; // varchar(140) 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)
45 { return Memcached_DataObject::staticGet('Profile',$k,$v); }
47 /* the code above is auto generated do not remove the tag below */
50 function &pkeyGet($kv)
52 return Memcached_DataObject::pkeyGet('Profile', $kv);
55 function getAvatar($width, $height=null)
57 if (is_null($height)) {
60 return Avatar::pkeyGet(array('profile_id' => $this->id,
62 'height' => $height));
65 function getOriginalAvatar()
67 $avatar = DB_DataObject::factory('avatar');
68 $avatar->profile_id = $this->id;
69 $avatar->original = true;
70 if ($avatar->find(true)) {
77 function setOriginal($filename)
79 $imagefile = new ImageFile($this->id, Avatar::path($filename));
81 $avatar = new Avatar();
82 $avatar->profile_id = $this->id;
83 $avatar->width = $imagefile->width;
84 $avatar->height = $imagefile->height;
85 $avatar->mediatype = image_type_to_mime_type($imagefile->type);
86 $avatar->filename = $filename;
87 $avatar->original = true;
88 $avatar->url = Avatar::url($filename);
89 $avatar->created = DB_DataObject_Cast::dateTime(); # current time
91 # XXX: start a transaction here
93 if (!$this->delete_avatars() || !$avatar->insert()) {
94 @unlink(Avatar::path($filename));
98 foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
99 # We don't do a scaled one if original is our scaled size
100 if (!($avatar->width == $size && $avatar->height == $size)) {
102 $scaled_filename = $imagefile->resize($size);
104 //$scaled = DB_DataObject::factory('avatar');
105 $scaled = new Avatar();
106 $scaled->profile_id = $this->id;
107 $scaled->width = $size;
108 $scaled->height = $size;
109 $scaled->original = false;
110 $scaled->mediatype = image_type_to_mime_type($imagefile->type);
111 $scaled->filename = $scaled_filename;
112 $scaled->url = Avatar::url($scaled_filename);
113 $scaled->created = DB_DataObject_Cast::dateTime(); # current time
115 if (!$scaled->insert()) {
124 function delete_avatars($original=true)
126 $avatar = new Avatar();
127 $avatar->profile_id = $this->id;
129 while ($avatar->fetch()) {
130 if ($avatar->original) {
131 if ($original == false) {
140 function getBestName()
142 return ($this->fullname) ? $this->fullname : $this->nickname;
145 # Get latest notice on or before date; default now
146 function getCurrentNotice($dt=null)
148 $notice = new Notice();
149 $notice->profile_id = $this->id;
151 $notice->whereAdd('created < "' . $dt . '"');
153 $notice->orderBy('created DESC, notice.id DESC');
155 if ($notice->find(true)) {
161 function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0, $since=null)
163 $ids = Notice::stream(array($this, '_streamTaggedDirect'),
165 'profile:notice_ids_tagged:' . $this->id . ':' . $tag,
166 $offset, $limit, $since_id, $max_id, $since);
167 return Notice::getStreamByIds($ids);
170 function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0, $since=null)
172 // XXX: I'm not sure this is going to be any faster. It probably isn't.
173 $ids = Notice::stream(array($this, '_streamDirect'),
175 'profile:notice_ids:' . $this->id,
176 $offset, $limit, $since_id, $max_id, $since);
178 return Notice::getStreamByIds($ids);
181 function _streamTaggedDirect($tag, $offset, $limit, $since_id, $max_id, $since)
183 // XXX It would be nice to do this without a join
185 $notice = new Notice();
188 "select id from notice join notice_tag on id=notice_id where tag='".
189 $notice->escape($tag) .
190 "' and profile_id=" . $notice->escape($this->id);
192 if ($since_id != 0) {
193 $query .= " and id > $since_id";
197 $query .= " and id < $max_id";
200 if (!is_null($since)) {
201 $query .= " and created > '" . date('Y-m-d H:i:s', $since) . "'";
204 $query .= ' order by id DESC';
206 if (!is_null($offset)) {
207 $query .= " LIMIT $limit OFFSET $offset";
210 $notice->query($query);
214 while ($notice->fetch()) {
215 $ids[] = $notice->id;
221 function _streamDirect($offset, $limit, $since_id, $max_id, $since = null)
223 $notice = new Notice();
225 $notice->profile_id = $this->id;
227 $notice->selectAdd();
228 $notice->selectAdd('id');
230 if ($since_id != 0) {
231 $notice->whereAdd('id > ' . $since_id);
235 $notice->whereAdd('id <= ' . $max_id);
238 if (!is_null($since)) {
239 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
242 $notice->orderBy('id DESC');
244 if (!is_null($offset)) {
245 $notice->limit($offset, $limit);
250 if ($notice->find()) {
251 while ($notice->fetch()) {
252 $ids[] = $notice->id;
259 function isMember($group)
261 $mem = new Group_member();
263 $mem->group_id = $group->id;
264 $mem->profile_id = $this->id;
273 function isAdmin($group)
275 $mem = new Group_member();
277 $mem->group_id = $group->id;
278 $mem->profile_id = $this->id;
288 function avatarUrl($size=AVATAR_PROFILE_SIZE)
290 $avatar = $this->getAvatar($size);
292 return $avatar->displayUrl();
294 return Avatar::defaultImage($size);
298 function getSubscriptions($offset=0, $limit=null)
301 'SELECT profile.* ' .
302 'FROM profile JOIN subscription ' .
303 'ON profile.id = subscription.subscribed ' .
304 'WHERE subscription.subscriber = %d ' .
305 'AND subscription.subscribed != subscription.subscriber ' .
306 'ORDER BY subscription.created DESC ';
308 if (common_config('db','type') == 'pgsql') {
309 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
311 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
314 $profile = new Profile();
316 $profile->query(sprintf($qry, $this->id));
321 function getSubscribers($offset=0, $limit=null)
324 'SELECT profile.* ' .
325 'FROM profile JOIN subscription ' .
326 'ON profile.id = subscription.subscriber ' .
327 'WHERE subscription.subscribed = %d ' .
328 'AND subscription.subscribed != subscription.subscriber ' .
329 'ORDER BY subscription.created DESC ';
332 if (common_config('db','type') == 'pgsql') {
333 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
335 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
339 $profile = new Profile();
341 $cnt = $profile->query(sprintf($qry, $this->id));
346 function subscriptionCount()
348 $c = common_memcache();
351 $cnt = $c->get(common_cache_key('profile:subscription_count:'.$this->id));
352 if (is_integer($cnt)) {
357 $sub = new Subscription();
358 $sub->subscriber = $this->id;
360 $cnt = (int) $sub->count('distinct subscribed');
362 $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
365 $c->set(common_cache_key('profile:subscription_count:'.$this->id), $cnt);
371 function subscriberCount()
373 $c = common_memcache();
375 $cnt = $c->get(common_cache_key('profile:subscriber_count:'.$this->id));
376 if (is_integer($cnt)) {
381 $sub = new Subscription();
382 $sub->subscribed = $this->id;
384 $cnt = (int) $sub->count('distinct subscriber');
386 $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
389 $c->set(common_cache_key('profile:subscriber_count:'.$this->id), $cnt);
397 $c = common_memcache();
399 $cnt = $c->get(common_cache_key('profile:fave_count:'.$this->id));
400 if (is_integer($cnt)) {
406 $faves->user_id = $this->id;
407 $cnt = (int) $faves->count('distinct notice_id');
410 $c->set(common_cache_key('profile:fave_count:'.$this->id), $cnt);
416 function noticeCount()
418 $c = common_memcache();
421 $cnt = $c->get(common_cache_key('profile:notice_count:'.$this->id));
422 if (is_integer($cnt)) {
427 $notices = new Notice();
428 $notices->profile_id = $this->id;
429 $cnt = (int) $notices->count('distinct id');
432 $c->set(common_cache_key('profile:notice_count:'.$this->id), $cnt);
438 function blowSubscriberCount()
440 $c = common_memcache();
442 $c->delete(common_cache_key('profile:subscriber_count:'.$this->id));
446 function blowSubscriptionCount()
448 $c = common_memcache();
450 $c->delete(common_cache_key('profile:subscription_count:'.$this->id));
454 function blowFaveCount()
456 $c = common_memcache();
458 $c->delete(common_cache_key('profile:fave_count:'.$this->id));
462 function blowNoticeCount()
464 $c = common_memcache();
466 $c->delete(common_cache_key('profile:notice_count:'.$this->id));