Skip to content

Commit

Permalink
[TASK] Upgrade compatibility and support command line execution
Browse files Browse the repository at this point in the history
- Offers compatibility with TYPO3 9.5.x and EXT:news v7.x
  includes temporary requirement of EXT:typo3db_legacy
- Support commandline support for all important tasks
  documents also for EXT:typo3_console
- Implements EXT:fal_ttnews migration from EXT:news_falttnewsimport
  adds migration for tt_news 'forceFirstImageIsPreview'
- Implements EXT:realurl migration for unique alias & path_segment
- Adjust Readme for first instructions
  • Loading branch information
jkummer committed Feb 7, 2019
1 parent 03bf31a commit 4ef8aa5
Show file tree
Hide file tree
Showing 8 changed files with 451 additions and 14 deletions.
51 changes: 51 additions & 0 deletions Classes/Command/FalMediaShowInPreviewCommandController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
namespace BeechIt\NewsTtnewsimport\Command;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;

/**
* Controller to set first news.fal_media to showinpreview
* Compare to tt_news flexform settings forceFirstImageIsPreview
*
* @author J.Kummer
*/
class FalMediaShowInPreviewCommandController extends CommandController
{
/**
* Set news.fal_media first entry value for showinpreview
*
* @return void
* @cli
*/
public function setFirstFalMediaShowInPreviewCommand()
{
$res = $this->getDatabaseConnection()->exec_SELECTquery(
'uid',
'sys_file_reference',
'tablenames = \'tx_news_domain_model_news\' AND fieldname = \'fal_media\' AND deleted = 0',
'uid_foreign',
'sorting DESC',
''
);
$count = 0;
while ($row = $this->getDatabaseConnection()->sql_fetch_assoc($res)) {
$this->getDatabaseConnection()->exec_UPDATEquery(
'sys_file_reference',
'uid = ' . $row['uid'],
['showinpreview' => 1],
false
);
$count++;
}
echo $count . ' news.fal_media entries checked for showinpreview.';
}

/**
* @return \TYPO3\CMS\Core\Database\DatabaseConnection
*/
protected function getDatabaseConnection()
{
return $GLOBALS['TYPO3_DB'];
}
}
139 changes: 139 additions & 0 deletions Classes/Command/FalTtNewsMigrationCommandController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php
namespace BeechIt\NewsTtnewsimport\Command;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;

/**
* Controller to migrate ext:fal_ttnews file references to ext:news
* From EXT:news_falttnewsimport 1.0.1
*
* @author J.Kummer
* @author Ephraim Härer <ephraim.haerer@renolit.com> RENOLIT SE
*/
class FalTtNewsMigrationCommandController extends CommandController
{

/**
* Migrate ext:fal_ttnews file references to ext:news
* Move file references from tt_news to news
*
* @return void
* @cli
*/
public function migrateFalTtNewsCommand()
{
$tables = [0 => 'tt_news', 1 => 'tx_news_domain_model_news', 2 => 'sys_file_reference'];
$updatedNews = 0;
$resetFileReferences = ['tx_falttnews_fal_images' => 0, 'tx_falttnews_fal_media' => 0];
$movedReferences = 0;

// get news records which were imported
$news = $this->getDatabaseConnection()->exec_SELECTgetRows(
'uid, import_id, import_source', $tables[1], 'import_source LIKE \'TT_NEWS_IMPORT\''
);
//\TYPO3\CMS\Core\Utility\DebugUtility::debug($news);
foreach ($news as $n) {
$updateFields = [];
// get original tt_news
$tt_n = $this->getSingleTtNews($n['import_id']);
if (!empty($tt_n)) {
if (isset($tt_n['tx_falttnews_fal_images']) && (int) $tt_n['tx_falttnews_fal_images'] > 0) {
$updateFields['fal_media'] = $tt_n['tx_falttnews_fal_images'];
}
if (isset($tt_n['tx_falttnews_fal_media']) && (int) $tt_n['tx_falttnews_fal_media'] > 0) {
$updateFields['fal_related_files'] = $tt_n['tx_falttnews_fal_media'];
}
}
// set the number of file references for news
if (!empty($updateFields)) {
$this->getDatabaseConnection()->exec_UPDATEquery($tables[1], 'uid = ' . (int) $n['uid'], $updateFields);
$updatedNews++;
$this->getDatabaseConnection()->exec_UPDATEquery($tables[0], 'uid = ' . (int) $n['import_id'], $resetFileReferences);
// get file references
$movedRef = $this->rewriteSingleNewsReferences($n['import_id'], $n['uid']);
$movedReferences = $movedReferences + $movedRef;
}
}

if ($updatedNews > 0) {
echo 'A total of ' . $movedReferences . ' file references for ' . $updatedNews . ' news has been moved.';
} else {
echo 'No file references for moving found, no changes.';
}
}

/**
* rewrite sys_file_references for a single news entry
* @param integer $uid_foreign
* @param integer $uid_foreign_new
* @return integer
*/
protected function rewriteSingleNewsReferences($uid_foreign, $uid_foreign_new)
{
$movedReferences = 0;
$table = 'sys_file_reference';
// get single news record which were imported
$references = $this->getDatabaseConnection()->exec_SELECTgetRows(
'uid, uid_foreign, tablenames, fieldname', $table, 'uid_foreign=' . (int) $uid_foreign . ' AND tablenames = \'tt_news\''
);
if (count($references) > 0) {
foreach ($references as $reference) {
if ($reference['fieldname'] === 'tx_falttnews_fal_images') {
$updateFieldsMedia = [
'uid_foreign' => $uid_foreign_new,
'tablenames' => 'tx_news_domain_model_news',
'fieldname' => 'fal_media'
];
$this->getDatabaseConnection()->exec_UPDATEquery($table, 'uid=' . (int) $reference['uid'] . ' AND tablenames=\'tt_news\' AND fieldname=\'tx_falttnews_fal_images\'', $updateFieldsMedia);
$movedReferences++;
}
if ($reference['fieldname'] === 'tx_falttnews_fal_media') {
$updateFieldsFiles = [
'uid_foreign' => $uid_foreign_new,
'tablenames' => 'tx_news_domain_model_news',
'fieldname' => 'fal_related_files'
];
$this->getDatabaseConnection()->exec_UPDATEquery($table, 'uid=' . (int) $reference['uid'] . ' AND tablenames=\'tt_news\' AND fieldname=\'tx_falttnews_fal_media\'', $updateFieldsFiles);
$movedReferences++;
}
}
}
return $movedReferences;
}

/**
* get single news by uid from table
* @param integer $uid
* @param string $table
* @return array
*/
protected function getSingleTtNews($uid)
{
// get single news record which were imported
$news = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
'uid, tx_falttnews_fal_images, tx_falttnews_fal_media', 'tt_news', 'uid=' . (int) $uid
);
if (isset($news['uid'])) {
return $news;
} else {
return [];
}
}

/**
* @return \TYPO3\CMS\Extbase\Object\ObjectManager
*/
protected function getObjectManager()
{
return GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
}

/**
* @return \TYPO3\CMS\Core\Database\DatabaseConnection
*/
protected function getDatabaseConnection()
{
return $GLOBALS['TYPO3_DB'];
}
}
123 changes: 123 additions & 0 deletions Classes/Command/RealUrlUniqueAliasMigrationCommandController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
namespace BeechIt\NewsTtnewsimport\Command;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;

