Skip to content

Commit

Permalink
Merge pull request #33 from neos/5.0
Browse files Browse the repository at this point in the history
Upmerge into main
  • Loading branch information
dlubitz authored Oct 4, 2023
2 parents e7c0781 + ae54f94 commit 8dfa9b4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Classes/Domain/Repository/RedirectRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ public function findByTargetUriPathAndHost(string $targetUriPath, ?string $host
$query = $this->entityManager->createQuery(
'SELECT r FROM Neos\RedirectHandler\DatabaseStorage\Domain\Model\Redirect r
WHERE (r.targetUriPathHash = :targetUriPathHash AND r.host = :host)
OR (r.targetUriPath LIKE :hostAndTargetUri)
');
OR (r.targetUriPath LIKE :hostAndTargetUri)'
);
$query->setParameter('targetUriPathHash', md5(trim($targetUriPath, '/')));
$query->setParameter('hostAndTargetUri', '%/' . $host . '/' . $targetUriPath);
$query->setParameter('host', $host);
Expand Down
53 changes: 38 additions & 15 deletions Classes/RedirectStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ public function addRedirect(
$comment,
$type,
$startDateTime,
$endDateTime));
$endDateTime
));
}, $hosts);
} else {
$redirects = array_merge($redirects, $this->addRedirectByHost(
Expand All @@ -187,7 +188,8 @@ public function addRedirect(
$comment,
$type,
$startDateTime,
$endDateTime));
$endDateTime
));
}
$this->emitRedirectCreated($redirects);

Expand Down Expand Up @@ -229,8 +231,17 @@ protected function addRedirectByHost(
$endDateTime->setTimezone(new \DateTimeZone(date_default_timezone_get()));
}

$redirect = new Redirect($sourceUriPath, $targetUriPath, $statusCode, $host, $creator, $comment, $type,
$startDateTime, $endDateTime);
$redirect = new Redirect(
$sourceUriPath,
$targetUriPath,
$statusCode,
$host,
$creator,
$comment,
$type,
$startDateTime,
$endDateTime
);
$updatedRedirects = $this->updateDependingRedirects($redirect);
$this->persistenceManager->persistAll();
$this->redirectRepository->add($redirect);
Expand All @@ -253,28 +264,40 @@ protected function updateDependingRedirects(RedirectInterface $newRedirect): arr
$updatedRedirects = [];

/** @var $existingRedirectForSourceUriPath Redirect */
$existingRedirectForSourceUriPath = $this->redirectRepository->findOneBySourceUriPathAndHost($newRedirect->getSourceUriPath(),
$newRedirect->getHost(), false);
$existingRedirectForSourceUriPath = $this->redirectRepository->findOneBySourceUriPathAndHost(
$newRedirect->getSourceUriPath(),
$newRedirect->getHost(),
false
);
if ($existingRedirectForSourceUriPath !== null) {
$this->removeAndLog($existingRedirectForSourceUriPath,
sprintf('Existing redirect for the source URI path "%s" removed.', $newRedirect->getSourceUriPath()));
$this->removeAndLog(
$existingRedirectForSourceUriPath,
sprintf('Existing redirect for the source URI path "%s" removed.', $newRedirect->getSourceUriPath())
);
$this->routerCachingService->flushCachesForUriPath($existingRedirectForSourceUriPath->getSourceUriPath());
}

/** @var $existingRedirectForTargetUriPath Redirect */
$existingRedirectForTargetUriPath = $this->redirectRepository->findOneBySourceUriPathAndHost($newRedirect->getTargetUriPath(),
$newRedirect->getHost(), false);
$existingRedirectForTargetUriPath = $this->redirectRepository->findOneBySourceUriPathAndHost(
$newRedirect->getTargetUriPath(),
$newRedirect->getHost(),
false
);
if ($existingRedirectForTargetUriPath !== null) {
$this->removeAndLog($existingRedirectForTargetUriPath,
sprintf('Existing redirect for the target URI path "%s" removed.', $newRedirect->getTargetUriPath()));
$this->removeAndLog(
$existingRedirectForTargetUriPath,
sprintf('Existing redirect for the target URI path "%s" removed.', $newRedirect->getTargetUriPath())
);
$this->routerCachingService->flushCachesForUriPath($existingRedirectForTargetUriPath->getSourceUriPath());
}

$absoluteUriPattern = '/^https?:\/\//i';
$newTargetIsAbsolute = preg_match($absoluteUriPattern, $newRedirect->getTargetUriPath()) === 1;

$obsoleteRedirectInstances = $this->redirectRepository->findByTargetUriPathAndHost($newRedirect->getSourceUriPath(),
$newRedirect->getHost());
$obsoleteRedirectInstances = $this->redirectRepository->findByTargetUriPathAndHost(
$newRedirect->getSourceUriPath(),
$newRedirect->getHost()
);
/** @var $obsoleteRedirect Redirect */
foreach ($obsoleteRedirectInstances as $obsoleteRedirect) {
// Remove duplicates of the newly added redirect
Expand All @@ -290,7 +313,7 @@ protected function updateDependingRedirects(RedirectInterface $newRedirect): arr
$obsoleteRedirect->setTargetUriPath($newRedirect->getTargetUriPath());
}
$this->redirectRepository->update($obsoleteRedirect);
$updatedRedirects[]= $obsoleteRedirect;
$updatedRedirects[] = $obsoleteRedirect;
}
}
return $updatedRedirects;
Expand Down
5 changes: 4 additions & 1 deletion Migrations/Mysql/Version20220912100052.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public function up(Schema $schema): void
"Migration can only be executed safely on '\Doctrine\DBAL\Platforms\MySqlPlatform'."
);

$this->addSql('DROP INDEX sourceuripathhash ON neos_redirecthandler_databasestorage_domain_model_redirect');
$indexes = $this->sm->listTableIndexes('neos_redirecthandler_databasestorage_domain_model_redirect');
if (array_key_exists('sourceuripathhash', $indexes)) {
$this->addSql('DROP INDEX sourceuripathhash ON neos_redirecthandler_databasestorage_domain_model_redirect');
}
}

public function down(Schema $schema): void
Expand Down

0 comments on commit 8dfa9b4

Please sign in to comment.