From 72222b4fb8c8561270d7882663dae865800224a3 Mon Sep 17 00:00:00 2001
From: Christian Kuhn <lolli@schwarzbu.ch>
Date: Sun, 26 Mar 2017 00:09:16 +0100
Subject: [PATCH] [TASK] Move MonitorUtility to compatiblity7
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Move the MonitorUtility to ext:compatibility7, implement the static call
as eofe hook and move the reports system status to compat7, too.

Change-Id: I152a726d9d47e9a1f38dc2416384551727bbcc88
Resolves: #80450
Releases: master
Reviewed-on: https://review.typo3.org/52164
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Frank Nägler <frank.naegler@typo3.org>
Tested-by: Frank Nägler <frank.naegler@typo3.org>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
---
 .../Classes/Report/Status/SystemStatus.php    | 96 +++++++++++++++++++
 .../Classes/Utility/MonitorUtility.php        | 14 ++-
 .../Migrations/Code/ClassAliasMap.php         |  1 +
 typo3/sysext/compatibility7/ext_localconf.php |  4 +
 typo3/sysext/compatibility7/ext_tables.php    |  2 +
 ...450-MonitorUtilityMovedToCompatibility.rst | 16 ++++
 .../frontend/Classes/Http/RequestHandler.php  |  3 -
 .../Classes/Report/Status/SystemStatus.php    | 51 +---------
 8 files changed, 133 insertions(+), 54 deletions(-)
 create mode 100644 typo3/sysext/compatibility7/Classes/Report/Status/SystemStatus.php
 rename typo3/sysext/{core => compatibility7}/Classes/Utility/MonitorUtility.php (83%)
 create mode 100644 typo3/sysext/core/Documentation/Changelog/master/Important-80450-MonitorUtilityMovedToCompatibility.rst

diff --git a/typo3/sysext/compatibility7/Classes/Report/Status/SystemStatus.php b/typo3/sysext/compatibility7/Classes/Report/Status/SystemStatus.php
new file mode 100644
index 000000000000..2278f20ffa36
--- /dev/null
+++ b/typo3/sysext/compatibility7/Classes/Report/Status/SystemStatus.php
@@ -0,0 +1,96 @@
+<?php
+namespace TYPO3\CMS\Compatibility7\Report\Status;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Registry;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Lang\LanguageService;
+use TYPO3\CMS\Reports\Status as ReportStatus;
+use TYPO3\CMS\Reports\StatusProviderInterface;
+
+/**
+ * Adds peak memory stats from frontend call.
+ */
+class SystemStatus implements StatusProviderInterface
+{
+    /**
+     * Main API method
+     *
+     * @return array List of statuses
+     */
+    public function getStatus()
+    {
+        $this->executeAdminCommand();
+        $statuses = [
+            'PhpPeakMemory' => $this->getPhpPeakMemoryStatus(),
+        ];
+        return $statuses;
+    }
+
+    /**
+     * Executes commands like clearing the memory status flag
+     *
+     * @return void
+     */
+    protected function executeAdminCommand()
+    {
+        $command = GeneralUtility::_GET('adminCmd');
+        switch ($command) {
+            case 'clear_peak_memory_usage_flag':
+                /** @var Registry $registry */
+                $registry = GeneralUtility::makeInstance(Registry::class);
+                $registry->remove('core', 'reports-peakMemoryUsage');
+                break;
+            default:
+                // Do nothing
+        }
+    }
+
+    /**
+     * Checks if there was a request in the past which approached the memory limit
+     *
+     * @return \TYPO3\CMS\Reports\Status A status of whether the memory limit was approached by one of the requests
+     */
+    protected function getPhpPeakMemoryStatus()
+    {
+        /** @var Registry $registry */
+        $registry = GeneralUtility::makeInstance(Registry::class);
+        $peakMemoryUsage = $registry->get('core', 'reports-peakMemoryUsage');
+        $memoryLimit = GeneralUtility::getBytesFromSizeMeasurement(ini_get('memory_limit'));
+        $value = $this->getLanguageService()->getLL('status_ok');
+        $message = '';
+        $severity = ReportStatus::OK;
+        $bytesUsed = $peakMemoryUsage['used'];
+        $percentageUsed = $memoryLimit ? number_format($bytesUsed / $memoryLimit * 100, 1) . '%' : '?';
+        $dateOfPeak = date($GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'], $peakMemoryUsage['tstamp']);
+        $urlOfPeak = '<a href="' . htmlspecialchars($peakMemoryUsage['url']) . '">' . htmlspecialchars($peakMemoryUsage['url']) . '</a>';
+        $clearFlagUrl = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL') . '&amp;adminCmd=clear_peak_memory_usage_flag';
+        if ($peakMemoryUsage['used']) {
+            $message = sprintf($this->getLanguageService()->getLL('status_phpPeakMemoryTooHigh'), GeneralUtility::formatSize($peakMemoryUsage['used']), $percentageUsed, GeneralUtility::formatSize($memoryLimit), $dateOfPeak, $urlOfPeak);
+            $message .= ' <a href="' . $clearFlagUrl . '">' . $this->getLanguageService()->getLL('status_phpPeakMemoryClearFlag') . '</a>.';
+            $severity = ReportStatus::WARNING;
+            $value = $percentageUsed;
+        }
+        return GeneralUtility::makeInstance(ReportStatus::class, $this->getLanguageService()->getLL('status_phpPeakMemory'), $value, $message, $severity);
+    }
+
+    /**
+     * @return LanguageService
+     */
+    protected function getLanguageService()
+    {
+        return $GLOBALS['LANG'];
+    }
+}
diff --git a/typo3/sysext/core/Classes/Utility/MonitorUtility.php b/typo3/sysext/compatibility7/Classes/Utility/MonitorUtility.php
similarity index 83%
rename from typo3/sysext/core/Classes/Utility/MonitorUtility.php
rename to typo3/sysext/compatibility7/Classes/Utility/MonitorUtility.php
index 2ee1b1e74e48..57965ebf43b7 100644
--- a/typo3/sysext/core/Classes/Utility/MonitorUtility.php
+++ b/typo3/sysext/compatibility7/Classes/Utility/MonitorUtility.php
@@ -1,5 +1,5 @@
 <?php
-namespace TYPO3\CMS\Core\Utility;
+namespace TYPO3\CMS\Compatibility7\Utility;
 
 /*
  * This file is part of the TYPO3 CMS project.
@@ -14,6 +14,8 @@ namespace TYPO3\CMS\Core\Utility;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
 /**
  * Class to handle monitoring actions.
  */
@@ -41,4 +43,14 @@ class MonitorUtility
             }
         }
     }
+
+    /**
+     * Hook called by TypoScriptFrontendController
+     *
+     * @return void
+     */
+    public function monitorUtilityFrontendHook()
+    {
+        self::peakMemoryUsage();
+    }
 }
diff --git a/typo3/sysext/compatibility7/Migrations/Code/ClassAliasMap.php b/typo3/sysext/compatibility7/Migrations/Code/ClassAliasMap.php
index c31620c31a31..410b5a3cc72a 100644
--- a/typo3/sysext/compatibility7/Migrations/Code/ClassAliasMap.php
+++ b/typo3/sysext/compatibility7/Migrations/Code/ClassAliasMap.php
@@ -1,5 +1,6 @@
 <?php
 return [
+    'TYPO3\\CMS\\Core\\Utility\\MonitorUtility' => \TYPO3\CMS\Compatibility7\Utility\MonitorUtility::class,
     'TYPO3\\CMS\\IndexedSearch\\Controller\\SearchFormController' => \TYPO3\CMS\Compatibility7\Controller\SearchFormController::class,
     'TYPO3\\CMS\\Version\\ContextMenu\\ItemProvider' => \TYPO3\CMS\Compatibility7\ContextMenu\ItemProvider::class,
     'TYPO3\\CMS\\Version\\Controller\\VersionModuleController' => \TYPO3\CMS\Compatibility7\Controller\VersionModuleController::class,
diff --git a/typo3/sysext/compatibility7/ext_localconf.php b/typo3/sysext/compatibility7/ext_localconf.php
index e2603a8b1b0e..78f1c96b2336 100644
--- a/typo3/sysext/compatibility7/ext_localconf.php
+++ b/typo3/sysext/compatibility7/ext_localconf.php
@@ -105,3 +105,7 @@ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['settingLa
 // Enable TypoScript functionality config.beLoginLinkIPList
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['hook_eofe']['compatibility7_backendloginlink']
     = \TYPO3\CMS\Compatibility7\Hooks\BackendLoginLinkHook::class . '->renderBackendLoginLink';
+
+// Call MonitorUtility peakMemoryUsage
+$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['hook_eofe']['compatibility7_monitorutility']
+    = \TYPO3\CMS\Compatibility7\Utility\MonitorUtility::class . '->monitorUtilityFrontendHook';
diff --git a/typo3/sysext/compatibility7/ext_tables.php b/typo3/sysext/compatibility7/ext_tables.php
index 5536a4dbb39c..d6404b1189f1 100644
--- a/typo3/sysext/compatibility7/ext_tables.php
+++ b/typo3/sysext/compatibility7/ext_tables.php
@@ -11,4 +11,6 @@ if (TYPO3_MODE === 'BE') {
         !\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces')) {
         $GLOBALS['TYPO3_CONF_VARS']['BE']['ContextMenu']['ItemProviders'][1486418676] = \TYPO3\CMS\Compatibility7\ContextMenu\ItemProvider::class;
     }
+
+    $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']['tx_reports']['status']['providers']['system'][] = \TYPO3\CMS\Compatibility7\Report\Status\SystemStatus::class;
 }
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Important-80450-MonitorUtilityMovedToCompatibility.rst b/typo3/sysext/core/Documentation/Changelog/master/Important-80450-MonitorUtilityMovedToCompatibility.rst
new file mode 100644
index 000000000000..6f792ce7140a
--- /dev/null
+++ b/typo3/sysext/core/Documentation/Changelog/master/Important-80450-MonitorUtilityMovedToCompatibility.rst
@@ -0,0 +1,16 @@
+.. include:: ../../Includes.txt
+
+======================================================
+Important: #80450 - MonitorUtilityMovedToCompatibility
+======================================================
+
+See :issue:`80450`
+
+Description
+===========
+
+The "peak memory measurement" in the frontend has been moved to extension compatiblity7. The functionality
+is semi useful and should live a happy life in an extension for people who may need it, but there is no need
+to have that within the core on each frontend call.
+
+.. index:: Backend, Frontend, PHP-API
\ No newline at end of file
diff --git a/typo3/sysext/frontend/Classes/Http/RequestHandler.php b/typo3/sysext/frontend/Classes/Http/RequestHandler.php
index ef30c09a7f05..59a53c52e270 100644
--- a/typo3/sysext/frontend/Classes/Http/RequestHandler.php
+++ b/typo3/sysext/frontend/Classes/Http/RequestHandler.php
@@ -21,7 +21,6 @@ use TYPO3\CMS\Core\Http\RequestHandlerInterface;
 use TYPO3\CMS\Core\TimeTracker\TimeTracker;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\MathUtility;
-use TYPO3\CMS\Core\Utility\MonitorUtility;
 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
 use TYPO3\CMS\Frontend\Page\PageGenerator;
 use TYPO3\CMS\Frontend\Utility\CompressionUtility;
@@ -255,8 +254,6 @@ class RequestHandler implements RequestHandlerInterface
         $this->controller->hook_eofe();
         // Finish timetracking
         $this->timeTracker->pull();
-        // Check memory usage
-        MonitorUtility::peakMemoryUsage();
 
         // Admin panel
         if ($this->controller->isBackendUserLoggedIn() && $GLOBALS['BE_USER'] instanceof FrontendBackendUserAuthentication) {
diff --git a/typo3/sysext/reports/Classes/Report/Status/SystemStatus.php b/typo3/sysext/reports/Classes/Report/Status/SystemStatus.php
index 9c5a1deb55ce..d95f11def0a5 100644
--- a/typo3/sysext/reports/Classes/Report/Status/SystemStatus.php
+++ b/typo3/sysext/reports/Classes/Report/Status/SystemStatus.php
@@ -13,7 +13,7 @@ namespace TYPO3\CMS\Reports\Report\Status;
  *
  * The TYPO3 project - inspiring people to share!
  */
-use TYPO3\CMS\Core\Registry;
+
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Lang\LanguageService;
 use TYPO3\CMS\Reports\Status as ReportStatus;
@@ -31,61 +31,12 @@ class SystemStatus implements StatusProviderInterface
      */
     public function getStatus()
     {
-        $this->executeAdminCommand();
         $statuses = [
-            'PhpPeakMemory' => $this->getPhpPeakMemoryStatus(),
             'PhpModules' => $this->getMissingPhpModulesOfExtensions()
         ];
         return $statuses;
     }
 
-    /**
-     * Executes commands like clearing the memory status flag
-     *
-     * @return void
-     */
-    protected function executeAdminCommand()
-    {
-        $command = GeneralUtility::_GET('adminCmd');
-        switch ($command) {
-            case 'clear_peak_memory_usage_flag':
-                /** @var Registry $registry */
-                $registry = GeneralUtility::makeInstance(Registry::class);
-                $registry->remove('core', 'reports-peakMemoryUsage');
-                break;
-            default:
-                // Do nothing
-        }
-    }
-
-    /**
-     * Checks if there was a request in the past which approached the memory limit
-     *
-     * @return \TYPO3\CMS\Reports\Status A status of whether the memory limit was approached by one of the requests
-     */
-    protected function getPhpPeakMemoryStatus()
-    {
-        /** @var Registry $registry */
-        $registry = GeneralUtility::makeInstance(Registry::class);
-        $peakMemoryUsage = $registry->get('core', 'reports-peakMemoryUsage');
-        $memoryLimit = GeneralUtility::getBytesFromSizeMeasurement(ini_get('memory_limit'));
-        $value = $this->getLanguageService()->getLL('status_ok');
-        $message = '';
-        $severity = ReportStatus::OK;
-        $bytesUsed = $peakMemoryUsage['used'];
-        $percentageUsed = $memoryLimit ? number_format($bytesUsed / $memoryLimit * 100, 1) . '%' : '?';
-        $dateOfPeak = date($GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'], $peakMemoryUsage['tstamp']);
-        $urlOfPeak = '<a href="' . htmlspecialchars($peakMemoryUsage['url']) . '">' . htmlspecialchars($peakMemoryUsage['url']) . '</a>';
-        $clearFlagUrl = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL') . '&amp;adminCmd=clear_peak_memory_usage_flag';
-        if ($peakMemoryUsage['used']) {
-            $message = sprintf($this->getLanguageService()->getLL('status_phpPeakMemoryTooHigh'), GeneralUtility::formatSize($peakMemoryUsage['used']), $percentageUsed, GeneralUtility::formatSize($memoryLimit), $dateOfPeak, $urlOfPeak);
-            $message .= ' <a href="' . $clearFlagUrl . '">' . $this->getLanguageService()->getLL('status_phpPeakMemoryClearFlag') . '</a>.';
-            $severity = ReportStatus::WARNING;
-            $value = $percentageUsed;
-        }
-        return GeneralUtility::makeInstance(ReportStatus::class, $this->getLanguageService()->getLL('status_phpPeakMemory'), $value, $message, $severity);
-    }
-
     /**
      * Reports whether extensions need additional PHP modules different from standard core requirements
      *
-- 
GitLab