]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Profile.php
5fb7bb6782753f4d3111142e0a00db3b175b6295
[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') && !defined('LACONICA')) { 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;                             // 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
46
47     /* Static get */
48     function staticGet($k,$v=NULL) {
49         return Memcached_DataObject::staticGet('Profile',$k,$v);
50     }
51
52     /* the code above is auto generated do not remove the tag below */
53     ###END_AUTOCODE
54
55     function getAvatar($width, $height=null)
56     {
57         if (is_null($height)) {
58             $height = $width;
59         }
60         return Avatar::pkeyGet(array('profile_id' => $this->id,
61                                      'width' => $width,
62                                      'height' => $height));
63     }
64
65     function getOriginalAvatar()
66     {
67         $avatar = DB_DataObject::factory('avatar');
68         $avatar->profile_id = $this->id;
69         $avatar->original = true;
70         if ($avatar->find(true)) {
71             return $avatar;
72         } else {
73             return null;
74         }
75     }
76
77     function setOriginal($filename)
78     {
79         $imagefile = new ImageFile($this->id, Avatar::path($filename));
80
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
90
91         # XXX: start a transaction here
92
93         if (!$this->delete_avatars() || !$avatar->insert()) {
94             @unlink(Avatar::path($filename));
95             return null;
96         }
97
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)) {
101
102                 $scaled_filename = $imagefile->resize($size);
103
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
114
115                 if (!$scaled->insert()) {
116                     return null;
117                 }
118             }
119         }
120
121         return $avatar;
122     }
123
124     function delete_avatars($original=true)
125     {
126         $avatar = new Avatar();
127         $avatar->profile_id = $this->id;
128         $avatar->find();
129         while ($avatar->fetch()) {
130             if ($avatar->original) {
131                 if ($original == false) {
132                     continue;
133                 }
134             }
135             $avatar->delete();
136         }
137         return true;
138     }
139
140     function getBestName()
141     {
142         return ($this->fullname) ? $this->fullname : $this->nickname;
143     }
144
145     # Get latest notice on or before date; default now
146     function getCurrentNotice($dt=null)
147     {
148         $notice = new Notice();
149         $notice->profile_id = $this->id;
150         if ($dt) {
151             $notice->whereAdd('created < "' . $dt . '"');
152         }
153         $notice->orderBy('created DESC, notice.id DESC');
154         $notice->limit(1);
155         if ($notice->find(true)) {
156             return $notice;
157         }
158         return null;
159     }
160
161     function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0, $since=null)
162     {
163         $ids = Notice::stream(array($this, '_streamTaggedDirect'),
164                               array($tag),
165                               'profile:notice_ids_tagged:' . $this->id . ':' . $tag,
166                               $offset, $limit, $since_id, $max_id, $since);
167         return Notice::getStreamByIds($ids);
168     }
169
170     function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0, $since=null)
171     {
172         // XXX: I'm not sure this is going to be any faster. It probably isn't.
173         $ids = Notice::stream(array($this, '_streamDirect'),
174                               array(),
175                               'profile:notice_ids:' . $this->id,
176                               $offset, $limit, $since_id, $max_id, $since);
177
178         return Notice::getStreamByIds($ids);
179     }
180
181     function _streamTaggedDirect($tag, $offset, $limit, $since_id, $max_id, $since)
182     {
183         // XXX It would be nice to do this without a join
184
185         $notice = new Notice();
186
187         $query =
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);
191
192         if ($since_id != 0) {
193             $query .= " and id > $since_id";
194         }
195
196         if ($max_id != 0) {
197             $query .= " and id < $max_id";
198         }
199
200         if (!is_null($since)) {
201             $query .= " and created > '" . date('Y-m-d H:i:s', $since) . "'";
202         }
203
204         $query .= ' order by id DESC';
205
206         if (!is_null($offset)) {
207             $query .= " LIMIT $limit OFFSET $offset";
208         }
209
210         $notice->query($query);
211
212         $ids = array();
213
214         while ($notice->fetch()) {
215             $ids[] = $notice->id;
216         }
217
218         return $ids;
219     }
220
221     function _streamDirect($offset, $limit, $since_id, $max_id, $since = null)
222     {
223         $notice = new Notice();
224
225         $notice->profile_id = $this->id;
226
227         $notice->selectAdd();
228         $notice->selectAdd('id');
229
230         if ($since_id != 0) {
231             $notice->whereAdd('id > ' . $since_id);
232         }
233
234         if ($max_id != 0) {
235             $notice->whereAdd('id <= ' . $max_id);
236         }
237
238         if (!is_null($since)) {
239             $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
240         }
241
242         $notice->orderBy('id DESC');
243
244         if (!is_null($offset)) {
245             $notice->limit($offset, $limit);
246         }
247
248         $ids = array();
249
250         if ($notice->find()) {
251             while ($notice->fetch()) {
252                 $ids[] = $notice->id;
253             }
254         }
255
256         return $ids;
257     }
258
259     function isMember($group)
260     {
261         $mem = new Group_member();
262
263         $mem->group_id = $group->id;
264         $mem->profile_id = $this->id;
265
266         if ($mem->find()) {
267             return true;
268         } else {
269             return false;
270         }
271     }
272
273     function isAdmin($group)
274     {
275         $mem = new Group_member();
276
277         $mem->group_id = $group->id;
278         $mem->profile_id = $this->id;
279         $mem->is_admin = 1;
280
281         if ($mem->find()) {
282             return true;
283         } else {
284             return false;
285         }
286     }
287
288     function avatarUrl($size=AVATAR_PROFILE_SIZE)
289     {
290         $avatar = $this->getAvatar($size);
291         if ($avatar) {
292             return $avatar->displayUrl();
293         } else {
294             return Avatar::defaultImage($size);
295         }
296     }
297
298     function getSubscriptions($offset=0, $limit=null)
299     {
300         $qry =
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 ';
307
308         if (common_config('db','type') == 'pgsql') {
309             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
310         } else {
311             $qry .= ' LIMIT ' . $offset . ', ' . $limit;
312         }
313
314         $profile = new Profile();
315
316         $profile->query(sprintf($qry, $this->id));
317
318         return $profile;
319     }
320
321     function getSubscribers($offset=0, $limit=null)
322     {
323         $qry =
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 ';
330
331         if ($offset) {
332             if (common_config('db','type') == 'pgsql') {
333                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
334             } else {
335                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
336             }
337         }
338
339         $profile = new Profile();
340
341         $cnt = $profile->query(sprintf($qry, $this->id));
342
343         return $profile;
344     }
345
346     function subscriptionCount()
347     {
348         $c = common_memcache();
349
350         if (!empty($c)) {
351             $cnt = $c->get(common_cache_key('profile:subscription_count:'.$this->id));
352             if (is_integer($cnt)) {
353                 return (int) $cnt;
354             }
355         }
356
357         $sub = new Subscription();
358         $sub->subscriber = $this->id;
359
360         $cnt = (int) $sub->count('distinct subscribed');
361
362         $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
363
364         if (!empty($c)) {
365             $c->set(common_cache_key('profile:subscription_count:'.$this->id), $cnt);
366         }
367
368         return $cnt;
369     }
370
371     function subscriberCount()
372     {
373         $c = common_memcache();
374         if (!empty($c)) {
375             $cnt = $c->get(common_cache_key('profile:subscriber_count:'.$this->id));
376             if (is_integer($cnt)) {
377                 return (int) $cnt;
378             }
379         }
380
381         $sub = new Subscription();
382         $sub->subscribed = $this->id;
383
384         $cnt = (int) $sub->count('distinct subscriber');
385
386         $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
387
388         if (!empty($c)) {
389             $c->set(common_cache_key('profile:subscriber_count:'.$this->id), $cnt);
390         }
391
392         return $cnt;
393     }
394
395     function faveCount()
396     {
397         $c = common_memcache();
398         if (!empty($c)) {
399             $cnt = $c->get(common_cache_key('profile:fave_count:'.$this->id));
400             if (is_integer($cnt)) {
401                 return (int) $cnt;
402             }
403         }
404
405         $faves = new Fave();
406         $faves->user_id = $this->id;
407         $cnt = (int) $faves->count('distinct notice_id');
408
409         if (!empty($c)) {
410             $c->set(common_cache_key('profile:fave_count:'.$this->id), $cnt);
411         }
412
413         return $cnt;
414     }
415
416     function noticeCount()
417     {
418         $c = common_memcache();
419
420         if (!empty($c)) {
421             $cnt = $c->get(common_cache_key('profile:notice_count:'.$this->id));
422             if (is_integer($cnt)) {
423                 return (int) $cnt;
424             }
425         }
426
427         $notices = new Notice();
428         $notices->profile_id = $this->id;
429         $cnt = (int) $notices->count('distinct id');
430
431         if (!empty($c)) {
432             $c->set(common_cache_key('profile:notice_count:'.$this->id), $cnt);
433         }
434
435         return $cnt;
436     }
437
438     function blowSubscriberCount()
439     {
440         $c = common_memcache();
441         if (!empty($c)) {
442             $c->delete(common_cache_key('profile:subscriber_count:'.$this->id));
443         }
444     }
445
446     function blowSubscriptionCount()
447     {
448         $c = common_memcache();
449         if (!empty($c)) {
450             $c->delete(common_cache_key('profile:subscription_count:'.$this->id));
451         }
452     }
453
454     function blowFaveCount()
455     {
456         $c = common_memcache();
457         if (!empty($c)) {
458             $c->delete(common_cache_key('profile:fave_count:'.$this->id));
459         }
460     }
461
462     function blowNoticeCount()
463     {
464         $c = common_memcache();
465         if (!empty($c)) {
466             $c->delete(common_cache_key('profile:notice_count:'.$this->id));
467         }
468     }
469
470     static function maxBio()
471     {
472         $biolimit = common_config('profile', 'biolimit');
473         // null => use global limit (distinct from 0!)
474         if (is_null($biolimit)) {
475             $biolimit = common_config('site', 'textlimit');
476         }
477         return $biolimit;
478     }
479
480     static function bioTooLong($bio)
481     {
482         $biolimit = self::maxBio();
483         return ($biolimit > 0 && !empty($bio) && (mb_strlen($bio) > $biolimit));
484     }
485
486     function delete()
487     {
488         $this->_deleteNotices();
489         $this->_deleteSubscriptions();
490         $this->_deleteMessages();
491         $this->_deleteTags();
492         $this->_deleteBlocks();
493
494         $related = array('Avatar',
495                          'Reply',
496                          'Group_member',
497                          );
498
499         foreach ($related as $cls) {
500             $inst = new $cls();
501             $inst->profile_id = $this->id;
502             $inst->delete();
503         }
504
505         parent::delete();
506     }
507
508     function _deleteNotices()
509     {
510         $notice = new Notice();
511         $notice->profile_id = $this->id;
512
513         if ($notice->find()) {
514             while ($notice->fetch()) {
515                 $other = clone($notice);
516                 $other->delete();
517             }
518         }
519     }
520
521     function _deleteSubscriptions()
522     {
523         $sub = new Subscription();
524         $sub->subscriber = $this->id;
525         $sub->delete();
526
527         $subd = new Subscription();
528         $subd->subscribed = $this->id;
529         $subd->delete();
530     }
531
532     function _deleteMessages()
533     {
534         $msg = new Message();
535         $msg->from_profile = $this->id;
536         $msg->delete();
537
538         $msg = new Message();
539         $msg->to_profile = $this->id;
540         $msg->delete();
541     }
542
543     function _deleteTags()
544     {
545         $tag = new Profile_tag();
546         $tag->tagged = $this->id;
547         $tag->delete();
548     }
549
550     function _deleteBlocks()
551     {
552         $block = new Profile_block();
553         $block->blocked = $this->id;
554         $block->delete();
555
556         $block = new Group_block();
557         $block->blocked = $this->id;
558         $block->delete();
559     }
560 }