Implement `FileRuntimeCache`, which also loads the thumbnails
authorCyperghost <olaf_schmitz_1@t-online.de>
Mon, 11 Nov 2024 08:23:36 +0000 (09:23 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Mon, 11 Nov 2024 08:23:36 +0000 (09:23 +0100)
wcfsetup/install/files/lib/acp/page/UserListPage.class.php
wcfsetup/install/files/lib/data/attachment/AttachmentList.class.php
wcfsetup/install/files/lib/data/file/FileEditor.class.php
wcfsetup/install/files/lib/data/user/TUserAvatarObjectList.class.php
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/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
wcfsetup/install/files/lib/system/cache/runtime/FileRuntimeCache.class.php [new file with mode: 0644]

index e4ea36d586131ccd29a4790581a5a31cc0b04160..e87312076d8c00c71e4b4e437bab3cf8e6480cb7 100755 (executable)
@@ -2,13 +2,13 @@
 
 namespace wcf\acp\page;
 
-use wcf\data\file\FileList;
 use wcf\data\user\group\UserGroup;
 use wcf\data\user\option\ViewableUserOption;
 use wcf\data\user\User;
 use wcf\data\user\UserProfile;
 use wcf\page\SortablePage;
 use wcf\system\cache\builder\UserOptionCacheBuilder;
+use wcf\system\cache\runtime\FileRuntimeCache;
 use wcf\system\clipboard\ClipboardHandler;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\event\EventHandler;
@@ -315,18 +315,10 @@ class UserListPage extends SortablePage
                 }
             }
 
-            $fileList = new FileList();
-            $fileList->loadThumbnails = true;
-            $fileList->setObjectIDs($avatarFileIDs);
-            $fileList->readObjects();
-            $avatarFiles = $fileList->getObjects();
+            FileRuntimeCache::getInstance()->cacheObjectIDs($avatarFileIDs);
 
             // get special columns
             foreach ($this->users as $user) {
-                if ($user->avatarFileID !== null) {
-                    $user->setFileAvatar($avatarFiles[$user->avatarFileID]);
-                }
-
                 foreach ($this->columns as $column) {
                     switch ($column) {
                         case 'email':
index f4b35143c15ca961e68eca90b2605b46217b437f..77636895d631ff3967663c43f727cab4d6c66d70 100644 (file)
@@ -3,8 +3,7 @@
 namespace wcf\data\attachment;
 
 use wcf\data\DatabaseObjectList;
-use wcf\data\file\FileList;
-use wcf\data\file\thumbnail\FileThumbnailList;
+use wcf\system\cache\runtime\FileRuntimeCache;
 
 /**
  * Represents a list of attachments.
@@ -51,20 +50,10 @@ class AttachmentList extends DatabaseObjectList
             return;
         }
 
-        $fileList = new FileList();
-        $fileList->getConditionBuilder()->add("fileID IN (?)", [$fileIDs]);
-        $fileList->readObjects();
-        $files = $fileList->getObjects();
-
-        $thumbnailList = new FileThumbnailList();
-        $thumbnailList->getConditionBuilder()->add("fileID IN (?)", [$fileIDs]);
-        $thumbnailList->readObjects();
-        foreach ($thumbnailList as $thumbnail) {
-            $files[$thumbnail->fileID]->addThumbnail($thumbnail);
-        }
+        FileRuntimeCache::getInstance()->cacheObjectIDs($fileIDs);
 
         foreach ($this->objects as $attachment) {
-            $file = $files[$attachment->fileID] ?? null;
+            $file = FileRuntimeCache::getInstance()->getObject($attachment->fileID) ?? null;
             if ($file !== null) {
                 $attachment->setFile($file);
             }
index 831893701814d00e456f4db24d28b37288a5c687..50564a6e8d82923d4f46da81d8e4be936fe42446 100644 (file)
@@ -5,7 +5,6 @@ namespace wcf\data\file;
 use wcf\data\DatabaseObjectEditor;
 use wcf\data\file\temporary\FileTemporary;
 use wcf\data\file\thumbnail\FileThumbnailEditor;
-use wcf\data\file\thumbnail\FileThumbnailList;
 use wcf\system\file\processor\FileProcessor;
 use wcf\system\image\ImageHandler;
 use wcf\util\ExifUtil;
@@ -41,6 +40,7 @@ class FileEditor extends DatabaseObjectEditor
     public static function deleteAll(array $objectIDs = [])
     {
         $fileList = new FileList();
+        $fileList->loadThumbnails = true;
         $fileList->getConditionBuilder()->add("fileID IN (?)", [$objectIDs]);
         $fileList->readObjects();
         $files = $fileList->getObjects();
@@ -48,13 +48,6 @@ class FileEditor extends DatabaseObjectEditor
             return 0;
         }
 
-        $thumbnailList = new FileThumbnailList();
-        $thumbnailList->getConditionBuilder()->add("fileID IN (?)", [$objectIDs]);
-        $thumbnailList->readObjects();
-        foreach ($thumbnailList as $thumbnail) {
-            $files[$thumbnail->fileID]->addThumbnail($thumbnail);
-        }
-
         foreach ($files as $file) {
             (new FileEditor($file))->deleteFiles();
         }
index c0655fd4ee86088ecbf7b4a29ba3992afa86bae5..238c4a6eaabb4c8ac2ce4c2fd42a0ae8b039890e 100644 (file)
@@ -3,7 +3,7 @@
 namespace wcf\data\user;
 
 use wcf\data\DatabaseObjectList;
-use wcf\data\file\FileList;
+use wcf\system\cache\runtime\FileRuntimeCache;
 
 /**
  * Contains methods to load avatar files for a list of `UserProfile`.
@@ -19,14 +19,8 @@ use wcf\data\file\FileList;
  */
 trait TUserAvatarObjectList
 {
-    public bool $loadAvatarFiles = true;
-
-    protected function loadAvatarFiles(): void
+    protected function cacheAvatarFiles(): void
     {
-        if (!$this->loadAvatarFiles) {
-            return;
-        }
-
         $avatarFileIDs = [];
         foreach ($this->objects as $user) {
             if ($user->avatarFileID !== null) {
@@ -37,16 +31,6 @@ trait TUserAvatarObjectList
             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]);
-            }
-        }
+        FileRuntimeCache::getInstance()->cacheObjectIDs($avatarFileIDs);
     }
 }
index 09483a8b201ffd650a858886a95b5193255cf4d3..a05fd141e74d1f18a6126173f8954af63660301b 100644 (file)
@@ -4,7 +4,6 @@ namespace wcf\data\user;
 
 use wcf\data\DatabaseObjectDecorator;
 use wcf\data\file\File;
-use wcf\data\file\FileList;
 use wcf\data\ITitledLinkObject;
 use wcf\data\trophy\Trophy;
 use wcf\data\trophy\TrophyCache;
@@ -21,6 +20,7 @@ use wcf\data\user\option\ViewableUserOption;
 use wcf\data\user\rank\UserRank;
 use wcf\system\cache\builder\UserGroupPermissionCacheBuilder;
 use wcf\system\cache\builder\UserRankCacheBuilder;
+use wcf\system\cache\runtime\FileRuntimeCache;
 use wcf\system\cache\runtime\UserProfileRuntimeCache;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\email\Mailbox;
@@ -352,12 +352,7 @@ class UserProfile extends DatabaseObjectDecorator implements ITitledLinkObject
                     if ($this->avatarFileID !== null) {
                         $data = UserStorageHandler::getInstance()->getField('avatar', $this->userID);
                         if ($data === null) {
-                            $fileList = new FileList();
-                            $fileList->setObjectIDs([$this->avatarFileID]);
-                            $fileList->loadThumbnails = true;
-                            $fileList->readObjects();
-
-                            $this->avatar = $fileList->getSingleObject();
+                            $this->avatar = FileRuntimeCache::getInstance()->getObject($this->avatarFileID);
 
                             UserStorageHandler::getInstance()->update(
                                 $this->userID,
index 61d30aa0fe7f21bde8abbc194aaf6bc49d9b9b9c..bb362da65ef4c3382048b8aa22edee419ac795b4 100644 (file)
@@ -58,6 +58,6 @@ class UserProfileList extends UserList
 
         parent::readObjects();
 
-        $this->loadAvatarFiles();
+        $this->cacheAvatarFiles();
     }
 }
index 5f75ea3e3721849d449434f49448135185e6e239..232ed2c32021a173b46526c0c247709c4ecad6b8 100644 (file)
@@ -62,6 +62,6 @@ class UserFollowerList extends UserFollowList
     {
         parent::readObjects();
 
-        $this->loadAvatarFiles();
+        $this->cacheAvatarFiles();
     }
 }
index 7a46f42dd6cbe22f2d8430d0db420ef582d12704..6a419c2c1713eef1400287d037d2ac0c1919bc92 100644 (file)
@@ -64,6 +64,6 @@ class ViewableUserIgnoreList extends UserIgnoreList
     {
         parent::readObjects();
 
-        $this->loadAvatarFiles();
+        $this->cacheAvatarFiles();
     }
 }