/**
* Controller to migrate tx_realurl_uniqalias
* If a lot of similar titles are used it might be a good a idea
* to migrate the unique aliases to be sure that the same alias is used
*
* HINT: Requires tables from realurl version 2.x
* If realurl version is below 2.x - just switch the uncomment lines in migrateTtNewsRealurlUniqueAliasCommand method
*
* @author J.Kummer
* @author Georg Ringer 2017
* @see https://github.com/georgringer/news/commit/89e418fb68575116fd9c86e657f8523b1680bd59
*/
class RealUrlUniqueAliasMigrationCommandController extends CommandController
{
/**
* First error for execution of SQL statements
*
* @var array $error
*/
protected $error = null;

/**
* Command for tt_news tx_realurl_uniqalias migration
* Requires EXT:realurl version 2.x
* For EXT:realurl version 1.x switch the uncomment lines...
*
* @return void
* @cli
*/
public function migrateTtNewsRealurlUniqueAliasCommand()
{
// Create temporary table (or drop and recreate)
$queries[] = 'DROP TABLE IF EXISTS tx_realurl_uniqalias_migration;';
$queries[] = 'CREATE TABLE tx_realurl_uniqalias_migration LIKE tx_realurl_uniqalias;';
// Copy
$queries[] = 'INSERT INTO tx_realurl_uniqalias_migration SELECT * FROM tx_realurl_uniqalias WHERE tablename=\'tt_news\';';
// Fix it
$queries[] = 'UPDATE tx_realurl_uniqalias_migration SET value_id = (SELECT tx_news_domain_model_news.uid FROM `tx_news_domain_model_news` WHERE tx_news_domain_model_news.import_id=tx_realurl_uniqalias_migration.value_id),tablename=\'tx_news_domain_model_news\' WHERE tablename=\'tt_news\';';
// Remove wrong alias (news which have not been imported)
$queries[] = 'DELETE FROM tx_realurl_uniqalias_migration WHERE tablename=\'tx_news_domain_model_news\' AND value_id=0;';
// Insert alias back into realurl table
// RealUrl version < 2.0
#$queries[] = 'INSERT INTO tx_realurl_uniqalias (tstamp,tablename,field_id,value_alias,value_id,lang,expire) SELECT tstamp,tablename,field_id,value_alias,value_id,lang,expire FROM tx_realurl_uniqalias_migration;';
// RealUrl version >= 2.0
$queries[] = 'INSERT INTO tx_realurl_uniqalias (pid,tablename,field_id,value_alias,value_id,lang,expire) SELECT pid,tablename,field_id,value_alias,value_id,lang,expire FROM tx_realurl_uniqalias_migration;';
// Drop temporarly table
$queries[] = 'DROP TABLE tx_realurl_uniqalias_migration;';
// Run each query
$countSuccessfulExecutedQueries = 0;
foreach ($queries as $query) {
if ($this->executeQuery($query) === false) {
break;
}
$countSuccessfulExecutedQueries++;
}
$results = $countSuccessfulExecutedQueries . ' queries of ' . count($queries) . ' executed. ';
if ($this->error) {
$results .= 'Break with error! ' . $this->error;
} else {
$results .= 'Without errors.';
}
echo $results;
}

/**
* Command for tx_realurl_uniqalias into slug/path_segment migration
* Copies realurl alias to news where path_segment if is empty.
* Requires, that path_segment was not automaticly filled before!
* This can still lead in empty slugs field, which can be updated via installtool
* Use: Upgrade Wizard "Updates slug field 'path_segment' of EXT:news records" (identifier: ’newsSlug’)

* Or helhum/typo3-console: '$ typo3cms upgrade:wizard newsSlug'
*
* @return void
* @cli
*/
public function migrateRealurlUniqueAliasIntoPathSegmentCommand()
{
$result = '';
$query = '
UPDATE tx_news_domain_model_news AS n
JOIN tx_realurl_uniqalias AS r ON (n.uid = r.value_id AND n.sys_language_uid = r.lang AND r.tablename = \'tx_news_domain_model_news\')
SET n.path_segment = r.value_alias
WHERE (n.path_segment IS NULL OR n.path_segment = \'\');';
// Run
if ($this->executeQuery($query) === false) {
$result .= 'Break with error! ' . $this->error;
} else {
$result .= 'Done.';
}
echo $result;
}

/**
* Execute SQL query
*
* @param string $query
* @return bool
*/
protected function executeQuery($query)
{
$resource = $this->getDatabaseConnection()->sql_query($query);
if ($this->getDatabaseConnection()->sql_error()) {
$this->error = 'SQL-ERROR for ' . $query . ': ' . htmlspecialchars($this->getDatabaseConnection()->sql_error());
return false;
} else {
return true;
}
}

/**
* @return \TYPO3\CMS\Core\Database\DatabaseConnection
*/
protected function getDatabaseConnection()
{
return $GLOBALS['TYPO3_DB'];
}
}
37 changes: 37 additions & 0 deletions Classes/Command/TtNewsImportCommandController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
namespace BeechIt\NewsTtnewsimport\Command;

use GeorgRinger\News\Jobs\ImportJobInterface;
use GeorgRinger\News\Utility\ImportJob;
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;

/**
* Controller to import news from tt_news
*
* @author J.Kummer
*/
class TtNewsImportCommandController extends \GeorgRinger\News\Command\NewsImportCommandController
{

/**
* Import tt_news category records
*
* @cli
*/
public function importTtNewsCategoryCommand()
{
$job = $this->objectManager->get(\BeechIt\NewsTtnewsimport\Jobs\TTNewsCategoryImportJob::class);
$job->run(0);
}

/**
* Import tt_news news records
*
* @cli
*/
public function importTtNewsNewsCommand()
{
$job = $this->objectManager->get(\BeechIt\NewsTtnewsimport\Jobs\TTNewsNewsImportJob::class);
$job->run(0);
}
}
2 changes: 1 addition & 1 deletion Classes/Jobs/TTNewsNewsImportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TTNewsNewsImportJob extends AbstractImportJob {
/**
* @var int
*/
protected $numberOfRecordsPerRun = 30;
#protected $numberOfRecordsPerRun = 30; // Do not limit! Import all if running on console

protected $importServiceSettings = array(
'findCategoriesByImportSource' => 'TT_NEWS_CATEGORY_IMPORT'
Expand Down

0 comments on commit 4ef8aa5

Please sign in to comment.