diff --git a/CHANGELOG.md b/CHANGELOG.md index 69c91f18..1d029397 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [v2.1.42](https://github.com/roadiz/core-bundle-dev-app/compare/v2.1.41...v2.1.42) (2023-09-04) + + +### Bug Fixes + +* **NodeNameChecker:** Limit generated unique nodeName to 250 chars, no matter suffix added to it ([bb1e271](https://github.com/roadiz/core-bundle-dev-app/commit/bb1e27178cb524f74152f063e8d0105ed7df17ef)) + ## [v2.1.41](https://github.com/roadiz/core-bundle-dev-app/compare/v2.1.40...v2.1.41) (2023-08-04) diff --git a/lib/RoadizCoreBundle/config/services.yaml b/lib/RoadizCoreBundle/config/services.yaml index f5562640..6f0f6530 100644 --- a/lib/RoadizCoreBundle/config/services.yaml +++ b/lib/RoadizCoreBundle/config/services.yaml @@ -1,6 +1,6 @@ --- parameters: - roadiz_core.cms_version: '2.1.41' + roadiz_core.cms_version: '2.1.42' roadiz_core.cms_version_prefix: 'main' env(APP_NAMESPACE): "roadiz" env(APP_VERSION): "0.1.0" diff --git a/lib/RoadizCoreBundle/src/EventSubscriber/NodeNameSubscriber.php b/lib/RoadizCoreBundle/src/EventSubscriber/NodeNameSubscriber.php index bad16cf7..17517016 100644 --- a/lib/RoadizCoreBundle/src/EventSubscriber/NodeNameSubscriber.php +++ b/lib/RoadizCoreBundle/src/EventSubscriber/NodeNameSubscriber.php @@ -67,7 +67,7 @@ public function onBeforeUpdate( $testingNodeName = $this->nodeNamePolicy->getCanonicalNodeName($nodeSource); /* - * Node name wont be updated if name already taken OR + * Node name won't be updated if name already taken OR * if it is ALREADY suffixed with a unique ID. */ if ( diff --git a/lib/RoadizCoreBundle/src/Node/NodeNameChecker.php b/lib/RoadizCoreBundle/src/Node/NodeNameChecker.php index 594b633d..e85ef268 100644 --- a/lib/RoadizCoreBundle/src/Node/NodeNameChecker.php +++ b/lib/RoadizCoreBundle/src/Node/NodeNameChecker.php @@ -14,6 +14,7 @@ class NodeNameChecker implements NodeNamePolicyInterface { + public const MAX_LENGTH = 250; protected bool $useTypedSuffix; private ManagerRegistry $managerRegistry; @@ -29,38 +30,72 @@ public function __construct(ManagerRegistry $managerRegistry, bool $useTypedSuff public function getCanonicalNodeName(NodesSources $nodeSource): string { + $nodeTypeSuffix = StringHandler::slugify($nodeSource->getNodeTypeName()); if ($nodeSource->getTitle() !== '') { + $title = StringHandler::slugify($nodeSource->getTitle()); if ($nodeSource->isReachable() || !$this->useTypedSuffix) { - return StringHandler::slugify($nodeSource->getTitle()); + // truncate title to 250 chars if needed + if (strlen($title) > self::MAX_LENGTH) { + $title = substr($title, 0, self::MAX_LENGTH); + } + return $title; + } + // truncate title if title + suffix + 1 exceed 250 chars + if ((strlen($title) + strlen($nodeTypeSuffix) + 1) > self::MAX_LENGTH) { + $title = substr($title, 0, self::MAX_LENGTH - (strlen($nodeTypeSuffix) + 1)); } return sprintf( '%s-%s', - StringHandler::slugify($nodeSource->getTitle()), - StringHandler::slugify($nodeSource->getNodeTypeName()), + $title, + $nodeTypeSuffix, ); } return sprintf( '%s-%s', - StringHandler::slugify($nodeSource->getNodeTypeName()), + $nodeTypeSuffix, null !== $nodeSource->getNode() ? $nodeSource->getNode()->getId() : $nodeSource->getId() ); } public function getSafeNodeName(NodesSources $nodeSource): string { + $canonicalNodeName = $this->getCanonicalNodeName($nodeSource); + $uniqueId = uniqid(); + + // truncate canonicalNodeName if canonicalNodeName + uniqueId + 1 exceed 250 chars + if ((strlen($canonicalNodeName) + strlen($uniqueId) + 1) > self::MAX_LENGTH) { + $canonicalNodeName = substr( + $canonicalNodeName, + 0, + self::MAX_LENGTH - (strlen($uniqueId) + 1) + ); + } + return sprintf( '%s-%s', - $this->getCanonicalNodeName($nodeSource), - uniqid() + $canonicalNodeName, + $uniqueId ); } public function getDatestampedNodeName(NodesSources $nodeSource): string { + $canonicalNodeName = $this->getCanonicalNodeName($nodeSource); + $timestamp = $nodeSource->getPublishedAt()->format('Y-m-d'); + + // truncate canonicalNodeName if canonicalNodeName + uniqueId + 1 exceed 250 chars + if ((strlen($canonicalNodeName) + strlen($timestamp) + 1) > self::MAX_LENGTH) { + $canonicalNodeName = substr( + $canonicalNodeName, + 0, + self::MAX_LENGTH - (strlen($timestamp) + 1) + ); + } + return sprintf( '%s-%s', - $this->getCanonicalNodeName($nodeSource), - $nodeSource->getPublishedAt()->format('Y-m-d') + $canonicalNodeName, + $timestamp ); } @@ -116,8 +151,8 @@ public function isNodeNameAlreadyUsed(string $nodeName): bool ->setDisplayingNotPublishedNodes(true); if ( - false === (bool) $urlAliasRepo->exists($nodeName) && - false === (bool) $nodeRepo->exists($nodeName) + false === $urlAliasRepo->exists($nodeName) && + false === $nodeRepo->exists($nodeName) ) { return false; }