‪TYPO3CMS  10.4
CheckBrokenRteLinkEventListener.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 
25 
32 {
37 
39  {
40  $this->brokenLinkRepository = ‪$brokenLinkRepository;
41  }
42 
43  public function ‪checkExternalLink(‪BrokenLinkAnalysisEvent $event): void
44  {
45  if ($event->‪getLinkType() !== ‪LinkService::TYPE_URL) {
46  return;
47  }
48  $url = (string)($event->‪getLinkData()['url'] ?? '');
49  if (!empty($url)) {
50  if ($this->brokenLinkRepository->isLinkTargetBrokenLink($url)) {
51  $event->‪markAsBrokenLink('External link is broken');
52  }
53  }
54  $event->‪markAsCheckedLink();
55  }
56 
57  public function ‪checkPageLink(‪BrokenLinkAnalysisEvent $event): void
58  {
59  if ($event->‪getLinkType() !== ‪LinkService::TYPE_PAGE) {
60  return;
61  }
62  $event->‪markAsCheckedLink();
63  $hrefInformation = $event->‪getLinkData();
64  $pageUid = $hrefInformation['pageuid'] ?? '';
65  if ($pageUid === '' || $pageUid === 'current') {
66  return;
67  }
68  $pageRecord = ‪BackendUtility::getRecord('pages', $hrefInformation['pageuid']);
69  // Page does not exist
70  if (!is_array($pageRecord)) {
71  $event->‪markAsBrokenLink('Page with ID ' . htmlspecialchars($hrefInformation['pageuid']) . ' not found');
72  }
73  }
74 
75  public function ‪checkFileLink(‪BrokenLinkAnalysisEvent $event): void
76  {
77  if ($event->‪getLinkType() !== ‪LinkService::TYPE_FILE) {
78  return;
79  }
80  $event->‪markAsCheckedLink();
81 
82  $hrefInformation = $event->‪getLinkData();
83  $file = $hrefInformation['file'] ?? null;
84  if (!$file instanceof ‪FileInterface) {
85  $event->‪markAsBrokenLink('File link is broken');
86  return;
87  }
88 
89  if (!$file->hasProperty('uid') || (int)$file->getProperty('uid') === 0) {
90  $event->‪markAsBrokenLink('File link is broken');
91  return;
92  }
93 
94  if ($this->brokenLinkRepository->isLinkTargetBrokenLink('file:' . $file->getProperty('uid'))) {
95  $event->‪markAsBrokenLink('File with ID ' . $file->getProperty('uid') . ' not found');
96  }
97  }
98 }
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Linkvalidator\EventListener
Definition: CheckBrokenRteLinkEventListener.php:18
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪TYPO3\CMS\Backend\Utility\BackendUtility\getRecord
‪static array null getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)
Definition: BackendUtility.php:95