‪TYPO3CMS  9.5
RedirectRepository.php
Go to the documentation of this file.
1 <?php
2 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 
22 
28 {
32  protected ‪$demand;
33 
39  public function ‪__construct(‪Demand ‪$demand = null)
40  {
41  $this->demand = ‪$demand ?? new ‪Demand();
42  }
43 
49  public function ‪findRedirectsByDemand(): array
50  {
51  return $this->‪getQueryBuilderForDemand()
52  ->‪setMaxResults($this->demand->getLimit())
53  ->‪setFirstResult($this->demand->getOffset())
54  ->‪execute()
55  ->fetchAll();
56  }
57 
61  public function ‪countRedirectsByByDemand(): int
62  {
63  return $this->‪getQueryBuilderForDemand()->‪execute()->rowCount();
64  }
65 
71  protected function ‪getQueryBuilderForDemand(): ‪QueryBuilder
72  {
73  $queryBuilder = $this->‪getQueryBuilder();
74  $queryBuilder
75  ->select('*')
76  ->from('sys_redirect')
77  ->orderBy('source_host')
78  ->addOrderBy('source_path');
79 
80  $constraints = [];
81  if ($this->demand->hasSourceHost()) {
82  $constraints[] =$queryBuilder->expr()->eq(
83  'source_host',
84  $queryBuilder->createNamedParameter($this->demand->getSourceHost(), \PDO::PARAM_STR)
85  );
86  }
87 
88  if ($this->demand->hasSourcePath()) {
89  $escapedLikeString = '%' . $queryBuilder->escapeLikeWildcards($this->demand->getSourcePath()) . '%';
90  $constraints[] = $queryBuilder->expr()->like(
91  'source_path',
92  $queryBuilder->createNamedParameter($escapedLikeString, \PDO::PARAM_STR)
93  );
94  }
95 
96  if ($this->demand->hasTarget()) {
97  $escapedLikeString = '%' . $queryBuilder->escapeLikeWildcards($this->demand->getTarget()) . '%';
98  $constraints[] = $queryBuilder->expr()->like(
99  'target',
100  $queryBuilder->createNamedParameter($escapedLikeString, \PDO::PARAM_STR)
101  );
102  }
103 
104  if ($this->demand->hasStatusCode()) {
105  $constraints[] =$queryBuilder->expr()->eq(
106  'target_statuscode',
107  $queryBuilder->createNamedParameter($this->demand->getStatusCode(), \PDO::PARAM_INT)
108  );
109  }
110 
111  if (!empty($constraints)) {
112  $queryBuilder->where(...$constraints);
113  }
114  return $queryBuilder;
115  }
116 
122  public function ‪findHostsOfRedirects(): array
123  {
124  return $this->‪getQueryBuilder()
125  ->‪select('source_host as name')
126  ->‪from('sys_redirect')
127  ->‪orderBy('source_host')
128  ->‪groupBy('source_host')
129  ->‪execute()
130  ->fetchAll();
131  }
132 
138  public function ‪findStatusCodesOfRedirects(): array
139  {
140  return $this->‪getQueryBuilder()
141  ->‪select('target_statuscode as code')
142  ->‪from('sys_redirect')
143  ->‪orderBy('target_statuscode')
144  ->‪groupBy('target_statuscode')
145  ->‪execute()
146  ->fetchAll();
147  }
148 
152  protected function ‪getQueryBuilder(): ‪QueryBuilder
153  {
154  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_redirect');
155  $queryBuilder->‪getRestrictions()
156  ->‪removeAll()
157  ->‪add(GeneralUtility::makeInstance(DeletedRestriction::class));
158  return $queryBuilder;
159  }
160 }
‪TYPO3\CMS\Redirects\Repository\Demand
Definition: Demand.php:25
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\groupBy
‪QueryBuilder groupBy(... $groupBy)
Definition: QueryBuilder.php:679
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\select
‪QueryBuilder select(string ... $selects)
Definition: QueryBuilder.php:390
‪TYPO3\CMS\Redirects\Repository\RedirectRepository\countRedirectsByByDemand
‪int countRedirectsByByDemand()
Definition: RedirectRepository.php:60
‪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:794
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\getRestrictions
‪QueryRestrictionContainerInterface getRestrictions()
Definition: QueryBuilder.php:89
‪TYPO3\CMS\Redirects\Repository\RedirectRepository\findStatusCodesOfRedirects
‪array findStatusCodesOfRedirects()
Definition: RedirectRepository.php:137
‪TYPO3\CMS\Redirects\Repository\RedirectRepository\getQueryBuilderForDemand
‪QueryBuilder getQueryBuilderForDemand()
Definition: RedirectRepository.php:70
‪TYPO3\CMS\Core\Database\Query\QueryBuilder
Definition: QueryBuilder.php:47
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\from
‪QueryBuilder from(string $from, string $alias=null)
Definition: QueryBuilder.php:506
‪TYPO3\CMS\Redirects\Repository\RedirectRepository\findHostsOfRedirects
‪array findHostsOfRedirects()
Definition: RedirectRepository.php:121
‪TYPO3\CMS\Redirects\Repository\RedirectRepository\getQueryBuilder
‪QueryBuilder getQueryBuilder()
Definition: RedirectRepository.php:151
‪TYPO3\CMS\Redirects\Repository\RedirectRepository\__construct
‪__construct(Demand $demand=null)
Definition: RedirectRepository.php:38
‪TYPO3\CMS\Redirects\Repository\RedirectRepository\$demand
‪Demand $demand
Definition: RedirectRepository.php:31
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\execute
‪Doctrine DBAL Driver Statement int execute()
Definition: QueryBuilder.php:178
‪TYPO3\CMS\Redirects\Repository\RedirectRepository
Definition: RedirectRepository.php:28
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\setMaxResults
‪QueryBuilder setMaxResults(int $maxResults)
Definition: QueryBuilder.php:329
‪TYPO3\CMS\Redirects\Repository
Definition: Demand.php:3
‪TYPO3\CMS\Redirects\Repository\RedirectRepository\findRedirectsByDemand
‪array findRedirectsByDemand()
Definition: RedirectRepository.php:48
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\setFirstResult
‪QueryBuilder setFirstResult(int $firstResult)
Definition: QueryBuilder.php:304
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:26
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionContainerInterface\add
‪QueryRestrictionContainerInterface add(QueryRestrictionInterface $restriction)
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45