From 5b9bf53b1d29edfffa3b728669c2d8ad48676113 Mon Sep 17 00:00:00 2001
From: Benni Mack <benni@typo3.org>
Date: Mon, 27 Nov 2017 18:14:37 +0100
Subject: [PATCH] [TASK] Deprecate DataHandler->newlog2()

The "shorthand" method "newlog2()" is deprecated in favor of starting
to integrate a better logging API.

Resolves: #83121
Releases: master
Change-Id: Ieff947e021efb9547fd11d1c527dd072d443d6d8
Reviewed-on: https://review.typo3.org/54796
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
---
 .../core/Classes/DataHandling/DataHandler.php | 14 ++++++--
 ...83121-LoggingMethodDataHandler-newlog2.rst | 33 +++++++++++++++++++
 .../Unit/DataHandling/DataHandlerTest.php     |  6 ++--
 .../Php/MethodCallMatcher.php                 |  7 ++++
 .../Classes/Hook/DataHandlerHook.php          | 24 +++++++++++---
 5 files changed, 74 insertions(+), 10 deletions(-)
 create mode 100644 typo3/sysext/core/Documentation/Changelog/master/Deprecation-83121-LoggingMethodDataHandler-newlog2.rst

diff --git a/typo3/sysext/core/Classes/DataHandling/DataHandler.php b/typo3/sysext/core/Classes/DataHandling/DataHandler.php
index 9b020d27dafc..cb00b027b4eb 100644
--- a/typo3/sysext/core/Classes/DataHandling/DataHandler.php
+++ b/typo3/sysext/core/Classes/DataHandling/DataHandler.php
@@ -1375,7 +1375,9 @@ class DataHandler implements LoggerAwareInterface
                     }
                 }
                 if (!empty($newRecord)) {
-                    $this->newlog2('Shadowing done on fields <i>' . implode(',', array_keys($newRecord)) . '</i> in placeholder record ' . $table . ':' . $liveRec['uid'] . ' (offline version UID=' . $id . ')', $table, $liveRec['uid'], $liveRec['pid']);
+                    if ($this->enableLogging) {
+                        $this->log($table, $liveRec['uid'], 0, 0, 0, 'Shadowing done on fields <i>' . implode(',', array_keys($newRecord)) . '</i> in placeholder record ' . $table . ':' . $liveRec['uid'] . ' (offline version UID=' . $id . ')', -1, [], $this->eventPid($table, $liveRec['uid'], $liveRec['pid']));
+                    }
                     $this->updateDB($table, $liveRec['uid'], $newRecord);
                 }
             }
@@ -4878,7 +4880,9 @@ class DataHandler implements LoggerAwareInterface
             // Fetch the live record
             $parentRecordLocalization = BackendUtility::getRecordLocalization($table, $id, $command['language'], 'AND pid<>-1');
             if (empty($parentRecordLocalization)) {
-                $this->newlog2('Localization for parent record ' . $table . ':' . $id . '" cannot be fetched', $table, $id, $parentRecord['pid']);
+                if ($this->enableLogging) {
+                    $this->log($table, $id, 0, 0, 0, 'Localization for parent record ' . $table . ':' . $id . '" cannot be fetched', -1, [], $this->eventPid($table, $id, $parentRecord['pid']));
+                }
                 return;
             }
             $parentRecord = $parentRecordLocalization[0];
@@ -5306,7 +5310,9 @@ class DataHandler implements LoggerAwareInterface
     {
         $uid = (int)$uid;
         if ($uid === 0) {
-            $this->newlog2('Deleting all pages starting from the root-page is disabled.', 'pages', 0, 0, 2);
+            if ($this->enableLogging) {
+                $this->log('pages', $uid, 0, 0, 2, 'Deleting all pages starting from the root-page is disabled.', -1, [], 0);
+            }
             return;
         }
         // Getting list of pages to delete:
@@ -8708,9 +8714,11 @@ class DataHandler implements LoggerAwareInterface
      * @param int $error Error code, see log()
      * @return int Log entry UID
      * @see log()
+     * @deprecated since TYPO3 v9 will be removed in TYPO3 v10.0, use DataHandler->log() directly instead.
      */
     public function newlog2($message, $table, $uid, $pid = null, $error = 0)
     {
+        trigger_error('DataHandler->newlog2() will be removed in TYPO3 v10.0, use the generic log() function instead.', E_USER_DEPRECATED);
         if (!$this->enableLogging) {
             return 0;
         }
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Deprecation-83121-LoggingMethodDataHandler-newlog2.rst b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-83121-LoggingMethodDataHandler-newlog2.rst
new file mode 100644
index 000000000000..4cc46cf69157
--- /dev/null
+++ b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-83121-LoggingMethodDataHandler-newlog2.rst
@@ -0,0 +1,33 @@
+.. include:: ../../Includes.txt
+
+===========================================================
+Deprecation: #83121 - Logging method DataHandler->newlog2()
+===========================================================
+
+See :issue:`83121`
+
+Description
+===========
+
+The PHP method :php:`DataHandler->newlog2()` within DataHandler, TYPO3's core persistence API,
+has been marked as deprecated.
+
+
+Impact
+======
+
+Calling this method in PHP will trigger a deprecation warning.
+
+
+Affected Installations
+======================
+
+Custom extensions calling DataHandler and using the method above directly in PHP.
+
+
+Migration
+=========
+
+Use DataHandlers' log() functionality or the TYPO3 Logging API for logging.
+
+.. index:: PHP-API, FullyScanned
\ No newline at end of file
diff --git a/typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php b/typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php
index ca0c84ab15d9..c7900bbb9acb 100644
--- a/typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php
+++ b/typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php
@@ -746,15 +746,15 @@ class DataHandlerTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
     {
         /** @var DataHandler|\PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface $dataHandlerMock */
         $dataHandlerMock = $this->getMockBuilder(DataHandler::class)
-            ->setMethods(['canDeletePage', 'newlog2'])
+            ->setMethods(['canDeletePage', 'log'])
             ->getMock();
         $dataHandlerMock
             ->expects($this->never())
             ->method('canDeletePage');
         $dataHandlerMock
             ->expects($this->once())
-            ->method('newlog2')
-            ->with('Deleting all pages starting from the root-page is disabled.', 'pages', 0, 0, 2);
+            ->method('log')
+            ->with('pages', 0, 0, 0, 2, 'Deleting all pages starting from the root-page is disabled.', -1, [], 0);
 
         $dataHandlerMock->deletePages(0);
     }
diff --git a/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallMatcher.php b/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallMatcher.php
index 1780c888f3a8..99ed4e29ffec 100644
--- a/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallMatcher.php
+++ b/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallMatcher.php
@@ -1402,4 +1402,11 @@ return [
             'Deprecation-82445-PageTranslationRelatedFunctionality.rst',
         ],
     ],
+    'TYPO3\CMS\Core\DataHandling\DataHandler->newlog2' => [
+        'numberOfMandatoryArguments' => 3,
+        'maximumNumberOfArguments' => 5,
+        'restFiles' => [
+            'Deprecation-83121-LoggingMethodDataHandler-newlog2.rst',
+        ],
+    ],
 ];
