From 0cba5ced60e9a2d3438a1c78d2f9d79f86598f54 Mon Sep 17 00:00:00 2001 From: Torsten Oppermann <torsten@sgalinski.de> Date: Tue, 24 Jul 2018 13:23:06 +0200 Subject: [PATCH] [TASK] Fixing file reference issues --- .../Command/MigrateNewsCommandController.php | 71 ++++++++++--------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/Classes/Command/MigrateNewsCommandController.php b/Classes/Command/MigrateNewsCommandController.php index 399d025..68e8390 100644 --- a/Classes/Command/MigrateNewsCommandController.php +++ b/Classes/Command/MigrateNewsCommandController.php @@ -31,6 +31,8 @@ use SGalinski\SgNews\Domain\Model\Tag; use TYPO3\CMS\Backend\FrontendBackendUserAuthentication; use TYPO3\CMS\Core\Database\DatabaseConnection; use TYPO3\CMS\Core\DataHandling\DataHandler; +use TYPO3\CMS\Core\Resource\File; +use TYPO3\CMS\Core\Resource\FileInterface; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Domain\Model\FileReference; use TYPO3\CMS\Extbase\Mvc\Controller\CommandController; @@ -178,21 +180,22 @@ class MigrateNewsCommandController extends CommandController { $newsPage->addTag($matchingTag); } - /** @var FileReference $image */ - $image = $this->getMatchingImage($row); - if ($image !== NULL) { - $newsPage->addTeaser1Image($image); - - $originalResource = $image->getOriginalResource(); - if ($originalResource !== NULL) { - $originalFile = $originalResource->getOriginalFile(); - if ($originalFile !== NULL) { - $teaserImage2 = $this->fileReferenceRepository->addFileReferenceFromFile( - $originalFile, $this->newsPagesMap[$row['uid']], - $this->newsPagesMap[$row['uid']], 'pages', 'tx_sgnews_teaser2_image' - ); - $newsPage->addTeaser2Image($teaserImage2); - } + /** @var File $image */ + $file = $this->getMatchingFile($row); + if ($file instanceof File) { + $teaserImage1 = $this->fileReferenceRepository->addFileReferenceFromFile( + $file, $this->newsPagesMap[$row['uid']], + $this->newsPagesMap[$row['uid']], 'pages', 'tx_sgnews_teaser1_image' + ); + + if ($teaserImage1) { + $newsPage->addTeaser1Image($teaserImage1); + + $teaserImage2 = $this->fileReferenceRepository->addFileReferenceFromFile( + $file, $this->newsPagesMap[$row['uid']], + $this->newsPagesMap[$row['uid']], 'pages', 'tx_sgnews_teaser2_image' + ); + $newsPage->addTeaser2Image($teaserImage2); } } @@ -213,33 +216,37 @@ class MigrateNewsCommandController extends CommandController { * Get the image file matching the news * * @param array $row - * @return null|FileReference + * @return null|FileInterface + * @throws \Exception */ - private function getMatchingImage(array $row) { + private function getMatchingFile(array $row) { + // match old page id with old file reference id /** @var DatabaseConnection $db */ $db = $GLOBALS['TYPO3_DB']; - $where = 'tablenames = "tx_news_domain_model_news" AND fieldname = "fal_media" AND uid_foreign = ' . $row['uid']; - + $where = 'tablenames = "tx_news_domain_model_news" AND fieldname = "fal_media" AND uid_foreign = ' . $this->newsPagesMap[$row['uid']]; /** @var \mysqli_result $result */ - $result = $db->exec_SELECTgetSingleRow('uid, uid_local', 'sys_file_reference', $where); - if (!$result) { + $fileReferenceResult = $db->exec_SELECTgetSingleRow( + 'uid, uid_local', 'sys_file_reference_news_migration', $where + ); + if (!$fileReferenceResult) { return NULL; } - $where = 'uid = ' . $result['uid']; - $db->exec_UPDATEquery( - 'sys_file_reference', - $where, - ['pid' => $this->newsPagesMap[$row['uid']], 'uid_foreign' => $this->newsPagesMap[$row['uid']], 'tablenames' => 'pages', 'fieldname' => 'tx_sgnews_teaser1_image'] - ); + $where = 'uid = ' . $fileReferenceResult['uid_local']; + /** @var \mysqli_result $result */ + $fileResult = $db->exec_SELECTgetSingleRow('identifier', 'sys_file_news_migration', $where); + if (!$fileResult) { + return NULL; + } - $result = $this->fileReferenceRepository->findByUid((int) $result['uid']); - if ($result instanceof FileReference) { - $result->setPid($this->newsPagesMap[$row['uid']]); - return $result; + $oldIdentifier = $fileResult['identifier']; + $resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance(); + $storage = $resourceFactory->getStorageObject(1); + if (!$storage->hasFile($oldIdentifier)) { + return NULL; } - return NULL; + return $storage->getFile($oldIdentifier); } /** -- GitLab