Skip to content

Commit

Permalink
Merge branch 'hotfix/v2.1.42'
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Sep 4, 2023
2 parents ff6e44a + 1285f39 commit ad15ace
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)


Expand Down
2 changes: 1 addition & 1 deletion lib/RoadizCoreBundle/config/services.yaml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
55 changes: 45 additions & 10 deletions lib/RoadizCoreBundle/src/Node/NodeNameChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

class NodeNameChecker implements NodeNamePolicyInterface
{
public const MAX_LENGTH = 250;
protected bool $useTypedSuffix;
private ManagerRegistry $managerRegistry;

Expand All @@ -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
);
}

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit ad15ace

Please sign in to comment.