‪TYPO3CMS  11.5
BackendLogController.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Http\Message\ResponseInterface;
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
27 use TYPO3\CMS\Core\Page\PageRenderer;
34 
41 {
45 
46  public function ‪__construct(
50  ) {
51  $this->moduleTemplateFactory = ‪$moduleTemplateFactory;
52  $this->logEntryRepository = ‪$logEntryRepository;
53  $this->workspaceRepository = ‪$workspaceRepository;
54  }
55 
59  public function ‪initializeListAction()
60  {
61  if (!isset($this->settings['dateFormat'])) {
62  $this->settings['dateFormat'] = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?: 'd-m-Y';
63  }
64  if (!isset($this->settings['timeFormat'])) {
65  $this->settings['timeFormat'] = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'];
66  }
67  // Static format needed for date picker (flatpickr), see BackendController::generateJavascript() and #91606
68  $this->settings['dateTimeFormat'] = (‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['USdateFormat'] ? 'H:i m-d-Y' : 'H:i d-m-Y');
69  $constraintConfiguration = $this->arguments->getArgument('constraint')->getPropertyMappingConfiguration();
70  $constraintConfiguration->allowAllProperties();
71  $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
72  $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/GlobalEventHandler');
73  $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/DateTimePicker');
74  $pageRenderer->loadRequireJsModule('TYPO3/CMS/Belog/BackendLog');
75  }
76 
86  public function ‪listAction(‪Constraint $constraint = null, int $pageId = null, string $layout = 'Default', string $operation = ''): ResponseInterface
87  {
88  if ($operation === 'reset-filters') {
89  $constraint = new ‪Constraint();
90  } elseif ($constraint === null) {
91  $constraint = $this->‪getConstraintFromBeUserData();
92  }
93  $this->‪persistConstraintInBeUserData($constraint);
94  $constraint->setPageId($pageId);
96  $this->‪setStartAndEndTimeFromTimeSelector($constraint);
97  $this->‪forceWorkspaceSelectionIfInWorkspace($constraint);
98  $logEntries = $this->logEntryRepository->findByConstraint($constraint);
99  $groupedLogEntries = $this->‪groupLogEntriesDay($logEntries);
100  $this->view->assignMultiple([
101  'pageId' => $pageId,
102  'layout' => $layout,
103  'groupedLogEntries' => $groupedLogEntries,
104  'constraint' => $constraint,
105  'userGroups' => $this->‪createUserAndGroupListForSelectOptions(),
106  'workspaces' => $this->‪createWorkspaceListForSelectOptions(),
107  'pageDepths' => $this->‪createPageDepthOptions(),
108  'channels' => $this->logEntryRepository->getUsedChannels(),
109  'channel' => $constraint->getChannel(),
110  'levels' => $this->logEntryRepository->getUsedLevels(),
111  'level' => $constraint->getLevel(),
112  ]);
113 
114  if ($layout === 'Default') {
115  $moduleTemplate = $this->moduleTemplateFactory->create($this->request);
116  $moduleTemplate->setTitle(‪LocalizationUtility::translate('LLL:EXT:belog/Resources/Private/Language/locallang_mod.xlf:mlang_tabs_tab'));
117  $moduleTemplate->setContent($this->view->render());
118  return $this->‪htmlResponse($moduleTemplate->renderContent());
119  }
120  return $this->‪htmlResponse();
121  }
122 
129  public function ‪deleteMessageAction(int $errorUid): ResponseInterface
130  {
132  $logEntry = $this->logEntryRepository->findByUid($errorUid);
133  if (!$logEntry) {
134  $this->‪addFlashMessage(‪LocalizationUtility::translate('actions.delete.noRowFound', 'belog') ?? '', '', ‪AbstractMessage::WARNING);
135  return new ‪ForwardResponse('list');
136  }
137  $numberOfDeletedRows = $this->logEntryRepository->deleteByMessageDetails($logEntry);
138  $this->‪addFlashMessage(sprintf(‪LocalizationUtility::translate('actions.delete.message', 'belog') ?? '', $numberOfDeletedRows));
139  return new ‪ForwardResponse('list');
140  }
141 
147  protected function ‪getConstraintFromBeUserData()
148  {
149  $serializedConstraint = ‪$GLOBALS['BE_USER']->getModuleData(static::class);
150  $constraint = null;
151  if (is_string($serializedConstraint) && !empty($serializedConstraint)) {
152  $constraint = @unserialize($serializedConstraint, ['allowed_classes' => [Constraint::class, \DateTime::class]]);
153  }
154  return $constraint ?: GeneralUtility::makeInstance(Constraint::class);
155  }
156 
162  protected function ‪persistConstraintInBeUserData(‪Constraint $constraint)
163  {
164  ‪$GLOBALS['BE_USER']->pushModuleData(static::class, serialize($constraint));
165  }
166 
173  {
174  $reservedMemory = new \SplFixedArray(187500); // 3M
175  register_shutdown_function(function () use (&$reservedMemory): void {
176  $reservedMemory = null; // free the reserved memory
177  $error = error_get_last();
178  if (str_contains($error['message'] ?? '', 'Allowed memory size of')) {
179  $constraint = GeneralUtility::makeInstance(Constraint::class);
180  $this->‪persistConstraintInBeUserData($constraint);
181  }
182  });
183  }
184 
196  protected function ‪groupLogEntriesDay(‪QueryResultInterface $logEntries): array
197  {
198  $targetStructure = [];
200  foreach ($logEntries as $entry) {
201  $pid = -1;
202  // Create array if it is not defined yet
203  if (!is_array($targetStructure[$pid] ?? false)) {
204  $targetStructure[-1] = [];
205  }
206  // Get day timestamp of log entry and create sub array if needed
207  // @todo Replace deprecated strftime in php 8.1. Suppress warning in v11.
208  $timestampDay = strtotime(@strftime('%d.%m.%Y', $entry->getTstamp()) ?: '');
209  if (!is_array($targetStructure[$pid][$timestampDay] ?? false)) {
210  $targetStructure[$pid][$timestampDay] = [];
211  }
212  // Add row
213  $targetStructure[$pid][$timestampDay][] = $entry;
214  }
215  ksort($targetStructure);
216  return $targetStructure;
217  }
218 
227  {
228  $userGroupArray = [];
229  // Two meta entries: 'all' and 'self'
230  $userGroupArray[0] = ‪LocalizationUtility::translate('allUsers', 'Belog');
231  $userGroupArray[-1] = ‪LocalizationUtility::translate('self', 'Belog');
232  // List of groups, key is gr-'uid'
233  $groups = BackendUtility::getGroupNames();
234  foreach ($groups as $group) {
235  $userGroupArray['gr-' . $group['uid']] = ‪LocalizationUtility::translate('group', 'Belog') . ' ' . $group['title'];
236  }
237  // List of users, key is us-'uid'
238  $users = BackendUtility::getUserNames();
239  foreach ($users as $user) {
240  $userGroupArray['us-' . $user['uid']] = ‪LocalizationUtility::translate('user', 'Belog') . ' ' . $user['username'];
241  }
242  return $userGroupArray;
243  }
244 
251  {
252  if (!‪ExtensionManagementUtility::isLoaded('workspaces')) {
253  return [];
254  }
255  $workspaceArray = [];
256  // Two meta entries: 'all' and 'live'
257  $workspaceArray[-99] = ‪LocalizationUtility::translate('any', 'Belog');
258  $workspaceArray[0] = ‪LocalizationUtility::translate('live', 'Belog');
259  $workspaces = $this->workspaceRepository->findAll();
261  foreach ($workspaces as $workspace) {
262  $workspaceArray[$workspace->getUid()] = $workspace->getUid() . ': ' . $workspace->getTitle();
263  }
264  return $workspaceArray;
265  }
266 
275  {
276  if (!‪ExtensionManagementUtility::isLoaded('workspaces')) {
277  $this->view->assign('showWorkspaceSelector', false);
278  } elseif (‪$GLOBALS['BE_USER']->workspace !== 0) {
279  $constraint->‪setWorkspaceUid(‪$GLOBALS['BE_USER']->workspace);
280  $this->view->assign('showWorkspaceSelector', false);
281  } else {
282  $this->view->assign('showWorkspaceSelector', true);
283  }
284  }
285 
292  protected function ‪createPageDepthOptions()
293  {
294  $options = [
295  0 => ‪LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_0', 'lang'),
296  1 => ‪LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_1', 'lang'),
297  2 => ‪LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_2', 'lang'),
298  3 => ‪LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_3', 'lang'),
299  4 => ‪LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_4', 'lang'),
300  999 => ‪LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_infi', 'lang'),
301  ];
302  return $options;
303  }
304 
310  protected function ‪setStartAndEndTimeFromTimeSelector(‪Constraint $constraint)
311  {
312  $startTime = $constraint->‪getManualDateStart() ? $constraint->‪getManualDateStart()->getTimestamp() : 0;
313  $endTime = $constraint->‪getManualDateStop() ? $constraint->‪getManualDateStop()->getTimestamp() : 0;
314  if ($endTime <= $startTime) {
315  $endTime = ‪$GLOBALS['EXEC_TIME'];
316  }
317  $constraint->‪setStartTimestamp($startTime);
318  $constraint->‪setEndTimestamp($endTime);
319  }
320 }
‪TYPO3\CMS\Belog\Controller\BackendLogController\initializeListAction
‪initializeListAction()
Definition: BackendLogController.php:59
‪TYPO3\CMS\Core\Messaging\AbstractMessage
Definition: AbstractMessage.php:26
‪TYPO3\CMS\Belog\Domain\Model\Constraint\getManualDateStart
‪DateTime null getManualDateStart()
Definition: Constraint.php:279
‪TYPO3\CMS\Belog\Controller\BackendLogController\createUserAndGroupListForSelectOptions
‪array createUserAndGroupListForSelectOptions()
Definition: BackendLogController.php:226
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\htmlResponse
‪ResponseInterface htmlResponse(string $html=null)
Definition: ActionController.php:1067
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:33
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository
Definition: LogEntryRepository.php:41
‪TYPO3\CMS\Belog\Controller\BackendLogController\forceWorkspaceSelectionIfInWorkspace
‪forceWorkspaceSelectionIfInWorkspace(Constraint $constraint)
Definition: BackendLogController.php:274
‪TYPO3\CMS\Backend\Template\ModuleTemplateFactory
Definition: ModuleTemplateFactory.php:29
‪TYPO3\CMS\Belog\Domain\Model\Constraint\setEndTimestamp
‪setEndTimestamp($timestamp)
Definition: Constraint.php:209
‪TYPO3\CMS\Belog\Domain\Model\Constraint\setWorkspaceUid
‪setWorkspaceUid($workspace)
Definition: Constraint.php:137
‪TYPO3\CMS\Belog\Controller\BackendLogController
Definition: BackendLogController.php:41
‪TYPO3\CMS\Belog\Domain\Model\Constraint\setStartTimestamp
‪setStartTimestamp($timestamp)
Definition: Constraint.php:189
‪TYPO3\CMS\Belog\Controller
Definition: BackendLogController.php:16
‪TYPO3\CMS\Belog\Domain\Model\Workspace
Definition: Workspace.php:27
‪TYPO3\CMS\Extbase\Http\ForwardResponse
Definition: ForwardResponse.php:24
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\addFlashMessage
‪addFlashMessage($messageBody, $messageTitle='', $severity=AbstractMessage::OK, $storeInSession=true)
Definition: ActionController.php:828
‪TYPO3\CMS\Belog\Controller\BackendLogController\listAction
‪ResponseInterface listAction(Constraint $constraint=null, int $pageId=null, string $layout='Default', string $operation='')
Definition: BackendLogController.php:86
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:43
‪TYPO3\CMS\Core\Messaging\AbstractMessage\WARNING
‪const WARNING
Definition: AbstractMessage.php:30
‪TYPO3\CMS\Belog\Controller\BackendLogController\setStartAndEndTimeFromTimeSelector
‪setStartAndEndTimeFromTimeSelector(Constraint $constraint)
Definition: BackendLogController.php:310
‪TYPO3\CMS\Belog\Controller\BackendLogController\groupLogEntriesDay
‪array groupLogEntriesDay(QueryResultInterface $logEntries)
Definition: BackendLogController.php:196
‪TYPO3\CMS\Belog\Controller\BackendLogController\deleteMessageAction
‪deleteMessageAction(int $errorUid)
Definition: BackendLogController.php:129
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate(string $key, ?string $extensionName=null, array $arguments=null, string $languageKey=null, array $alternativeLanguageKeys=null)
Definition: LocalizationUtility.php:67
‪TYPO3\CMS\Belog\Controller\BackendLogController\__construct
‪__construct(ModuleTemplateFactory $moduleTemplateFactory, LogEntryRepository $logEntryRepository, WorkspaceRepository $workspaceRepository)
Definition: BackendLogController.php:46
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface
Definition: QueryResultInterface.php:22
‪TYPO3\CMS\Belog\Domain\Model\LogEntry
Definition: LogEntry.php:28
‪TYPO3\CMS\Belog\Controller\BackendLogController\$logEntryRepository
‪LogEntryRepository $logEntryRepository
Definition: BackendLogController.php:43
‪TYPO3\CMS\Belog\Controller\BackendLogController\createPageDepthOptions
‪array createPageDepthOptions()
Definition: BackendLogController.php:292
‪TYPO3\CMS\Belog\Controller\BackendLogController\persistConstraintInBeUserData
‪persistConstraintInBeUserData(Constraint $constraint)
Definition: BackendLogController.php:162
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Belog\Domain\Model\Constraint\getManualDateStop
‪DateTime null getManualDateStop()
Definition: Constraint.php:299
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController
Definition: ActionController.php:65
‪TYPO3\CMS\Belog\Controller\BackendLogController\createWorkspaceListForSelectOptions
‪array createWorkspaceListForSelectOptions()
Definition: BackendLogController.php:250
‪TYPO3\CMS\Belog\Controller\BackendLogController\$moduleTemplateFactory
‪ModuleTemplateFactory $moduleTemplateFactory
Definition: BackendLogController.php:42
‪TYPO3\CMS\Belog\Domain\Repository\WorkspaceRepository
Definition: WorkspaceRepository.php:29
‪TYPO3\CMS\Belog\Controller\BackendLogController\resetConstraintsOnMemoryExhaustionError
‪resetConstraintsOnMemoryExhaustionError()
Definition: BackendLogController.php:172
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Belog\Domain\Model\Constraint
Definition: Constraint.php:25
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\isLoaded
‪static bool isLoaded($key)
Definition: ExtensionManagementUtility.php:114
‪TYPO3\CMS\Belog\Controller\BackendLogController\$workspaceRepository
‪WorkspaceRepository $workspaceRepository
Definition: BackendLogController.php:44
‪TYPO3\CMS\Belog\Controller\BackendLogController\getConstraintFromBeUserData
‪Constraint getConstraintFromBeUserData()
Definition: BackendLogController.php:147