]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Notice.php
Update/add translator documentation.
[quix0rs-gnu-social.git] / classes / Notice.php
index 5a91e6f13a84c80021478795a0fa8b6c156f1a3d..d18ac2e536eb860a4c1f765ca63b5d32c67b3ed8 100644 (file)
@@ -96,17 +96,21 @@ class Notice extends Memcached_DataObject
     const GROUP_SCOPE     = 4;
     const FOLLOWER_SCOPE  = 8;
 
+    protected $_profile = -1;
+
     function getProfile()
     {
-        $profile = Profile::staticGet('id', $this->profile_id);
+        if (is_int($this->_profile) && $this->_profile == -1) {
+            $this->_profile = Profile::staticGet('id', $this->profile_id);
 
-        if (empty($profile)) {
-            // TRANS: Server exception thrown when a user profile for a notice cannot be found.
-            // TRANS: %1$d is a profile ID (number), %2$d is a notice ID (number).
-            throw new ServerException(sprintf(_('No such profile (%1$d) for notice (%2$d).'), $this->profile_id, $this->id));
+            if (empty($this->_profile)) {
+                // TRANS: Server exception thrown when a user profile for a notice cannot be found.
+                // TRANS: %1$d is a profile ID (number), %2$d is a notice ID (number).
+                throw new ServerException(sprintf(_('No such profile (%1$d) for notice (%2$d).'), $this->profile_id, $this->id));
+            }
         }
 
-        return $profile;
+        return $this->_profile;
     }
 
     function delete()
@@ -204,7 +208,7 @@ class Notice extends Memcached_DataObject
 
         if (!$id) {
             // TRANS: Server exception. %s are the error details.
-            throw new ServerException(sprintf(_('Database error inserting hashtag: %s'),
+            throw new ServerException(sprintf(_('Database error inserting hashtag: %s.'),
                                               $last_error->message));
             return;
         }
@@ -573,6 +577,7 @@ class Notice extends Memcached_DataObject
         // was not the root of the conversation.  What to do now?
 
         self::blow('notice:conversation_ids:%d', $this->conversation);
+        self::blow('conversation::notice_count:%d', $this->conversation);
 
         if (!empty($this->repeat_of)) {
             self::blow('notice:repeats:%d', $this->repeat_of);
@@ -724,18 +729,34 @@ class Notice extends Memcached_DataObject
     }
 
     function attachments() {
-        // XXX: cache this
-        $att = array();
-        $f2p = new File_to_post;
-        $f2p->post_id = $this->id;
-        if ($f2p->find()) {
-            while ($f2p->fetch()) {
-                $f = File::staticGet($f2p->file_id);
-                if ($f) {
-                    $att[] = clone($f);
+
+        $keypart = sprintf('notice:file_ids:%d', $this->id);
+
+        $idstr = self::cacheGet($keypart);
+
+        if ($idstr !== false) {
+            $ids = explode(',', $idstr);
+        } else {
+            $ids = array();
+            $f2p = new File_to_post;
+            $f2p->post_id = $this->id;
+            if ($f2p->find()) {
+                while ($f2p->fetch()) {
+                    $ids[] = $f2p->file_id;
                 }
             }
+            self::cacheSet($keypart, implode(',', $ids));
+        }
+
+        $att = array();
+
+        foreach ($ids as $id) {
+            $f = File::staticGet('id', $id);
+            if (!empty($f)) {
+                $att[] = clone($f);
+            }
         }
+
         return $att;
     }
 
@@ -1169,6 +1190,7 @@ class Notice extends Memcached_DataObject
             $result = $ptagi->insert();
             if (!$result) {
                 common_log_db_error($ptagi, 'INSERT', __FILE__);
+                // TRANS: Server exception thrown when saving profile_tag inbox fails.
                 throw new ServerException(_('Problem saving profile_tag inbox.'));
             }
 
@@ -1307,23 +1329,28 @@ class Notice extends Memcached_DataObject
      */
     function getReplies()
     {
-        // XXX: cache me
+        $keypart = sprintf('notice:reply_ids:%d', $this->id);
 
-        $ids = array();
+        $idstr = self::cacheGet($keypart);
 
-        $reply = new Reply();
-        $reply->selectAdd();
-        $reply->selectAdd('profile_id');
-        $reply->notice_id = $this->id;
+        if ($idstr !== false) {
+            $ids = explode(',', $idstr);
+        } else {
+            $ids = array();
 
-        if ($reply->find()) {
-            while($reply->fetch()) {
-                $ids[] = $reply->profile_id;
+            $reply = new Reply();
+            $reply->selectAdd();
+            $reply->selectAdd('profile_id');
+            $reply->notice_id = $this->id;
+
+            if ($reply->find()) {
+                while($reply->fetch()) {
+                    $ids[] = $reply->profile_id;
+                }
             }
+            self::cacheSet($keypart, implode(',', $ids));
         }
 
-        $reply->free();
-
         return $ids;
     }
 
@@ -1365,27 +1392,39 @@ class Notice extends Memcached_DataObject
             return array();
         }
 
-        // XXX: cache me
+        $ids = array();
 
-        $groups = array();
+        $keypart = sprintf('notice:groups:%d', $this->id);
 
-        $gi = new Group_inbox();
+        $idstr = self::cacheGet($keypart);
 
-        $gi->selectAdd();
-        $gi->selectAdd('group_id');
+        if ($idstr !== false) {
+            $ids = explode(',', $idstr);
+        } else {
+            $gi = new Group_inbox();
 
-        $gi->notice_id = $this->id;
+            $gi->selectAdd();
+            $gi->selectAdd('group_id');
 
-        if ($gi->find()) {
-            while ($gi->fetch()) {
-                $group = User_group::staticGet('id', $gi->group_id);
-                if ($group) {
-                    $groups[] = $group;
+            $gi->notice_id = $this->id;
+
+            if ($gi->find()) {
+                while ($gi->fetch()) {
+                    $ids[] = $gi->group_id;
                 }
             }
+
+            self::cacheSet($keypart, implode(',', $ids));
         }
 
-        $gi->free();
+        $groups = array();
+
+        foreach ($ids as $id) {
+            $group = User_group::staticGet('id', $id);
+            if ($group) {
+                $groups[] = $group;
+            }
+        }
 
         return $groups;
     }
@@ -1472,9 +1511,9 @@ class Notice extends Memcached_DataObject
             $reply_ids = $this->getReplies();
 
             foreach ($reply_ids as $id) {
-                $profile = Profile::staticGet('id', $id);
-                if (!empty($profile)) {
-                    $ctx->attention[] = $profile->getUri();
+                $rprofile = Profile::staticGet('id', $id);
+                if (!empty($rprofile)) {
+                    $ctx->attention[] = $rprofile->getUri();
                 }
             }
 
@@ -2106,14 +2145,24 @@ class Notice extends Memcached_DataObject
     public function getTags()
     {
         $tags = array();
-        $tag = new Notice_tag();
-        $tag->notice_id = $this->id;
-        if ($tag->find()) {
-            while ($tag->fetch()) {
-                $tags[] = $tag->tag;
+
+        $keypart = sprintf('notice:tags:%d', $this->id);
+
+        $tagstr = self::cacheGet($keypart);
+
+        if ($tagstr !== false) {
+            $tags = explode(',', $tagstr);
+        } else {
+            $tag = new Notice_tag();
+            $tag->notice_id = $this->id;
+            if ($tag->find()) {
+                while ($tag->fetch()) {
+                    $tags[] = $tag->tag;
+                }
             }
+            self::cacheSet($keypart, implode(',', $tags));
         }
-        $tag->free();
+
         return $tags;
     }
 
@@ -2241,7 +2290,7 @@ class Notice extends Memcached_DataObject
     /**
      * Check that the given profile is allowed to read, respond to, or otherwise
      * act on this notice.
-     * 
+     *
      * The $scope member is a bitmask of scopes, representing a logical AND of the
      * scope requirement. So, 0x03 (Notice::ADDRESSEE_SCOPE | Notice::SITE_SCOPE) means
      * "only visible to people who are mentioned in the notice AND are users on this site."
@@ -2253,6 +2302,21 @@ class Notice extends Memcached_DataObject
      * @return boolean whether the profile is in the notice's scope
      */
     function inScope($profile)
+    {
+        $keypart = sprintf('notice:in-scope-for:%d:%d', $this->id, $profile->id);
+
+        $result = self::cacheGet($keypart);
+
+        if ($result === false) {
+            $bResult = $this->_inScope($profile);
+            $result = ($bResult) ? 1 : 0;
+            self::cacheSet($keypart, $result, 0, 300);
+        }
+
+        return ($result == 1) ? true : false;
+    }
+
+    protected function _inScope($profile)
     {
         // If there's no scope, anyone (even anon) is in scope.