Feature: Newsarchive und Kalender inkl. Eintraege kopieren und Referenzen aktualisieren
This commit is contained in:
@@ -28,6 +28,8 @@ class DummyCopierModule extends BackendModule
|
||||
$this->Template->requestToken = $this->getCsrfToken();
|
||||
$this->Template->pageChoices = $this->getPageChoices($connection);
|
||||
$this->Template->moduleChoices = $this->getModuleChoices($connection);
|
||||
$this->Template->newsArchiveChoices = $this->getNewsArchiveChoices($connection);
|
||||
$this->Template->calendarChoices = $this->getCalendarChoices($connection);
|
||||
$this->Template->directoryChoices = $this->getDirectoryChoices();
|
||||
|
||||
$targetParentPageId = $this->parseSingleIdInput(Input::postRaw('targetParentPage'));
|
||||
@@ -36,6 +38,8 @@ class DummyCopierModule extends BackendModule
|
||||
$this->Template->selected = [
|
||||
'sourcePages' => $this->parseIdInput(Input::postRaw('sourcePages')),
|
||||
'sourceModules' => $this->parseIdInput(Input::postRaw('sourceModules')),
|
||||
'sourceNewsArchives' => $this->parseIdInput(Input::postRaw('sourceNewsArchives')),
|
||||
'sourceCalendars' => $this->parseIdInput(Input::postRaw('sourceCalendars')),
|
||||
'sourceDirectories' => $this->parsePathInput(Input::postRaw('sourceDirectories')),
|
||||
'targetParentPage' => $targetParentPageId,
|
||||
'targetDirectory' => trim((string) Input::post('targetDirectory')),
|
||||
@@ -53,6 +57,8 @@ class DummyCopierModule extends BackendModule
|
||||
$options = new DummyCopyOptions(
|
||||
$this->parseIdInput(Input::postRaw('sourcePages')),
|
||||
$this->parseIdInput(Input::postRaw('sourceModules')),
|
||||
$this->parseIdInput(Input::postRaw('sourceNewsArchives')),
|
||||
$this->parseIdInput(Input::postRaw('sourceCalendars')),
|
||||
[],
|
||||
$this->parsePathInput(Input::postRaw('sourceDirectories')),
|
||||
$targetParentPageId,
|
||||
@@ -74,10 +80,14 @@ class DummyCopierModule extends BackendModule
|
||||
$result = $copier->execute($options);
|
||||
|
||||
Message::addConfirmation(sprintf(
|
||||
'Fertig. Seiten: %d, Module: %d, Content: %d, Verzeichnisse: %d',
|
||||
'Fertig. Seiten: %d, Module: %d, Content: %d, Newsarchive: %d, Newsbeitraege: %d, Kalender: %d, Events: %d, Verzeichnisse: %d',
|
||||
$result->copiedPages,
|
||||
$result->copiedModules,
|
||||
$result->copiedContent,
|
||||
$result->copiedNewsArchives,
|
||||
$result->copiedNewsItems,
|
||||
$result->copiedCalendars,
|
||||
$result->copiedEvents,
|
||||
$result->copiedDirectories
|
||||
));
|
||||
|
||||
@@ -285,6 +295,50 @@ class DummyCopierModule extends BackendModule
|
||||
return $choices;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int,string>
|
||||
*/
|
||||
private function getNewsArchiveChoices(Connection $connection): array
|
||||
{
|
||||
$rows = $connection->fetchAllAssociative('SELECT id, title FROM tl_news_archive ORDER BY title');
|
||||
$choices = [];
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$id = (int) ($row['id'] ?? 0);
|
||||
|
||||
if ($id < 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$title = trim((string) ($row['title'] ?? ''));
|
||||
$choices[$id] = sprintf('%s [ID %d]', $title !== '' ? $title : ('Newsarchiv ' . $id), $id);
|
||||
}
|
||||
|
||||
return $choices;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int,string>
|
||||
*/
|
||||
private function getCalendarChoices(Connection $connection): array
|
||||
{
|
||||
$rows = $connection->fetchAllAssociative('SELECT id, title FROM tl_calendar ORDER BY title');
|
||||
$choices = [];
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$id = (int) ($row['id'] ?? 0);
|
||||
|
||||
if ($id < 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$title = trim((string) ($row['title'] ?? ''));
|
||||
$choices[$id] = sprintf('%s [ID %d]', $title !== '' ? $title : ('Kalender ' . $id), $id);
|
||||
}
|
||||
|
||||
return $choices;
|
||||
}
|
||||
|
||||
private function normalizeHeadline($headline): string
|
||||
{
|
||||
if (\is_string($headline)) {
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Webfarben\DummyCopier\Service;
|
||||
|
||||
use Contao\StringUtil;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
|
||||
@@ -29,6 +30,10 @@ final class DummyCopier
|
||||
$result->copiedModules = $options->copyModules ? $this->countRows('tl_module', $options->sourceModuleIds) : 0;
|
||||
$result->copiedContent = $this->estimateContentCount($options);
|
||||
$result->copiedDirectories = $options->copyDirectories ? \count($options->sourceDirectories) : 0;
|
||||
$result->copiedNewsArchives = $this->countRows('tl_news_archive', $options->sourceNewsArchiveIds);
|
||||
$result->copiedNewsItems = $this->countByPid('tl_news', 'pid', $options->sourceNewsArchiveIds);
|
||||
$result->copiedCalendars = $this->countRows('tl_calendar', $options->sourceCalendarIds);
|
||||
$result->copiedEvents = $this->countByPid('tl_calendar_events', 'pid', $options->sourceCalendarIds);
|
||||
$result->addNote('Dry-Run aktiv: Es wurden keine Daten geschrieben.');
|
||||
|
||||
return $result;
|
||||
@@ -49,6 +54,14 @@ final class DummyCopier
|
||||
}
|
||||
}
|
||||
|
||||
if ($options->sourceNewsArchiveIds !== []) {
|
||||
$result->newsArchiveMap = $this->copyNewsArchives($options->sourceNewsArchiveIds, $options->namePrefix, $result);
|
||||
}
|
||||
|
||||
if ($options->sourceCalendarIds !== []) {
|
||||
$result->calendarMap = $this->copyCalendars($options->sourceCalendarIds, $options->namePrefix, $result);
|
||||
}
|
||||
|
||||
if ($options->targetArticleId > 0) {
|
||||
foreach ($options->sourceContentIds as $sourceContentId) {
|
||||
$this->copySingleContent($sourceContentId, $options->targetArticleId, $result->moduleMap, $result);
|
||||
@@ -57,6 +70,10 @@ final class DummyCopier
|
||||
|
||||
$this->rewriteReferences($result);
|
||||
|
||||
if ($options->copyModules) {
|
||||
$this->rewriteModuleArchiveReferences($result);
|
||||
}
|
||||
|
||||
if ($options->copyDirectories) {
|
||||
$this->copyDirectories($options, $result);
|
||||
}
|
||||
@@ -287,6 +304,166 @@ final class DummyCopier
|
||||
}
|
||||
}
|
||||
|
||||
private function rewriteModuleArchiveReferences(DummyCopyResult $result): void
|
||||
{
|
||||
if ($result->moduleMap === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($result->moduleMap as $newModuleId) {
|
||||
$row = $this->fetchRow('tl_module', $newModuleId);
|
||||
|
||||
if ($row === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$updates = [];
|
||||
|
||||
if (array_key_exists('news_archives', $row) && $result->newsArchiveMap !== []) {
|
||||
$updates['news_archives'] = $this->remapSerializedIds((string) ($row['news_archives'] ?? ''), $result->newsArchiveMap);
|
||||
}
|
||||
|
||||
if (array_key_exists('cal_calendar', $row) && $result->calendarMap !== []) {
|
||||
$updates['cal_calendar'] = $this->remapSerializedIds((string) ($row['cal_calendar'] ?? ''), $result->calendarMap);
|
||||
}
|
||||
|
||||
if ($updates !== []) {
|
||||
$updates['tstamp'] = time();
|
||||
$this->connection->update('tl_module', $updates, ['id' => $newModuleId]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int> $sourceArchiveIds
|
||||
*
|
||||
* @return array<int,int>
|
||||
*/
|
||||
private function copyNewsArchives(array $sourceArchiveIds, string $prefix, DummyCopyResult $result): array
|
||||
{
|
||||
$map = [];
|
||||
|
||||
foreach ($sourceArchiveIds as $sourceArchiveId) {
|
||||
$archiveRow = $this->fetchRow('tl_news_archive', (int) $sourceArchiveId);
|
||||
|
||||
if ($archiveRow === null) {
|
||||
$result->addNote(sprintf('Newsarchiv %d nicht gefunden, wurde uebersprungen.', $sourceArchiveId));
|
||||
continue;
|
||||
}
|
||||
|
||||
unset($archiveRow['id']);
|
||||
$archiveRow['tstamp'] = time();
|
||||
|
||||
if (isset($archiveRow['title'])) {
|
||||
$archiveRow['title'] = $this->prefixed((string) $archiveRow['title'], $prefix);
|
||||
}
|
||||
|
||||
if (isset($archiveRow['jumpTo']) && isset($result->pageMap[(int) $archiveRow['jumpTo']])) {
|
||||
$archiveRow['jumpTo'] = $result->pageMap[(int) $archiveRow['jumpTo']];
|
||||
}
|
||||
|
||||
$newArchiveId = $this->insertRow('tl_news_archive', $archiveRow);
|
||||
$map[(int) $sourceArchiveId] = $newArchiveId;
|
||||
$result->copiedNewsArchives++;
|
||||
|
||||
$newsIds = $this->connection->fetchFirstColumn('SELECT id FROM tl_news WHERE pid = ? ORDER BY date, sorting, id', [(int) $sourceArchiveId]);
|
||||
|
||||
foreach ($newsIds as $newsId) {
|
||||
$newsRow = $this->fetchRow('tl_news', (int) $newsId);
|
||||
|
||||
if ($newsRow === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
unset($newsRow['id']);
|
||||
$newsRow['pid'] = $newArchiveId;
|
||||
$newsRow['tstamp'] = time();
|
||||
|
||||
if (isset($newsRow['headline'])) {
|
||||
$newsRow['headline'] = $this->prefixed((string) $newsRow['headline'], $prefix);
|
||||
}
|
||||
|
||||
if (isset($newsRow['alias']) && trim((string) $newsRow['alias']) !== '') {
|
||||
$newsRow['alias'] = $this->makeUniqueAliasInTable('tl_news', $this->prefixed((string) $newsRow['alias'], $prefix));
|
||||
}
|
||||
|
||||
if (isset($newsRow['jumpTo']) && isset($result->pageMap[(int) $newsRow['jumpTo']])) {
|
||||
$newsRow['jumpTo'] = $result->pageMap[(int) $newsRow['jumpTo']];
|
||||
}
|
||||
|
||||
$this->insertRow('tl_news', $newsRow);
|
||||
$result->copiedNewsItems++;
|
||||
}
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int> $sourceCalendarIds
|
||||
*
|
||||
* @return array<int,int>
|
||||
*/
|
||||
private function copyCalendars(array $sourceCalendarIds, string $prefix, DummyCopyResult $result): array
|
||||
{
|
||||
$map = [];
|
||||
|
||||
foreach ($sourceCalendarIds as $sourceCalendarId) {
|
||||
$calendarRow = $this->fetchRow('tl_calendar', (int) $sourceCalendarId);
|
||||
|
||||
if ($calendarRow === null) {
|
||||
$result->addNote(sprintf('Kalender %d nicht gefunden, wurde uebersprungen.', $sourceCalendarId));
|
||||
continue;
|
||||
}
|
||||
|
||||
unset($calendarRow['id']);
|
||||
$calendarRow['tstamp'] = time();
|
||||
|
||||
if (isset($calendarRow['title'])) {
|
||||
$calendarRow['title'] = $this->prefixed((string) $calendarRow['title'], $prefix);
|
||||
}
|
||||
|
||||
if (isset($calendarRow['jumpTo']) && isset($result->pageMap[(int) $calendarRow['jumpTo']])) {
|
||||
$calendarRow['jumpTo'] = $result->pageMap[(int) $calendarRow['jumpTo']];
|
||||
}
|
||||
|
||||
$newCalendarId = $this->insertRow('tl_calendar', $calendarRow);
|
||||
$map[(int) $sourceCalendarId] = $newCalendarId;
|
||||
$result->copiedCalendars++;
|
||||
|
||||
$eventIds = $this->connection->fetchFirstColumn('SELECT id FROM tl_calendar_events WHERE pid = ? ORDER BY startTime, sorting, id', [(int) $sourceCalendarId]);
|
||||
|
||||
foreach ($eventIds as $eventId) {
|
||||
$eventRow = $this->fetchRow('tl_calendar_events', (int) $eventId);
|
||||
|
||||
if ($eventRow === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
unset($eventRow['id']);
|
||||
$eventRow['pid'] = $newCalendarId;
|
||||
$eventRow['tstamp'] = time();
|
||||
|
||||
if (isset($eventRow['title'])) {
|
||||
$eventRow['title'] = $this->prefixed((string) $eventRow['title'], $prefix);
|
||||
}
|
||||
|
||||
if (isset($eventRow['alias']) && trim((string) $eventRow['alias']) !== '') {
|
||||
$eventRow['alias'] = $this->makeUniqueAliasInTable('tl_calendar_events', $this->prefixed((string) $eventRow['alias'], $prefix));
|
||||
}
|
||||
|
||||
if (isset($eventRow['jumpTo']) && isset($result->pageMap[(int) $eventRow['jumpTo']])) {
|
||||
$eventRow['jumpTo'] = $result->pageMap[(int) $eventRow['jumpTo']];
|
||||
}
|
||||
|
||||
$this->insertRow('tl_calendar_events', $eventRow);
|
||||
$result->copiedEvents++;
|
||||
}
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
private function copyDirectories(DummyCopyOptions $options, DummyCopyResult $result): void
|
||||
{
|
||||
if ($options->targetDirectory === '') {
|
||||
@@ -331,11 +508,16 @@ final class DummyCopier
|
||||
}
|
||||
|
||||
private function makeUniqueAlias(string $baseAlias): string
|
||||
{
|
||||
return $this->makeUniqueAliasInTable('tl_page', $baseAlias);
|
||||
}
|
||||
|
||||
private function makeUniqueAliasInTable(string $table, string $baseAlias): string
|
||||
{
|
||||
$alias = $this->slugify($baseAlias);
|
||||
$counter = 1;
|
||||
|
||||
while ((int) $this->connection->fetchOne('SELECT COUNT(*) FROM tl_page WHERE alias = ?', [$alias]) > 0) {
|
||||
while ((int) $this->connection->fetchOne(sprintf('SELECT COUNT(*) FROM %s WHERE alias = ?', $table), [$alias]) > 0) {
|
||||
$alias = $this->slugify($baseAlias) . '-' . $counter;
|
||||
$counter++;
|
||||
}
|
||||
@@ -385,6 +567,38 @@ final class DummyCopier
|
||||
return (int) $this->connection->fetchOne(sprintf('SELECT COUNT(*) FROM %s WHERE id IN (%s)', $table, $placeholders), $ids);
|
||||
}
|
||||
|
||||
private function countByPid(string $table, string $pidField, array $pidValues): int
|
||||
{
|
||||
if ($pidValues === []) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$placeholders = implode(',', array_fill(0, \count($pidValues), '?'));
|
||||
|
||||
return (int) $this->connection->fetchOne(sprintf('SELECT COUNT(*) FROM %s WHERE %s IN (%s)', $table, $pidField, $placeholders), $pidValues);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int,int> $idMap
|
||||
*/
|
||||
private function remapSerializedIds(string $serialized, array $idMap): string
|
||||
{
|
||||
$values = StringUtil::deserialize($serialized, true);
|
||||
$mapped = [];
|
||||
|
||||
foreach ($values as $value) {
|
||||
$id = (int) $value;
|
||||
|
||||
if ($id < 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$mapped[] = (string) ($idMap[$id] ?? $id);
|
||||
}
|
||||
|
||||
return StringUtil::serialize($mapped);
|
||||
}
|
||||
|
||||
private function estimateContentCount(DummyCopyOptions $options): int
|
||||
{
|
||||
$count = $this->countContentTree(array_map('intval', $options->sourceContentIds));
|
||||
|
||||
@@ -9,6 +9,8 @@ final class DummyCopyOptions
|
||||
public function __construct(
|
||||
public readonly array $sourcePageIds,
|
||||
public readonly array $sourceModuleIds,
|
||||
public readonly array $sourceNewsArchiveIds,
|
||||
public readonly array $sourceCalendarIds,
|
||||
public readonly array $sourceContentIds,
|
||||
public readonly array $sourceDirectories,
|
||||
public readonly int $targetParentPageId,
|
||||
|
||||
@@ -10,6 +10,10 @@ final class DummyCopyResult
|
||||
public int $copiedModules = 0;
|
||||
public int $copiedContent = 0;
|
||||
public int $copiedDirectories = 0;
|
||||
public int $copiedNewsArchives = 0;
|
||||
public int $copiedNewsItems = 0;
|
||||
public int $copiedCalendars = 0;
|
||||
public int $copiedEvents = 0;
|
||||
|
||||
/** @var array<int,int> */
|
||||
public array $pageMap = [];
|
||||
@@ -20,6 +24,12 @@ final class DummyCopyResult
|
||||
/** @var array<int,int> */
|
||||
public array $contentMap = [];
|
||||
|
||||
/** @var array<int,int> */
|
||||
public array $newsArchiveMap = [];
|
||||
|
||||
/** @var array<int,int> */
|
||||
public array $calendarMap = [];
|
||||
|
||||
/** @var array<string> */
|
||||
public array $notes = [];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user