‪TYPO3CMS  11.5
LogEntryRepository.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
20 use Psr\Log\LogLevel;
27 use ‪TYPO3\CMS\Core\Log\LogLevel as Typo3LogLevel;
35 
41 {
43 
48  {
49  $this->querySettings = ‪$querySettings;
50  }
51 
55  public function ‪initializeObject(): void
56  {
57  $this->‪setDefaultQuerySettings($this->querySettings->setRespectStoragePage(false));
58  }
59 
67  {
68  $query = $this->‪createQuery();
69  $queryConstraints = $this->‪createQueryConstraints($query, $constraint);
70  if (!empty($queryConstraints)) {
71  $query->matching($query->logicalAnd($queryConstraints));
72  }
73  $query->setOrderings(['uid' => ‪QueryInterface::ORDER_DESCENDING]);
74  $query->setLimit($constraint->‪getNumber());
75  return $query->execute();
76  }
77 
85  protected function ‪createQueryConstraints(‪QueryInterface $query, ‪Constraint $constraint): array
86  {
87  $queryConstraints = [];
88  // User / group handling
89  $this->‪addUsersAndGroupsToQueryConstraints($constraint, $query, $queryConstraints);
90  // Workspace
91  if ((int)$constraint->‪getWorkspaceUid() !== ‪Workspace::UID_ANY_WORKSPACE) {
92  $queryConstraints[] = $query->‪equals('workspace', $constraint->‪getWorkspaceUid());
93  }
94  // Channel
95  if ($channel = $constraint->‪getChannel()) {
96  $queryConstraints[] = $query->‪equals('channel', $channel);
97  }
98  // Level
99  if ($level = $constraint->‪getLevel()) {
100  $queryConstraints[] = $query->‪in('level', Typo3LogLevel::atLeast($level));
101  }
102  // Start / endtime handling: The timestamp calculation was already done
103  // in the controller, since we need those calculated values in the view as well.
104  $queryConstraints[] = $query->‪greaterThanOrEqual('tstamp', $constraint->‪getStartTimestamp());
105  $queryConstraints[] = $query->‪lessThan('tstamp', $constraint->‪getEndTimestamp());
106  // Page and level constraint if in page context
107  $this->‪addPageTreeConstraintsToQuery($constraint, $query, $queryConstraints);
108  return $queryConstraints;
109  }
110 
120  ‪Constraint $constraint,
121  ‪QueryInterface $query,
122  array &$queryConstraints
123  ): void {
124  $pageIds = [];
125  // Check if we should get a whole tree of pages and not only a single page
126  if ($constraint->‪getDepth() > 0) {
127  $pageTree = GeneralUtility::makeInstance(PageTreeView::class);
128  $pageTree->init('AND ' . ‪$GLOBALS['BE_USER']->getPagePermsClause(‪Permission::PAGE_SHOW));
129  $pageTree->makeHTML = 0;
130  $pageTree->fieldArray = ['uid'];
131  $pageTree->getTree($constraint->‪getPageId(), $constraint->‪getDepth());
132  $pageIds = $pageTree->ids;
133  }
134  if (!empty($constraint->‪getPageId())) {
135  $pageIds[] = $constraint->‪getPageId();
136  }
137  if (!empty($pageIds)) {
138  $queryConstraints[] = $query->‪in('eventPid', $pageIds);
139  }
140  }
141 
150  ‪Constraint $constraint,
151  ‪QueryInterface $query,
152  array &$queryConstraints
153  ): void {
154  $userOrGroup = $constraint->‪getUserOrGroup();
155  if ($userOrGroup === '') {
156  return;
157  }
158  // Constraint for a group
159  if (strpos($userOrGroup, 'gr-') === 0) {
160  $groupId = (int)substr($userOrGroup, 3);
161  $groupResolver = GeneralUtility::makeInstance(GroupResolver::class);
162  $userIds = $groupResolver->findAllUsersInGroups([$groupId], 'be_groups', 'be_users');
163  if (!empty($userIds)) {
164  $userIds = array_column($userIds, 'uid');
165  $userIds = array_map('intval', $userIds);
166  $queryConstraints[] = $query->‪in('userid', $userIds);
167  } else {
168  // If there are no group members -> use -1 as constraint to not find anything
169  $queryConstraints[] = $query->‪in('userid', [-1]);
170  }
171  } elseif (strpos($userOrGroup, 'us-') === 0) {
172  $queryConstraints[] = $query->‪equals('userid', (int)substr($userOrGroup, 3));
173  } elseif ($userOrGroup === '-1') {
174  $queryConstraints[] = $query->‪equals('userid', (int)‪$GLOBALS['BE_USER']->user['uid']);
175  }
176  }
177 
184  public function ‪deleteByMessageDetails(‪LogEntry $logEntry): int
185  {
186  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
187  ->getQueryBuilderForTable('sys_log');
188  $constraints = [];
189  $constraints[] = $queryBuilder->expr()->eq('details', $queryBuilder->createNamedParameter($logEntry->‪getDetails()));
190  // If the detailsNo is 11 or 12 we got messages that are heavily using placeholders. In this case
191  // we need to compare both the message and the actual log data to not remove too many log entries.
192  if (GeneralUtility::inList('11,12', (string)$logEntry->‪getDetailsNumber())) {
193  $constraints[] = $queryBuilder->expr()->eq('log_data', $queryBuilder->createNamedParameter($logEntry->‪getLogData()));
194  }
195  return (int)$queryBuilder->delete('sys_log')
196  ->where(...$constraints)
197  ->executeStatement();
198  }
199 
200  public function ‪getUsedChannels(): array
201  {
202  $conn = GeneralUtility::makeInstance(ConnectionPool::class)
203  ->getConnectionForTable('sys_log');
204 
205  $channels = $conn->createQueryBuilder()
206  ->select('channel')
207  ->distinct()
208  ->from('sys_log')
209  ->orderBy('channel')
210  ->executeQuery()
211  ->fetchFirstColumn();
212 
213  return array_combine($channels, $channels);
214  }
215 
216  public function ‪getUsedLevels(): array
217  {
218  static $allLevels = [
219  LogLevel::EMERGENCY,
220  LogLevel::ALERT,
221  LogLevel::CRITICAL,
222  LogLevel::ERROR,
223  LogLevel::WARNING,
224  LogLevel::NOTICE,
225  LogLevel::INFO,
226  LogLevel::DEBUG,
227  ];
228 
229  $conn = GeneralUtility::makeInstance(ConnectionPool::class)
230  ->getConnectionForTable('sys_log');
231 
232  $levels = $conn->createQueryBuilder()
233  ->select('level')
234  ->distinct()
235  ->from('sys_log')
236  ->executeQuery()
237  ->fetchFirstColumn();
238 
239  $levelsUsed = array_intersect($allLevels, $levels);
240 
241  return array_combine($levelsUsed, $levelsUsed);
242  }
243 }
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository\injectQuerySettings
‪injectQuerySettings(QuerySettingsInterface $querySettings)
Definition: LogEntryRepository.php:47
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository
Definition: LogEntryRepository.php:41
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository\initializeObject
‪initializeObject()
Definition: LogEntryRepository.php:55
‪TYPO3\CMS\Extbase\Persistence\QueryInterface
Definition: QueryInterface.php:29
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\equals
‪TYPO3 CMS Extbase Persistence Generic Qom ComparisonInterface equals($propertyName, $operand, $caseSensitive=true)
‪TYPO3\CMS\Belog\Domain\Model\Constraint\getWorkspaceUid
‪int getWorkspaceUid()
Definition: Constraint.php:147
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\ORDER_DESCENDING
‪const ORDER_DESCENDING
Definition: QueryInterface.php:99
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository\findByConstraint
‪QueryResultInterface findByConstraint(Constraint $constraint)
Definition: LogEntryRepository.php:66
‪TYPO3\CMS\Belog\Domain\Model\Constraint\getLevel
‪getLevel()
Definition: Constraint.php:179
‪TYPO3\CMS\Belog\Domain\Model\Workspace
Definition: Workspace.php:27
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository\addUsersAndGroupsToQueryConstraints
‪addUsersAndGroupsToQueryConstraints(Constraint $constraint, QueryInterface $query, array &$queryConstraints)
Definition: LogEntryRepository.php:149
‪TYPO3\CMS\Backend\Tree\View\PageTreeView
Definition: PageTreeView.php:24
‪TYPO3\CMS\Belog\Domain\Model\Constraint\getDepth
‪int getDepth()
Definition: Constraint.php:259
‪TYPO3\CMS\Core\Type\Bitmask\Permission
Definition: Permission.php:26
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\lessThan
‪ComparisonInterface lessThan($propertyName, $operand)
‪TYPO3\CMS\Belog\Domain\Model\LogEntry\getLogData
‪array getLogData()
Definition: LogEntry.php:415
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\in
‪ComparisonInterface in($propertyName, $operand)
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository\$querySettings
‪QuerySettingsInterface $querySettings
Definition: LogEntryRepository.php:42
‪TYPO3\CMS\Belog\Domain\Model\LogEntry\getDetails
‪string getDetails()
Definition: LogEntry.php:280
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository\createQueryConstraints
‪ConstraintInterface[] createQueryConstraints(QueryInterface $query, Constraint $constraint)
Definition: LogEntryRepository.php:85
‪TYPO3\CMS\Belog\Domain\Model\Constraint\getEndTimestamp
‪int getEndTimestamp()
Definition: Constraint.php:219
‪TYPO3\CMS\Extbase\Persistence\Repository\createQuery
‪TYPO3 CMS Extbase Persistence QueryInterface createQuery()
Definition: Repository.php:206
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository\getUsedLevels
‪getUsedLevels()
Definition: LogEntryRepository.php:216
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository\getUsedChannels
‪getUsedChannels()
Definition: LogEntryRepository.php:200
‪TYPO3\CMS\Extbase\Persistence\Repository
Definition: Repository.php:29
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface
Definition: QueryResultInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\Repository\setDefaultQuerySettings
‪setDefaultQuerySettings(QuerySettingsInterface $defaultQuerySettings)
Definition: Repository.php:196
‪TYPO3\CMS\Core\Type\Bitmask\Permission\PAGE_SHOW
‪const PAGE_SHOW
Definition: Permission.php:35
‪TYPO3\CMS\Belog\Domain\Model\Constraint\getStartTimestamp
‪int getStartTimestamp()
Definition: Constraint.php:199
‪TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
Definition: QuerySettingsInterface.php:22
‪TYPO3\CMS\Core\Authentication\GroupResolver
Definition: GroupResolver.php:36
‪TYPO3\CMS\Belog\Domain\Model\LogEntry
Definition: LogEntry.php:28
‪TYPO3\CMS\Belog\Domain\Repository
Definition: LogEntryRepository.php:18
‪TYPO3\CMS\Belog\Domain\Model\Workspace\UID_ANY_WORKSPACE
‪const UID_ANY_WORKSPACE
Definition: Workspace.php:36
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Belog\Domain\Model\Constraint\getChannel
‪getChannel()
Definition: Constraint.php:163
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\greaterThanOrEqual
‪ComparisonInterface greaterThanOrEqual($propertyName, $operand)
‪TYPO3\CMS\Belog\Domain\Model\Constraint\getUserOrGroup
‪string getUserOrGroup()
Definition: Constraint.php:107
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface
Definition: ConstraintInterface.php:25
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Belog\Domain\Model\Constraint
Definition: Constraint.php:25
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository\addPageTreeConstraintsToQuery
‪addPageTreeConstraintsToQuery(Constraint $constraint, QueryInterface $query, array &$queryConstraints)
Definition: LogEntryRepository.php:119
‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository\deleteByMessageDetails
‪int deleteByMessageDetails(LogEntry $logEntry)
Definition: LogEntryRepository.php:184
‪TYPO3\CMS\Core\Log\LogLevel
Definition: LogLevel.php:24
‪TYPO3\CMS\Belog\Domain\Model\Constraint\getNumber
‪int getNumber()
Definition: Constraint.php:127
‪TYPO3\CMS\Belog\Domain\Model\Constraint\getPageId
‪int getPageId()
Definition: Constraint.php:239
‪TYPO3\CMS\Belog\Domain\Model\LogEntry\getDetailsNumber
‪int getDetailsNumber()
Definition: LogEntry.php:375