‪TYPO3CMS  11.5
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;
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, 'external')) {
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  // pageUid should be int at this point
69  $pageUid = (int)$pageUid;
70  $pageRecord = BackendUtility::getRecord('pages', $pageUid);
71  // Page does not exist
72  if (!is_array($pageRecord)) {
73  $event->‪markAsBrokenLink('Page with ID ' . $pageUid . ' not found');
74  return;
75  }
76  if (($pageRecord['hidden'] ?? 0) === 1) {
77  $event->‪markAsBrokenLink('Page with ID ' . $pageUid . ' is hidden');
78  } else {
79  $fragment = $hrefInformation['fragment'] ?? '';
80  if ($fragment !== '') {
81  $url = $hrefInformation['pageuid'] . '#c' . $fragment;
82  if ($this->brokenLinkRepository->isLinkTargetBrokenLink($url, 'db')) {
83  $event->‪markAsBrokenLink('Page with ID ' . $pageUid
84  . ' exists, but fragment ' . htmlspecialchars($fragment) . ' does not');
85  }
86  }
87  }
88  }
89 
90  public function ‪checkFileLink(‪BrokenLinkAnalysisEvent $event): void
91  {
92  if ($event->‪getLinkType() !== ‪LinkService::TYPE_FILE) {
93  return;
94  }
95  $event->‪markAsCheckedLink();
96 
97  $hrefInformation = $event->‪getLinkData();
98  $file = $hrefInformation['file'] ?? null;
99  if (!$file instanceof ‪FileInterface) {
100  $event->‪markAsBrokenLink('File link is broken');
101  return;
102  }
103 
104  if (!$file->hasProperty('uid') || (int)$file->getProperty('uid') === 0) {
105  $event->‪markAsBrokenLink('File link is broken');
106  return;
107  }
108 
109  if ($this->brokenLinkRepository->isLinkTargetBrokenLink('file:' . $file->getProperty('uid'), 'file')) {
110  $event->‪markAsBrokenLink('File with ID ' . $file->getProperty('uid') . ' not found');
111  }
112  }
113 }
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Linkvalidator\EventListener
Definition: CheckBrokenRteLinkEventListener.php:18