‪TYPO3CMS  ‪main
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 
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
26 
33 {
34  public function ‪__construct(private readonly ‪BrokenLinkRepository $brokenLinkRepository) {}
35 
36  #[AsEventListener('rte-check-link-external')]
37  public function ‪checkExternalLink(‪BrokenLinkAnalysisEvent $event): void
38  {
39  if ($event->‪getLinkType() !== ‪LinkService::TYPE_URL) {
40  return;
41  }
42  ‪$url = (string)($event->‪getLinkData()['url'] ?? '');
43  if (!empty(‪$url)) {
44  if ($this->brokenLinkRepository->isLinkTargetBrokenLink(‪$url, 'external')) {
45  $event->‪markAsBrokenLink('External link is broken');
46  }
47  }
48  $event->‪markAsCheckedLink();
49  }
50 
51  #[AsEventListener('rte-check-link-to-page')]
52  public function ‪checkPageLink(‪BrokenLinkAnalysisEvent $event): void
53  {
54  if ($event->‪getLinkType() !== ‪LinkService::TYPE_PAGE) {
55  return;
56  }
57  $event->‪markAsCheckedLink();
58  $hrefInformation = $event->‪getLinkData();
59  $pageUid = $hrefInformation['pageuid'] ?? '';
60  if ($pageUid === '' || $pageUid === 'current') {
61  return;
62  }
63  // pageUid should be int at this point
64  $pageUid = (int)$pageUid;
65  $pageRecord = BackendUtility::getRecord('pages', $pageUid);
66  // Page does not exist
67  if (!is_array($pageRecord)) {
68  $event->‪markAsBrokenLink('Page with ID ' . $pageUid . ' not found');
69  return;
70  }
71  if (($pageRecord['hidden'] ?? 0) === 1) {
72  $event->‪markAsBrokenLink('Page with ID ' . $pageUid . ' is hidden');
73  } else {
74  $fragment = $hrefInformation['fragment'] ?? '';
75  if ($fragment !== '') {
76  ‪$url = $hrefInformation['pageuid'] . '#c' . $fragment;
77  if ($this->brokenLinkRepository->isLinkTargetBrokenLink(‪$url, 'db')) {
78  $event->‪markAsBrokenLink('Page with ID ' . $pageUid
79  . ' exists, but fragment ' . htmlspecialchars($fragment) . ' does not');
80  }
81  }
82  }
83  }
84 
85  #[AsEventListener('rte-check-link-to-file')]
86  public function ‪checkFileLink(‪BrokenLinkAnalysisEvent $event): void
87  {
88  if ($event->‪getLinkType() !== ‪LinkService::TYPE_FILE) {
89  return;
90  }
91  $event->‪markAsCheckedLink();
92 
93  $hrefInformation = $event->‪getLinkData();
94  $file = $hrefInformation['file'] ?? null;
95  if (!$file instanceof ‪FileInterface) {
96  $event->‪markAsBrokenLink('File link is broken');
97  return;
98  }
99 
100  if (!$file->hasProperty('uid') || (int)$file->getProperty('uid') === 0) {
101  $event->‪markAsBrokenLink('File link is broken');
102  return;
103  }
104 
105  if ($this->brokenLinkRepository->isLinkTargetBrokenLink('file:' . $file->getProperty('uid'), 'file')) {
106  $event->‪markAsBrokenLink('File with ID ' . $file->getProperty('uid') . ' not found');
107  }
108  }
109 }
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Core\Attribute\AsEventListener
Definition: AsEventListener.php:25
‪TYPO3\CMS\Linkvalidator\EventListener
Definition: CheckBrokenRteLinkEventListener.php:18
‪TYPO3\CMS\Webhooks\Message\$url
‪identifier readonly UriInterface $url
Definition: LoginErrorOccurredMessage.php:36