Skip to content

Commit

Permalink
Merge pull request #25 from mrk-te/release/1.1.4
Browse files Browse the repository at this point in the history
#24 feat[Sitemap]:  - Catch exceptions raised by the retrival of miss…
  • Loading branch information
mrk-te authored Aug 16, 2023
2 parents 9118175 + 138f1aa commit 978ddfc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.4] - 2023-08-16

### Fixed
* Catch exception on corrupted `image` field types to prevent a full loss of the sitemap (Issue #24)

## [1.1.3] - 2023-03-29

### Fixed
Expand Down
43 changes: 27 additions & 16 deletions bundle/Service/SitemapContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use Codein\IbexaSeoToolkit\Helper\SiteAccessConfigResolver;
use Codein\IbexaSeoToolkit\Helper\SitemapQueryHelper;
use DOMDocument;
use Exception;
use eZ\Publish\API\Repository\SearchService;
use eZ\Publish\Core\Helper\FieldHelper;
use eZ\Publish\SPI\Variation\VariationHandler;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

Expand All @@ -23,21 +25,24 @@ final class SitemapContentService
private $sitemapQueryHelper;
private $siteAccessConfigResolver;
private $fieldHelper;
private $logger;

public function __construct(
SiteAccessConfigResolver $siteAccessConfigResolver,
UrlGeneratorInterface $urlGenerator,
SearchService $searchService,
SitemapQueryHelper $sitemapQueryHelper,
FieldHelper $fieldHelper,
RequestStack $requestStack
RequestStack $requestStack,
LoggerInterface $logger
) {
$this->siteAccessConfigResolver = $siteAccessConfigResolver;
$this->urlGenerator = $urlGenerator;
$this->searchService = $searchService;
$this->sitemapQueryHelper = $sitemapQueryHelper;
$this->requestStack = $requestStack;
$this->fieldHelper = $fieldHelper;
$this->logger = $logger;
}

public function generate(): DOMDocument
Expand Down Expand Up @@ -87,7 +92,7 @@ public function generateIndex(DOMDocument $sitemap, int $numberOfResults, int $l
'codein_ibexa_seo_toolkit.sitemap_page_result',
['page' => $page]
);
} catch (\Exception $e) {
} catch (Exception $e) {
continue;
}

Expand Down Expand Up @@ -121,7 +126,7 @@ public function generateContentTypeIndex(DOMDocument $sitemap): DOMDocument
'codein_ibexa_seo_toolkit.sitemap_page_content_type',
['contentTypeIdentifier' => $contentType]
);
} catch (\Exception $e) {
} catch (Exception $e) {
continue;
}

Expand Down Expand Up @@ -187,7 +192,7 @@ public function generateResults(
'ez_urlalias',
['location' => $location]
);
} catch (\Exception $e) {
} catch (Exception $e) {
continue;
}

Expand All @@ -207,19 +212,25 @@ public function generateResults(
if ($this->fieldHelper->isFieldEmpty($location->getContent(), $field->fieldDefIdentifier)) {
continue;
}

$variation = $this->variationHandler->getVariation($field, $location->getContent()->getVersionInfo(), 'original');

$imageBlock = $sitemap->createElement('image:image');
$imageLoc = $sitemap->createElement('image:loc', $variation->uri);
$imageBlock->appendChild($imageLoc);

if ($field->value->alternativeText) {
$imageCaption = $sitemap->createElement('image:caption', $field->value->alternativeText);
$imageBlock->appendChild($imageCaption);
try {
$variation = $this->variationHandler->getVariation($field, $location->getContent()->getVersionInfo(), 'original');

$imageBlock = $sitemap->createElement('image:image');
$imageLoc = $sitemap->createElement('image:loc', $variation->uri);
$imageBlock->appendChild($imageLoc);

if ($field->value->alternativeText) {
$imageCaption = $sitemap->createElement('image:caption', $field->value->alternativeText);
$imageBlock->appendChild($imageCaption);
}

$sitemapUrl->appendChild($imageBlock);
} catch (Exception $exception) {
$this->logger->critical(
sprintf('Unable to get original image variation for content %s on field %s', self::class, $location->contentId, $field->fieldDefIdentifier),
[ 'exception' => $exception ]
);
}

$sitemapUrl->appendChild($imageBlock);
}
}

Expand Down

0 comments on commit 978ddfc

Please sign in to comment.