diff --git a/typo3/sysext/workspaces/Classes/Hook/DataHandlerHook.php b/typo3/sysext/workspaces/Classes/Hook/DataHandlerHook.php
index 4e751a8eccc4..bf1dc453e02a 100644
--- a/typo3/sysext/workspaces/Classes/Hook/DataHandlerHook.php
+++ b/typo3/sysext/workspaces/Classes/Hook/DataHandlerHook.php
@@ -666,7 +666,11 @@ class DataHandlerHook
                 $mail->send();
             }
             $emailRecipients = implode(',', $emailRecipients);
-            $dataHandler->newlog2('Notification email for stage change was sent to "' . $emailRecipients . '"', $table, $id);
+            if ($dataHandler->enableLogging) {
+                $propertyArray = $dataHandler->getRecordProperties($table, $id);
+                $pid = $propertyArray['pid'];
+                $dataHandler->log($table, $id, 0, 0, 0, 'Notification email for stage change was sent to "' . $emailRecipients . '"', -1, [], $dataHandler->eventPid($table, $id, $pid));
+            }
         }
     }
 
@@ -732,7 +736,12 @@ class DataHandlerHook
                         ],
                         ['uid' => (int)$id]
                     );
-                $dataHandler->newlog2('Stage for record was changed to ' . $stageId . '. Comment was: "' . substr($comment, 0, 100) . '"', $table, $id);
+
+                if ($dataHandler->enableLogging) {
+                    $propertyArray = $dataHandler->getRecordProperties($table, $id);
+                    $pid = $propertyArray['pid'];
+                    $dataHandler->log($table, $id, 0, 0, 0, 'Stage for record was changed to ' . $stageId . '. Comment was: "' . substr($comment, 0, 100) . '"', -1, [], $dataHandler->eventPid($table, $id, $pid));
+                }
                 // TEMPORARY, except 6-30 as action/detail number which is observed elsewhere!
                 $dataHandler->log($table, $id, 6, 0, 0, 'Stage raised...', 30, ['comment' => $comment, 'stage' => $stageId]);
                 if ((int)$stat['stagechg_notification'] > 0) {
@@ -999,7 +1008,10 @@ class DataHandlerHook
                 // Force delete
                 $dataHandler->deleteEl($table, $id, true);
             }
-            $dataHandler->newlog2(($swapIntoWS ? 'Swapping' : 'Publishing') . ' successful for table "' . $table . '" uid ' . $id . '=>' . $swapWith, $table, $id, $swapVersion['pid']);
+            if ($dataHandler->enableLogging) {
+                $dataHandler->log($table, $id, 0, 0, 0, ($swapIntoWS ? 'Swapping' : 'Publishing') . ' successful for table "' . $table . '" uid ' . $id . '=>' . $swapWith, -1, [], $dataHandler->eventPid($table, $id, $swapVersion['pid']));
+            }
+
             // Update reference index of the live record:
             $dataHandler->addRemapStackRefIndex($table, $id);
             // Set log entry for live record:
@@ -1033,7 +1045,11 @@ class DataHandlerHook
                 $this->notifyStageChange($wsAccess, $stageId, $table, $id, $comment, $dataHandler, $notificationAlternativeRecipients);
             }
             // Write to log with stageId -20
-            $dataHandler->newlog2('Stage for record was changed to ' . $stageId . '. Comment was: "' . substr($comment, 0, 100) . '"', $table, $id);
+            if ($dataHandler->enableLogging) {
+                $propArr = $dataHandler->getRecordProperties($table, $id);
+                $pid = $propArr['pid'];
+                $dataHandler->log($table, $id, 0, 0, 0, 'Stage for record was changed to ' . $stageId . '. Comment was: "' . substr($comment, 0, 100) . '"', -1, [], $dataHandler->eventPid($table, $id, $pid));
+            }
             $dataHandler->log($table, $id, 6, 0, 0, 'Published', 30, ['comment' => $comment, 'stage' => $stageId]);
 
             // Clear cache:
-- 
GitLab