Skip to content

Instantly share code, notes, and snippets.

@jokumer
Last active February 14, 2019 16:30
Show Gist options
  • Save jokumer/cf25b78b1a608ad0b5f680ce567e58a0 to your computer and use it in GitHub Desktop.
Save jokumer/cf25b78b1a608ad0b5f680ce567e58a0 to your computer and use it in GitHub Desktop.
TYPO3 - Migrate tt_news tt_content.records for tx_news
<?php
namespace Jokumer\Upgrader\Command;
use Doctrine\DBAL\DBALException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\ReferenceIndex;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Migrate tt_news tt_content.records for tx_news
*
* @package TYPO3
* @subpackage tx_upgrader
* @author 2019 J.Kummer
* @copyright Copyright belongs to the respective authors
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
*/
class MigrateNewsTtContentRecordsCommand extends Command
{
/**
* Configure the command by defining the name, options and arguments
*/
public function configure()
{
$this->setDescription('Migrate tt_news tt_content.records for tx_news');
$this->setHelp('Migrate record strings from tt_news_123 to tx_news_domain_model_news_321 imported with EXT:news_ttnewsimport');
}
/**
* Executes the command for showing sys_log entries
*
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
// Get affected rows for tt_content
$entries = $this->getEntries();
if (!empty($entries)) {
foreach ($entries as $entry) {
// Rewrite records for tt_content
$legazyRecordsArray = GeneralUtility::trimExplode(',', $entry['records']);
$resolvedRecordsArray = [];
if (!empty($legazyRecordsArray)) {
foreach ($legazyRecordsArray as $legazyRecord) {
if (strpos($legazyRecord, 'tt_news_') !== false) {
$ttnewsUid = (int) substr($legazyRecord, 8);
if ($ttnewsUid) {
$newsUid = (int) $this->getNewsUidByTtNewsUid($ttnewsUid);
if ($newsUid) {
$resolvedRecordsArray[] = 'tx_news_domain_model_news_' . $newsUid;
}
}
} else { // !Keep other records
$resolvedRecordsArray[] = $legazyRecord;
}
}
}
// Update records for tt_content (also if empty, cause of missing tt_news entries)
$resolvedRecords = implode(',', $resolvedRecordsArray);
$this->updateEntry($entry['uid'], ['records' => $resolvedRecords]);
}
}
}
/**
* Get entries
*/
protected function getEntries() {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tt_content');
// Remove restrictions
$queryBuilder
->getRestrictions()
->removeAll();
$result = $queryBuilder
->select('uid', 'records')
->from('tt_content')
->where(
$queryBuilder->expr()->andX(
$queryBuilder->expr()->eq(
'CType',
$queryBuilder->createNamedParameter('shortcut', \PDO::PARAM_STR)
),
$queryBuilder->expr()->like(
'records',
$queryBuilder->createNamedParameter('%tt_news_%', \PDO::PARAM_STR)
)
)
)
->execute()->fetchAll();
return $result;
}
/**
* Get news uid by tt_news uid
*
* @param int $ttnewsUid
* @return int $newsUid
*/
protected function getNewsUidByTtNewsUid($ttnewsUid) {
$newsUid = null;
if ((int) $ttnewsUid) {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_news_domain_model_news');
// Remove restrictions
$queryBuilder
->getRestrictions()
->removeAll();
$result = $queryBuilder
->select('uid')
->from('tx_news_domain_model_news')
->where(
$queryBuilder->expr()->andX(
$queryBuilder->expr()->eq(
'import_id',
$queryBuilder->createNamedParameter($ttnewsUid, \PDO::PARAM_INT)
),
$queryBuilder->expr()->eq(
'import_source',
$queryBuilder->createNamedParameter('TT_NEWS_IMPORT', \PDO::PARAM_STR)
)
)
)
->execute()->fetch();
if (isset($result['uid'])) {
$newsUid = (int) $result['uid'];
}
}
return $newsUid;
}
/**
* Update rows
*
* @param int $entryUid
* @param array $updateFields
*/
protected function updateEntry($entryUid, $updateFields) {
/** @var Connection $connection */
$connection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tt_content');
try {
$connection->update('tt_content', $updateFields, ['uid' => (int) $entryUid]);
$this->updateRefIndex('tt_content', (int) $entryUid);
} catch (DBALException $e) {
#$updateErrorMessage = $e->getPrevious()->getMessage();
}
}
/**
* Update Reference index
*
* @param string $tableName
* @param int $recordUid
* @return array
*/
protected function updateRefIndex($tableName, $recordUid)
{
/** @var $refIndexObj ReferenceIndex */
$refIndexObj = GeneralUtility::makeInstance(ReferenceIndex::class);
$result = $refIndexObj->updateRefIndexTable($tableName, $recordUid, $testOnly);
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment