No longer use `wcf1_user_avatar` to load the user's avatar
authorCyperghost <olaf_schmitz_1@t-online.de>
Fri, 8 Nov 2024 10:09:09 +0000 (11:09 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Fri, 8 Nov 2024 10:09:09 +0000 (11:09 +0100)
wcfsetup/install/files/lib/acp/page/UserListPage.class.php
wcfsetup/install/files/lib/data/user/TUserAvatarObjectList.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/data/user/UserProfile.class.php
wcfsetup/install/files/lib/data/user/UserProfileList.class.php
wcfsetup/install/files/lib/data/user/follow/UserFollowerList.class.php
wcfsetup/install/files/lib/data/user/follow/UserFollowingList.class.php
wcfsetup/install/files/lib/data/user/ignore/ViewableUserIgnoreList.class.php
wcfsetup/install/files/lib/data/user/online/UsersOnlineList.class.php
wcfsetup/install/files/lib/data/user/profile/visitor/UserProfileVisitorList.class.php

index c03a1e2c2f6750eb5e87fedc091c6b713b233e44..e4ea36d586131ccd29a4790581a5a31cc0b04160 100755 (executable)
@@ -286,12 +286,10 @@ class UserListPage extends SortablePage
             $statement->execute($conditions->getParameters());
             $userToGroups = $statement->fetchMap('userID', 'groupID', false);
 
-            $sql = "SELECT      user_avatar.*, option_value.*, user_table.*
+            $sql = "SELECT      option_value.*, user_table.*
                     FROM        wcf1_user user_table
                     LEFT JOIN   wcf1_user_option_value option_value
                     ON          option_value.userID = user_table.userID
-                    LEFT JOIN   wcf1_user_avatar user_avatar
-                    ON          user_avatar.avatarID = user_table.avatarID
                     " . $conditions . "
                     ORDER BY    " . (($this->sortField != 'email' && isset($this->options[$this->sortField])) ? 'option_value.userOption' . $this->options[$this->sortField]->optionID : 'user_table.' . $this->sortField) . " " . $this->sortOrder;
             $statement = WCF::getDB()->prepare($sql);
diff --git a/wcfsetup/install/files/lib/data/user/TUserAvatarObjectList.class.php b/wcfsetup/install/files/lib/data/user/TUserAvatarObjectList.class.php
new file mode 100644 (file)
index 0000000..7c9ef00
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+
+namespace wcf\data\user;
+
+use wcf\data\DatabaseObjectList;
+use wcf\data\file\FileList;
+
+/**
+ * Contains methods to load avatar files for a list of `UserProfile`.
+ *
+ * @author      Olaf Braun
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ *
+ * @property    UserProfile[] $objects
+ * @mixin DatabaseObjectList
+ */
+trait TUserAvatarObjectList
+{
+    public bool $loadAvatarFiles = true;
+
+    protected function loadAvatarFiles(): void
+    {
+        if (!$this->loadAvatarFiles) {
+            return;
+        }
+
+        $avatarFileIDs = [];
+        foreach ($this->objects as $user) {
+            if ($user->avatarFileID !== null) {
+                $avatarFileIDs[] = $user->avatarFileID;
+            }
+        }
+        if ($avatarFileIDs === []) {
+            return;
+        }
+
+        $fileList = new FileList();
+        $fileList->loadThumbnails = true;
+        $fileList->setObjectIDs($avatarFileIDs);
+        $fileList->readObjects();
+        $files = $fileList->getObjects();
+
+        foreach ($this->objects as $user) {
+            if ($user->avatarFileID !== null) {
+                $user->setFileAvatar($files[$user->avatarFileID]);
+            }
+        }
+    }
+}
index 6fa145aaae1410dfb6aeb9b84411d0f2ae8257db..c56761dddfd5fb0c76739e69953118b51563fa55 100644 (file)
@@ -4,14 +4,13 @@ namespace wcf\data\user;
 
 use wcf\data\DatabaseObjectDecorator;
 use wcf\data\file\File;
-use wcf\data\file\thumbnail\FileThumbnailList;
+use wcf\data\file\FileList;
 use wcf\data\ITitledLinkObject;
 use wcf\data\trophy\Trophy;
 use wcf\data\trophy\TrophyCache;
 use wcf\data\user\avatar\AvatarDecorator;
 use wcf\data\user\avatar\DefaultAvatar;
 use wcf\data\user\avatar\IUserAvatar;
-use wcf\data\user\avatar\UserAvatar;
 use wcf\data\user\cover\photo\DefaultUserCoverPhoto;
 use wcf\data\user\cover\photo\IUserCoverPhoto;
 use wcf\data\user\cover\photo\UserCoverPhoto;
@@ -347,21 +346,18 @@ class UserProfile extends DatabaseObjectDecorator implements ITitledLinkObject
      */
     public function getAvatar()
     {
-        // TODO simplify this function?
         if ($this->avatar === null) {
             if (!$this->disableAvatar) {
                 if ($this->canSeeAvatar()) {
                     if ($this->avatarFileID !== null) {
                         $data = UserStorageHandler::getInstance()->getField('avatar', $this->userID);
                         if ($data === null) {
-                            $this->avatar = new File($this->avatarFileID);
+                            $fileList = new FileList();
+                            $fileList->setObjectIDs([$this->avatarFileID]);
+                            $fileList->loadThumbnails = true;
+                            $fileList->readObjects();
 
-                            $thumbnailList = new FileThumbnailList();
-                            $thumbnailList->getConditionBuilder()->add("fileID = ?", [$this->avatarFileID]);
-                            $thumbnailList->readObjects();
-                            foreach ($thumbnailList as $thumbnail) {
-                                $this->avatar->addThumbnail($thumbnail);
-                            }
+                            $this->avatar = $fileList->getSingleObject();
 
                             UserStorageHandler::getInstance()->update(
                                 $this->userID,
@@ -371,22 +367,6 @@ class UserProfile extends DatabaseObjectDecorator implements ITitledLinkObject
                         } else {
                             $this->avatar = \unserialize($data);
                         }
-                    } elseif ($this->avatarID) {
-                        if (!$this->fileHash) {
-                            $data = UserStorageHandler::getInstance()->getField('avatar', $this->userID);
-                            if ($data === null) {
-                                $this->avatar = new UserAvatar($this->avatarID);
-                                UserStorageHandler::getInstance()->update(
-                                    $this->userID,
-                                    'avatar',
-                                    \serialize($this->avatar)
-                                );
-                            } else {
-                                $this->avatar = \unserialize($data);
-                            }
-                        } else {
-                            $this->avatar = new UserAvatar(null, $this->getDecoratedObject()->data);
-                        }
                     } else {
                         $parameters = ['avatar' => null];
                         EventHandler::getInstance()->fireAction($this, 'getAvatar', $parameters);
index 869b66fbcd199465b5b061ce54443a7af298c821..61d30aa0fe7f21bde8abbc194aaf6bc49d9b9b9c 100644 (file)
@@ -2,8 +2,6 @@
 
 namespace wcf\data\user;
 
-use wcf\data\file\FileList;
-
 /**
  * Represents a list of user profiles.
  *
@@ -19,6 +17,8 @@ use wcf\data\file\FileList;
  */
 class UserProfileList extends UserList
 {
+    use TUserAvatarObjectList;
+
     /**
      * @inheritDoc
      */
@@ -29,8 +29,6 @@ class UserProfileList extends UserList
      */
     public $decoratorClassName = UserProfile::class;
 
-    public bool $loadAvatarFiles = true;
-
     /**
      * @inheritDoc
      */
@@ -62,33 +60,4 @@ class UserProfileList extends UserList
 
         $this->loadAvatarFiles();
     }
-
-    protected function loadAvatarFiles(): void
-    {
-        if (!$this->loadAvatarFiles) {
-            return;
-        }
-
-        $avatarFileIDs = [];
-        foreach ($this->objects as $user) {
-            if ($user->avatarFileID !== null) {
-                $avatarFileIDs[] = $user->avatarFileID;
-            }
-        }
-        if ($avatarFileIDs === []) {
-            return;
-        }
-
-        $fileList = new FileList();
-        $fileList->loadThumbnails = true;
-        $fileList->setObjectIDs($avatarFileIDs);
-        $fileList->readObjects();
-        $files = $fileList->getObjects();
-
-        foreach ($this->objects as $user) {
-            if ($user->avatarFileID !== null) {
-                $user->setFileAvatar($files[$user->avatarFileID]);
-            }
-        }
-    }
 }
index d98244c9959a0608fde050e3346fa14b6619904c..5f75ea3e3721849d449434f49448135185e6e239 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace wcf\data\user\follow;
 
+use wcf\data\user\TUserAvatarObjectList;
 use wcf\data\user\User;
 use wcf\data\user\UserProfile;
 
@@ -20,6 +21,8 @@ use wcf\data\user\UserProfile;
  */
 class UserFollowerList extends UserFollowList
 {
+    use TUserAvatarObjectList;
+
     /**
      * @inheritDoc
      */
@@ -48,12 +51,17 @@ class UserFollowerList extends UserFollowList
         parent::__construct();
 
         $this->sqlSelects .= "user_table.username, user_table.email, user_table.disableAvatar";
-        $this->sqlSelects .= ", user_avatar.*";
 
         $this->sqlJoins .= "
             LEFT JOIN   wcf1_user user_table
-            ON          user_table.userID = user_follow.userID
-            LEFT JOIN   wcf1_user_avatar user_avatar
-            ON          user_avatar.avatarID = user_table.avatarID";
+            ON          user_table.userID = user_follow.userID";
+    }
+
+    #[\Override]
+    public function readObjects()
+    {
+        parent::readObjects();
+
+        $this->loadAvatarFiles();
     }
 }
index 8d266ce42f0a2649da3c71471d7588f5d63e0ae3..04e5741b58c9a9d2968b2ce6004301dca206cf05 100644 (file)
@@ -25,15 +25,13 @@ class UserFollowingList extends UserFollowerList
     {
         UserFollowList::__construct();
 
-        $this->sqlSelects .= "user_avatar.*, user_follow.followID, user_option_value.*";
+        $this->sqlSelects .= "user_follow.followID, user_option_value.*";
 
         $this->sqlJoins .= "
             LEFT JOIN   wcf1_user user_table
             ON          user_table.userID = user_follow.followUserID
             LEFT JOIN   wcf1_user_option_value user_option_value
-            ON          user_option_value.userID = user_table.userID
-            LEFT JOIN   wcf1_user_avatar user_avatar
-            ON          user_avatar.avatarID = user_table.avatarID";
+            ON          user_option_value.userID = user_table.userID";
 
         $this->sqlSelects .= ", user_table.*";
     }
index 3a682cd71cdc818a8acd6445a678e19a578fea99..7a46f42dd6cbe22f2d8430d0db420ef582d12704 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace wcf\data\user\ignore;
 
+use wcf\data\user\TUserAvatarObjectList;
 use wcf\data\user\User;
 use wcf\data\user\UserProfile;
 
@@ -14,6 +15,8 @@ use wcf\data\user\UserProfile;
  */
 class ViewableUserIgnoreList extends UserIgnoreList
 {
+    use TUserAvatarObjectList;
+
     /**
      * @inheritDoc
      */
@@ -46,16 +49,21 @@ class ViewableUserIgnoreList extends UserIgnoreList
         }
         $this->sqlSelects .= "user_ignore.ignoreID";
         $this->sqlSelects .= ", user_option_value.*";
-        $this->sqlSelects .= ", user_avatar.*";
 
         $this->sqlJoins .= "
             LEFT JOIN   wcf1_user user_table
             ON          user_table.userID = user_ignore.ignoreUserID
             LEFT JOIN   wcf1_user_option_value user_option_value
-            ON          user_option_value.userID = user_table.userID
-            LEFT JOIN   wcf1_user_avatar user_avatar
-            ON          user_avatar.avatarID = user_table.avatarID";
+            ON          user_option_value.userID = user_table.userID";
 
         $this->sqlSelects .= ", user_table.*";
     }
+
+    #[\Override]
+    public function readObjects()
+    {
+        parent::readObjects();
+
+        $this->loadAvatarFiles();
+    }
 }
index 06b342d8737c49a22cee13848385890ebddefc76..8dfdccd6def33eeaf1158eb9d186f378aa606c28 100644 (file)
@@ -5,6 +5,7 @@ namespace wcf\data\user\online;
 use wcf\data\option\OptionAction;
 use wcf\data\session\SessionList;
 use wcf\data\user\group\UserGroup;
+use wcf\data\user\TUserAvatarObjectList;
 use wcf\data\user\User;
 use wcf\data\user\UserProfile;
 use wcf\system\event\EventHandler;
@@ -26,6 +27,8 @@ use wcf\util\StringUtil;
  */
 class UsersOnlineList extends SessionList
 {
+    use TUserAvatarObjectList;
+
     /**
      * @inheritDoc
      */
@@ -55,7 +58,7 @@ class UsersOnlineList extends SessionList
     {
         parent::__construct();
 
-        $this->sqlSelects .= "user_avatar.*, user_option_value.*, user_group.userOnlineMarking, user_table.*";
+        $this->sqlSelects .= "user_option_value.*, user_group.userOnlineMarking, user_table.*";
 
         $this->sqlConditionJoins .= "
             LEFT JOIN   wcf1_user user_table
@@ -65,8 +68,6 @@ class UsersOnlineList extends SessionList
             ON          user_table.userID = session.userID
             LEFT JOIN   wcf1_user_option_value user_option_value
             ON          user_option_value.userID = user_table.userID
-            LEFT JOIN   wcf1_user_avatar user_avatar
-            ON          user_avatar.avatarID = user_table.avatarID
             LEFT JOIN   wcf1_user_group user_group
             ON          user_group.groupID = user_table.userOnlineGroupID";
 
@@ -92,6 +93,8 @@ class UsersOnlineList extends SessionList
         }
         $this->objectIDs = $this->indexToObject;
         $this->rewind();
+
+        $this->loadAvatarFiles();
     }
 
     /**
index 39233b8bd9009c63375dc549947f66b1c1ad03e3..7e8995768841cf3749fb713d4ba21735864fee50 100644 (file)
@@ -3,6 +3,7 @@
 namespace wcf\data\user\profile\visitor;
 
 use wcf\data\DatabaseObjectList;
+use wcf\data\user\TUserAvatarObjectList;
 use wcf\data\user\User;
 use wcf\data\user\UserProfile;
 
@@ -21,6 +22,8 @@ use wcf\data\user\UserProfile;
  */
 class UserProfileVisitorList extends DatabaseObjectList
 {
+    use TUserAvatarObjectList;
+
     /**
      * @inheritDoc
      */
@@ -44,12 +47,17 @@ class UserProfileVisitorList extends DatabaseObjectList
         parent::__construct();
 
         $this->sqlSelects .= "user_table.username, user_table.email, user_table.disableAvatar";
-        $this->sqlSelects .= ", user_avatar.*";
 
         $this->sqlJoins .= "
             LEFT JOIN   wcf1_user user_table
-            ON          user_table.userID = user_profile_visitor.userID
-            LEFT JOIN   wcf1_user_avatar user_avatar
-            ON          user_avatar.avatarID = user_table.avatarID";
+            ON          user_table.userID = user_profile_visitor.userID";
+    }
+
+    #[\Override]
+    public function readObjects()
+    {
+        parent::readObjects();
+
+        $this->loadAvatarFiles();
     }
 }