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