index 8dfdccd6def33eeaf1158eb9d186f378aa606c28..a8cfb3846221af2506914d8d33f5a3bb8ca1bbc5 100644 (file)
@@ -94,7 +94,7 @@ class UsersOnlineList extends SessionList
         $this->objectIDs = $this->indexToObject;
         $this->rewind();
 
-        $this->loadAvatarFiles();
+        $this->cacheAvatarFiles();
     }
 
     /**
index 7e8995768841cf3749fb713d4ba21735864fee50..1852452c4d191676de5e5080295f5fa6b21d805b 100644 (file)
@@ -58,6 +58,6 @@ class UserProfileVisitorList extends DatabaseObjectList
     {
         parent::readObjects();
 
-        $this->loadAvatarFiles();
+        $this->cacheAvatarFiles();
     }
 }
diff --git a/wcfsetup/install/files/lib/system/cache/runtime/FileRuntimeCache.class.php b/wcfsetup/install/files/lib/system/cache/runtime/FileRuntimeCache.class.php
new file mode 100644 (file)
index 0000000..d087130
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+
+namespace wcf\system\cache\runtime;
+
+use wcf\data\file\File;
+use wcf\data\file\FileList;
+
+/**
+ * Runtime cache implementation for files with thumbnails.
+ *
+ * @author      Olaf Braun
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since       6.2
+ *
+ * @method  File[]      getCachedObjects()
+ * @method  File|null   getObject($objectID)
+ * @method  File[]      getObjects(array $objectIDs)
+ */
+class FileRuntimeCache extends AbstractRuntimeCache
+{
+    /**
+     * @inheritDoc
+     */
+    protected $listClassName = FileList::class;
+
+    #[\Override]
+    protected function getObjectList()
+    {
+        $fileList = new FileList();
+        $fileList->loadThumbnails = true;
+
+        return $fileList;
+    }
+}