Use UserProfileCache
authorMatthias Schmidt <gravatronics@live.com>
Sat, 8 Aug 2015 10:27:29 +0000 (12:27 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sat, 8 Aug 2015 10:27:29 +0000 (12:27 +0200)
files/lib/data/conversation/UserConversationList.class.php
files/lib/data/conversation/ViewableConversation.class.php
files/lib/data/conversation/message/ViewableConversationMessage.class.php
files/lib/data/conversation/message/ViewableConversationMessageList.class.php
files/lib/data/modification/log/ConversationLogModificationLogList.class.php
files/lib/data/modification/log/ViewableConversationModificationLog.class.php

index f1e502a1bb9101c86b2a27fc3750e353e01e894b..d93e76a00155a866948b7f5cab9d70875455691f 100644 (file)
@@ -2,6 +2,7 @@
 namespace wcf\data\conversation;
 use wcf\data\conversation\label\ConversationLabel;
 use wcf\data\conversation\label\ConversationLabelList;
+use wcf\data\user\UserProfileCache;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\WCF;
 
@@ -80,18 +81,6 @@ class UserConversationList extends ConversationList {
                if (!empty($this->sqlSelects)) $this->sqlSelects .= ',';
                $this->sqlSelects .= "conversation_to_user.*";
                $this->sqlJoins .= "LEFT JOIN wcf".WCF_N."_conversation_to_user conversation_to_user ON (conversation_to_user.participantID = ".$userID." AND conversation_to_user.conversationID = conversation.conversationID)";
-               
-               // get avatars
-               if (!empty($this->sqlSelects)) $this->sqlSelects .= ',';
-               $this->sqlSelects .= "user_avatar.*, user_table.email, user_table.disableAvatar, user_table.enableGravatar, user_table.gravatarFileExtension";
-               $this->sqlJoins .= " LEFT JOIN wcf".WCF_N."_user user_table ON (user_table.userID = conversation.userID)";
-               $this->sqlJoins .= " LEFT JOIN wcf".WCF_N."_user_avatar user_avatar ON (user_avatar.avatarID = user_table.avatarID)";
-               
-               if (!empty($this->sqlSelects)) $this->sqlSelects .= ',';
-               $this->sqlSelects .= "lastposter_avatar.avatarID AS lastPosterAvatarID, lastposter_avatar.avatarName AS lastPosterAvatarName, lastposter_avatar.avatarExtension AS lastPosterAvatarExtension, lastposter_avatar.width AS lastPosterAvatarWidth, lastposter_avatar.height AS lastPosterAvatarHeight,";
-               $this->sqlSelects .= "lastposter.email AS lastPosterEmail, lastposter.disableAvatar AS lastPosterDisableAvatar, lastposter.enableGravatar AS lastPosterEnableGravatar, lastposter.gravatarFileExtension AS lastPosterGravatarFileExtension";
-               $this->sqlJoins .= " LEFT JOIN wcf".WCF_N."_user lastposter ON (lastposter.userID = conversation.lastPosterID)";
-               $this->sqlJoins .= " LEFT JOIN wcf".WCF_N."_user_avatar lastposter_avatar ON (lastposter_avatar.avatarID = lastposter.avatarID)";
        }
        
        /**
@@ -151,12 +140,24 @@ class UserConversationList extends ConversationList {
                if (!empty($this->objects)) {
                        $labels = $this->loadLabelAssignments();
                        
+                       $userIDs = [ ];
                        foreach ($this->objects as $conversationID => $conversation) {
                                if (isset($labels[$conversationID])) {
                                        foreach ($labels[$conversationID] as $label) {
                                                $conversation->assignLabel($label);
                                        }
                                }
+                               
+                               if ($conversation->userID) {
+                                       $userIDs[] = $conversation->userID;
+                               }
+                               if ($conversation->lastPosterID) {
+                                       $userIDs[] = $conversation->lastPosterID;
+                               }
+                       }
+                       
+                       if (!empty($userIDs)) {
+                               UserProfileCache::getInstance()->cacheUserIDs($userIDs);
                        }
                }
        }
index d7f4a05f1814c5aaf92fd3fbcfeb2f96b85b2860..caa3015ee5a5de8561f1f7c766ee242c8324cdbb 100644 (file)
@@ -4,7 +4,9 @@ use wcf\data\conversation\label\ConversationLabel;
 use wcf\data\conversation\label\ConversationLabelList;
 use wcf\data\user\User;
 use wcf\data\user\UserProfile;
+use wcf\data\user\UserProfileCache;
 use wcf\data\DatabaseObjectDecorator;
+use wcf\data\TLegacyUserPropertyAccess;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\WCF;
 
@@ -19,6 +21,8 @@ use wcf\system\WCF;
  * @category   Community Framework
  */
 class ViewableConversation extends DatabaseObjectDecorator {
+       use TLegacyUserPropertyAccess;
+       
        /**
         * participant summary
         * @var string
@@ -37,16 +41,60 @@ class ViewableConversation extends DatabaseObjectDecorator {
         */
        protected $lastPosterProfile = null;
        
+       /**
+        * list of assigned labels
+        * @var array<\wcf\data\conversation\label\ConversationLabel>
+        */
+       protected $labels = array();
+       
        /**
         * @see \wcf\data\DatabaseObjectDecorator::$baseClass
         */
        protected static $baseClass = 'wcf\data\conversation\Conversation';
        
        /**
-        * list of assigned labels
-        * @var array<\wcf\data\conversation\label\ConversationLabel>
+        * maps legacy direct access to last poster's user profile data to the real
+        * user profile property names
+        * @var array<string>
+        * @deprecated
         */
-       protected $labels = array();
+       protected static $__lastUserAvatarPropertyMapping = array(
+               'lastPosterAvatarID' => 'avatarID',
+               'lastPosterAvatarName' => 'avatarName',
+               'lastPosterAvatarExtension' => 'avatarExtension',
+               'lastPosterAvatarWidth' => 'width',
+               'lastPosterAvatarHeight' => 'height',
+               'lastPosterEmail' => 'email',
+               'lastPosterDisableAvatar' => 'disableAvatar',
+               'lastPosterEnableGravatar' => 'enableGravatar',
+               'lastPosterGravatarFileExtension' => 'gravatarFileExtension',
+               'lastPosterAvatarFileHash' => 'fileHash'
+       );
+       
+       /**
+        * @see \wcf\data\IStorableObject::__get()
+        * @deprecated
+        */
+       public function __get($name) {
+               $value = parent::__get($name);
+               if ($value !== null) {
+                       return $value;
+               }
+               else if (array_key_exists($name, $this->object->data)) {
+                       return null;
+               }
+               
+               $value = $this->getUserProfile()->$name;
+               if ($value !== null) {
+                       return $value;
+               }
+               
+               if (isset(static::$__lastUserAvatarPropertyMapping[$name])) {
+                       return $this->getLastPosterProfile()->getAvatar()->{static::$__lastUserAvatarPropertyMapping[$name]};
+               }
+               
+               return null;
+       }
        
        /**
         * Returns the user profile object.
@@ -55,7 +103,14 @@ class ViewableConversation extends DatabaseObjectDecorator {
         */
        public function getUserProfile() {
                if ($this->userProfile === null) {
-                       $this->userProfile = new UserProfile(new User(null, $this->getDecoratedObject()->data));
+                       if ($this->userID) {
+                               $this->userProfile = UserProfileCache::getInstance()->getUserProfile($this->userID);
+                       }
+                       else {
+                               $this->userProfile = new UserProfile(new User(null, array(
+                                       'username' => $this->username
+                               )));
+                       }
                }
                
                return $this->userProfile;
@@ -68,22 +123,12 @@ class ViewableConversation extends DatabaseObjectDecorator {
         */
        public function getLastPosterProfile() {
                if ($this->lastPosterProfile === null) {
-                       if ($this->userID && $this->userID == $this->lastPosterID) {
-                               $this->lastPosterProfile = $this->getUserProfile();
+                       if ($this->lastPosterID) {
+                               $this->lastPosterProfile = UserProfileCache::getInstance()->getUserProfile($this->lastPosterID);
                        }
                        else {
                                $this->lastPosterProfile = new UserProfile(new User(null, array(
-                                       'userID' => $this->lastPosterID,
-                                       'username' => $this->lastPoster,
-                                       'avatarID' => $this->lastPosterAvatarID,
-                                       'avatarName' => $this->lastPosterAvatarName,
-                                       'avatarExtension' => $this->lastPosterAvatarExtension,
-                                       'width' => $this->lastPosterAvatarWidth,
-                                       'height' => $this->lastPosterAvatarHeight,
-                                       'email' => $this->lastPosterEmail,
-                                       'disableAvatar' => $this->lastPosterDisableAvatar,
-                                       'enableGravatar' => $this->lastPosterEnableGravatar,
-                                       'gravatarFileExtension' => $this->lastPosterGravatarFileExtension
+                                       'username' => $this->lastPoster
                                )));
                        }
                }
index f9fc01d7917f8e3bd0d364affd8e0fcf28459f6c..5e6056fc972d784b534aa282c5e19b09f3dffa8d 100644 (file)
@@ -2,7 +2,9 @@
 namespace wcf\data\conversation\message;
 use wcf\data\user\User;
 use wcf\data\user\UserProfile;
+use wcf\data\user\UserProfileCache;
 use wcf\data\DatabaseObjectDecorator;
+use wcf\data\TLegacyUserPropertyAccess;
 
 /**
  * Represents a viewable conversation message.
@@ -15,6 +17,8 @@ use wcf\data\DatabaseObjectDecorator;
  * @category   Community Framework
  */
 class ViewableConversationMessage extends DatabaseObjectDecorator {
+       use TLegacyUserPropertyAccess;
+       
        /**
         * @see \wcf\data\DatabaseObjectDecorator::$baseClass
         */
@@ -33,7 +37,14 @@ class ViewableConversationMessage extends DatabaseObjectDecorator {
         */
        public function getUserProfile() {
                if ($this->userProfile === null) {
-                       $this->userProfile = new UserProfile(new User(null, $this->getDecoratedObject()->data));
+                       if ($this->userID) {
+                               $this->userProfile = UserProfileCache::getInstance()->getUserProfile($this->userID);
+                       }
+                       else {
+                               $this->userProfile = new UserProfile(new User(null, array(
+                                       'username' => $this->username
+                               )));
+                       }
                }
                
                return $this->userProfile;
index 6ac77cd75db914a64b43c681ce9fa49412ab28c6..30b82d44372d070784d5ac46f92f0b60999412c8 100644 (file)
@@ -3,6 +3,7 @@ namespace wcf\data\conversation\message;
 use wcf\data\attachment\GroupedAttachmentList;
 use wcf\data\conversation\Conversation;
 use wcf\data\object\type\ObjectTypeCache;
+use wcf\data\user\UserProfileCache;
 use wcf\system\message\embedded\object\MessageEmbeddedObjectManager;
 
 /**
@@ -68,22 +69,6 @@ class ViewableConversationMessageList extends ConversationMessageList {
         */
        protected $conversation = null;
        
-       /**
-        * Creates a new ViewableConversationMessageList object.
-        */
-       public function __construct() {
-               parent::__construct();
-               
-               $this->sqlSelects .= "user_option_value.*, user_table.*";
-               $this->sqlJoins .= " LEFT JOIN wcf".WCF_N."_user user_table ON (user_table.userID = conversation_message.userID)";
-               $this->sqlJoins .= " LEFT JOIN wcf".WCF_N."_user_option_value user_option_value ON (user_option_value.userID = user_table.userID)";
-               
-               // get avatars
-               if (!empty($this->sqlSelects)) $this->sqlSelects .= ',';
-               $this->sqlSelects .= "user_avatar.*";
-               $this->sqlJoins .= " LEFT JOIN wcf".WCF_N."_user_avatar user_avatar ON (user_avatar.avatarID = user_table.avatarID)";
-       }
-       
        /**
         * @see \wcf\data\DatabaseObjectList::readObjects()
         */
@@ -94,6 +79,7 @@ class ViewableConversationMessageList extends ConversationMessageList {
                
                parent::readObjects();
                
+               $userIDs = [ ];
                foreach ($this->objects as $message) {
                        if ($message->time > $this->maxPostTime) {
                                $this->maxPostTime = $message->time;
@@ -109,6 +95,13 @@ class ViewableConversationMessageList extends ConversationMessageList {
                        if ($message->hasEmbeddedObjects) {
                                $this->embeddedObjectMessageIDs[] = $message->messageID;
                        }
+                       if ($message->userID) {
+                               $userIDs[] = $message->userID;
+                       }
+               }
+               
+               if (!empty($userIDs)) {
+                       UserProfileCache::getInstance()->cacheUserIDs($userIDs);
                }
                
                if ($this->embeddedObjectLoading) {
@@ -126,7 +119,7 @@ class ViewableConversationMessageList extends ConversationMessageList {
                if (!empty($this->embeddedObjectMessageIDs)) {
                        // add message objects to attachment object cache to save SQL queries
                        ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.attachment.objectType', 'com.woltlab.wcf.conversation.message')->getProcessor()->setCachedObjects($this->objects);
-                               
+                       
                        // load embedded objects
                        MessageEmbeddedObjectManager::getInstance()->loadObjects('com.woltlab.wcf.conversation.message', $this->embeddedObjectMessageIDs);
                }
index f9e15290c214b9dce93742976e34543eb8f56111..0b9f9b4d3c9a30df76c225075777245a0ee004ca 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 namespace wcf\data\modification\log;
 use wcf\data\conversation\Conversation;
+use wcf\data\user\UserProfileCache;
 use wcf\system\log\modification\ConversationModificationLogHandler;
 use wcf\system\WCF;
 
@@ -69,15 +70,11 @@ class ConversationLogModificationLogList extends ModificationLogList {
         * @see \wcf\data\DatabaseObjectList::readObjects()
         */
        public function readObjects() {
-               $sql = "SELECT          user_avatar.*,
-                                       user_table.email, user_table.enableGravatar, user_table.disableAvatar, user_table.gravatarFileExtension,
-                                       modification_log.*
-                       FROM            wcf".WCF_N."_modification_log modification_log
-                       LEFT JOIN       wcf".WCF_N."_user user_table ON (user_table.userID = modification_log.userID)
-                       LEFT JOIN       wcf".WCF_N."_user_avatar user_avatar ON (user_avatar.avatarID = user_table.avatarID)            
-                       WHERE           modification_log.objectTypeID = ?
-                                       AND modification_log.objectID = ?
-                                       ".(!empty($this->sqlOrderBy) ? "ORDER BY ".$this->sqlOrderBy : '');
+               $sql = "SELECT  modification_log.*
+                       FROM    wcf".WCF_N."_modification_log modification_log
+                       WHERE   modification_log.objectTypeID = ?
+                               AND modification_log.objectID = ?
+                       ".(!empty($this->sqlOrderBy) ? "ORDER BY ".$this->sqlOrderBy : '');
                $statement = WCF::getDB()->prepareStatement($sql, $this->sqlLimit, $this->sqlOffset);
                $statement->execute(array(
                        $this->conversationObjectTypeID,
@@ -86,16 +83,24 @@ class ConversationLogModificationLogList extends ModificationLogList {
                $this->objects = $statement->fetchObjects(($this->objectClassName ?: $this->className));
                
                // use table index as array index
-               $objects = array();
+               $objects = $userIDs = [ ];
                foreach ($this->objects as $object) {
                        $objectID = $object->{$this->getDatabaseTableIndexName()};
                        $objects[$objectID] = $object;
                        
                        $this->indexToObject[] = $objectID;
+                       
+                       if ($object->userID) {
+                               $userIDs[] = $object->userID;
+                       }
                }
                $this->objectIDs = $this->indexToObject;
                $this->objects = $objects;
                
+               if (!empty($userIDs)) {
+                       UserProfileCache::getInstance()->cacheUserIDs($userIDs);
+               }
+               
                foreach ($this->objects as &$object) {
                        $object = new ViewableConversationModificationLog($object);
                }
index e738625af71460f8378373e5b98d4ec61fb4adfa..d7f246cbc40831e34c9e8742ee64107943883d2c 100644 (file)
@@ -2,7 +2,9 @@
 namespace wcf\data\modification\log;
 use wcf\data\user\User;
 use wcf\data\user\UserProfile;
+use wcf\data\user\UserProfileCache;
 use wcf\data\DatabaseObjectDecorator;
+use wcf\data\TLegacyUserPropertyAccess;
 use wcf\system\WCF;
 
 /**
@@ -16,6 +18,8 @@ use wcf\system\WCF;
  * @category   Community Framework
  */
 class ViewableConversationModificationLog extends DatabaseObjectDecorator {
+       use TLegacyUserPropertyAccess;
+       
        /**
         * @see \wcf\data\DatabaseObjectDecorator::$baseClass
         */
@@ -41,7 +45,14 @@ class ViewableConversationModificationLog extends DatabaseObjectDecorator {
         */
        public function getUserProfile() {
                if ($this->userProfile === null) {
-                       $this->userProfile = new UserProfile(new User(null, $this->getDecoratedObject()->data));
+                       if ($this->userID) {
+                               $this->userProfile = UserProfileCache::getInstance()->getUserProfile($this->userID);
+                       }
+                       else {
+                               $this->userProfile = new UserProfile(new User(null, array(
+                                       'username' => $this->username
+                               )));
+                       }
                }
                
                return $this->userProfile;