‪TYPO3CMS  10.4
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 
30 
36 {
40  private const ‪TIMEFRAME_THISWEEK = 0;
41 
45  private const ‪TIMEFRAME_LASTWEEK = 1;
46 
50  private const ‪TIMEFRAME_LASTSEVENDAYS = 2;
51 
55  private const ‪TIMEFRAME_THISMONTH = 10;
56 
60  private const ‪TIMEFRAME_LASTMONTH = 11;
61 
65  private const ‪TIMEFRAME_LAST31DAYS = 12;
66 
70  private const ‪TIMEFRAME_CUSTOM = 30;
71 
75  protected ‪$logEntryRepository;
76 
81  {
82  $this->logEntryRepository = ‪$logEntryRepository;
83  }
84 
88  public function ‪initializeListAction()
89  {
90  if (!isset($this->settings['dateFormat'])) {
91  $this->settings['dateFormat'] = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?: 'd-m-Y';
92  }
93  if (!isset($this->settings['timeFormat'])) {
94  $this->settings['timeFormat'] = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'];
95  }
96  $constraintConfiguration = $this->arguments->getArgument('constraint')->getPropertyMappingConfiguration();
97  $constraintConfiguration->allowAllProperties();
98  GeneralUtility::makeInstance(PageRenderer::class)
99  ->loadRequireJsModule('TYPO3/CMS/Backend/GlobalEventHandler');
100  }
101 
109  public function ‪listAction(Constraint $constraint = null, int $pageId = null, string $layout = 'Default')
110  {
111  // Constraint object handling:
112  // If there is none from GET, try to get it from BE user data, else create new
113  if ($constraint === null) {
114  $constraint = $this->‪getConstraintFromBeUserData();
115  } else {
116  $this->‪persistConstraintInBeUserData($constraint);
117  }
118  $constraint->setPageId($pageId);
120  $this->‪setStartAndEndTimeFromTimeSelector($constraint);
121  $this->‪forceWorkspaceSelectionIfInWorkspace($constraint);
122  $logEntries = $this->logEntryRepository->findByConstraint($constraint);
123  $groupedLogEntries = $this->‪groupLogEntriesByPageAndDay($logEntries, $constraint->getGroupByPage());
124  $this->view->assignMultiple([
125  'pageId' => $pageId,
126  'layout' => $layout,
127  'groupedLogEntries' => $groupedLogEntries,
128  'constraint' => $constraint,
129  'userGroups' => $this->‪createUserAndGroupListForSelectOptions(),
130  'workspaces' => $this->‪createWorkspaceListForSelectOptions(),
131  'pageDepths' => $this->‪createPageDepthOptions(),
132  ]);
133  }
134 
141  public function ‪deleteMessageAction(int $errorUid)
142  {
144  $logEntry = $this->logEntryRepository->findByUid($errorUid);
145  if (!$logEntry) {
146  $this->‪addFlashMessage(‪LocalizationUtility::translate('actions.delete.noRowFound', 'belog') ?? '', '', ‪AbstractMessage::WARNING);
147  $this->‪redirect('list');
148  }
149  $numberOfDeletedRows = $this->logEntryRepository->deleteByMessageDetails($logEntry);
150  $this->‪addFlashMessage(sprintf(‪LocalizationUtility::translate('actions.delete.message', 'belog') ?? '', $numberOfDeletedRows));
151  $this->‪redirect('list');
152  }
153 
159  protected function ‪getConstraintFromBeUserData()
160  {
161  $serializedConstraint = ‪$GLOBALS['BE_USER']->getModuleData(static::class);
162  $constraint = null;
163  if (is_string($serializedConstraint) && !empty($serializedConstraint)) {
164  $constraint = @unserialize($serializedConstraint, ['allowed_classes' => [Constraint::class, \DateTime::class]]);
165  }
166  return $constraint ?: $this->objectManager->get(Constraint::class);
167  }
168 
174  protected function ‪persistConstraintInBeUserData(Constraint $constraint)
175  {
176  ‪$GLOBALS['BE_USER']->pushModuleData(static::class, serialize($constraint));
177  }
178 
184  protected function ‪resetConstraintsOnMemoryExhaustionError()
185  {
186  $reservedMemory = new \SplFixedArray(187500); // 3M
187  register_shutdown_function(function () use (&$reservedMemory): void {
188  $reservedMemory = null; // free the reserved memory
189  $error = error_get_last();
190  if (strpos($error['message'], 'Allowed memory size of') !== false) {
191  $constraint = $this->objectManager->get(Constraint::class);
192  $this->‪persistConstraintInBeUserData($constraint);
193  }
194  });
195  }
196 
211  protected function ‪groupLogEntriesByPageAndDay(QueryResultInterface $logEntries, $groupByPage = false)
212  {
213  $targetStructure = [];
215  foreach ($logEntries as $entry) {
216  // Create page split list or flat list
217  if ($groupByPage) {
218  $pid = $entry->getEventPid();
219  } else {
220  $pid = -1;
221  }
222  // Create array if it is not defined yet
223  if (!is_array($targetStructure[$pid])) {
224  $targetStructure[$pid] = [];
225  }
226  // Get day timestamp of log entry and create sub array if needed
227  $timestampDay = strtotime(strftime('%d.%m.%Y', $entry->getTstamp()));
228  if (!is_array($targetStructure[$pid][$timestampDay])) {
229  $targetStructure[$pid][$timestampDay] = [];
230  }
231  // Add row
232  $targetStructure[$pid][$timestampDay][] = $entry;
233  }
234  ksort($targetStructure);
235  return $targetStructure;
236  }
237 
245  protected function ‪createUserAndGroupListForSelectOptions()
246  {
247  $userGroupArray = [];
248  // Two meta entries: 'all' and 'self'
249  $userGroupArray[0] = ‪LocalizationUtility::translate('allUsers', 'Belog');
250  $userGroupArray[-1] = ‪LocalizationUtility::translate('self', 'Belog');
251  // List of groups, key is gr-'uid'
253  foreach ($groups as $group) {
254  $userGroupArray['gr-' . $group['uid']] = ‪LocalizationUtility::translate('group', 'Belog') . ' ' . $group['title'];
255  }
256  // List of users, key is us-'uid'
258  foreach ($users as $user) {
259  $userGroupArray['us-' . $user['uid']] = ‪LocalizationUtility::translate('user', 'Belog') . ' ' . $user['username'];
260  }
261  return $userGroupArray;
262  }
263 
269  protected function ‪createWorkspaceListForSelectOptions()
270  {
271  if (!‪ExtensionManagementUtility::isLoaded('workspaces')) {
272  return [];
273  }
274  $workspaceArray = [];
275  // Two meta entries: 'all' and 'live'
276  $workspaceArray[-99] = ‪LocalizationUtility::translate('any', 'Belog');
277  $workspaceArray[0] = ‪LocalizationUtility::translate('live', 'Belog');
278  $workspaces = $this->objectManager->get(WorkspaceRepository::class)->findAll();
280  foreach ($workspaces as $workspace) {
281  $workspaceArray[$workspace->getUid()] = $workspace->getUid() . ': ' . $workspace->getTitle();
282  }
283  return $workspaceArray;
284  }
285 
293  protected function ‪forceWorkspaceSelectionIfInWorkspace(Constraint $constraint)
294  {
295  if (!‪ExtensionManagementUtility::isLoaded('workspaces')) {
296  $this->view->assign('showWorkspaceSelector', false);
297  } elseif (‪$GLOBALS['BE_USER']->workspace !== 0) {
298  $constraint->setWorkspaceUid(‪$GLOBALS['BE_USER']->workspace);
299  $this->view->assign('showWorkspaceSelector', false);
300  } else {
301  $this->view->assign('showWorkspaceSelector', true);
302  }
303  }
304 
311  protected function ‪createPageDepthOptions()
312  {
313  $options = [
314  0 => ‪LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_0', 'lang'),
315  1 => ‪LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_1', 'lang'),
316  2 => ‪LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_2', 'lang'),
317  3 => ‪LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_3', 'lang'),
318  4 => ‪LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_4', 'lang'),
319  999 => ‪LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_infi', 'lang')
320  ];
321  return $options;
322  }
323 
329  protected function ‪setStartAndEndTimeFromTimeSelector(Constraint $constraint)
330  {
331  $startTime = 0;
332  $endTime = ‪$GLOBALS['EXEC_TIME'];
333  // @TODO: Refactor this construct
334  switch ($constraint->getTimeFrame()) {
336  // This week
337  $week = (date('w') ?: 7) - 1;
338  $startTime = mktime(0, 0, 0) - $week * 3600 * 24;
339  break;
341  // Last week
342  $week = (date('w') ?: 7) - 1;
343  $startTime = mktime(0, 0, 0) - ($week + 7) * 3600 * 24;
344  $endTime = mktime(0, 0, 0) - $week * 3600 * 24;
345  break;
347  // Last 7 days
348  $startTime = mktime(0, 0, 0) - 7 * 3600 * 24;
349  break;
351  // This month
352  $startTime = mktime(0, 0, 0, (int)date('m'), 1);
353  break;
355  // Last month
356  $startTime = mktime(0, 0, 0, (int)date('m') - 1, 1);
357  $endTime = mktime(0, 0, 0, (int)date('m'), 1);
358  break;
360  // Last 31 days
361  $startTime = mktime(0, 0, 0) - 31 * 3600 * 24;
362  break;
364  $startTime = $constraint->getManualDateStart() ? $constraint->getManualDateStart()->getTimestamp() : 0;
365  $endTime = $constraint->getManualDateStop() ? $constraint->getManualDateStop()->getTimestamp() : 0;
366  if ($endTime <= $startTime) {
367  $endTime = ‪$GLOBALS['EXEC_TIME'];
368  }
369  break;
370  default:
371  }
372  $constraint->setStartTimestamp($startTime);
373  $constraint->setEndTimestamp($endTime);
374  }
375 }
‪TYPO3\CMS\Belog\Controller\BackendLogController\initializeListAction
‪initializeListAction()
Definition: BackendLogController.php:87
‪TYPO3\CMS\Core\Messaging\AbstractMessage
Definition: AbstractMessage.php:26
‪TYPO3\CMS\Belog\Domain\Model\Constraint\getManualDateStart
‪DateTime null getManualDateStart()
Definition: Constraint.php:309
‪TYPO3\CMS\Belog\Controller\BackendLogController\createUserAndGroupListForSelectOptions
‪array createUserAndGroupListForSelectOptions()
Definition: BackendLogController.php:244
‪TYPO3\CMS\Belog\Controller\BackendLogController\TIMEFRAME_LASTSEVENDAYS
‪const TIMEFRAME_LASTSEVENDAYS
Definition: BackendLogController.php:50
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:33
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository
Definition: LogEntryRepository.php:35
‪TYPO3\CMS\Belog\Controller\BackendLogController\forceWorkspaceSelectionIfInWorkspace
‪forceWorkspaceSelectionIfInWorkspace(Constraint $constraint)
Definition: BackendLogController.php:292
‪TYPO3\CMS\Belog\Domain\Model\Constraint\setEndTimestamp
‪setEndTimestamp($timestamp)
Definition: Constraint.php:239
‪TYPO3\CMS\Belog\Controller\BackendLogController\TIMEFRAME_LASTMONTH
‪const TIMEFRAME_LASTMONTH
Definition: BackendLogController.php:60
‪TYPO3\CMS\Belog\Domain\Model\Constraint\setWorkspaceUid
‪setWorkspaceUid($workspace)
Definition: Constraint.php:139
‪TYPO3\CMS\Belog\Controller\BackendLogController
Definition: BackendLogController.php:36
‪TYPO3\CMS\Belog\Domain\Model\Constraint\setStartTimestamp
‪setStartTimestamp($timestamp)
Definition: Constraint.php:219
‪TYPO3\CMS\Belog\Controller
Definition: BackendLogController.php:16
‪TYPO3\CMS\Belog\Controller\BackendLogController\TIMEFRAME_CUSTOM
‪const TIMEFRAME_CUSTOM
Definition: BackendLogController.php:70
‪TYPO3\CMS\Belog\Controller\BackendLogController\injectLogEntryRepository
‪injectLogEntryRepository(LogEntryRepository $logEntryRepository)
Definition: BackendLogController.php:79
‪TYPO3\CMS\Backend\Utility\BackendUtility\getUserNames
‪static array getUserNames($fields='username, usergroup, usergroup_cached_list, uid', $where='')
Definition: BackendUtility.php:818
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\addFlashMessage
‪addFlashMessage($messageBody, $messageTitle='', $severity=AbstractMessage::OK, $storeInSession=true)
Definition: ActionController.php:747
‪TYPO3\CMS\Belog\Controller\BackendLogController\groupLogEntriesByPageAndDay
‪array groupLogEntriesByPageAndDay(QueryResultInterface $logEntries, $groupByPage=false)
Definition: BackendLogController.php:210
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:43
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\redirect
‪redirect($actionName, $controllerName=null, $extensionName=null, array $arguments=null, $pageUid=null, $delay=0, $statusCode=303)
Definition: ActionController.php:852
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:42
‪TYPO3\CMS\Core\Messaging\AbstractMessage\WARNING
‪const WARNING
Definition: AbstractMessage.php:30
‪TYPO3\CMS\Belog\Controller\BackendLogController\setStartAndEndTimeFromTimeSelector
‪setStartAndEndTimeFromTimeSelector(Constraint $constraint)
Definition: BackendLogController.php:328
‪TYPO3\CMS\Belog\Controller\BackendLogController\TIMEFRAME_THISWEEK
‪const TIMEFRAME_THISWEEK
Definition: BackendLogController.php:40
‪TYPO3\CMS\Belog\Controller\BackendLogController\deleteMessageAction
‪deleteMessageAction(int $errorUid)
Definition: BackendLogController.php:140
‪TYPO3\CMS\Backend\Utility\BackendUtility\getGroupNames
‪static array getGroupNames($fields='title, uid', $where='')
Definition: BackendUtility.php:836
‪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\Domain\Model\Constraint\getTimeFrame
‪int getTimeFrame()
Definition: Constraint.php:169
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface
Definition: QueryResultInterface.php:22
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪TYPO3\CMS\Belog\Domain\Model\LogEntry
Definition: LogEntry.php:28
‪TYPO3\CMS\Belog\Controller\BackendLogController\TIMEFRAME_THISMONTH
‪const TIMEFRAME_THISMONTH
Definition: BackendLogController.php:55
‪TYPO3\CMS\Belog\Controller\BackendLogController\TIMEFRAME_LASTWEEK
‪const TIMEFRAME_LASTWEEK
Definition: BackendLogController.php:45
‪TYPO3\CMS\Belog\Controller\BackendLogController\createPageDepthOptions
‪array createPageDepthOptions()
Definition: BackendLogController.php:310
‪TYPO3\CMS\Belog\Controller\BackendLogController\persistConstraintInBeUserData
‪persistConstraintInBeUserData(Constraint $constraint)
Definition: BackendLogController.php:173
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Belog\Domain\Model\Constraint\getManualDateStop
‪DateTime null getManualDateStop()
Definition: Constraint.php:329
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController
Definition: ActionController.php:55
‪TYPO3\CMS\Belog\Controller\BackendLogController\createWorkspaceListForSelectOptions
‪array createWorkspaceListForSelectOptions()
Definition: BackendLogController.php:268
‪TYPO3\CMS\Belog\Controller\BackendLogController\listAction
‪listAction(Constraint $constraint=null, int $pageId=null, string $layout='Default')
Definition: BackendLogController.php:108
‪TYPO3\CMS\Belog\Domain\Repository\WorkspaceRepository
Definition: WorkspaceRepository.php:27
‪TYPO3\CMS\Belog\Controller\BackendLogController\resetConstraintsOnMemoryExhaustionError
‪resetConstraintsOnMemoryExhaustionError()
Definition: BackendLogController.php:183
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Belog\Domain\Model\Constraint
Definition: Constraint.php:23
‪TYPO3\CMS\Belog\Controller\BackendLogController\$logEntryRepository
‪TYPO3 CMS Belog Domain Repository LogEntryRepository $logEntryRepository
Definition: BackendLogController.php:74
‪TYPO3\CMS\Belog\Controller\BackendLogController\TIMEFRAME_LAST31DAYS
‪const TIMEFRAME_LAST31DAYS
Definition: BackendLogController.php:65
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\isLoaded
‪static bool isLoaded($key)
Definition: ExtensionManagementUtility.php:114
‪TYPO3\CMS\Belog\Controller\BackendLogController\getConstraintFromBeUserData
‪Constraint getConstraintFromBeUserData()
Definition: BackendLogController.php:158