]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/User_group.php
reindent
[quix0rs-gnu-social.git] / classes / User_group.php
index e9288747467472491459be7b6c72868557040107..60217e960ed7460f7545a8075edca48a7f2c4403 100644 (file)
@@ -154,6 +154,21 @@ class User_group extends Memcached_DataObject
         return $members;
     }
 
+    function getMemberCount()
+    {
+        // XXX: WORM cache this
+
+        $members = $this->getMembers();
+        $member_count = 0;
+
+        /** $member->count() doesn't work. */
+        while ($members->fetch()) {
+            $member_count++;
+        }
+
+        return $member_count;
+    }
+
     function getAdmins($offset=0, $limit=null)
     {
         $qry =
@@ -219,6 +234,22 @@ class User_group extends Memcached_DataObject
         return ($this->fullname) ? $this->fullname : $this->nickname;
     }
 
+    /**
+     * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
+     * if no fullname is provided.
+     *
+     * @return string
+     */
+    function getFancyName()
+    {
+        if ($this->fullname) {
+            // TRANS: Full name of a profile or group followed by nickname in parens
+            return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
+        } else {
+            return $this->nickname;
+        }
+    }
+
     function getAliases()
     {
         $aliases = array();
@@ -279,12 +310,26 @@ class User_group extends Memcached_DataObject
         return true;
     }
 
-    static function getForNickname($nickname)
+    static function getForNickname($nickname, $profile=null)
     {
         $nickname = common_canonical_nickname($nickname);
-        $group = User_group::staticGet('nickname', $nickname);
+
+        // Are there any matching remote groups this profile's in?
+        if ($profile) {
+            $group = $profile->getGroups();
+            while ($group->fetch()) {
+                if ($group->nickname == $nickname) {
+                    // @fixme is this the best way?
+                    return clone($group);
+                }
+            }
+        }
+
+        // If not, check local groups.
+
+        $group = Local_group::staticGet('nickname', $nickname);
         if (!empty($group)) {
-            return $group;
+            return User_group::staticGet('id', $group->group_id);
         }
         $alias = Group_alias::staticGet('alias', $nickname);
         if (!empty($alias)) {
@@ -357,16 +402,15 @@ class User_group extends Memcached_DataObject
 
         if ($source) {
             $xs->elementStart('source');
+            $xs->element('id', null, $this->permalink());
             $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
             $xs->element('link', array('href' => $this->permalink()));
-        }
-
-        if ($source) {
+            $xs->element('updated', null, $this->modified);
             $xs->elementEnd('source');
         }
 
         $xs->element('title', null, $this->nickname);
-        $xs->element('summary', null, $this->description);
+        $xs->element('summary', null, common_xml_safe_str($this->description));
 
         $xs->element('link', array('rel' => 'alternate',
                                    'href' => $this->permalink()));
@@ -376,7 +420,11 @@ class User_group extends Memcached_DataObject
         $xs->element('published', null, common_date_w3dtf($this->created));
         $xs->element('updated', null, common_date_w3dtf($this->modified));
 
-        $xs->element('content', array('type' => 'html'), $this->description);
+        $xs->element(
+            'content',
+            array('type' => 'html'),
+            common_xml_safe_str($this->description)
+        );
 
         $xs->elementEnd('entry');
 
@@ -433,7 +481,6 @@ class User_group extends Memcached_DataObject
     }
 
     static function register($fields) {
-
         // MAGICALLY put fields into current scope
 
         extract($fields);
@@ -442,6 +489,11 @@ class User_group extends Memcached_DataObject
 
         $group->query('BEGIN');
 
+        if (empty($uri)) {
+            // fill in later...
+            $uri = null;
+        }
+
         $group->nickname    = $nickname;
         $group->fullname    = $fullname;
         $group->homepage    = $homepage;
@@ -455,6 +507,7 @@ class User_group extends Memcached_DataObject
 
         if (!$result) {
             common_log_db_error($group, 'INSERT', __FILE__);
+            // TRANS: Server exception thrown when creating a group failed.
             throw new ServerException(_('Could not create group.'));
         }
 
@@ -464,6 +517,7 @@ class User_group extends Memcached_DataObject
             $result = $group->update($orig);
             if (!$result) {
                 common_log_db_error($group, 'UPDATE', __FILE__);
+                // TRANS: Server exception thrown when updating a group URI failed.
                 throw new ServerException(_('Could not set group URI.'));
             }
         }
@@ -471,6 +525,7 @@ class User_group extends Memcached_DataObject
         $result = $group->setAliases($aliases);
 
         if (!$result) {
+            // TRANS: Server exception thrown when creating group aliases failed.
             throw new ServerException(_('Could not create aliases.'));
         }
 
@@ -485,6 +540,7 @@ class User_group extends Memcached_DataObject
 
         if (!$result) {
             common_log_db_error($member, 'INSERT', __FILE__);
+            // TRANS: Server exception thrown when setting group membership failed.
             throw new ServerException(_('Could not set group membership.'));
         }
 
@@ -499,6 +555,7 @@ class User_group extends Memcached_DataObject
 
             if (!$result) {
                 common_log_db_error($local_group, 'INSERT', __FILE__);
+                // TRANS: Server exception thrown when saving local group information failed.
                 throw new ServerException(_('Could not save local group info.'));
             }
         }
@@ -506,4 +563,61 @@ class User_group extends Memcached_DataObject
         $group->query('COMMIT');
         return $group;
     }
+
+    /**
+     * Handle cascading deletion, on the model of notice and profile.
+     *
+     * This should handle freeing up cached entries for the group's
+     * id, nickname, URI, and aliases. There may be other areas that
+     * are not de-cached in the UI, including the sidebar lists on
+     * GroupsAction
+     */
+    function delete()
+    {
+        if ($this->id) {
+
+            // Safe to delete in bulk for now
+
+            $related = array('Group_inbox',
+                             'Group_block',
+                             'Group_member',
+                             'Related_group');
+
+            Event::handle('UserGroupDeleteRelated', array($this, &$related));
+
+            foreach ($related as $cls) {
+
+                $inst = new $cls();
+                $inst->group_id = $this->id;
+
+                if ($inst->find()) {
+                    while ($inst->fetch()) {
+                        $dup = clone($inst);
+                        $dup->delete();
+                    }
+                }
+            }
+
+            // And related groups in the other direction...
+            $inst = new Related_group();
+            $inst->related_group_id = $this->id;
+            $inst->delete();
+
+            // Aliases and the local_group entry need to be cleared explicitly
+            // or we'll miss clearing some cache keys; that can make it hard
+            // to create a new group with one of those names or aliases.
+            $this->setAliases(array());
+            $local = Local_group::staticGet('group_id', $this->id);
+            if ($local) {
+                $local->delete();
+            }
+
+            // blow the cached ids
+            self::blow('user_group:notice_ids:%d', $this->id);
+
+        } else {
+            common_log(LOG_WARN, "Ambiguous user_group->delete(); skipping related tables.");
+        }
+        parent::delete();
+    }
 }