diff --git a/Classes/Command/MigrateNewsCommandController.php b/Classes/Command/MigrateNewsCommandController.php index 29fad9c6e8dfc3164c285a33cdd5506ac18a884a..9ab8bd93c7c591cfc79413893f885af4c5b5ff96 100644 --- a/Classes/Command/MigrateNewsCommandController.php +++ b/Classes/Command/MigrateNewsCommandController.php @@ -28,14 +28,12 @@ namespace SGalinski\SgNews\Command; use SGalinski\SgNews\Domain\Model\News; use TYPO3\CMS\Backend\FrontendBackendUserAuthentication; -use TYPO3\CMS\Backend\Utility\BackendUtility; use TYPO3\CMS\Core\Database\DatabaseConnection; use TYPO3\CMS\Core\DataHandling\DataHandler; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Mvc\Controller\CommandController; use TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException; 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 @@ -60,30 +58,62 @@ class MigrateNewsCommandController extends CommandController { protected $newsRepository; /** - * @param int $copyPageId - * @param string $copyPageId + * this array maps new pages to their original entry in the tx_news table + * + * @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 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 */ $db = $GLOBALS['TYPO3_DB']; $where = 'hidden = 0 and deleted = 0'; /** @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); $beUser = $this->simulateBackendUser(); $localCommandMap = [ 'pages' => [ $copyPageId => [ - 'copy' => 340 + 'copy' => (int) $categoryPid ] ] ]; $resultArray = []; 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; // if no l10n_parent exists, create a copy of the page @@ -96,6 +126,9 @@ class MigrateNewsCommandController extends CommandController { // get the id of the new object $newPageId = $localDataHandler->copyMappingArray['pages'][$copyPageId]; + // make entry in news map, so we can fetch it later + $this->newsPagesMap[$row['uid']] = $newPageId; + /** @var News $newsPage */ $newsPage = $this->newsRepository->findByUidIgnoreEnableFields($newPageId); @@ -109,19 +142,19 @@ class MigrateNewsCommandController extends CommandController { $this->persistenceManager->persistAll(); // update content element from the new page - $where = 'uid = ' . $newPageId; + $where = 'pid = ' . $newPageId; $db->exec_UPDATEquery('tt_content', $where, ['bodytext' => $row['bodytext']]); } } else { // this row is a translation and should simply update the content accordingly $this->updateTranslation($row); } - - return; } + + $this->pagesToMigrate = $resultArray; } /** - * Updates content element of a news translation + * Updates content element of a news translationuid * * @param array $row */ @@ -129,8 +162,24 @@ class MigrateNewsCommandController extends CommandController { /** @var DatabaseConnection $db */ $db = $GLOBALS['TYPO3_DB']; - // update content element from the new page - $where = 'pid = ' . $row['l10n_parent']; + // get entry in news map + $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']]); }