Skip to content
Snippets Groups Projects
Commit 1554b6a1 authored by Torsten Oppermann's avatar Torsten Oppermann
Browse files

[TASK] implemented content element mapping

parent fca7883e
No related branches found
No related tags found
1 merge request!6Feature news migration
...@@ -28,14 +28,12 @@ namespace SGalinski\SgNews\Command; ...@@ -28,14 +28,12 @@ namespace SGalinski\SgNews\Command;
use SGalinski\SgNews\Domain\Model\News; use SGalinski\SgNews\Domain\Model\News;
use TYPO3\CMS\Backend\FrontendBackendUserAuthentication; use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Database\DatabaseConnection; use TYPO3\CMS\Core\Database\DatabaseConnection;
use TYPO3\CMS\Core\DataHandling\DataHandler; use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController; use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
use TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException; use TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException;
use TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException; use TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException;
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
/** /**
* Command controller, that migrates data from tx_news to sg_news * Command controller, that migrates data from tx_news to sg_news
...@@ -60,30 +58,62 @@ class MigrateNewsCommandController extends CommandController { ...@@ -60,30 +58,62 @@ class MigrateNewsCommandController extends CommandController {
protected $newsRepository; protected $newsRepository;
/** /**
* @param int $copyPageId * this array maps new pages to their original entry in the tx_news table
* @param string $copyPageId *
* @var array $newsPagesMap
*/
protected $newsPagesMap = [];
/**
* this array contains all pages that should be migrated to sg_news
*
* @var array $pagesToMigrate
*/
protected $pagesToMigrate = [];
/**
* @var array $languageMap
*/
protected $languageMap = [];
/**
* @param string $copyPageId the page id of the template that should be copied
* @param int $categoryPid the page id of the category page
* @param int $year only news from that year will be migrated
* @param string $languageMapAsJson a json, mapping language ids (old => new). this is needed if the sys_language_uids have changed
*
* @throws IllegalObjectTypeException * @throws IllegalObjectTypeException
* @throws UnknownObjectException * @throws UnknownObjectException
*/ */
public function runMigrateNewsCommand($copyPageId, $categoryPids = '340') { public function runMigrateNewsCommand(
$copyPageId, $categoryPid, $year = 2015,
$languageMapAsJson = '{"3":1,"1":0,"2":2,"0":3}'
) {
$this->languageMap = json_decode($languageMapAsJson, TRUE);
/** @var DatabaseConnection $db */ /** @var DatabaseConnection $db */
$db = $GLOBALS['TYPO3_DB']; $db = $GLOBALS['TYPO3_DB'];
$where = 'hidden = 0 and deleted = 0'; $where = 'hidden = 0 and deleted = 0';
/** @var \mysqli_result $result */ /** @var \mysqli_result $result */
$result = $db->exec_SELECTquery('*', 'tx_news_domain_model_news', $where, '', '', '50'); $result = $db->exec_SELECTquery('*', 'tx_news_domain_model_news', $where);
$localDataHandler = GeneralUtility::makeInstance(DataHandler::class); $localDataHandler = GeneralUtility::makeInstance(DataHandler::class);
$beUser = $this->simulateBackendUser(); $beUser = $this->simulateBackendUser();
$localCommandMap = [ $localCommandMap = [
'pages' => [ 'pages' => [
$copyPageId => [ $copyPageId => [
'copy' => 340 'copy' => (int) $categoryPid
] ]
] ]
]; ];
$resultArray = []; $resultArray = [];
while ($row = $result->fetch_assoc()) { while ($row = $result->fetch_assoc()) {
// ignore the entry if its not within the given year
if ((int) date('Y', ($row['datetime'])) !== $year) {
continue;
}
$resultArray[] = $row; $resultArray[] = $row;
// if no l10n_parent exists, create a copy of the page // if no l10n_parent exists, create a copy of the page
...@@ -96,6 +126,9 @@ class MigrateNewsCommandController extends CommandController { ...@@ -96,6 +126,9 @@ class MigrateNewsCommandController extends CommandController {
// get the id of the new object // get the id of the new object
$newPageId = $localDataHandler->copyMappingArray['pages'][$copyPageId]; $newPageId = $localDataHandler->copyMappingArray['pages'][$copyPageId];
// make entry in news map, so we can fetch it later
$this->newsPagesMap[$row['uid']] = $newPageId;
/** @var News $newsPage */ /** @var News $newsPage */
$newsPage = $this->newsRepository->findByUidIgnoreEnableFields($newPageId); $newsPage = $this->newsRepository->findByUidIgnoreEnableFields($newPageId);
...@@ -109,19 +142,19 @@ class MigrateNewsCommandController extends CommandController { ...@@ -109,19 +142,19 @@ class MigrateNewsCommandController extends CommandController {
$this->persistenceManager->persistAll(); $this->persistenceManager->persistAll();
// update content element from the new page // update content element from the new page
$where = 'uid = ' . $newPageId; $where = 'pid = ' . $newPageId;
$db->exec_UPDATEquery('tt_content', $where, ['bodytext' => $row['bodytext']]); $db->exec_UPDATEquery('tt_content', $where, ['bodytext' => $row['bodytext']]);
} }
} else { // this row is a translation and should simply update the content accordingly } else { // this row is a translation and should simply update the content accordingly
$this->updateTranslation($row); $this->updateTranslation($row);
} }
return;
} }
$this->pagesToMigrate = $resultArray;
} }
/** /**
* Updates content element of a news translation * Updates content element of a news translationuid
* *
* @param array $row * @param array $row
*/ */
...@@ -129,8 +162,24 @@ class MigrateNewsCommandController extends CommandController { ...@@ -129,8 +162,24 @@ class MigrateNewsCommandController extends CommandController {
/** @var DatabaseConnection $db */ /** @var DatabaseConnection $db */
$db = $GLOBALS['TYPO3_DB']; $db = $GLOBALS['TYPO3_DB'];
// update content element from the new page // get entry in news map
$where = 'pid = ' . $row['l10n_parent']; $parentId = $this->newsPagesMap[$row['l10n_parent']];
// get content element from the original page
$where = 'pid = ' . (int) $parentId;
/** @var \mysqli_result $result */
$result = $db->exec_SELECTquery('l18n_parent', 'tt_content', $where);
$originalContentElement = $result->fetch_row();
$where = 'l18n_parent = ' . (int) $originalContentElement[0];
// look up the correct language id, if they have changed
if ($this->languageMap[(int) $row['sys_language_uid']]) {
$where .= ' AND sys_language_uid = ' . $this->languageMap[(int) $row['sys_language_uid']];
} else {
$where .= ' AND sys_language_uid = ' . (int) $row['sys_language_uid'];
}
$db->exec_UPDATEquery('tt_content', $where, ['bodytext' => $row['bodytext']]); $db->exec_UPDATEquery('tt_content', $where, ['bodytext' => $row['bodytext']]);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment