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