From d64c5fcd05b528ac71c9e471afad30352cd12ac2 Mon Sep 17 00:00:00 2001
From: Torben Hansen <derhansen@gmail.com>
Date: Sat, 23 Mar 2024 11:03:12 +0100
Subject: [PATCH] [TASK] Move getUserWhoDeleted function to RecordHistory

In ext:recycler, there is an open todo to move the function
`getUserWhoDeleted` from `RecyclerAjaxController to `RecordHistory`.

This change moves the function to `RecordHistory` and additionally
renames it to a more appropriate name.

Resolves: #103474
Releases: main
Change-Id: Id49d2e42c6c2393ef6c0b888bf293cd835cca1ed
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/83578
Reviewed-by: Simon Schaufelberger <simonschaufi+typo3@gmail.com>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Simon Schaufelberger <simonschaufi+typo3@gmail.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: core-ci <typo3@b13.com>
---
 .../backend/Classes/History/RecordHistory.php | 31 +++++++++++++
 .../Controller/RecyclerAjaxController.php     | 45 +++----------------
 2 files changed, 37 insertions(+), 39 deletions(-)

diff --git a/typo3/sysext/backend/Classes/History/RecordHistory.php b/typo3/sysext/backend/Classes/History/RecordHistory.php
index 728e4d75047d..92bcc80cb088 100644
--- a/typo3/sysext/backend/Classes/History/RecordHistory.php
+++ b/typo3/sysext/backend/Classes/History/RecordHistory.php
@@ -262,6 +262,37 @@ class RecordHistory
         return $this->findEventsForRecord($table, $uid, ($this->maxSteps ?: 0), $lastHistoryEntry);
     }
 
+    /**
+     * Get the user uid of the user who deleted the record for the given table
+     */
+    public function getUserIdFromDeleteActionForRecord(string $table, int $uid): int
+    {
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_history');
+        $queryBuilder->select('userid')
+            ->from('sys_history')
+            ->where(
+                $queryBuilder->expr()->eq(
+                    'tablename',
+                    $queryBuilder->createNamedParameter($table)
+                ),
+                $queryBuilder->expr()->eq(
+                    'usertype',
+                    $queryBuilder->createNamedParameter('BE')
+                ),
+                $queryBuilder->expr()->eq(
+                    'recuid',
+                    $queryBuilder->createNamedParameter($uid, Connection::PARAM_INT)
+                ),
+                $queryBuilder->expr()->eq(
+                    'actiontype',
+                    $queryBuilder->createNamedParameter(RecordHistoryStore::ACTION_DELETE, Connection::PARAM_INT)
+                )
+            )
+            ->setMaxResults(1);
+
+        return (int)$queryBuilder->executeQuery()->fetchOne();
+    }
+
     /*******************************
      *
      * Various helper functions
diff --git a/typo3/sysext/recycler/Classes/Controller/RecyclerAjaxController.php b/typo3/sysext/recycler/Classes/Controller/RecyclerAjaxController.php
index 6ff557ffbc79..ab15707b0ee0 100644
--- a/typo3/sysext/recycler/Classes/Controller/RecyclerAjaxController.php
+++ b/typo3/sysext/recycler/Classes/Controller/RecyclerAjaxController.php
@@ -27,7 +27,6 @@ use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
 use TYPO3\CMS\Core\Database\Connection;
 use TYPO3\CMS\Core\Database\ConnectionPool;
-use TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore;
 use TYPO3\CMS\Core\Http\JsonResponse;
 use TYPO3\CMS\Core\Imaging\IconFactory;
 use TYPO3\CMS\Core\Imaging\IconSize;
@@ -55,7 +54,8 @@ class RecyclerAjaxController
         protected readonly BackendViewFactory $backendViewFactory,
         protected readonly FrontendInterface $runtimeCache,
         protected readonly IconFactory $iconFactory,
-        protected readonly ConnectionPool $connectionPool
+        protected readonly ConnectionPool $connectionPool,
+        protected readonly RecordHistory $recordHistory
     ) {}
 
     /**
@@ -164,7 +164,6 @@ class RecyclerAjaxController
         $groupedRecords = [];
         $lang = $this->getLanguageService();
 
-        $recordHistory = GeneralUtility::makeInstance(RecordHistory::class);
         foreach ($deletedRowsArray as $table => $rows) {
             $groupedRecords[$table]['information'] = [
                 'table' => $table,
@@ -172,10 +171,10 @@ class RecyclerAjaxController
             ];
             foreach ($rows as $row) {
                 $pageTitle = $this->getPageTitle((int)$row['pid']);
-                $ownerInformation = $recordHistory->getCreationInformationForRecord($table, $row);
+                $ownerInformation = $this->recordHistory->getCreationInformationForRecord($table, $row);
                 $ownerUid = (int)(is_array($ownerInformation) && $ownerInformation['actiontype'] === 'BE' ? $ownerInformation['userid'] : 0);
                 $backendUserName = $this->getBackendUserInformation($ownerUid);
-                $userIdWhoDeleted = $this->getUserWhoDeleted($table, (int)$row['uid']);
+                $deleteUserUid = $this->recordHistory->getUserIdFromDeleteActionForRecord($table, (int)$row['uid']);
 
                 $groupedRecords[$table]['records'][] = [
                     'uid' => $row['uid'],
@@ -188,8 +187,8 @@ class RecyclerAjaxController
                     'owner_uid' => $ownerUid,
                     'title' => BackendUtility::getRecordTitle($table, $row),
                     'path' => $this->getRecordPath((int)$row['pid']),
-                    'delete_user_uid' => $userIdWhoDeleted,
-                    'delete_user' => $this->getBackendUserInformation($userIdWhoDeleted),
+                    'delete_user_uid' => $deleteUserUid,
+                    'delete_user' => $this->getBackendUserInformation($deleteUserUid),
                     'isParentDeleted' => $table === 'pages' && $this->isParentPageDeleted((int)$row['pid']),
                 ];
             }
@@ -242,38 +241,6 @@ class RecyclerAjaxController
         return $username;
     }
 
-    /**
-     * Get the user uid of the user who deleted the record
-     * @todo: move this to RecordHistory class
-     */
-    protected function getUserWhoDeleted(string $table, int $uid): int
-    {
-        $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_history');
-        $queryBuilder->select('userid')
-            ->from('sys_history')
-            ->where(
-                $queryBuilder->expr()->eq(
-                    'tablename',
-                    $queryBuilder->createNamedParameter($table)
-                ),
-                $queryBuilder->expr()->eq(
-                    'usertype',
-                    $queryBuilder->createNamedParameter('BE')
-                ),
-                $queryBuilder->expr()->eq(
-                    'recuid',
-                    $queryBuilder->createNamedParameter($uid, Connection::PARAM_INT)
-                ),
-                $queryBuilder->expr()->eq(
-                    'actiontype',
-                    $queryBuilder->createNamedParameter(RecordHistoryStore::ACTION_DELETE, Connection::PARAM_INT)
-                )
-            )
-            ->setMaxResults(1);
-
-        return (int)$queryBuilder->executeQuery()->fetchOne();
-    }
-
     /**
      * Sets data in the session of the current backend user.
      *
-- 
GitLab