]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Profile.php
change LACONICA to STATUSNET
[quix0rs-gnu-social.git] / classes / Profile.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('STATUSNET')) { exit(1); }
21
22 /**
23  * Table Definition for profile
24  */
25 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
26
27 class Profile extends Memcached_DataObject
28 {
29     ###START_AUTOCODE
30     /* the code below is auto generated do not remove the above tag */
31
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
42
43     /* Static get */
44     function staticGet($k,$v=null)
45     { return Memcached_DataObject::staticGet('Profile',$k,$v); }
46
47     /* the code above is auto generated do not remove the tag below */
48     ###END_AUTOCODE
49
50     function getAvatar($width, $height=null)
51     {
52         if (is_null($height)) {
53             $height = $width;
54         }
55         return Avatar::pkeyGet(array('profile_id' => $this->id,
56                                      'width' => $width,
57                                      'height' => $height));
58     }
59
60     function getOriginalAvatar()
61     {
62         $avatar = DB_DataObject::factory('avatar');
63         $avatar->profile_id = $this->id;
64         $avatar->original = true;
65         if ($avatar->find(true)) {
66             return $avatar;
67         } else {
68             return null;
69         }
70     }
71
72     function setOriginal($filename)
73     {
74         $imagefile = new ImageFile($this->id, Avatar::path($filename));
75
76         $avatar = new Avatar();
77         $avatar->profile_id = $this->id;
78         $avatar->width = $imagefile->width;
79         $avatar->height = $imagefile->height;
80         $avatar->mediatype = image_type_to_mime_type($imagefile->type);
81         $avatar->filename = $filename;
82         $avatar->original = true;
83         $avatar->url = Avatar::url($filename);
84         $avatar->created = DB_DataObject_Cast::dateTime(); # current time
85
86         # XXX: start a transaction here
87
88         if (!$this->delete_avatars() || !$avatar->insert()) {
89             @unlink(Avatar::path($filename));
90             return null;
91         }
92
93         foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
94             # We don't do a scaled one if original is our scaled size
95             if (!($avatar->width == $size && $avatar->height == $size)) {
96
97                 $scaled_filename = $imagefile->resize($size);
98
99                 //$scaled = DB_DataObject::factory('avatar');
100                 $scaled = new Avatar();
101                 $scaled->profile_id = $this->id;
102                 $scaled->width = $size;
103                 $scaled->height = $size;
104                 $scaled->original = false;
105                 $scaled->mediatype = image_type_to_mime_type($imagefile->type);
106                 $scaled->filename = $scaled_filename;
107                 $scaled->url = Avatar::url($scaled_filename);
108                 $scaled->created = DB_DataObject_Cast::dateTime(); # current time
109
110                 if (!$scaled->insert()) {
111                     return null;
112                 }
113             }
114         }
115
116         return $avatar;
117     }
118
119     function delete_avatars($original=true)
120     {
121         $avatar = new Avatar();
122         $avatar->profile_id = $this->id;
123         $avatar->find();
124         while ($avatar->fetch()) {
125             if ($avatar->original) {
126                 if ($original == false) {
127                     continue;
128                 }
129             }
130             $avatar->delete();
131         }
132         return true;
133     }
134
135     function getBestName()
136     {
137         return ($this->fullname) ? $this->fullname : $this->nickname;
138     }
139
140     # Get latest notice on or before date; default now
141     function getCurrentNotice($dt=null)
142     {
143         $notice = new Notice();
144         $notice->profile_id = $this->id;
145         if ($dt) {
146             $notice->whereAdd('created < "' . $dt . '"');
147         }
148         $notice->orderBy('created DESC, notice.id DESC');
149         $notice->limit(1);
150         if ($notice->find(true)) {
151             return $notice;
152         }
153         return null;
154     }
155
156     function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0, $since=null)
157     {
158         $ids = Notice::stream(array($this, '_streamTaggedDirect'),
159                               array($tag),
160                               'profile:notice_ids_tagged:' . $this->id . ':' . $tag,
161                               $offset, $limit, $since_id, $max_id, $since);
162         return Notice::getStreamByIds($ids);
163     }
164
165     function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0, $since=null)
166     {
167         // XXX: I'm not sure this is going to be any faster. It probably isn't.
168         $ids = Notice::stream(array($this, '_streamDirect'),
169                               array(),
170                               'profile:notice_ids:' . $this->id,
171                               $offset, $limit, $since_id, $max_id, $since);
172
173         return Notice::getStreamByIds($ids);
174     }
175
176     function _streamTaggedDirect($tag, $offset, $limit, $since_id, $max_id, $since)
177     {
178         // XXX It would be nice to do this without a join
179
180         $notice = new Notice();
181
182         $query =
183           "select id from notice join notice_tag on id=notice_id where tag='".
184           $notice->escape($tag) .
185           "' and profile_id=" . $notice->escape($this->id);
186
187         if ($since_id != 0) {
188             $query .= " and id > $since_id";
189         }
190
191         if ($max_id != 0) {
192             $query .= " and id < $max_id";
193         }
194
195         if (!is_null($since)) {
196             $query .= " and created > '" . date('Y-m-d H:i:s', $since) . "'";
197         }
198
199         $query .= ' order by id DESC';
200
201         if (!is_null($offset)) {
202             $query .= " LIMIT $limit OFFSET $offset";
203         }
204
205         $notice->query($query);
206
207         $ids = array();
208
209         while ($notice->fetch()) {
210             $ids[] = $notice->id;
211         }
212
213         return $ids;
214     }
215
216     function _streamDirect($offset, $limit, $since_id, $max_id, $since = null)
217     {
218         $notice = new Notice();
219
220         $notice->profile_id = $this->id;
221
222         $notice->selectAdd();
223         $notice->selectAdd('id');
224
225         if ($since_id != 0) {
226             $notice->whereAdd('id > ' . $since_id);
227         }
228
229         if ($max_id != 0) {
230             $notice->whereAdd('id <= ' . $max_id);
231         }
232
233         if (!is_null($since)) {
234             $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
235         }
236
237         $notice->orderBy('id DESC');
238
239         if (!is_null($offset)) {
240             $notice->limit($offset, $limit);
241         }
242
243         $ids = array();
244
245         if ($notice->find()) {
246             while ($notice->fetch()) {
247                 $ids[] = $notice->id;
248             }
249         }
250
251         return $ids;
252     }
253
254     function isMember($group)
255     {
256         $mem = new Group_member();
257
258         $mem->group_id = $group->id;
259         $mem->profile_id = $this->id;
260
261         if ($mem->find()) {
262             return true;
263         } else {
264             return false;
265         }
266     }
267
268     function isAdmin($group)
269     {
270         $mem = new Group_member();
271
272         $mem->group_id = $group->id;
273         $mem->profile_id = $this->id;
274         $mem->is_admin = 1;
275
276         if ($mem->find()) {
277             return true;
278         } else {
279             return false;
280         }
281     }
282
283     function avatarUrl($size=AVATAR_PROFILE_SIZE)
284     {
285         $avatar = $this->getAvatar($size);
286         if ($avatar) {
287             return $avatar->displayUrl();
288         } else {
289             return Avatar::defaultImage($size);
290         }
291     }
292
293     function getSubscriptions($offset=0, $limit=null)
294     {
295         $qry =
296           'SELECT profile.* ' .
297           'FROM profile JOIN subscription ' .
298           'ON profile.id = subscription.subscribed ' .
299           'WHERE subscription.subscriber = %d ' .
300           'AND subscription.subscribed != subscription.subscriber ' .
301           'ORDER BY subscription.created DESC ';
302
303         if (common_config('db','type') == 'pgsql') {
304             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
305         } else {
306             $qry .= ' LIMIT ' . $offset . ', ' . $limit;
307         }
308
309         $profile = new Profile();
310
311         $profile->query(sprintf($qry, $this->id));
312
313         return $profile;
314     }
315
316     function getSubscribers($offset=0, $limit=null)
317     {
318         $qry =
319           'SELECT profile.* ' .
320           'FROM profile JOIN subscription ' .
321           'ON profile.id = subscription.subscriber ' .
322           'WHERE subscription.subscribed = %d ' .
323           'AND subscription.subscribed != subscription.subscriber ' .
324           'ORDER BY subscription.created DESC ';
325
326         if ($offset) {
327             if (common_config('db','type') == 'pgsql') {
328                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
329             } else {
330                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
331             }
332         }
333
334         $profile = new Profile();
335
336         $cnt = $profile->query(sprintf($qry, $this->id));
337
338         return $profile;
339     }
340
341     function subscriptionCount()
342     {
343         $c = common_memcache();
344
345         if (!empty($c)) {
346             $cnt = $c->get(common_cache_key('profile:subscription_count:'.$this->id));
347             if (is_integer($cnt)) {
348                 return (int) $cnt;
349             }
350         }
351
352         $sub = new Subscription();
353         $sub->subscriber = $this->id;
354
355         $cnt = (int) $sub->count('distinct subscribed');
356
357         $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
358
359         if (!empty($c)) {
360             $c->set(common_cache_key('profile:subscription_count:'.$this->id), $cnt);
361         }
362
363         return $cnt;
364     }
365
366     function subscriberCount()
367     {
368         $c = common_memcache();
369         if (!empty($c)) {
370             $cnt = $c->get(common_cache_key('profile:subscriber_count:'.$this->id));
371             if (is_integer($cnt)) {
372                 return (int) $cnt;
373             }
374         }
375
376         $sub = new Subscription();
377         $sub->subscribed = $this->id;
378
379         $cnt = (int) $sub->count('distinct subscriber');
380
381         $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
382
383         if (!empty($c)) {
384             $c->set(common_cache_key('profile:subscriber_count:'.$this->id), $cnt);
385         }
386
387         return $cnt;
388     }
389
390     function faveCount()
391     {
392         $c = common_memcache();
393         if (!empty($c)) {
394             $cnt = $c->get(common_cache_key('profile:fave_count:'.$this->id));
395             if (is_integer($cnt)) {
396                 return (int) $cnt;
397             }
398         }
399
400         $faves = new Fave();
401         $faves->user_id = $this->id;
402         $cnt = (int) $faves->count('distinct notice_id');
403
404         if (!empty($c)) {
405             $c->set(common_cache_key('profile:fave_count:'.$this->id), $cnt);
406         }
407
408         return $cnt;
409     }
410
411     function noticeCount()
412     {
413         $c = common_memcache();
414
415         if (!empty($c)) {
416             $cnt = $c->get(common_cache_key('profile:notice_count:'.$this->id));
417             if (is_integer($cnt)) {
418                 return (int) $cnt;
419             }
420         }
421
422         $notices = new Notice();
423         $notices->profile_id = $this->id;
424         $cnt = (int) $notices->count('distinct id');
425
426         if (!empty($c)) {
427             $c->set(common_cache_key('profile:notice_count:'.$this->id), $cnt);
428         }
429
430         return $cnt;
431     }
432
433     function blowSubscriberCount()
434     {
435         $c = common_memcache();
436         if (!empty($c)) {
437             $c->delete(common_cache_key('profile:subscriber_count:'.$this->id));
438         }
439     }
440
441     function blowSubscriptionCount()
442     {
443         $c = common_memcache();
444         if (!empty($c)) {
445             $c->delete(common_cache_key('profile:subscription_count:'.$this->id));
446         }
447     }
448
449     function blowFaveCount()
450     {
451         $c = common_memcache();
452         if (!empty($c)) {
453             $c->delete(common_cache_key('profile:fave_count:'.$this->id));
454         }
455     }
456
457     function blowNoticeCount()
458     {
459         $c = common_memcache();
460         if (!empty($c)) {
461             $c->delete(common_cache_key('profile:notice_count:'.$this->id));
462         }
463     }
464 }