]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User_group.php
Less raw SQL in User_group
[quix0rs-gnu-social.git] / classes / User_group.php
1 <?php
2 /**
3  * Table Definition for user_group
4  */
5
6 class User_group extends Managed_DataObject
7 {
8     const JOIN_POLICY_OPEN = 0;
9     const JOIN_POLICY_MODERATE = 1;
10     const CACHE_WINDOW = 201;
11
12     ###START_AUTOCODE
13     /* the code below is auto generated do not remove the above tag */
14
15     public $__table = 'user_group';                      // table name
16     public $id;                              // int(4)  primary_key not_null
17     public $nickname;                        // varchar(64)
18     public $fullname;                        // varchar(255)
19     public $homepage;                        // varchar(255)
20     public $description;                     // text
21     public $location;                        // varchar(255)
22     public $original_logo;                   // varchar(255)
23     public $homepage_logo;                   // varchar(255)
24     public $stream_logo;                     // varchar(255)
25     public $mini_logo;                       // varchar(255)
26     public $created;                         // datetime   not_null default_0000-00-00%2000%3A00%3A00
27     public $modified;                        // timestamp   not_null default_CURRENT_TIMESTAMP
28     public $uri;                             // varchar(255)  unique_key
29     public $mainpage;                        // varchar(255)
30     public $join_policy;                     // tinyint
31     public $force_scope;                     // tinyint
32
33     /* the code above is auto generated do not remove the tag below */
34     ###END_AUTOCODE
35
36     public static function schemaDef()
37     {
38         return array(
39             'fields' => array(
40                 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
41                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
42
43                 'nickname' => array('type' => 'varchar', 'length' => 64, 'description' => 'nickname for addressing'),
44                 'fullname' => array('type' => 'varchar', 'length' => 255, 'description' => 'display name'),
45                 'homepage' => array('type' => 'varchar', 'length' => 255, 'description' => 'URL, cached so we dont regenerate'),
46                 'description' => array('type' => 'text', 'description' => 'group description'),
47                 'location' => array('type' => 'varchar', 'length' => 255, 'description' => 'related physical location, if any'),
48
49                 'original_logo' => array('type' => 'varchar', 'length' => 255, 'description' => 'original size logo'),
50                 'homepage_logo' => array('type' => 'varchar', 'length' => 255, 'description' => 'homepage (profile) size logo'),
51                 'stream_logo' => array('type' => 'varchar', 'length' => 255, 'description' => 'stream-sized logo'),
52                 'mini_logo' => array('type' => 'varchar', 'length' => 255, 'description' => 'mini logo'),
53
54                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
55                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
56
57                 'uri' => array('type' => 'varchar', 'length' => 255, 'description' => 'universal identifier'),
58                 'mainpage' => array('type' => 'varchar', 'length' => 255, 'description' => 'page for group info to link to'),
59                 'join_policy' => array('type' => 'int', 'size' => 'tiny', 'description' => '0=open; 1=requires admin approval'),      
60                 'force_scope' => array('type' => 'int', 'size' => 'tiny', 'description' => '0=never,1=sometimes,-1=always'),
61             ),
62             'primary key' => array('id'),
63             'unique keys' => array(
64                 'user_group_uri_key' => array('uri'),
65 // when it's safe and everyone's run upgrade.php                'user_profile_id_key' => array('profile_id'),
66             ),
67             'foreign keys' => array(
68                 'user_group_id_fkey' => array('profile', array('profile_id' => 'id')),
69             ),
70             'indexes' => array(
71                 'user_group_nickname_idx' => array('nickname'),
72                 'user_group_profile_id_idx' => array('profile_id'), //make this unique in future
73             ),
74         );
75     }
76
77     protected $_profile = null;
78
79     /**
80      * @return Profile
81      *
82      * @throws UserNoProfileException if user has no profile
83      */
84     public function getProfile()
85     {
86         if (!($this->_profile instanceof Profile)) {
87             $this->_profile = Profile::getKV('id', $this->profile_id);
88             if (!($this->_profile instanceof Profile)) {
89                 throw new GroupNoProfileException($this);
90             }
91         }
92
93         return $this->_profile;
94     }
95
96     public static function defaultLogo($size)
97     {
98         static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
99                                   AVATAR_STREAM_SIZE => 'stream',
100                                   AVATAR_MINI_SIZE => 'mini');
101         return Theme::path('default-avatar-'.$sizenames[$size].'.png');
102     }
103
104     function homeUrl()
105     {
106         $url = null;
107         if (Event::handle('StartUserGroupHomeUrl', array($this, &$url))) {
108             // normally stored in mainpage, but older ones may be null
109             if (!empty($this->mainpage)) {
110                 $url = $this->mainpage;
111             } else {
112                 $url = common_local_url('showgroup',
113                                         array('nickname' => $this->nickname));
114             }
115         }
116         Event::handle('EndUserGroupHomeUrl', array($this, &$url));
117         return $url;
118     }
119
120     function getUri()
121     {
122         $uri = null;
123         if (Event::handle('StartUserGroupGetUri', array($this, &$uri))) {
124             if (!empty($this->uri)) {
125                 $uri = $this->uri;
126             } else {
127                 $uri = common_local_url('groupbyid',
128                                         array('id' => $this->id));
129             }
130         }
131         Event::handle('EndUserGroupGetUri', array($this, &$uri));
132         return $uri;
133     }
134
135     function permalink()
136     {
137         $url = null;
138         if (Event::handle('StartUserGroupPermalink', array($this, &$url))) {
139             $url = common_local_url('groupbyid',
140                                     array('id' => $this->id));
141         }
142         Event::handle('EndUserGroupPermalink', array($this, &$url));
143         return $url;
144     }
145
146     function getNotices($offset, $limit, $since_id=null, $max_id=null)
147     {
148         $stream = new GroupNoticeStream($this);
149
150         return $stream->getNotices($offset, $limit, $since_id, $max_id);
151     }
152
153     function getMembers($offset=0, $limit=null) {
154         $ids = null;
155         if (is_null($limit) || $offset + $limit > User_group::CACHE_WINDOW) {
156             $ids = $this->getMemberIDs($offset,
157                                        $limit);
158         } else {
159             $key = sprintf('group:member_ids:%d', $this->id);
160             $window = self::cacheGet($key);
161             if ($window === false) {
162                 $window = $this->getMemberIDs(0,
163                                               User_group::CACHE_WINDOW);
164                 self::cacheSet($key, $window);
165             }
166
167             $ids = array_slice($window,
168                                $offset,
169                                $limit);
170         }
171
172         return Profile::multiGet('id', $ids);
173     }
174
175     function getMemberIDs($offset=0, $limit=null)
176     {
177         $gm = new Group_member();
178
179         $gm->selectAdd();
180         $gm->selectAdd('profile_id');
181
182         $gm->group_id = $this->id;
183
184         $gm->orderBy('created DESC');
185
186         if (!is_null($limit)) {
187             $gm->limit($offset, $limit);
188         }
189
190         $ids = array();
191
192         if ($gm->find()) {
193             while ($gm->fetch()) {
194                 $ids[] = $gm->profile_id;
195             }
196         }
197
198         return $ids;
199     }
200
201     /**
202      * Get pending members, who have not yet been approved.
203      *
204      * @param int $offset
205      * @param int $limit
206      * @return Profile
207      */
208     function getRequests($offset=0, $limit=null)
209     {
210         $qry =
211           'SELECT profile.* ' .
212           'FROM profile JOIN group_join_queue '.
213           'ON profile.id = group_join_queue.profile_id ' .
214           'WHERE group_join_queue.group_id = %d ' .
215           'ORDER BY group_join_queue.created DESC ';
216
217         if ($limit != null) {
218             if (common_config('db','type') == 'pgsql') {
219                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
220             } else {
221                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
222             }
223         }
224
225         $members = new Profile();
226
227         $members->query(sprintf($qry, $this->id));
228         return $members;
229     }
230
231     public function getAdminCount()
232     {
233         $block = new Group_member();
234         $block->group_id = $this->id;
235         $block->is_admin = 1;
236
237         return $block->count();
238     }
239
240     public function getMemberCount()
241     {
242         $key = sprintf("group:member_count:%d", $this->id);
243
244         $cnt = self::cacheGet($key);
245
246         if (is_integer($cnt)) {
247             return (int) $cnt;
248         }
249
250         $mem = new Group_member();
251         $mem->group_id = $this->id;
252
253         // XXX: why 'distinct'?
254
255         $cnt = (int) $mem->count('distinct profile_id');
256
257         self::cacheSet($key, $cnt);
258
259         return $cnt;
260     }
261
262     function getBlockedCount()
263     {
264         // XXX: WORM cache this
265
266         $block = new Group_block();
267         $block->group_id = $this->id;
268
269         return $block->count();
270     }
271
272     function getQueueCount()
273     {
274         // XXX: WORM cache this
275
276         $queue = new Group_join_queue();
277         $queue->group_id = $this->id;
278
279         return $queue->count();
280     }
281
282     function getAdmins($offset=null, $limit=null)   // offset is null because DataObject wants it, 0 would mean no results
283     {
284         $admins = new Profile();
285         $admins->joinAdd(array('id', 'group_member:profile_id'));
286         $admins->whereAdd(sprintf('group_member.group_id = %u AND group_member.is_admin = 1', $this->id));
287         $admins->orderBy('group_member.modified ASC');
288         $admins->limit($offset, $limit);
289         $admins->find();
290
291         return $admins;
292     }
293
294     function getBlocked($offset=null, $limit=null)   // offset is null because DataObject wants it, 0 would mean no results
295     {
296         $blocked = new Profile();
297         $blocked->joinAdd(array('id', 'group_block:blocked'));
298         $blocked->whereAdd(sprintf('group_block.group_id = %u', $this->id));
299         $blocked->orderBy('group_block.modified DESC');
300         $blocked->limit($offset, $limit);
301         $blocked->find();
302
303         return $blocked;
304     }
305
306     function setOriginal($filename)
307     {
308         $imagefile = new ImageFile($this->id, Avatar::path($filename));
309
310         $orig = clone($this);
311         $this->original_logo = Avatar::url($filename);
312         $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
313         $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
314         $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
315         common_debug(common_log_objstring($this));
316         return $this->update($orig);
317     }
318
319     function getBestName()
320     {
321         return ($this->fullname) ? $this->fullname : $this->nickname;
322     }
323
324     /**
325      * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
326      * if no fullname is provided.
327      *
328      * @return string
329      */
330     function getFancyName()
331     {
332         if ($this->fullname) {
333             // TRANS: Full name of a profile or group followed by nickname in parens
334             return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
335         } else {
336             return $this->nickname;
337         }
338     }
339
340     function getAliases()
341     {
342         $aliases = array();
343
344         // XXX: cache this
345
346         $alias = new Group_alias();
347
348         $alias->group_id = $this->id;
349
350         if ($alias->find()) {
351             while ($alias->fetch()) {
352                 $aliases[] = $alias->alias;
353             }
354         }
355
356         $alias->free();
357
358         return $aliases;
359     }
360
361     function setAliases($newaliases) {
362
363         $newaliases = array_unique($newaliases);
364
365         $oldaliases = $this->getAliases();
366
367         // Delete stuff that's old that not in new
368
369         $to_delete = array_diff($oldaliases, $newaliases);
370
371         // Insert stuff that's in new and not in old
372
373         $to_insert = array_diff($newaliases, $oldaliases);
374
375         $alias = new Group_alias();
376
377         $alias->group_id = $this->id;
378
379         foreach ($to_delete as $delalias) {
380             $alias->alias = $delalias;
381             $result = $alias->delete();
382             if (!$result) {
383                 common_log_db_error($alias, 'DELETE', __FILE__);
384                 return false;
385             }
386         }
387
388         foreach ($to_insert as $insalias) {
389             if ($insalias === $this->nickname) {
390                 continue;
391             }
392             $alias->alias = Nickname::normalize($insalias, true);
393             $result = $alias->insert();
394             if (!$result) {
395                 common_log_db_error($alias, 'INSERT', __FILE__);
396                 return false;
397             }
398         }
399
400         return true;
401     }
402
403     static function getForNickname($nickname, Profile $profile=null)
404     {
405         $nickname = Nickname::normalize($nickname);
406
407         // Are there any matching remote groups this profile's in?
408         if ($profile instanceof Profile) {
409             $group = $profile->getGroups(0, null);
410             while ($group instanceof User_group && $group->fetch()) {
411                 if ($group->nickname == $nickname) {
412                     // @fixme is this the best way?
413                     return clone($group);
414                 }
415             }
416         }
417
418         // If not, check local groups.
419         $group = Local_group::getKV('nickname', $nickname);
420         if ($group instanceof Local_group) {
421             return User_group::getKV('id', $group->group_id);
422         }
423         $alias = Group_alias::getKV('alias', $nickname);
424         if ($alias instanceof Group_alias) {
425             return User_group::getKV('id', $alias->group_id);
426         }
427         return null;
428     }
429
430     function getUserMembers()
431     {
432         // XXX: cache this
433
434         $user = new User();
435         if(common_config('db','quote_identifiers'))
436             $user_table = '"user"';
437         else $user_table = 'user';
438
439         $qry =
440           'SELECT id ' .
441           'FROM '. $user_table .' JOIN group_member '.
442           'ON '. $user_table .'.id = group_member.profile_id ' .
443           'WHERE group_member.group_id = %d ';
444
445         $user->query(sprintf($qry, $this->id));
446
447         $ids = array();
448
449         while ($user->fetch()) {
450             $ids[] = $user->id;
451         }
452
453         $user->free();
454
455         return $ids;
456     }
457
458     static function maxDescription()
459     {
460         $desclimit = common_config('group', 'desclimit');
461         // null => use global limit (distinct from 0!)
462         if (is_null($desclimit)) {
463             $desclimit = common_config('site', 'textlimit');
464         }
465         return $desclimit;
466     }
467
468     static function descriptionTooLong($desc)
469     {
470         $desclimit = self::maxDescription();
471         return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
472     }
473
474     function asAtomEntry($namespace=false, $source=false)
475     {
476         $xs = new XMLStringer(true);
477
478         if ($namespace) {
479             $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
480                            'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
481         } else {
482             $attrs = array();
483         }
484
485         $xs->elementStart('entry', $attrs);
486
487         if ($source) {
488             $xs->elementStart('source');
489             $xs->element('id', null, $this->permalink());
490             $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
491             $xs->element('link', array('href' => $this->permalink()));
492             $xs->element('updated', null, $this->modified);
493             $xs->elementEnd('source');
494         }
495
496         $xs->element('title', null, $this->nickname);
497         $xs->element('summary', null, common_xml_safe_str($this->description));
498
499         $xs->element('link', array('rel' => 'alternate',
500                                    'href' => $this->permalink()));
501
502         $xs->element('id', null, $this->permalink());
503
504         $xs->element('published', null, common_date_w3dtf($this->created));
505         $xs->element('updated', null, common_date_w3dtf($this->modified));
506
507         $xs->element(
508             'content',
509             array('type' => 'html'),
510             common_xml_safe_str($this->description)
511         );
512
513         $xs->elementEnd('entry');
514
515         return $xs->getString();
516     }
517
518     function asAtomAuthor()
519     {
520         $xs = new XMLStringer(true);
521
522         $xs->elementStart('author');
523         $xs->element('name', null, $this->nickname);
524         $xs->element('uri', null, $this->permalink());
525         $xs->elementEnd('author');
526
527         return $xs->getString();
528     }
529
530     /**
531      * Returns an XML string fragment with group information as an
532      * Activity Streams noun object with the given element type.
533      *
534      * Assumes that 'activity', 'georss', and 'poco' namespace has been
535      * previously defined.
536      *
537      * @param string $element one of 'actor', 'subject', 'object', 'target'
538      *
539      * @return string
540      */
541     function asActivityNoun($element)
542     {
543         $noun = ActivityObject::fromGroup($this);
544         return $noun->asString('activity:' . $element);
545     }
546
547     function getAvatar()
548     {
549         return empty($this->homepage_logo)
550             ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
551             : $this->homepage_logo;
552     }
553
554     static function register($fields) {
555         if (!empty($fields['userid'])) {
556             $profile = Profile::getKV('id', $fields['userid']);
557             if ($profile && !$profile->hasRight(Right::CREATEGROUP)) {
558                 common_log(LOG_WARNING, "Attempted group creation from banned user: " . $profile->nickname);
559
560                 // TRANS: Client exception thrown when a user tries to create a group while banned.
561                 throw new ClientException(_('You are not allowed to create groups on this site.'), 403);
562             }
563         }
564
565         $fields['nickname'] = Nickname::normalize($fields['nickname']);
566
567         // MAGICALLY put fields into current scope
568         // @fixme kill extract(); it makes debugging absurdly hard
569
570                 $defaults = array('nickname' => null,
571                                                   'fullname' => null,
572                                                   'homepage' => null,
573                                                   'description' => null,
574                                                   'location' => null,
575                                                   'uri' => null,
576                                                   'mainpage' => null,
577                                                   'aliases' => array(),
578                                                   'userid' => null);
579                 
580                 $fields = array_merge($defaults, $fields);
581                 
582         extract($fields);
583
584         $group = new User_group();
585
586         if (empty($uri)) {
587             // fill in later...
588             $uri = null;
589         }
590         if (empty($mainpage)) {
591             $mainpage = common_local_url('showgroup', array('nickname' => $nickname));
592         }
593
594         // We must create a new, incrementally assigned profile_id
595         $profile = new Profile();
596         $profile->nickname   = $nickname;
597         $profile->fullname   = $fullname;
598         $profile->profileurl = $mainpage;
599         $profile->homepage   = $homepage;
600         $profile->bio        = $description;
601         $profile->location   = $location;
602         $profile->created    = common_sql_now();
603
604         $group->nickname    = $profile->nickname;
605         $group->fullname    = $profile->fullname;
606         $group->homepage    = $profile->homepage;
607         $group->description = $profile->bio;
608         $group->location    = $profile->location;
609         $group->mainpage    = $profile->profileurl;
610         $group->created     = $profile->created;
611
612         $profile->query('BEGIN');
613         $id = $profile->insert();
614         if ($id === false) {
615             $profile->query('ROLLBACK');
616             throw new ServerException(_('Profile insertion failed'));
617         }
618
619         $group->profile_id = $id;
620         $group->uri        = $uri;
621
622         if (isset($fields['join_policy'])) {
623             $group->join_policy = intval($fields['join_policy']);
624         } else {
625             $group->join_policy = 0;
626         }
627
628         if (isset($fields['force_scope'])) {
629             $group->force_scope = intval($fields['force_scope']);
630         } else {
631             $group->force_scope = 0;
632         }
633
634         if (Event::handle('StartGroupSave', array(&$group))) {
635
636             $result = $group->insert();
637
638             if ($result === false) {
639                 common_log_db_error($group, 'INSERT', __FILE__);
640                 // TRANS: Server exception thrown when creating a group failed.
641                 throw new ServerException(_('Could not create group.'));
642             }
643
644             if (!isset($uri) || empty($uri)) {
645                 $orig = clone($group);
646                 $group->uri = common_local_url('groupbyid', array('id' => $group->id));
647                 $result = $group->update($orig);
648                 if (!$result) {
649                     common_log_db_error($group, 'UPDATE', __FILE__);
650                     // TRANS: Server exception thrown when updating a group URI failed.
651                     throw new ServerException(_('Could not set group URI.'));
652                 }
653             }
654
655             $result = $group->setAliases($aliases);
656
657             if (!$result) {
658                 // TRANS: Server exception thrown when creating group aliases failed.
659                 throw new ServerException(_('Could not create aliases.'));
660             }
661
662             $member = new Group_member();
663
664             $member->group_id   = $group->id;
665             $member->profile_id = $userid;
666             $member->is_admin   = 1;
667             $member->created    = $group->created;
668
669             $result = $member->insert();
670
671             if (!$result) {
672                 common_log_db_error($member, 'INSERT', __FILE__);
673                 // TRANS: Server exception thrown when setting group membership failed.
674                 throw new ServerException(_('Could not set group membership.'));
675             }
676
677             self::blow('profile:groups:%d', $userid);
678             
679             if ($local) {
680                 $local_group = new Local_group();
681
682                 $local_group->group_id = $group->id;
683                 $local_group->nickname = $nickname;
684                 $local_group->created  = common_sql_now();
685
686                 $result = $local_group->insert();
687
688                 if (!$result) {
689                     common_log_db_error($local_group, 'INSERT', __FILE__);
690                     // TRANS: Server exception thrown when saving local group information failed.
691                     throw new ServerException(_('Could not save local group info.'));
692                 }
693             }
694
695             Event::handle('EndGroupSave', array($group));
696         }
697
698         $profile->query('COMMIT');
699
700         return $group;
701     }
702
703     /**
704      * Handle cascading deletion, on the model of notice and profile.
705      *
706      * This should handle freeing up cached entries for the group's
707      * id, nickname, URI, and aliases. There may be other areas that
708      * are not de-cached in the UI, including the sidebar lists on
709      * GroupsAction
710      */
711     function delete($useWhere=false)
712     {
713         if (empty($this->id)) {
714             common_log(LOG_WARNING, "Ambiguous User_group->delete(); skipping related tables.");
715             return parent::delete($useWhere);
716         }
717
718         try {
719             $profile = $this->getProfile();
720             $profile->delete();
721         } catch (GroupNoProfileException $unp) {
722             common_log(LOG_INFO, "Group {$this->nickname} has no profile; continuing deletion.");
723         }
724
725         // Safe to delete in bulk for now
726
727         $related = array('Group_inbox',
728                          'Group_block',
729                          'Group_member',
730                          'Related_group');
731
732         Event::handle('UserGroupDeleteRelated', array($this, &$related));
733
734         foreach ($related as $cls) {
735             $inst = new $cls();
736             $inst->group_id = $this->id;
737
738             if ($inst->find()) {
739                 while ($inst->fetch()) {
740                     $dup = clone($inst);
741                     $dup->delete();
742                 }
743             }
744         }
745
746         // And related groups in the other direction...
747         $inst = new Related_group();
748         $inst->related_group_id = $this->id;
749         $inst->delete();
750
751         // Aliases and the local_group entry need to be cleared explicitly
752         // or we'll miss clearing some cache keys; that can make it hard
753         // to create a new group with one of those names or aliases.
754         $this->setAliases(array());
755         $local = Local_group::getKV('group_id', $this->id);
756         if ($local instanceof Local_group) {
757             $local->delete();
758         }
759
760         // blow the cached ids
761         self::blow('user_group:notice_ids:%d', $this->id);
762
763         return parent::delete($useWhere);
764     }
765
766     public function update($dataObject=false)
767     {
768         // Whenever the User_group is updated, find the Local_group
769         // and update its nickname too.
770         if ($this->nickname != $dataObject->nickname) {
771             $local = Local_group::getKV('group_id', $this->id);
772             if ($local instanceof Local_group) {
773                 common_debug("Updating Local_group ({$this->id}) nickname from {$dataObject->nickname} to {$this->nickname}");
774                 $local->setNickname($this->nickname);
775             }
776         }
777
778         // Also make sure the Profile table is up to date!
779         $fields = array(/*group field => profile field*/
780                     'nickname'      => 'nickname',
781                     'fullname'      => 'fullname',
782                     'mainpage'      => 'profileurl',
783                     'homepage'      => 'homepage',
784                     'description'   => 'bio',
785                     'location'      => 'location',
786                     'created'       => 'created',
787                     'modified'      => 'modified',
788                     );
789         $profile = $this->getProfile();
790         $origpro = clone($profile);
791         foreach ($fields as $gf=>$pf) {
792             $profile->$pf = $this->$gf;
793         }
794         if ($profile->update($origpro) === false) {
795             throw new ServerException(_('Unable to update profile'));
796         }
797
798         return parent::update($dataObject);
799     }
800
801     function isPrivate()
802     {
803         return ($this->join_policy == self::JOIN_POLICY_MODERATE &&
804                 $this->force_scope == 1);
805     }
806
807     static function groupsFromText($text, Profile $profile)
808     {
809         $groups = array();
810
811         /* extract all !group */
812         $count = preg_match_all('/(?:^|\s)!(' . Nickname::DISPLAY_FMT . ')/',
813                                 strtolower($text),
814                                 $match);
815
816         if (!$count) {
817             return $groups;
818         }
819
820         foreach (array_unique($match[1]) as $nickname) {
821             $group = self::getForNickname($nickname, $profile);
822             if ($group instanceof User_group && $profile->isMember($group)) {
823                 $groups[] = clone($group);
824             }
825         }
826
827         return $groups;
828     }
829
830     static function idsFromText($text, Profile $profile)
831     {
832         $ids = array();
833         $groups = self::groupsFromText($text, $profile);
834         foreach ($groups as $group) {
835             $ids[$group->id] = true;
836         }
837         return array_keys($ids);
838     }
839 }