From 97ce71e55d719af3ff53d70367c5e72312bcd136 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Sun, 18 Aug 2013 21:02:33 +0200 Subject: [PATCH] Managed_DataObject now has listGet for all classes --- classes/Managed_DataObject.php | 18 ++++++++++++++++++ classes/Memcached_DataObject.php | 4 ++-- classes/Notice.php | 20 ++++++++++---------- lib/conversationnoticestream.php | 2 +- lib/threadednoticelist.php | 2 +- lib/useractivitystream.php | 4 ++-- 6 files changed, 34 insertions(+), 16 deletions(-) diff --git a/classes/Managed_DataObject.php b/classes/Managed_DataObject.php index e473a2bf03..b4eab98f8a 100644 --- a/classes/Managed_DataObject.php +++ b/classes/Managed_DataObject.php @@ -61,6 +61,24 @@ abstract class Managed_DataObject extends Memcached_DataObject return parent::pkeyGetClass(get_called_class(), $kv); } + /** + * Get a multi-instance object + * + * This is a utility method to get multiple instances with a given set of + * values for a specific key column. Usually used for the primary key when + * multiple values are desired. + * + * @param array $keyCol key column name + * @param array $keyVals array of key values + * + * @return get_called_class() object with multiple instances if found, or null for no hits + * + */ + static function listGet($keyCol, $keyVals) + { + return parent::listGetClass(get_called_class(), $keyCol, $keyVals); + } + /** * get/set an associative array of table columns * diff --git a/classes/Memcached_DataObject.php b/classes/Memcached_DataObject.php index f95d055c01..72eb9b9188 100644 --- a/classes/Memcached_DataObject.php +++ b/classes/Memcached_DataObject.php @@ -267,7 +267,7 @@ class Memcached_DataObject extends Safe_DataObject return $pkey; } - function listGet($cls, $keyCol, $keyVals) + static function listGetClass($cls, $keyCol, $keyVals) { $pkeyMap = array_fill_keys($keyVals, array()); $result = array_fill_keys($keyVals, array()); @@ -312,7 +312,7 @@ class Memcached_DataObject extends Safe_DataObject } $i->whereAddIn($keyCol, $toFetch, $i->columnType($keyCol)); if ($i->find()) { - sprintf("listGet() got {$i->N} results for class $cls key $keyCol"); + sprintf(__CLASS__ . "() got {$i->N} results for class $cls key $keyCol"); while ($i->fetch()) { $copy = clone($i); $copy->encache(); diff --git a/classes/Notice.php b/classes/Notice.php index 743eba12bf..4f78343798 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -823,7 +823,7 @@ class Notice extends Managed_DataObject return $this->_attachments; } - $f2ps = Memcached_DataObject::listGet('File_to_post', 'post_id', array($this->id)); + $f2ps = File_to_post::listGet('post_id', array($this->id)); $ids = array(); @@ -1380,7 +1380,7 @@ class Notice extends Managed_DataObject return $this->_replies; } - $replyMap = Memcached_DataObject::listGet('Reply', 'notice_id', array($this->id)); + $replyMap = Reply::listGet('notice_id', array($this->id)); $ids = array(); @@ -1458,7 +1458,7 @@ class Notice extends Managed_DataObject return $this->_groups; } - $gis = Memcached_DataObject::listGet('Group_inbox', 'notice_id', array($this->id)); + $gis = Group_inbox::listGet('notice_id', array($this->id)); $ids = array(); @@ -2607,7 +2607,7 @@ class Notice extends Managed_DataObject { $ids = self::_idsOf($notices); - $gis = Memcached_DataObject::listGet('Group_inbox', 'notice_id', $ids); + $gis = Group_inbox::listGet('notice_id', $ids); $gids = array(); @@ -2648,7 +2648,7 @@ class Notice extends Managed_DataObject { $ids = self::_idsOf($notices); - $f2pMap = Memcached_DataObject::listGet('File_to_post', 'post_id', $ids); + $f2pMap = File_to_post::listGet('post_id', $ids); $fileIds = array(); @@ -2686,7 +2686,7 @@ class Notice extends Managed_DataObject if (isset($this->_faves) && is_array($this->_faves)) { return $this->_faves; } - $faveMap = Memcached_DataObject::listGet('Fave', 'notice_id', array($this->id)); + $faveMap = Fave::listGet('notice_id', array($this->id)); $this->_faves = $faveMap[$this->id]; return $this->_faves; } @@ -2699,7 +2699,7 @@ class Notice extends Managed_DataObject static function fillFaves(&$notices) { $ids = self::_idsOf($notices); - $faveMap = Memcached_DataObject::listGet('Fave', 'notice_id', $ids); + $faveMap = Fave::listGet('notice_id', $ids); $cnt = 0; $faved = array(); foreach ($faveMap as $id => $faves) { @@ -2717,7 +2717,7 @@ class Notice extends Managed_DataObject static function fillReplies(&$notices) { $ids = self::_idsOf($notices); - $replyMap = Memcached_DataObject::listGet('Reply', 'notice_id', $ids); + $replyMap = Reply::listGet('notice_id', $ids); foreach ($notices as $notice) { $replies = $replyMap[$notice->id]; $ids = array(); @@ -2735,7 +2735,7 @@ class Notice extends Managed_DataObject if (isset($this->_repeats) && is_array($this->_repeats)) { return $this->_repeats; } - $repeatMap = Memcached_DataObject::listGet('Notice', 'repeat_of', array($this->id)); + $repeatMap = Notice::listGet('repeat_of', array($this->id)); $this->_repeats = $repeatMap[$this->id]; return $this->_repeats; } @@ -2748,7 +2748,7 @@ class Notice extends Managed_DataObject static function fillRepeats(&$notices) { $ids = self::_idsOf($notices); - $repeatMap = Memcached_DataObject::listGet('Notice', 'repeat_of', $ids); + $repeatMap = Notice::listGet('repeat_of', $ids); foreach ($notices as $notice) { $repeats = $repeatMap[$notice->id]; $notice->_setRepeats($repeats); diff --git a/lib/conversationnoticestream.php b/lib/conversationnoticestream.php index c43b5deb24..1d823760d7 100644 --- a/lib/conversationnoticestream.php +++ b/lib/conversationnoticestream.php @@ -78,7 +78,7 @@ class RawConversationNoticeStream extends NoticeStream function getNotices($offset, $limit, $sinceId = null, $maxId = null) { - $all = Memcached_DataObject::listGet('Notice', 'conversation', array($this->id)); + $all = Notice::listGet('conversation', array($this->id)); $notices = $all[$this->id]; // Re-order in reverse-chron usort($notices, array('RawConversationNoticeStream', '_reverseChron')); diff --git a/lib/threadednoticelist.php b/lib/threadednoticelist.php index 8621590ad6..5c45b5a831 100644 --- a/lib/threadednoticelist.php +++ b/lib/threadednoticelist.php @@ -130,7 +130,7 @@ class ThreadedNoticeList extends NoticeList $convId[] = $notice->conversation; } $convId = array_unique($convId); - $allMap = Memcached_DataObject::listGet('Notice', 'conversation', $convId); + $allMap = Notice::listGet('conversation', $convId); $allArray = array(); foreach ($allMap as $convId => $convNotices) { $allArray = array_merge($allArray, $convNotices); diff --git a/lib/useractivitystream.php b/lib/useractivitystream.php index f4a771c5f0..246bdb5d90 100644 --- a/lib/useractivitystream.php +++ b/lib/useractivitystream.php @@ -376,7 +376,7 @@ class UserActivityStream extends AtomUserNoticeFeed function getMessagesTo() { - $msgMap = Memcached_DataObject::listGet('Message', 'to_profile', array($this->user->id)); + $msgMap = Message::listGet('to_profile', array($this->user->id)); $messages = $msgMap[$this->user->id]; @@ -389,7 +389,7 @@ class UserActivityStream extends AtomUserNoticeFeed function getMessagesFrom() { - $msgMap = Memcached_DataObject::listGet('Message', 'from_profile', array($this->user->id)); + $msgMap = Message::listGet('from_profile', array($this->user->id)); $messages = $msgMap[$this->user->id];