‪TYPO3CMS  10.4
RedirectRepository.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 
24 
30 {
34  protected ‪$demand;
35 
41  public function ‪__construct(‪Demand ‪$demand = null)
42  {
43  $this->demand = ‪$demand ?? new ‪Demand();
44  }
45 
51  public function ‪findRedirectsByDemand(): array
52  {
53  return $this->‪getQueryBuilderForDemand()
54  ->‪setMaxResults($this->demand->getLimit())
55  ->‪setFirstResult($this->demand->getOffset())
56  ->‪execute()
57  ->fetchAll();
58  }
59 
63  public function ‪countRedirectsByByDemand(): int
64  {
65  return $this->‪getQueryBuilderForDemand()->‪execute()->rowCount();
66  }
67 
73  protected function ‪getQueryBuilderForDemand(): ‪QueryBuilder
74  {
75  $queryBuilder = $this->‪getQueryBuilder();
76  $queryBuilder
77  ->select('*')
78  ->from('sys_redirect')
79  ->orderBy('source_host')
80  ->addOrderBy('source_path');
81 
82  $constraints = [];
83  if ($this->demand->hasSourceHost()) {
84  $constraints[] =$queryBuilder->expr()->eq(
85  'source_host',
86  $queryBuilder->createNamedParameter($this->demand->getSourceHost(), \PDO::PARAM_STR)
87  );
88  }
89 
90  if ($this->demand->hasSourcePath()) {
91  $escapedLikeString = '%' . $queryBuilder->escapeLikeWildcards($this->demand->getSourcePath()) . '%';
92  $constraints[] = $queryBuilder->expr()->like(
93  'source_path',
94  $queryBuilder->createNamedParameter($escapedLikeString, \PDO::PARAM_STR)
95  );
96  }
97 
98  if ($this->demand->hasTarget()) {
99  $escapedLikeString = '%' . $queryBuilder->escapeLikeWildcards($this->demand->getTarget()) . '%';
100  $constraints[] = $queryBuilder->expr()->like(
101  'target',
102  $queryBuilder->createNamedParameter($escapedLikeString, \PDO::PARAM_STR)
103  );
104  }
105 
106  if ($this->demand->hasStatusCode()) {
107  $constraints[] =$queryBuilder->expr()->eq(
108  'target_statuscode',
109  $queryBuilder->createNamedParameter($this->demand->getStatusCode(), \PDO::PARAM_INT)
110  );
111  }
112 
113  if (!empty($constraints)) {
114  $queryBuilder->where(...$constraints);
115  }
116  return $queryBuilder;
117  }
118 
124  public function ‪findHostsOfRedirects(): array
125  {
126  return $this->‪getQueryBuilder()
127  ->‪select('source_host as name')
128  ->‪from('sys_redirect')
129  ->‪orderBy('source_host')
130  ->‪groupBy('source_host')
131  ->‪execute()
132  ->fetchAll();
133  }
134 
140  public function ‪findStatusCodesOfRedirects(): array
141  {
142  return $this->‪getQueryBuilder()
143  ->‪select('target_statuscode as code')
144  ->‪from('sys_redirect')
145  ->‪orderBy('target_statuscode')
146  ->‪groupBy('target_statuscode')
147  ->‪execute()
148  ->fetchAll();
149  }
150 
154  protected function ‪getQueryBuilder(): ‪QueryBuilder
155  {
156  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_redirect');
157  $queryBuilder->‪getRestrictions()
158  ->‪removeAll()
159  ->‪add(GeneralUtility::makeInstance(DeletedRestriction::class));
160  return $queryBuilder;
161  }
162 }
‪TYPO3\CMS\Redirects\Repository\Demand
Definition: Demand.php:27
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\groupBy
‪QueryBuilder groupBy(... $groupBy)
Definition: QueryBuilder.php:726
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\select
‪QueryBuilder select(string ... $selects)
Definition: QueryBuilder.php:416
‪TYPO3\CMS\Redirects\Repository\RedirectRepository\countRedirectsByByDemand
‪int countRedirectsByByDemand()
Definition: RedirectRepository.php:62
‪TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionContainerInterface\removeAll
‪QueryRestrictionContainerInterface removeAll()
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\orderBy
‪QueryBuilder orderBy(string $fieldName, string $order=null)
Definition: QueryBuilder.php:841
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\getRestrictions
‪QueryRestrictionContainerInterface getRestrictions()
Definition: QueryBuilder.php:104
‪TYPO3\CMS\Redirects\Repository\RedirectRepository\findStatusCodesOfRedirects
‪array findStatusCodesOfRedirects()
Definition: RedirectRepository.php:139
‪TYPO3\CMS\Redirects\Repository\RedirectRepository\getQueryBuilderForDemand
‪QueryBuilder getQueryBuilderForDemand()
Definition: RedirectRepository.php:72
‪TYPO3\CMS\Core\Database\Query\QueryBuilder
Definition: QueryBuilder.php:52
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\from
‪QueryBuilder from(string $from, string $alias=null)
Definition: QueryBuilder.php:531
‪TYPO3\CMS\Redirects\Repository\RedirectRepository\findHostsOfRedirects
‪array findHostsOfRedirects()
Definition: RedirectRepository.php:123
‪TYPO3\CMS\Redirects\Repository\RedirectRepository\getQueryBuilder
‪QueryBuilder getQueryBuilder()
Definition: RedirectRepository.php:153
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\execute
‪Statement Doctrine DBAL ForwardCompatibility Result Doctrine DBAL Driver ResultStatement int execute()
Definition: QueryBuilder.php:204
‪TYPO3\CMS\Redirects\Repository\RedirectRepository\__construct
‪__construct(Demand $demand=null)
Definition: RedirectRepository.php:40
‪TYPO3\CMS\Redirects\Repository\RedirectRepository\$demand
‪Demand $demand
Definition: RedirectRepository.php:33
‪TYPO3\CMS\Redirects\Repository\RedirectRepository
Definition: RedirectRepository.php:30
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\setMaxResults
‪QueryBuilder setMaxResults(int $maxResults)
Definition: QueryBuilder.php:355
‪TYPO3\CMS\Redirects\Repository
Definition: Demand.php:18
‪TYPO3\CMS\Redirects\Repository\RedirectRepository\findRedirectsByDemand
‪array findRedirectsByDemand()
Definition: RedirectRepository.php:50
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\setFirstResult
‪QueryBuilder setFirstResult(int $firstResult)
Definition: QueryBuilder.php:330
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionContainerInterface\add
‪QueryRestrictionContainerInterface add(QueryRestrictionInterface $restriction)
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46