Commit f17ea510 authored by Guido Schmechel's avatar Guido Schmechel Committed by Benni Mack
Browse files

[BUGFIX] Auto redirect resolve target page

When renaming pages, the pages in the automatic
redirect are now saved with the TYPO3 protocol.

Resolves: #89327
Releases: master, 10.4
Change-Id: I0207b4c4b81381f85b7522a130d17cbd20fe8b58
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/68202

Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Tested-by: Stefan Bürk's avatarStefan Bürk <stefan@buerk.tech>
Tested-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Reviewed-by: Stefan Bürk's avatarStefan Bürk <stefan@buerk.tech>
Reviewed-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
parent f7b7507c
......@@ -33,6 +33,7 @@ use TYPO3\CMS\Core\DataHandling\Model\CorrelationId;
use TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory;
use TYPO3\CMS\Core\DataHandling\SlugHelper;
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
use TYPO3\CMS\Core\LinkHandling\LinkService;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Site\Entity\SiteInterface;
use TYPO3\CMS\Core\Site\SiteFinder;
......@@ -71,6 +72,8 @@ class SlugService implements LoggerAwareInterface
*/
protected $pageRepository;
protected LinkService $linkService;
/**
* @var CorrelationId|string
*/
......@@ -101,11 +104,12 @@ class SlugService implements LoggerAwareInterface
*/
protected $httpStatusCode;
public function __construct(Context $context, SiteFinder $siteFinder, PageRepository $pageRepository)
public function __construct(Context $context, SiteFinder $siteFinder, PageRepository $pageRepository, LinkService $linkService)
{
$this->context = $context;
$this->siteFinder = $siteFinder;
$this->pageRepository = $pageRepository;
$this->linkService = $linkService;
}
public function rebuildSlugsForSlugChange(int $pageId, string $currentSlug, string $newSlug, CorrelationId $correlationId): void
......@@ -119,7 +123,7 @@ class SlugService implements LoggerAwareInterface
if ($this->autoUpdateSlugs || $this->autoCreateRedirects) {
$this->createCorrelationIds($pageId, $correlationId);
if ($this->autoCreateRedirects) {
$this->createRedirect($currentSlug, $newSlug, (int)$currentPageRecord['sys_language_uid'], (int)$pageId);
$this->createRedirect($currentSlug, (int)$currentPageRecord['uid'], (int)$currentPageRecord['sys_language_uid'], (int)$pageId);
}
if ($this->autoUpdateSlugs) {
$this->checkSubPages($currentPageRecord, $currentSlug, $newSlug);
......@@ -152,13 +156,18 @@ class SlugService implements LoggerAwareInterface
$this->correlationIdSlugUpdate = $correlationId->withAspects(self::CORRELATION_ID_IDENTIFIER, 'slug');
}
protected function createRedirect(string $originalSlug, string $newSlug, int $languageId, int $pid): void
protected function createRedirect(string $originalSlug, int $pageId, int $languageId, int $pid): void
{
$basePath = rtrim($this->site->getLanguageById($languageId)->getBase()->getPath(), '/');
/** @var DateTimeAspect $date */
$date = $this->context->getAspect('date');
$endtime = $date->getDateTime()->modify('+' . $this->redirectTTL . ' days');
$targetLink = $this->linkService->asString([
'type' => 'page',
'pageuid' => $pageId,
'parameters' => '_language=' . $languageId
]);
$record = [
'pid' => $pid,
'updatedon' => $date->get('timestamp'),
......@@ -173,7 +182,7 @@ class SlugService implements LoggerAwareInterface
'is_regexp' => 0,
'force_https' => 0,
'respect_query_parameters' => 0,
'target' => $basePath . $newSlug,
'target' => $targetLink,
'target_statuscode' => $this->httpStatusCode,
'hitcount' => 0,
'lasthiton' => 0,
......@@ -197,7 +206,7 @@ class SlugService implements LoggerAwareInterface
foreach ($subPageRecords as $subPageRecord) {
$newSlug = $this->updateSlug($subPageRecord, $oldSlugOfParentPage, $newSlugOfParentPage);
if ($newSlug !== null && $this->autoCreateRedirects) {
$this->createRedirect($subPageRecord['slug'], $newSlug, $languageUid, $pageId);
$this->createRedirect($subPageRecord['slug'], (int)$subPageRecord['uid'], $languageUid, $pageId);
}
}
}
......
......@@ -22,6 +22,7 @@ use TYPO3\CMS\Core\Configuration\SiteConfiguration;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\DataHandling\Model\CorrelationId;
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
use TYPO3\CMS\Core\LinkHandling\LinkService;
use TYPO3\CMS\Core\Routing\SiteMatcher;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
......@@ -118,10 +119,10 @@ class SlugServiceTest extends FunctionalTestCase
// This redirects should exists, after rebuildSlugsForSlugChange() has run
$redirects = [
['source_path' => '/dummy-1-2', 'target' => '/test-new'],
['source_path' => '/dummy-1-2/dummy-1-2-5', 'target' => '/test-new/dummy-1-2-5'],
['source_path' => '/dummy-1-2/dummy-1-2-6', 'target' => '/test-new/dummy-1-2-6'],
['source_path' => '/dummy-1-2/dummy-1-2-7', 'target' => '/test-new/dummy-1-2-7'],
['source_path' => '/dummy-1-2', 'target' => 't3://page?uid=2&_language=0'],
['source_path' => '/dummy-1-2/dummy-1-2-5', 'target' => 't3://page?uid=5&_language=0'],
['source_path' => '/dummy-1-2/dummy-1-2-6', 'target' => 't3://page?uid=6&_language=0'],
['source_path' => '/dummy-1-2/dummy-1-2-7', 'target' => 't3://page?uid=7&_language=0'],
];
$this->assertSlugsAndRedirectsExists($slugs, $redirects);
......@@ -157,16 +158,16 @@ class SlugServiceTest extends FunctionalTestCase
// This redirects should exists, after rebuildSlugsForSlugChange() has run
$redirects = [
['source_path' => '/', 'target' => '/new-home'],
['source_path' => '/dummy-1-2', 'target' => '/new-home/dummy-1-2'],
['source_path' => '/dummy-1-3', 'target' => '/new-home/dummy-1-3'],
['source_path' => '/dummy-1-4', 'target' => '/new-home/dummy-1-4'],
['source_path' => '/dummy-1-2/dummy-1-2-5', 'target' => '/new-home/dummy-1-2/dummy-1-2-5'],
['source_path' => '/dummy-1-2/dummy-1-2-6', 'target' => '/new-home/dummy-1-2/dummy-1-2-6'],
['source_path' => '/dummy-1-2/dummy-1-2-7', 'target' => '/new-home/dummy-1-2/dummy-1-2-7'],
['source_path' => '/dummy-1-3/dummy-1-3-8', 'target' => '/new-home/dummy-1-3/dummy-1-3-8'],
['source_path' => '/dummy-1-3/dummy-1-3-9', 'target' => '/new-home/dummy-1-3/dummy-1-3-9'],
['source_path' => '/dummy-1-4/dummy-1-4-10', 'target' => '/new-home/dummy-1-4/dummy-1-4-10'],
['source_path' => '/', 'target' => 't3://page?uid=1&_language=0'],
['source_path' => '/dummy-1-2', 'target' => 't3://page?uid=2&_language=0'],
['source_path' => '/dummy-1-3', 'target' => 't3://page?uid=3&_language=0'],
['source_path' => '/dummy-1-4', 'target' => 't3://page?uid=4&_language=0'],
['source_path' => '/dummy-1-2/dummy-1-2-5', 'target' => 't3://page?uid=5&_language=0'],
['source_path' => '/dummy-1-2/dummy-1-2-6', 'target' => 't3://page?uid=6&_language=0'],
['source_path' => '/dummy-1-2/dummy-1-2-7', 'target' => 't3://page?uid=7&_language=0'],
['source_path' => '/dummy-1-3/dummy-1-3-8', 'target' => 't3://page?uid=8&_language=0'],
['source_path' => '/dummy-1-3/dummy-1-3-9', 'target' => 't3://page?uid=9&_language=0'],
['source_path' => '/dummy-1-4/dummy-1-4-10', 'target' => 't3://page?uid=10&_language=0'],
];
$this->assertSlugsAndRedirectsExists($slugs, $redirects);
......@@ -202,10 +203,10 @@ class SlugServiceTest extends FunctionalTestCase
// This redirects should exists, after rebuildSlugsForSlugChange() has run
$redirects = [
['source_path' => '/sub-folder/dummy-1-2', 'target' => '/sub-folder/test-new'],
['source_path' => '/sub-folder/dummy-1-2/dummy-1-2-5', 'target' => '/sub-folder/test-new/dummy-1-2-5'],
['source_path' => '/sub-folder/dummy-1-2/dummy-1-2-6', 'target' => '/sub-folder/test-new/dummy-1-2-6'],
['source_path' => '/sub-folder/dummy-1-2/dummy-1-2-7', 'target' => '/sub-folder/test-new/dummy-1-2-7'],
['source_path' => '/sub-folder/dummy-1-2', 'target' => 't3://page?uid=2&_language=0'],
['source_path' => '/sub-folder/dummy-1-2/dummy-1-2-5', 'target' => 't3://page?uid=5&_language=0'],
['source_path' => '/sub-folder/dummy-1-2/dummy-1-2-6', 'target' => 't3://page?uid=6&_language=0'],
['source_path' => '/sub-folder/dummy-1-2/dummy-1-2-7', 'target' => 't3://page?uid=7&_language=0'],
];
$this->assertSlugsAndRedirectsExists($slugs, $redirects);
......@@ -243,8 +244,8 @@ class SlugServiceTest extends FunctionalTestCase
// This redirects should exists, after rebuildSlugsForSlugChange() has run
$redirects = [
['source_path' => '/de/dummy-1-3', 'target' => '/de/test-new'],
['source_path' => '/de/dummy-1-3/dummy-1-3-8', 'target' => '/de/test-new/dummy-1-3-8'],
['source_path' => '/de/dummy-1-3', 'target' => 't3://page?uid=31&_language=1'],
['source_path' => '/de/dummy-1-3/dummy-1-3-8', 'target' => 't3://page?uid=32&_language=1'],
];
$this->assertSlugsAndRedirectsExists($slugs, $redirects);
......@@ -282,8 +283,8 @@ class SlugServiceTest extends FunctionalTestCase
// This redirects should exists, after rebuildSlugsForSlugChange() has run
$redirects = [
['source_path' => '/sub-folder/de/dummy-1-3', 'target' => '/sub-folder/de/test-new'],
['source_path' => '/sub-folder/de/dummy-1-3/dummy-1-3-8', 'target' => '/sub-folder/de/test-new/dummy-1-3-8'],
['source_path' => '/sub-folder/de/dummy-1-3', 'target' => 't3://page?uid=31&_language=1'],
['source_path' => '/sub-folder/de/dummy-1-3/dummy-1-3-8', 'target' => 't3://page?uid=32&_language=1'],
];
$this->assertSlugsAndRedirectsExists($slugs, $redirects);
......@@ -317,10 +318,10 @@ class SlugServiceTest extends FunctionalTestCase
// This redirects should exists, after rebuildSlugsForSlugChange() has run
$redirects = [
['source_path' => '/de/', 'target' => '/de/test-new'],
['source_path' => '/de/dummy-1-2', 'target' => '/de/test-new/dummy-1-2'],
['source_path' => '/de/dummy-1-3', 'target' => '/de/test-new/dummy-1-3'],
['source_path' => '/de/dummy-1-2/dummy-1-2-3', 'target' => '/de/test-new/dummy-1-2/dummy-1-2-3'],
['source_path' => '/de/', 'target' => 't3://page?uid=5&_language=1'],
['source_path' => '/de/dummy-1-2', 'target' => 't3://page?uid=6&_language=1'],
['source_path' => '/de/dummy-1-3', 'target' => 't3://page?uid=7&_language=1'],
['source_path' => '/de/dummy-1-2/dummy-1-2-3', 'target' => 't3://page?uid=8&_language=1'],
];
$this->assertSlugsAndRedirectsExists($slugs, $redirects);
......@@ -374,7 +375,8 @@ class SlugServiceTest extends FunctionalTestCase
$this->subject = new SlugService(
GeneralUtility::makeInstance(Context::class),
GeneralUtility::makeInstance(SiteFinder::class),
GeneralUtility::makeInstance(PageRepository::class)
GeneralUtility::makeInstance(PageRepository::class),
GeneralUtility::makeInstance(LinkService::class)
);
$this->subject->setLogger(new NullLogger());
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment