[TASK] Simplify and clean up error log system information
[Packages/TYPO3.CMS.git] / typo3 / sysext / belog / Classes / Controller / SystemInformationController.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Belog\Controller;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem;
19 use TYPO3\CMS\Backend\Routing\UriBuilder;
20 use TYPO3\CMS\Backend\Toolbar\Enumeration\InformationStatus;
21 use TYPO3\CMS\Core\Database\Connection;
22 use TYPO3\CMS\Core\Database\ConnectionPool;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
25
26 /**
27 * Count newest exceptions for the system information menu
28 */
29 class SystemInformationController
30 {
31 /**
32 * @var array
33 */
34 protected $backendUserConfiguration;
35
36 public function __construct(array $backendUserConfiguration = null)
37 {
38 $this->backendUserConfiguration = $backendUserConfiguration ?? $GLOBALS['BE_USER']->uc;
39 }
40
41 /**
42 * Modifies the SystemInformation array
43 *
44 * @param SystemInformationToolbarItem $systemInformationToolbarItem
45 */
46 public function appendMessage(SystemInformationToolbarItem $systemInformationToolbarItem)
47 {
48 // we can't use the extbase repository here as the required TypoScript may not be parsed yet
49 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_log');
50 $count = $queryBuilder->count('error')
51 ->from('sys_log')
52 ->where(
53 $queryBuilder->expr()->gte(
54 'tstamp',
55 $queryBuilder->createNamedParameter($this->fetchLastAccessTimestamp(), \PDO::PARAM_INT)
56 ),
57 $queryBuilder->expr()->in(
58 'error',
59 $queryBuilder->createNamedParameter([-1, 1, 2], Connection::PARAM_INT_ARRAY)
60 )
61 )
62 ->execute()
63 ->fetchColumn(0);
64
65 if ($count > 0) {
66 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
67 $systemInformationToolbarItem->addSystemMessage(
68 sprintf(
69 LocalizationUtility::translate('systemmessage.errorsInPeriod', 'belog'),
70 $count,
71 (string)$uriBuilder->buildUriFromRoute(
72 'system_BelogLog',
73 ['tx_belog_system_beloglog' => ['constraint' => ['action' => -1]]]
74 )
75 ),
76 InformationStatus::STATUS_ERROR,
77 $count,
78 'system_BelogLog',
79 http_build_query(['tx_belog_system_beloglog' => ['constraint' => ['action' => -1]]])
80 );
81 }
82 }
83
84 protected function fetchLastAccessTimestamp(): int
85 {
86 if (!isset($this->backendUserConfiguration['systeminformation'])) {
87 return 0;
88 }
89 $systemInformationUc = json_decode($this->backendUserConfiguration['systeminformation'], true);
90 if (!isset($systemInformationUc['system_BelogLog']['lastAccess'])) {
91 return 0;
92 }
93
94 return (int)$systemInformationUc['system_BelogLog']['lastAccess'];
95 }
96 }