From 41d7cee8b7bec5795ee06d80bf83e73464a1a1db Mon Sep 17 00:00:00 2001 From: Sybille Peters <sypets@gmx.de> Date: Sun, 16 Feb 2020 09:08:08 +0100 Subject: [PATCH] [TASK] Simplify API for checking link target Previously, the function BrokenLinkRepository::getNumberOfBrokenLinks() was used in the event listener. It is not necessary to return the count. We now only check if the link target is in the list of broken links. This makes the API easier to read and reduces the number of methods with similar and misleading names. Resolves: #90390 Releases: master Change-Id: Ia94a7a0bd44bbc827864371f7c6694b4b8f8f52a Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/63267 Tested-by: TYPO3com <noreply@typo3.com> Tested-by: Benni Mack <benni@typo3.org> Tested-by: Georg Ringer <georg.ringer@gmail.com> Reviewed-by: Benni Mack <benni@typo3.org> Reviewed-by: Georg Ringer <georg.ringer@gmail.com> --- ...ygetNumberOfBrokenLinksInLinkvalidator.rst | 34 +++++++++++++++++++ .../CheckBrokenRteLinkEventListener.php | 6 ++-- .../Repository/BrokenLinkRepository.php | 34 ++++++++++++++++++- 3 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 typo3/sysext/core/Documentation/Changelog/master/Deprecation-90390-DeprecateBrokenLinkRepositorygetNumberOfBrokenLinksInLinkvalidator.rst diff --git a/typo3/sysext/core/Documentation/Changelog/master/Deprecation-90390-DeprecateBrokenLinkRepositorygetNumberOfBrokenLinksInLinkvalidator.rst b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-90390-DeprecateBrokenLinkRepositorygetNumberOfBrokenLinksInLinkvalidator.rst new file mode 100644 index 000000000000..29552b94d97c --- /dev/null +++ b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-90390-DeprecateBrokenLinkRepositorygetNumberOfBrokenLinksInLinkvalidator.rst @@ -0,0 +1,34 @@ +.. include:: ../../Includes.txt + +=============================================================================================== +Deprecation: #90390 - Deprecate BrokenLinkRepository::getNumberOfBrokenLinks() in linkvalidator +=============================================================================================== + +See :issue:`90390` + +Description +=========== + +The method :php:`BrokenLinkRepository::getNumberOfBrokenLinks()` is deprecated. + + +Impact +====== + +If third party extensions use this function, a `E_USER_DEPRECATED` +is triggered. + + +Affected Installations +====================== + +This only affects third party extensions which use this function. The +deprecated function is no longer used in the core. + + +Migration +========= + +Use :php:`BrokenLinkRepository::getNumberOfBrokenLinks()` instead. + +.. index:: Backend, NotScanned, ext:linkvalidator diff --git a/typo3/sysext/linkvalidator/Classes/EventListener/CheckBrokenRteLinkEventListener.php b/typo3/sysext/linkvalidator/Classes/EventListener/CheckBrokenRteLinkEventListener.php index 2d296995219b..3075309466bd 100644 --- a/typo3/sysext/linkvalidator/Classes/EventListener/CheckBrokenRteLinkEventListener.php +++ b/typo3/sysext/linkvalidator/Classes/EventListener/CheckBrokenRteLinkEventListener.php @@ -45,8 +45,7 @@ final class CheckBrokenRteLinkEventListener } $url = (string)$event->getLinkData()['url'] ?? ''; if (!empty($url)) { - $count = $this->brokenLinkRepository->getNumberOfBrokenLinks($url); - if ($count) { + if ($this->brokenLinkRepository->isLinkTargetBrokenLink($url)) { $event->markAsBrokenLink('External link is broken'); } } @@ -88,8 +87,7 @@ final class CheckBrokenRteLinkEventListener return; } - $count = $this->brokenLinkRepository->getNumberOfBrokenLinks('file:' . $file->getProperty('uid')); - if ($count) { + if ($this->brokenLinkRepository->isLinkTargetBrokenLink('file:' . $file->getProperty('uid'))) { $event->markAsBrokenLink('File with ID ' . $file->getProperty('uid') . ' not found'); } } diff --git a/typo3/sysext/linkvalidator/Classes/Repository/BrokenLinkRepository.php b/typo3/sysext/linkvalidator/Classes/Repository/BrokenLinkRepository.php index 8a322b087b61..36bf60b38079 100644 --- a/typo3/sysext/linkvalidator/Classes/Repository/BrokenLinkRepository.php +++ b/typo3/sysext/linkvalidator/Classes/Repository/BrokenLinkRepository.php @@ -33,9 +33,15 @@ class BrokenLinkRepository * @param string $linkTarget Url to check for. Can be a URL (for external links) * a page uid (for db links), a file reference (for file links), etc. * @return int the amount of usages this broken link is used in this installation + * @deprecated This method was deprecated in TYPO3 10.3 Use isLinkTargetBrokenLink() instead */ public function getNumberOfBrokenLinks(string $linkTarget): int { + trigger_error( + 'BrokenLinkRepository::getNumberOfBrokenLinks() was deprecated in TYPO3 10.3 Use BrokenLinkRepository::isLinkTargetBrokenLink() instead', + E_USER_DEPRECATED + ); + try { $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) ->getQueryBuilderForTable(static::TABLE); @@ -46,10 +52,36 @@ class BrokenLinkRepository $queryBuilder->expr()->eq('url', $queryBuilder->createNamedParameter($linkTarget)) ); return (int)$queryBuilder + ->execute() + ->fetchColumn(0); + } catch (\Doctrine\DBAL\Exception\TableNotFoundException $e) { + return 0; + } + } + + /** + * Check if linkTarget is in list of broken links. + * + * @param string $linkTarget Url to check for. Can be a URL (for external links) + * a page uid (for db links), a file reference (for file links), etc. + * @return bool is the link target a broken link + */ + public function isLinkTargetBrokenLink(string $linkTarget): bool + { + try { + $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) + ->getQueryBuilderForTable(static::TABLE); + $queryBuilder + ->count('uid') + ->from(static::TABLE) + ->where( + $queryBuilder->expr()->eq('url', $queryBuilder->createNamedParameter($linkTarget)) + ); + return (bool)$queryBuilder ->execute() ->fetchColumn(0); } catch (\Doctrine\DBAL\Exception\TableNotFoundException $e) { - return 0; + return false; } } -- GitLab