]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/User_group.php
staticGet for sub-Managed_DataObject classes now calls parent
[quix0rs-gnu-social.git] / classes / User_group.php
index 4870e39a15a768399d201c22fb188ee4e818411f..447dbded452811211e02d9860e9670b41d3ca2a5 100644 (file)
@@ -7,6 +7,7 @@ class User_group extends Managed_DataObject
 {
     const JOIN_POLICY_OPEN = 0;
     const JOIN_POLICY_MODERATE = 1;
+    const CACHE_WINDOW = 201;
 
     ###START_AUTOCODE
     /* the code below is auto generated do not remove the above tag */
@@ -29,11 +30,6 @@ class User_group extends Managed_DataObject
     public $join_policy;                     // tinyint
     public $force_scope;                     // tinyint
 
-    /* Static get */
-    function staticGet($k,$v=NULL) {
-        return Memcached_DataObject::staticGet('User_group',$k,$v);
-    }
-    
     function multiGet($keyCol, $keyVals, $skipNulls=true)
     {
         return parent::multiGet('User_group', $keyCol, $keyVals, $skipNulls);
@@ -141,27 +137,52 @@ class User_group extends Managed_DataObject
         return !in_array($nickname, $blacklist);
     }
 
-    function getMembers($offset=0, $limit=null)
+    function getMembers($offset=0, $limit=null) {
+        $ids = null;
+        if (is_null($limit) || $offset + $limit > User_group::CACHE_WINDOW) {
+            $ids = $this->getMemberIDs($offset,
+                                       $limit);
+        } else {
+            $key = sprintf('group:member_ids:%d', $this->id);
+            $window = self::cacheGet($key);
+            if ($window === false) {
+                $window = $this->getMemberIDs(0,
+                                              User_group::CACHE_WINDOW);
+                self::cacheSet($key, $window);
+            }
+
+            $ids = array_slice($window,
+                               $offset,
+                               $limit);
+        }
+
+        return Profile::multiGet('id', $ids);
+    }
+
+    function getMemberIDs($offset=0, $limit=null)
     {
-        $qry =
-          'SELECT profile.* ' .
-          'FROM profile JOIN group_member '.
-          'ON profile.id = group_member.profile_id ' .
-          'WHERE group_member.group_id = %d ' .
-          'ORDER BY group_member.created DESC ';
+        $gm = new Group_member();
 
-        if ($limit != null) {
-            if (common_config('db','type') == 'pgsql') {
-                $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
-            } else {
-                $qry .= ' LIMIT ' . $offset . ', ' . $limit;
-            }
+        $gm->selectAdd();
+        $gm->selectAdd('profile_id');
+
+        $gm->group_id = $this->id;
+
+        $gm->orderBy('created DESC');
+
+        if (!is_null($limit)) {
+            $gm->limit($offset, $limit);
         }
 
-        $members = new Profile();
+        $ids = array();
 
-        $members->query(sprintf($qry, $this->id));
-        return $members;
+        if ($gm->find()) {
+            while ($gm->fetch()) {
+                $ids[] = $gm->profile_id;
+            }
+        }
+
+        return $ids;
     }
 
     /**
@@ -196,17 +217,44 @@ class User_group extends Managed_DataObject
 
     function getMemberCount()
     {
-        // XXX: WORM cache this
+        $key = sprintf("group:member_count:%d", $this->id);
 
-        $members = $this->getMembers();
-        $member_count = 0;
+        $cnt = self::cacheGet($key);
 
-        /** $member->count() doesn't work. */
-        while ($members->fetch()) {
-            $member_count++;
+        if (is_integer($cnt)) {
+            return (int) $cnt;
         }
 
-        return $member_count;
+        $mem = new Group_member();
+        $mem->group_id = $this->id;
+
+        // XXX: why 'distinct'?
+
+        $cnt = (int) $mem->count('distinct profile_id');
+
+        self::cacheSet($key, $cnt);
+
+        return $cnt;
+    }
+
+    function getBlockedCount()
+    {
+        // XXX: WORM cache this
+
+        $block = new Group_block();
+        $block->group_id = $this->id;
+
+        return $block->count();
+    }
+
+    function getQueueCount()
+    {
+        // XXX: WORM cache this
+
+        $queue = new Group_join_queue();
+        $queue->group_id = $this->id;
+
+        return $queue->count();
     }
 
     function getAdmins($offset=0, $limit=null)
@@ -356,7 +404,7 @@ class User_group extends Managed_DataObject
 
         // Are there any matching remote groups this profile's in?
         if ($profile) {
-            $group = $profile->getGroups();
+            $group = $profile->getGroups(0, null);
             while ($group->fetch()) {
                 if ($group->nickname == $nickname) {
                     // @fixme is this the best way?