‪TYPO3CMS  9.5
LogEntryRepository.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
20 
26 {
32  protected ‪$beUserList = [];
33 
37  public function ‪initializeObject()
38  {
39  $this->beUserList = $this->‪getBackendUsers();
41  ‪$defaultQuerySettings = $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Persistence\Generic\QuerySettingsInterface::class);
42  ‪$defaultQuerySettings->setRespectStoragePage(false);
44  }
45 
52  public function ‪findByConstraint(\‪TYPO3\CMS\Belog\Domain\Model\Constraint $constraint)
53  {
54  $query = $this->‪createQuery();
55  $queryConstraints = $this->‪createQueryConstraints($query, $constraint);
56  if (!empty($queryConstraints)) {
57  $query->matching($query->logicalAnd($queryConstraints));
58  }
59  $query->setOrderings(['uid' => \‪TYPO3\CMS\‪Extbase\Persistence\QueryInterface::ORDER_DESCENDING]);
60  $query->setLimit($constraint->getNumber());
61  return $query->execute();
62  }
63 
71  protected function ‪createQueryConstraints(\‪TYPO3\CMS\‪Extbase\Persistence\QueryInterface $query, \‪TYPO3\CMS\Belog\Domain\Model\Constraint $constraint)
72  {
73  $queryConstraints = [];
74  // User / group handling
75  $this->‪addUsersAndGroupsToQueryConstraints($constraint, $query, $queryConstraints);
76  // Workspace
77  if ($constraint->getWorkspaceUid() != \‪TYPO3\CMS\Belog\Domain\Model\Workspace::UID_ANY_WORKSPACE) {
78  $queryConstraints[] = $query->equals('workspace', $constraint->getWorkspaceUid());
79  }
80  // Action (type):
81  if ($constraint->getAction() > 0) {
82  $queryConstraints[] = $query->equals('type', $constraint->getAction());
83  } elseif ($constraint->getAction() == -1) {
84  $queryConstraints[] = $query->equals('type', 5);
85  }
86  // Start / endtime handling: The timestamp calculation was already done
87  // in the controller, since we need those calculated values in the view as well.
88  $queryConstraints[] = $query->greaterThanOrEqual('tstamp', $constraint->getStartTimestamp());
89  $queryConstraints[] = $query->lessThan('tstamp', $constraint->getEndTimestamp());
90  // Page and level constraint if in page context
91  $this->‪addPageTreeConstraintsToQuery($constraint, $query, $queryConstraints);
92  return $queryConstraints;
93  }
94 
103  protected function ‪addPageTreeConstraintsToQuery(\‪TYPO3\CMS\Belog\Domain\Model\Constraint $constraint, \‪TYPO3\CMS\‪Extbase\Persistence\QueryInterface $query, array &$queryConstraints)
104  {
105  $pageIds = [];
106  // Check if we should get a whole tree of pages and not only a single page
107  if ($constraint->getDepth() > 0) {
109  $pageTree = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\‪TYPO3\CMS\Backend\Tree\View\PageTreeView::class);
110  $pageTree->init('AND ' . ‪$GLOBALS['BE_USER']->getPagePermsClause(‪Permission::PAGE_SHOW));
111  $pageTree->makeHTML = 0;
112  $pageTree->fieldArray = ['uid'];
113  $pageTree->getTree($constraint->getPageId(), $constraint->getDepth());
114  $pageIds = $pageTree->ids;
115  }
116  if (!empty($constraint->getPageId())) {
117  $pageIds[] = $constraint->getPageId();
118  }
119  if (!empty($pageIds)) {
120  $queryConstraints[] = $query->in('eventPid', $pageIds);
121  }
122  }
123 
131  protected function ‪addUsersAndGroupsToQueryConstraints(\‪TYPO3\CMS\Belog\Domain\Model\Constraint $constraint, \‪TYPO3\CMS\‪Extbase\Persistence\QueryInterface $query, array &$queryConstraints)
132  {
133  $userOrGroup = $constraint->getUserOrGroup();
134  if ($userOrGroup === '') {
135  return;
136  }
137  // Constraint for a group
138  if (strpos($userOrGroup, 'gr-') === 0) {
139  $groupId = (int)substr($userOrGroup, 3);
140  $userIds = [];
141  foreach ($this->beUserList as $userId => $userData) {
142  if (\‪TYPO3\CMS\Core\Utility\GeneralUtility::inList($userData['usergroup_cached_list'], $groupId)) {
143  $userIds[] = $userId;
144  }
145  }
146  if (!empty($userIds)) {
147  $queryConstraints[] = $query->in('userid', $userIds);
148  } else {
149  // If there are no group members -> use -1 as constraint to not find anything
150  $queryConstraints[] = $query->in('userid', [-1]);
151  }
152  } elseif (strpos($userOrGroup, 'us-') === 0) {
153  $queryConstraints[] = $query->equals('userid', (int)substr($userOrGroup, 3));
154  } elseif ($userOrGroup === '-1') {
155  $queryConstraints[] = $query->equals('userid', (int)‪$GLOBALS['BE_USER']->user['uid']);
156  }
157  }
158 
165  public function ‪deleteByMessageDetails(‪LogEntry $logEntry): int
166  {
167  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
168  ->getQueryBuilderForTable('sys_log');
169  $constraints = [];
170  $constraints[] = $queryBuilder->expr()->eq('details', $queryBuilder->createNamedParameter($logEntry->‪getDetails()));
171  // If the detailsNo is 11 or 12 we got messages that are heavily using placeholders. In this case
172  // we need to compare both the message and the actual log data to not remove too many log entries.
173  if (GeneralUtility::inList('11,12', $logEntry->‪getDetailsNumber())) {
174  $constraints[] = $queryBuilder->expr()->eq('log_data', $queryBuilder->createNamedParameter($logEntry->‪getLogData()));
175  }
176  return $queryBuilder->delete('sys_log')
177  ->where(...$constraints)
178  ->execute();
179  }
180 
186  protected function ‪getBackendUsers()
187  {
188  return \TYPO3\CMS\Backend\Utility\BackendUtility::getUserNames();
189  }
190 }
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository\addPageTreeConstraintsToQuery
‪addPageTreeConstraintsToQuery(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint, \TYPO3\CMS\Extbase\Persistence\QueryInterface $query, array &$queryConstraints)
Definition: LogEntryRepository.php:102
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository
Definition: LogEntryRepository.php:26
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository\initializeObject
‪initializeObject()
Definition: LogEntryRepository.php:36
‪TYPO3
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository\$beUserList
‪array $beUserList
Definition: LogEntryRepository.php:31
‪TYPO3\CMS\Core\Type\Bitmask\Permission
Definition: Permission.php:23
‪TYPO3\CMS\Belog\Domain\Model\LogEntry\getLogData
‪array getLogData()
Definition: LogEntry.php:402
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository\createQueryConstraints
‪array<\TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface > createQueryConstraints(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query, \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint)
Definition: LogEntryRepository.php:70
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository\findByConstraint
‪TYPO3 CMS Extbase Persistence QueryResultInterface<\TYPO3\CMS\Belog\Domain\Model\LogEntry > findByConstraint(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint)
Definition: LogEntryRepository.php:51
‪TYPO3\CMS\Belog\Domain\Model\LogEntry\getDetails
‪string getDetails()
Definition: LogEntry.php:299
‪TYPO3\CMS\Extbase\Persistence\Repository\createQuery
‪TYPO3 CMS Extbase Persistence QueryInterface createQuery()
Definition: Repository.php:189
‪TYPO3\CMS\Extbase\Persistence\Repository\setDefaultQuerySettings
‪setDefaultQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings)
Definition: Repository.php:179
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository\getBackendUsers
‪array getBackendUsers()
Definition: LogEntryRepository.php:185
‪TYPO3\CMS\Extbase\Persistence\Repository\$defaultQuerySettings
‪TYPO3 CMS Extbase Persistence Generic QuerySettingsInterface $defaultQuerySettings
Definition: Repository.php:42
‪TYPO3\CMS\Extbase\Persistence\Repository
Definition: Repository.php:23
‪TYPO3\CMS\Core\Type\Bitmask\Permission\PAGE_SHOW
‪const PAGE_SHOW
Definition: Permission.php:32
‪TYPO3\CMS\Belog\Domain\Model\LogEntry
Definition: LogEntry.php:25
‪TYPO3\CMS\Belog\Domain\Repository
Definition: LogEntryRepository.php:2
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository\addUsersAndGroupsToQueryConstraints
‪addUsersAndGroupsToQueryConstraints(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint, \TYPO3\CMS\Extbase\Persistence\QueryInterface $query, array &$queryConstraints)
Definition: LogEntryRepository.php:130
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository\deleteByMessageDetails
‪int deleteByMessageDetails(LogEntry $logEntry)
Definition: LogEntryRepository.php:164
‪TYPO3\CMS\Belog\Domain\Model\LogEntry\getDetailsNumber
‪int getDetailsNumber()
Definition: LogEntry.php:362