Skip to content

Commit

Permalink
fix(NodeNameChecker): Limit generated unique nodeName to 250 chars, n…
Browse files Browse the repository at this point in the history
…o matter suffix added to it
  • Loading branch information
ambroisemaupate committed Sep 4, 2023
1 parent ff6e44a commit bb1e271
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
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 bb1e271

Please sign in to comment.