-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This is a breaking change! It is advised to look at the Changelog in the documentation for more information. The checkLinks functions in all LinktypeInterface classes now results a LinkTargetResponse object. In this, a status is stored for the link checking, which makes it easier to handling other link target status apart from broken. This effectively makes the following possible: - show all links in the broken link list, not just the broken links - better handling of link targets, which can't be checked. This includes for example URLs with 401 or 403 HTTP status codes, where it is not possible to check the URLs. Previously, these URLs were considered broken while in fact we do not know if they are broken or not and we have no was to check them. This also includes URLs protected by cloudflare. They are now stored not as broken but as "can't be checked" - it is possible to filter in the link list by this new status Resolves: #296 Resolves: #289 Related: #13
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,13 +16,15 @@ | |
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
use Sypets\Brofix\CheckLinks\LinkTargetResponse\LinkTargetResponse; | ||
use TYPO3\CMS\Core\Database\ConnectionPool; | ||
use TYPO3\CMS\Core\Database\Query\QueryBuilder; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
/** | ||
* This class implements a persistent link target cache using | ||
* a database table. | ||
* @internal | ||
*/ | ||
class LinkTargetPersistentCache extends AbstractLinkTargetCache | ||
{ | ||
|
@@ -70,10 +72,9 @@ public function hasEntryForUrl(string $linkTarget, string $linkType, bool $useEx | |
* @param string $linkTarget | ||
* @param string $linkType | ||
* @param int $expire (optional, default is 0, in that case uses $this->expire) | ||
* @return mixed[] returns URL response as array or | ||
* empty array if no entry | ||
* @return LinkTargetResponse|null | ||
*/ | ||
public function getUrlResponseForUrl(string $linkTarget, string $linkType, int $expire = 0): array | ||
public function getUrlResponseForUrl(string $linkTarget, string $linkType, int $expire = 0): ?LinkTargetResponse | ||
{ | ||
$expire = $expire ?: $this->expire; | ||
$queryBuilder = $this->generateQueryBuilder(); | ||
|
@@ -90,36 +91,32 @@ public function getUrlResponseForUrl(string $linkTarget, string $linkType, int $ | |
->executeQuery() | ||
->fetchAssociative(); | ||
if (!$row) { | ||
return []; | ||
return null; | ||
} | ||
$urlResponse = json_decode($row['url_response'], true); | ||
$urlResponse['lastChecked'] = (int)$row['last_check']; | ||
return $urlResponse; | ||
return LinkTargetResponse::createInstanceFromJson($row['url_response']); | ||
} | ||
|
||
/** | ||
* Insert result / update existing result | ||
* @param string $linkTarget | ||
* @param string $linkType | ||
* @param mixed[] $urlResponse | ||
* @param LinkTargetResponse $linkTargetResponse | ||
*/ | ||
public function setResult(string $linkTarget, string $linkType, array $urlResponse): void | ||
public function setResult(string $linkTarget, string $linkType, LinkTargetResponse $linkTargetResponse): void | ||
{ | ||
$checkStatus = $urlResponse['valid'] ? self::CHECK_STATUS_OK : self::CHECK_STATUS_ERROR; | ||
if ($this->hasEntryForUrl($linkTarget, $linkType, false)) { | ||
$this->update($linkTarget, $linkType, $urlResponse, $checkStatus); | ||
$this->update($linkTarget, $linkType, $linkTargetResponse); | ||
} else { | ||
$this->insert($linkTarget, $linkType, $urlResponse, $checkStatus); | ||
$this->insert($linkTarget, $linkType, $linkTargetResponse); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $linkTarget | ||
* @param string $linkType | ||
* @param mixed[] $urlResponse | ||
* @param int $checkStatus | ||
* @param LinkTargetResponse $linkTargetResponse | ||
*/ | ||
protected function insert(string $linkTarget, string $linkType, array $urlResponse, int $checkStatus): void | ||
protected function insert(string $linkTarget, string $linkType, LinkTargetResponse $linkTargetResponse): void | ||
{ | ||
$queryBuilder = $this->generateQueryBuilder(); | ||
$queryBuilder | ||
|
@@ -128,8 +125,8 @@ protected function insert(string $linkTarget, string $linkType, array $urlRespon | |
[ | ||
'url' => $linkTarget, | ||
'link_type' => $linkType, | ||
'url_response' => \json_encode($urlResponse), | ||
'check_status' => $checkStatus, | ||
'url_response' => $linkTargetResponse->toJson(), | ||
'check_status' => $linkTargetResponse->getStatus(), | ||
'last_check' => \time() | ||
] | ||
) | ||
|
@@ -142,7 +139,7 @@ protected function insert(string $linkTarget, string $linkType, array $urlRespon | |
* @param mixed[] $urlResponse | ||
* @param int $checkStatus | ||
*/ | ||
protected function update(string $linkTarget, string $linkType, array $urlResponse, int $checkStatus): void | ||
protected function update(string $linkTarget, string $linkType, LinkTargetResponse $linkTargetResponse): void | ||
Check failure on line 142 in Classes/CheckLinks/LinkTargetCache/LinkTargetPersistentCache.php
|
||
{ | ||
$queryBuilder = $this->generateQueryBuilder(); | ||
$queryBuilder | ||
|
@@ -151,8 +148,8 @@ protected function update(string $linkTarget, string $linkType, array $urlRespon | |
$queryBuilder->expr()->eq('url', $queryBuilder->createNamedParameter($linkTarget)), | ||
$queryBuilder->expr()->eq('link_type', $queryBuilder->createNamedParameter($linkType)) | ||
) | ||
->set('url_response', \json_encode($urlResponse)) | ||
->set('check_status', (string)$checkStatus) | ||
->set('url_response', $linkTargetResponse->toJson()) | ||
->set('check_status', (string)$linkTargetResponse->getStatus()) | ||
->set('last_check', (string)\time()) | ||
->executeStatement(); | ||
} | ||
|