‪TYPO3CMS  10.4
BrokenLinkRepository.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 Doctrine\DBAL\Exception\TableNotFoundException;
25 
30 {
31  protected const ‪TABLE = 'tx_linkvalidator_link';
32 
41  public function ‪getNumberOfBrokenLinks(string $linkTarget): int
42  {
43  trigger_error(
44  'BrokenLinkRepository::getNumberOfBrokenLinks() was deprecated in TYPO3 10.3 Use BrokenLinkRepository::isLinkTargetBrokenLink() instead',
45  E_USER_DEPRECATED
46  );
47 
48  try {
49  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
50  ->getQueryBuilderForTable(static::TABLE);
51  $queryBuilder
52  ->count('uid')
53  ->from(static::TABLE)
54  ->where(
55  $queryBuilder->expr()->eq('url', $queryBuilder->createNamedParameter($linkTarget))
56  );
57  return (int)$queryBuilder
58  ->execute()
59  ->fetchColumn(0);
60  } catch (TableNotFoundException $e) {
61  return 0;
62  }
63  }
64 
72  public function ‪isLinkTargetBrokenLink(string $linkTarget): bool
73  {
74  try {
75  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
76  ->getQueryBuilderForTable(static::TABLE);
77  $queryBuilder
78  ->count('uid')
79  ->from(static::TABLE)
80  ->where(
81  $queryBuilder->expr()->eq('url', $queryBuilder->createNamedParameter($linkTarget))
82  );
83  return (bool)$queryBuilder
84  ->execute()
85  ->fetchColumn(0);
86  } catch (TableNotFoundException $e) {
87  return false;
88  }
89  }
90 
99  public function ‪getNumberOfBrokenLinksForRecordsOnPages(array $pageIds, array $searchFields): array
100  {
101  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
102  ->getQueryBuilderForTable(static::TABLE);
103  $queryBuilder->getRestrictions()->removeAll();
104  if (!‪$GLOBALS['BE_USER']->isAdmin()) {
105  $queryBuilder->getRestrictions()
106  ->add(GeneralUtility::makeInstance(EditableRestriction::class, $searchFields, $queryBuilder));
107  }
108  $statement = $queryBuilder->select('link_type')
109  ->addSelectLiteral($queryBuilder->expr()->count(static::TABLE . '.uid', 'amount'))
110  ->from(static::TABLE)
111  ->join(
112  static::TABLE,
113  'pages',
114  'pages',
115  $queryBuilder->expr()->eq('record_pid', $queryBuilder->quoteIdentifier('pages.uid'))
116  )
117  ->where(
118  $queryBuilder->expr()->orX(
119  $queryBuilder->expr()->andX(
120  $queryBuilder->expr()->in(
121  'record_uid',
122  $queryBuilder->createNamedParameter($pageIds, Connection::PARAM_INT_ARRAY)
123  ),
124  $queryBuilder->expr()->eq('table_name', $queryBuilder->createNamedParameter('pages'))
125  ),
126  $queryBuilder->expr()->andX(
127  $queryBuilder->expr()->in(
128  'record_pid',
129  $queryBuilder->createNamedParameter($pageIds, Connection::PARAM_INT_ARRAY)
130  ),
131  $queryBuilder->expr()->neq('table_name', $queryBuilder->createNamedParameter('pages'))
132  )
133  )
134  )
135  ->groupBy('link_type')
136  ->execute();
137 
138  $result = [
139  'total' => 0
140  ];
141  while ($row = $statement->fetch()) {
142  $result[$row['link_type']] = $row['amount'];
143  $result['total']+= $row['amount'];
144  }
145  return $result;
146  }
147 
148  public function ‪setNeedsRecheckForRecord(int $recordUid, string $tableName): void
149  {
150  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
151  ->getQueryBuilderForTable(static::TABLE);
152 
153  $queryBuilder->update(static::TABLE)
154  ->where(
155  $queryBuilder->expr()->eq(
156  'record_uid',
157  $queryBuilder->createNamedParameter($recordUid, \PDO::PARAM_INT)
158  ),
159  $queryBuilder->expr()->eq(
160  'table_name',
161  $queryBuilder->createNamedParameter($tableName)
162  )
163  )
164  ->set('needs_recheck', 1)
165  ->execute();
166  }
167 
168  public function ‪removeBrokenLinksForRecord(string $tableName, int $recordUid): void
169  {
170  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
171  ->getQueryBuilderForTable(static::TABLE);
172 
173  $queryBuilder->delete(static::TABLE)
174  ->where(
175  $queryBuilder->expr()->eq(
176  'record_uid',
177  $queryBuilder->createNamedParameter($recordUid, \PDO::PARAM_INT)
178  ),
179  $queryBuilder->expr()->eq(
180  'table_name',
181  $queryBuilder->createNamedParameter($tableName)
182  )
183  )
184  ->execute();
185  }
186 
187  public function ‪removeAllBrokenLinksOfRecordsOnPageIds(array $pageIds, array $linkTypes): void
188  {
189  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
190  ->getQueryBuilderForTable(static::TABLE);
191 
192  $queryBuilder->delete(static::TABLE)
193  ->where(
194  $queryBuilder->expr()->orX(
195  $queryBuilder->expr()->andX(
196  $queryBuilder->expr()->in(
197  'record_uid',
198  $queryBuilder->createNamedParameter($pageIds, Connection::PARAM_INT_ARRAY)
199  ),
200  $queryBuilder->expr()->eq('table_name', $queryBuilder->createNamedParameter('pages'))
201  ),
202  $queryBuilder->expr()->andX(
203  $queryBuilder->expr()->in(
204  'record_pid',
205  $queryBuilder->createNamedParameter($pageIds, Connection::PARAM_INT_ARRAY)
206  ),
207  $queryBuilder->expr()->neq(
208  'table_name',
209  $queryBuilder->createNamedParameter('pages')
210  )
211  )
212  ),
213  $queryBuilder->expr()->in(
214  'link_type',
215  $queryBuilder->createNamedParameter($linkTypes, Connection::PARAM_STR_ARRAY)
216  )
217  )
218  ->execute();
219  }
220 
232  public function ‪getAllBrokenLinksForPages(array $pageIds, array $linkTypes, array $searchFields = []): array
233  {
234  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
235  ->getQueryBuilderForTable(self::TABLE);
236  if (!‪$GLOBALS['BE_USER']->isAdmin()) {
237  $queryBuilder->getRestrictions()
238  ->add(GeneralUtility::makeInstance(EditableRestriction::class, $searchFields, $queryBuilder));
239  }
240  $records = $queryBuilder
241  ->select(self::TABLE . '.*')
242  ->from(self::TABLE)
243  ->join(
244  'tx_linkvalidator_link',
245  'pages',
246  'pages',
247  $queryBuilder->expr()->eq('tx_linkvalidator_link.record_pid', $queryBuilder->quoteIdentifier('pages.uid'))
248  )
249  ->where(
250  $queryBuilder->expr()->orX(
251  $queryBuilder->expr()->andX(
252  $queryBuilder->expr()->in(
253  'record_uid',
254  $queryBuilder->createNamedParameter($pageIds, Connection::PARAM_INT_ARRAY)
255  ),
256  $queryBuilder->expr()->eq('table_name', $queryBuilder->createNamedParameter('pages'))
257  ),
258  $queryBuilder->expr()->andX(
259  $queryBuilder->expr()->in(
260  'record_pid',
261  $queryBuilder->createNamedParameter($pageIds, Connection::PARAM_INT_ARRAY)
262  ),
263  $queryBuilder->expr()->neq('table_name', $queryBuilder->createNamedParameter('pages'))
264  )
265  ),
266  $queryBuilder->expr()->in(
267  'link_type',
268  $queryBuilder->createNamedParameter($linkTypes, Connection::PARAM_STR_ARRAY)
269  )
270  )
271  ->orderBy('tx_linkvalidator_link.record_uid')
272  ->addOrderBy('tx_linkvalidator_link.uid')
273  ->execute()
274  ->fetchAll();
275  foreach ($records as &$record) {
276  $response = json_decode($record['url_response'], true);
277  // Fallback mechanism to still support the old serialized data, could be removed in TYPO3 v12 or later
278  if ($response === null) {
279  $response = unserialize($record['url_response'], ['allowed_classes' => false]);
280  }
281  $record['url_response'] = $response;
282  }
283  return $records;
284  }
285 
286  public function ‪addBrokenLink($record, bool $isValid, array $errorParams = null): void
287  {
288  $response = ['valid' => $isValid];
289  if ($errorParams) {
290  $response['errorParams'] = $errorParams;
291  }
292  $record['url_response'] = json_encode($response);
293  GeneralUtility::makeInstance(ConnectionPool::class)
294  ->getConnectionForTable(self::TABLE)
295  ->insert(self::TABLE, $record);
296  }
297 }
‪TYPO3\CMS\Linkvalidator\QueryRestrictions\EditableRestriction
Definition: EditableRestriction.php:30
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:36
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Linkvalidator\Repository
Definition: BrokenLinkRepository.php:18