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