Skip to content

Commit

Permalink
UHF-10891: Skip non-404 responses, skip URL that seems to timeout wit…
Browse files Browse the repository at this point in the history
…hout VPN
  • Loading branch information
tuutti committed Oct 28, 2024
1 parent 7f39e31 commit 065eab6
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/Drush/Commands/TransliterateFilesCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Drush\Commands\DrushCommands;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
Expand Down Expand Up @@ -111,12 +112,35 @@ private function processEntityType(string $entityType, array $fields) : void {
* TRUE if remote file exists, FALSE if not.
*/
private function remoteFileExists(string $url) : bool {
// Skip wps since it seems to require a VPN.
if (str_contains('https://www.hel.fi/wps/', $url)) {
return TRUE;
}

try {
$this->httpClient->request('HEAD', $url);
$this->httpClient->request('HEAD', $url, ['timeout' => 15]);

return TRUE;
}
catch (ClientException) {
catch (ClientException $e) {
$response = $e->getResponse();

// Skip non-404 responses.
if ($response->getStatusCode() !== 404) {
return TRUE;
}
$skip = [
'text/html',
'text/plain',
];
foreach ($skip as $type) {
// Skip html content.
if (str_contains($response->getHeaderLine('Content-Type'), $type)) {
return TRUE;
}
}
}
catch (GuzzleException) {
}
return FALSE;
}
Expand All @@ -142,11 +166,13 @@ private function processFieldLinks(ContentEntityInterface $entity, string $field
if (!$href = $node->getAttribute('href')) {
continue;
}
$href = trim($href);

// Do nothing if file exists already.
if ($this->remoteFileExists($href)) {
continue;
}
$this->io()->note(sprintf('Found a broken link [%s]: "%s"', $entity->toUrl()->toString(), $href));
$this->io()->note(sprintf('Found a broken link "%s"', $href));
$basename = basename($href);

// Test sanitized filename and urldecoded+sanitized filename.
Expand All @@ -166,7 +192,7 @@ private function processFieldLinks(ContentEntityInterface $entity, string $field
}

if (!$newUrl) {
$this->io()->warning(sprintf('Failed to process [%s]: "%s"', $entity->toUrl()->toString(), $href));
$this->io()->warning(sprintf('Failed to process: "%s"', $href));

continue;
}
Expand Down

0 comments on commit 065eab6

Please sign in to comment.