Skip to content

Commit

Permalink
Throw a specific exception on paths not found importing pages from CIF
Browse files Browse the repository at this point in the history
  • Loading branch information
mlocati committed Nov 27, 2024
1 parent 4480be8 commit 51803e4
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Concrete\Core\Backup\ContentImporter\Exception;

use Concrete\Core\Error\UserMessageException;

/**
* Exception thrown when importing the page structure if the parent page does not exist (yet).
*/
class MissingPageAtPathException extends UserMessageException
{
/**
* @var string[]
*/
private $missingPagePaths = [];

/**
* @param string|string[] $missingPagePaths
* @param string $message
*/
public function __construct($missingPagePaths, $message = '')
{
$this->missingPagePaths = is_array($missingPagePaths) ? $missingPagePaths : [$missingPagePaths];
if (!$message) {
if (count($this->missingPagePaths) === 1) {
$message = t('Missing the page with path %s', $this->missingPagePaths[0]);
} else {
$message = t('Missing the pages with the following paths:') . "\n- " . implode("\n- ", $this->missingPagePaths);
}
}
parent::__construct($message);
}

/**
* @return string[]
*/
public function getMissingPagePaths()
{
return $this->missingPagePaths;
}

/**
* @return static
*/
public function merge(MissingPageAtPathException $other)
{
return new static(array_merge($this->getMissingPagePaths(), $other->getMissingPagePaths()));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Concrete\Core\Backup\ContentImporter\Importer\Routine;

use Concrete\Core\Backup\ContentImporter\Exception\MissingPageAtPathException;
use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\Entity\Page\PagePath;
use Concrete\Core\Error\UserMessageException;
Expand Down Expand Up @@ -60,31 +61,33 @@ public function import(SimpleXMLElement $sx)
$elements = $this->sortElementsByPath($elements);
while ($elements !== []) {
$delayed = [];
$errorMessages = [];
$missingPageAtPathException = null;
foreach ($elements as $element) {
$importResult = null;
switch ($element->getName()) {
case 'page':
$localeInfo = $this->extractLocale($element);
$importResult = $this->getOrCreatePage($element, $localeInfo);
if (is_object($importResult)) {
$this->importAdditionalPagePaths($element, $importResult);
}
break;
case 'external-link':
$importResult = $this->importExternalLink($element);
break;
case 'alias':
$importResult = $this->importAlias($element);
break;
}
if (is_string($importResult)) {
try {
switch ($element->getName()) {
case 'page':
$localeInfo = $this->extractLocale($element);
$page = $this->getOrCreatePage($element, $localeInfo);
$this->importAdditionalPagePaths($element, $page);
break;
case 'external-link':
$this->importExternalLink($element);
break;
case 'alias':
$this->importAlias($element);
break;
}
} catch (MissingPageAtPathException $x) {
$delayed[] = $element;
$errorMessages[] = $importResult;
if ($missingPageAtPathException === null) {
$missingPageAtPathException = $x;
} else {
$missingPageAtPathException = $missingPageAtPathException->merge($x);
}
}
}
if (count($delayed) == count($elements)) {
throw new UserMessageException(implode("\n", $errorMessages));
throw $missingPageAtPathException;
}
$elements = $delayed;
$this->clearPageCache();
Expand All @@ -98,7 +101,9 @@ private function clearPageCache()
}

/**
* @return \Concrete\Core\Page\Page|string returns a string describing why we should import the page later on, or the created page otherwise
* @throws \Concrete\Core\Backup\ContentImporter\Exception\MissingPageAtPathException
*
* @return \Concrete\Core\Page\Page
*/
private function getOrCreatePage(SimpleXMLElement $pageElement, array $localeInfo = null)
{
Expand Down Expand Up @@ -142,7 +147,7 @@ private function getOrCreatePage(SimpleXMLElement $pageElement, array $localeInf
$parentPagePath = '/' . implode('/', $slugs);
$parent = $this->getPageByPath($parentPagePath);
if ($parent === null) {
return (t('Missing the page with path %s', $parentPagePath));
throw new MissingPageAtPathException($parentPagePath);
}
}
if ($localeInfo === null || $this->localeAlreadyExists($localeInfo)) {
Expand Down Expand Up @@ -247,10 +252,9 @@ private function importAdditionalPagePaths(SimpleXMLElement $pageElement, Page $
}

/**
* @return \Concrete\Core\Page\Page|string|null returns:
* - NULL if there's already a collection with the same handle
* - a string if the parent page doesn't exist yet
* - the newly created page otherwise
* @throws \Concrete\Core\Backup\ContentImporter\Exception\MissingPageAtPathException
*
* @return \Concrete\Core\Page\Page|null returns NULL if there's already a collection with the same handle, the newly created page otherwise
*/
private function importExternalLink(SimpleXMLElement $externalLinkElement)
{
Expand All @@ -262,7 +266,7 @@ private function importExternalLink(SimpleXMLElement $externalLinkElement)
$parentPagePath = '/' . implode('/', $slugs);
$parent = $this->getPageByPath($parentPagePath);
if ($parent === null) {
return t('Missing the page with path %s', $parentPagePath);
throw new MissingPageAtPathException($parentPagePath);
}
if ($this->parentPageHasChildWithHandle($parent, $cHandle)) {
return null;
Expand All @@ -281,10 +285,9 @@ private function importExternalLink(SimpleXMLElement $externalLinkElement)
}

/**
* @return \Concrete\Core\Page\Page|string|null returns:
* - NULL if there's already a collection with the same handle
* - a string if the parent page and/or the original page don't exist yet
* - the newly created page otherwise
* @throws \Concrete\Core\Backup\ContentImporter\Exception\MissingPageAtPathException
*
* @return \Concrete\Core\Page\Page|string|null returns NULL if there's already a collection with the same handle, the newly created page otherwise
*/
private function importAlias(SimpleXMLElement $aliasElement)
{
Expand All @@ -296,15 +299,15 @@ private function importAlias(SimpleXMLElement $aliasElement)
$parentPagePath = '/' . implode('/', $slugs);
$parentPage = $this->getPageByPath($parentPagePath);
if ($parentPage === null) {
return t('Missing the page with path %s', $parentPagePath);
throw new MissingPageAtPathException($parentPagePath);
}
if ($this->parentPageHasChildWithHandle($parentPage, $cHandle)) {
return null;
}
$originalPagePath = '/' . trim((string) $aliasElement['original-path'], '/');
$originalPage = $this->getPageByPath($originalPagePath);
if ($originalPage === null) {
return t('Missing the page with path %s', $originalPagePath);
throw new MissingPageAtPathException($originalPagePath);
}
$alias = $originalPage->createAlias($parentPage, [
'name' => (string) $aliasElement['name'],
Expand Down

0 comments on commit 51803e4

Please sign in to comment.