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

[TASk] Finish file mapping and solvling repair translation issue

parent fe3c5bcd
No related branches found
No related tags found
1 merge request!6Feature news migration
...@@ -32,15 +32,16 @@ use TYPO3\CMS\Backend\FrontendBackendUserAuthentication; ...@@ -32,15 +32,16 @@ use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
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\Domain\Model\FileReference;
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\Frontend\Controller\TypoScriptFrontendController;
/** /**
* Command controller, that migrates data from tx_news to sg_news * Command controller, that migrates data from tx_news to sg_news
*/ */
class MigrateNewsCommandController extends CommandController { class MigrateNewsCommandController extends CommandController {
/** /**
* @var bool * @var bool
*/ */
...@@ -62,7 +63,13 @@ class MigrateNewsCommandController extends CommandController { ...@@ -62,7 +63,13 @@ class MigrateNewsCommandController extends CommandController {
* @var \SGalinski\SgNews\Domain\Repository\TagRepository * @var \SGalinski\SgNews\Domain\Repository\TagRepository
* @inject * @inject
*/ */
protected $tagRepository; private $tagRepository;
/**
* @var \SGalinski\SgNews\Domain\Repository\FileReferenceRepository
* @inject
*/
private $fileReferenceRepository;
/** /**
* this array maps new pages to their original entry in the tx_news table * this array maps new pages to their original entry in the tx_news table
...@@ -97,12 +104,21 @@ class MigrateNewsCommandController extends CommandController { ...@@ -97,12 +104,21 @@ class MigrateNewsCommandController extends CommandController {
* *
* @throws IllegalObjectTypeException * @throws IllegalObjectTypeException
* @throws UnknownObjectException * @throws UnknownObjectException
* @throws \Exception
*/ */
public function runMigrateNewsCommand( public function runMigrateNewsCommand(
$copyPageId, $categoryPid, $year = 2015, $copyPageId, $categoryPid, $year = 2015,
$languageMapAsJson = '{"3":1,"1":0,"2":2,"0":3}', $languageMapAsJson = '{"3":1,"1":0,"2":2,"0":3}',
$categoryMapAsJson = '{"2":10,"3":11,"4":12,"5":13,"6":14,"7":15,"8":16,"9":17}' $categoryMapAsJson = '{"2":10,"3":11,"4":12,"5":13,"6":14,"7":15,"8":16,"9":17}'
) { ) {
// fix repair translation bug where tsfee is missing from command controller, can be removed when v1.5 is released
if (!$GLOBALS['TSFE']) {
/** @var TypoScriptFrontendController $typoScriptController */
$GLOBALS['TSFE'] = $typoScriptController = $this->objectManager->get(
TypoScriptFrontendController::class, $GLOBALS['TYPO3_CONF_VARS'], 0, 0
);
}
$this->languageMap = json_decode($languageMapAsJson, TRUE); $this->languageMap = json_decode($languageMapAsJson, TRUE);
$this->categoryMap = json_decode($categoryMapAsJson, TRUE); $this->categoryMap = json_decode($categoryMapAsJson, TRUE);
...@@ -126,7 +142,7 @@ class MigrateNewsCommandController extends CommandController { ...@@ -126,7 +142,7 @@ class MigrateNewsCommandController extends CommandController {
$resultArray = []; $resultArray = [];
while ($row = $result->fetch_assoc()) { while ($row = $result->fetch_assoc()) {
// ignore the entry if its not within the given year // ignore the entry if its not within the given year
if ((int) date('Y', ($row['datetime'])) !== $year) { if ((int) date('Y', $row['datetime']) !== $year) {
continue; continue;
} }
$resultArray[] = $row; $resultArray[] = $row;
...@@ -150,12 +166,34 @@ class MigrateNewsCommandController extends CommandController { ...@@ -150,12 +166,34 @@ class MigrateNewsCommandController extends CommandController {
if ($newsPage !== NULL) { if ($newsPage !== NULL) {
$title = date('Y-m-d', $row['datetime']) . ' - ' . $row['title']; $title = date('Y-m-d', $row['datetime']) . ' - ' . $row['title'];
$newsPage->setTitle($title); $newsPage->setTitle($title);
$newsPage->setSubtitle($row['title']);
$date = new \DateTime('@' . $row['datetime']);
$newsPage->setLastUpdated($date);
$matchingTag = $this->getMatchingTag($row); $matchingTag = $this->getMatchingTag($row);
if ($matchingTag && $matchingTag instanceof Tag) { if ($matchingTag && $matchingTag instanceof Tag) {
$newsPage->addTag($matchingTag); $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);
}
}
}
$this->newsRepository->update($newsPage); $this->newsRepository->update($newsPage);
$this->persistenceManager->persistAll(); $this->persistenceManager->persistAll();
...@@ -170,11 +208,43 @@ class MigrateNewsCommandController extends CommandController { ...@@ -170,11 +208,43 @@ class MigrateNewsCommandController extends CommandController {
} }
/** /**
* Get the image file matching the news
*
* @param array $row * @param array $row
* @return Object $tag * @return null|FileReference
*/
private function getMatchingImage(array $row) {
/** @var DatabaseConnection $db */
$db = $GLOBALS['TYPO3_DB'];
$where = 'tablenames = "tx_news_domain_model_news" AND fieldname = "fal_media" AND uid_foreign = ' . $row['uid'];
/** @var \mysqli_result $result */
$result = $db->exec_SELECTgetSingleRow('uid, uid_local', 'sys_file_reference', $where);
if (!$result) {
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']
);
$result = $this->fileReferenceRepository->findByUid((int) $result['uid']);
if ($result instanceof FileReference) {
$result->setPid($this->newsPagesMap[$row['uid']]);
return $result;
}
return NULL;
}
/**
* Get the tag / category, matching the news
* *
* @throws IllegalObjectTypeException * @param array $row
* @throws UnknownObjectException * @return Object $tag
*/ */
private function getMatchingTag(array $row) { private function getMatchingTag(array $row) {
// look up the correct category id, if they have changed // look up the correct category id, if they have changed
...@@ -227,17 +297,24 @@ class MigrateNewsCommandController extends CommandController { ...@@ -227,17 +297,24 @@ class MigrateNewsCommandController extends CommandController {
$where = 'uid = ' . (int) $parentId; $where = 'uid = ' . (int) $parentId;
/** @var \mysqli_result $result */ /** @var \mysqli_result $result */
$result = $db->exec_SELECTquery('title', 'pages', $where); $result = $db->exec_SELECTgetSingleRow('title, subtitle', 'pages', $where);
if ($result) {
$where = 'pid = ' . (int) $parentId . ' AND sys_language_uid = ' . $this->languageMap[0]; $where = 'pid = ' . (int) $parentId . ' AND sys_language_uid = ' . $this->languageMap[0];
$db->exec_UPDATEquery('pages_language_overlay', $where, ['title' => $result->fetch_row()[0]]); $db->exec_UPDATEquery(
'pages_language_overlay', $where,
['title' => $result['title'], 'subtitle' => $result['subtitle']]
);
}
$where = 'uid = ' . (int) $parentId; $where = 'uid = ' . (int) $parentId;
$db->exec_UPDATEquery( $db->exec_UPDATEquery(
'pages', $where, ['title' => date('Y-m-d', $row['datetime']) . ' - ' . $row['title']] 'pages', $where, ['title' => date(
'Y-m-d', $row['datetime']
) . ' - ' . $row['title'], 'subtitle' => $row['title'], 'lastUpdated' => $row['datetime']]
); );
} else { } else {
// finally translate the page title if necessary // finally translate the page title if necessary
/** @noinspection NotOptimalIfConditionsInspection */
if (isset($this->languageMap[(int) $row['sys_language_uid']]) && $this->languageMap[(int) $row['sys_language_uid']] > 0) { if (isset($this->languageMap[(int) $row['sys_language_uid']]) && $this->languageMap[(int) $row['sys_language_uid']] > 0) {
$where = 'pid = ' . (int) $parentId . ' AND sys_language_uid = ' . $this->languageMap[(int) $row['sys_language_uid']]; $where = 'pid = ' . (int) $parentId . ' AND sys_language_uid = ' . $this->languageMap[(int) $row['sys_language_uid']];
} else { } else {
......
<?php
namespace SGalinski\SgNews\Domain\Model;
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (https://www.sgalinski.de)
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* FileReference
*/
class FileReference extends \TYPO3\CMS\Extbase\Domain\Model\FileReference {
}
<?php
namespace SGalinski\SgNews\Domain\Repository;
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (https://www.sgalinski.de)
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\SgNews\Domain\Model\FileReference;
use TYPO3\CMS\Core\Database\DatabaseConnection;
use TYPO3\CMS\Core\Resource\File;
/**
* FileReference Repository
*/
class FileReferenceRepository extends AbstractRepository {
/**
* Method creates a file reference entry in the database. This step is necessary because the
* extbase can not handle the default TCA for FAL records. Without this method the FAL records will miss
* the uid_foreign, field name and table names.
*
* @param File $file
* @param int $uid
* @param int $pid
* @param string $tablename
* @param string $fieldname
* @return FileReference
* @throws \Exception
*/
public function addFileReferenceFromFile(File $file, $uid, $pid, $tablename, $fieldname) {
$fileReferenceId = $this->addFileReferenceFromFileId(
$file->getUid(),
['uid' => $uid, 'pid' => $pid],
$tablename,
$fieldname
);
$fileReference = NULL;
if ($fileReferenceId > 0) {
/** @var FileReference $fileReference */
$fileReference = $this->findByUid($fileReferenceId);
}
return $fileReference;
}
/**
* Method creates a file reference entry in the database. This step is necessary because the
* extbase can not handle the default TCA for FAL records. Without this method the FAL records will miss
* the uid_foreign, field name and table names.
*
* @param $fileId
* @param array $reference
* @param string $tablename
* @param string $fieldname
* @return int
* @throws \Exception
*/
public function addFileReferenceFromFileId($fileId, array $reference, $tablename, $fieldname) {
/** @var DatabaseConnection $db */
$db = $GLOBALS['TYPO3_DB'];
$arguments = [
'crdate' => $GLOBALS['EXEC_TIME'],
'tstamp' => $GLOBALS['EXEC_TIME'],
'pid' => (int) $reference['pid'],
'table_local' => 'sys_file',
'uid_local' => $fileId,
'uid_foreign' => (int) $reference['uid'],
'tablenames' => $tablename,
'fieldname' => $fieldname,
];
if (!$db->exec_INSERTquery('sys_file_reference', $arguments)) {
throw new \Exception('An error occurred while adding a file reference record.', 1452590219);
}
return (int) $db->sql_insert_id();
}
}
...@@ -35,6 +35,12 @@ config.tx_extbase { ...@@ -35,6 +35,12 @@ config.tx_extbase {
tableName = sys_category tableName = sys_category
} }
} }
SGalinski\SgNews\Domain\Model\FileReference {
mapping {
tableName = sys_file_reference
}
}
} }
} }
} }
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