Commit 7beaccc8 authored by Kevin Ditscheid's avatar Kevin Ditscheid
Browse files

[BUGFIX] Replace AbstractUpdate class

parent ac2a7a92
<?php
namespace SGalinski\SgMail\Updates;
/***************************************************************
* 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\SgMail\Service\BackendService;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Install\Updates\AbstractUpdate;
/**
* Migrate template db entries to the correct root pages
*/
class MigrateData extends AbstractUpdate {
protected $identifier = '';
/**
* @var string
*/
protected $title = 'Find all templates & queue entries without site root and assign the correct site root id';
/**
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
*/
protected $objectManager;
/**
* @var array
*/
protected $tables = [
'tx_sgmail_domain_model_mail', 'tx_sgmail_domain_model_template'
];
/**
* Checks whether updates are required.
*
* @param string &$description The description for the update
* @return bool Whether an update is required (TRUE) or not (FALSE)
*/
public function checkForUpdate(&$description) {
$description = 'Set the site_root_id for all the queue entries and templates if not set or pid = 0';
$oldRowsFound = FALSE;
foreach ($this->tables as $table) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
$queryBuilder->getRestrictions()->removeAll();
$rowCount = $queryBuilder->select('*')
->from($table)
->where(
$queryBuilder->expr()->eq('site_root_id', 0)
)->execute()->rowCount();
if ($rowCount > 0) {
$oldRowsFound = TRUE;
break;
}
}
return !(!$oldRowsFound || $this->isWizardDone());
}
/**
* Performs the accordant updates.
*
* @param array &$dbQueries Queries done in this update
* @param mixed &$customMessages Custom messages
* @return bool Whether everything went smoothly or not
*/
public function performUpdate(array &$dbQueries, &$customMessages) {
foreach ($this->tables as $table) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
$queryBuilder->getRestrictions()->removeAll();
$rows = $queryBuilder->select('uid', 'pid')
->from($table)
->where(
$queryBuilder->expr()->eq('site_root_id', 0)
)->execute()->fetchAll();
$dbQueries[] = $queryBuilder->getSQL();
/** @var array $result */
foreach ($rows as $row) {
$siteRootId = BackendService::getSiteRoot($row[1]);
if ($siteRootId === NULL) {
$this->retrieveFirstSiteRoot();
}
$queryBuilder->update($table)
->where(
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($row[0]))
)
->set('site_root_id', (int) $siteRootId);
$dbQueries[] = $queryBuilder->getSQL();
}
}
$this->markWizardAsDone();
return TRUE;
}
/**
* Get the first site root that is not pid = 0
*
* @return int
*/
private function retrieveFirstSiteRoot() {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
$queryBuilder->getRestrictions()->removeAll();
$pagesResult = $queryBuilder->select('uid')
->from('pages')
->where(
$queryBuilder->expr()->andX(
$queryBuilder->expr()->eq('is_siteroot', 1),
$queryBuilder->expr()->neq('uid', 0)
)
)
->setMaxResults(1)
->execute()->fetch();
$dbQueries[] = $queryBuilder->getSQL();
if (empty($pagesResult)) {
return 0;
}
return $pagesResult[0][0];
}
}
......@@ -28,66 +28,44 @@ namespace SGalinski\SgMail\Updates;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Install\Updates\AbstractUpdate;
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;
/**
* Makes german templates the default and former default as english templates
*/
class UpdateGermanAsDefault extends AbstractUpdate {
class UpdateGermanAsDefault implements UpgradeWizardInterface {
/**
* @var string
*/
protected $identifier = 'tx_sgmail_update_german_as_default';
/**
* @var string
*/
protected $title = 'Makes german templates the default and former default as english templates. WARNING: ONLY EXECUTE THIS IF IT MAKES SENSE FOR YOUR TYPO3 INSTANCE';
protected $table = 'tx_sgmail_domain_model_template';
/**
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
* @return string
*/
protected $objectManager;
public function getIdentifier(): string {
return 'tx_sgmail_update_german_as_default';
}
/**
* @var string
* @return string
*/
protected $table = 'tx_sgmail_domain_model_template';
public function getTitle(): string {
return 'Makes german templates the default and former default as english templates. WARNING: ONLY EXECUTE THIS IF IT MAKES SENSE FOR YOUR TYPO3 INSTANCE';
}
/**
* Checks whether updates are required.
*
* @param string &$description The description for the update
* @return bool Whether an update is required (TRUE) or not (FALSE)
* @return string
*/
public function checkForUpdate(&$description) {
$description = 'Update if there are german templates';
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
$queryBuilder->getRestrictions()->removeAll();
$rowCount = $queryBuilder->select('*')
->from($this->table)
->where(
$queryBuilder->expr()->eq('language', $queryBuilder->createNamedParameter('de'))
)
->execute()->rowCount();
if ($rowCount > 0) {
return TRUE;
}
return !(!FALSE || $this->isWizardDone());
public function getDescription(): string {
return '';
}
/**
* Performs the according updates.
*
* @param array &$dbQueries Queries done in this update
* @param mixed &$customMessages Custom messages
* @return bool Whether everything went smoothly or not
* @return bool
*/
public function performUpdate(array &$dbQueries, &$customMessages) {
$dbQueries = [];
public function executeUpdate(): bool {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
$queryBuilder->getRestrictions()->removeAll();
$resultGerman = $queryBuilder->select('uid')
......@@ -96,7 +74,6 @@ class UpdateGermanAsDefault extends AbstractUpdate {
$queryBuilder->expr()->eq('language', $queryBuilder->createNamedParameter('de'))
)
->execute()->fetchAll();
$dbQueries[] = $queryBuilder->getSQL();
$resultDefault = $queryBuilder->select('uid')
->from($this->table)
......@@ -104,7 +81,6 @@ class UpdateGermanAsDefault extends AbstractUpdate {
$queryBuilder->expr()->eq('language', $queryBuilder->createNamedParameter('default'))
)
->execute()->fetchAll();
$dbQueries[] = $queryBuilder->getSQL();
/** @var array $resultGerman */
foreach ($resultGerman as $row) {
......@@ -114,7 +90,6 @@ class UpdateGermanAsDefault extends AbstractUpdate {
)
->set('language', 'default', TRUE)
->execute();
$dbQueries[] = $queryBuilder->getSQL();
}
/** @var array $resultGerman */
......@@ -125,10 +100,32 @@ class UpdateGermanAsDefault extends AbstractUpdate {
)
->set('language', 'en', TRUE)
->execute();
$dbQueries[] = $queryBuilder->getSQL();
}
$this->markWizardAsDone();
return TRUE;
}
/**
* @return bool
*/
public function updateNecessary(): bool {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
$queryBuilder->getRestrictions()->removeAll();
$rowCount = $queryBuilder->select('*')
->from($this->table)
->where(
$queryBuilder->expr()->eq('language', $queryBuilder->createNamedParameter('de'))
)
->execute()->rowCount();
return $rowCount > 0;
}
/**
* @return array|string[]
*/
public function getPrerequisites(): array {
return [
DatabaseUpdatedPrerequisite::class
];
}
}
......@@ -30,27 +30,13 @@ use SGalinski\SgMail\Service\BackendService;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Install\Updates\AbstractUpdate;
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;
/**
* Fix all incorrect / deprecated language codes (be languages instead of sys languages) in the database
*/
class UpdateLanguages extends AbstractUpdate {
/**
* @var string
*/
protected $identifier = 'tx_sgmail_update_languages';
/**
* @var string
*/
protected $title = 'Find all templates & queue entries with an incorrect (be languages instead of sys languages) language code and fix it';
/**
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
*/
protected $objectManager;
class UpdateLanguages implements UpgradeWizardInterface {
/**
* @var array
......@@ -73,19 +59,54 @@ class UpdateLanguages extends AbstractUpdate {
];
/**
* Checks whether updates are required.
*
* @param string &$description The description for the update
* @return bool Whether an update is required (TRUE) or not (FALSE)
* @throws \InvalidArgumentException
* @return string
*/
public function checkForUpdate(&$description) {
public function getIdentifier(): string {
return 'tx_sgmail_update_languages';
}
/**
* @return string
*/
public function getTitle(): string {
return 'Find all templates & queue entries with an incorrect (be languages instead of sys languages) language code and fix it';
}
/**
* @return string
*/
public function getDescription(): string {
return '';
}
/**
* @return bool
*/
public function executeUpdate(): bool {
foreach ($this->tables as $table) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
$queryBuilder->getRestrictions()->removeAll();
foreach ($this->languageMap as $origin => $target) {
$queryBuilder->update($table)
->where(
$queryBuilder->expr()->eq('language', $queryBuilder->createNamedParameter($origin))
)
->set('language', $target, TRUE)
->execute();
}
}
return TRUE;
}
/**
* @return bool
*/
public function updateNecessary(): bool {
$upgradeNecessary = FALSE;
if (count($this->languageMap) > 0) {
$languages = BackendService::getLanguages();;
$description = 'Check all the language codes in the database';
$languages = BackendService::getLanguages();
foreach ($this->tables as $table) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
......@@ -109,34 +130,15 @@ class UpdateLanguages extends AbstractUpdate {
}
}
return !(!$upgradeNecessary || $this->isWizardDone());
return $upgradeNecessary;
}
/**
* Performs the accordant updates.
*
* @param array &$dbQueries Queries done in this update
* @param mixed &$customMessages Custom messages
* @return bool Whether everything went smoothly or not
* @return array|string[]
*/
public function performUpdate(array &$dbQueries, &$customMessages) {
$dbQueries = [];
foreach ($this->tables as $table) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
$queryBuilder->getRestrictions()->removeAll();
foreach ($this->languageMap as $origin => $target) {
$queryBuilder->update($table)
->where(
$queryBuilder->expr()->eq('language', $queryBuilder->createNamedParameter($origin))
)
->set('language', $target, TRUE)
->execute();
$dbQueries[] = $queryBuilder->getSQL();
}
}
$this->markWizardAsDone();
return TRUE;
public function getPrerequisites(): array {
return [
DatabaseUpdatedPrerequisite::class
];
}
}
......@@ -30,91 +30,64 @@ use SGalinski\SgMail\Service\BackendService;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Install\Updates\AbstractUpdate;
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;
/**
* Migrate template db entries to the correct root pages
*/
class UpdatePidToSiteRoot extends AbstractUpdate {
class UpdatePidToSiteRoot implements UpgradeWizardInterface {
/**
* @var string
* @var array
*/
protected $identifier = 'tx_sgmail_update_pid_to_site_root';
protected $tables = [
'tx_sgmail_domain_model_mail', 'tx_sgmail_domain_model_template'
];
/**
* @var string
* check if site_root columns actually exist
*
* @param string $table
* @return bool
*/
protected $title = 'Find all templates & queue entries with site root and assign the correct site root id as pid. Also check if the pids are actually site roots and update them accordingly';
private function siteRootColumnExists($table) {
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
$result = $connection->getSchemaManager()->listTableColumns($table);
return \array_key_exists('site_root_id', $result);
}
/**
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
* @return string
*/
protected $objectManager;
public function getIdentifier(): string {
return 'tx_sgmail_update_pid_to_site_root';
}
/**
* @var array
* @return string
*/
protected $tables = [
'tx_sgmail_domain_model_mail', 'tx_sgmail_domain_model_template'
];
public function getTitle(): string {
return 'Find all templates & queue entries with site root and assign the correct site root id as pid. Also check if the pids are actually site roots and update them accordingly';
}
/**
* Checks whether updates are required.
*
* @param string &$description The description for the update
* @return bool Whether an update is required (TRUE) or not (FALSE)
* @return string
*/
public function checkForUpdate(&$description) {
$description = 'Move site root ids to pid & update pids to their correspondent site root ids';
// are there site root columns differing from pids?
foreach ($this->tables as $table) {
if (!$this->siteRootColumnExists($table)) {
continue;
}
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$rowCount = $queryBuilder->select('*')
->from($table)
->where($queryBuilder->expr()->neq('site_root_id', 'pid'))
->execute()->rowCount();
if ($rowCount > 0) {
return TRUE;
}
}
// are the pids not belonging to site root pages ?
foreach ($this->tables as $table) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$result = $queryBuilder->select('pid')->from($table)->groupBy('pid')->execute();
foreach ($result as $row) {
$siteRootId = BackendService::getSiteRoot($row['pid']);
if ($siteRootId !== $row['pid']) {
return TRUE;
}
}
}
return !(!FALSE || $this->isWizardDone());
public function getDescription(): string {
return 'Move site root ids to pid & update pids to their correspondent site root ids';
}
/**
* Performs the according updates.
*
* @param array &$dbQueries Queries done in this update
* @param mixed &$customMessages Custom messages
* @return bool Whether everything went smoothly or not
* @return bool
* @throws \Doctrine\DBAL\DBALException
*/
public function performUpdate(array &$dbQueries, &$customMessages) {
public function executeUpdate(): bool {
foreach ($this->tables as $table) {
if ($this->siteRootColumnExists($table)) {
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
$sql = 'UPDATE ' . $table . ' SET pid = site_root_id';
$connection->executeQuery($sql);
$dbQueries[] = $sql;
} else {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
$queryBuilder->getRestrictions()->removeAll()->add(
......@@ -129,24 +102,55 @@ class UpdatePidToSiteRoot extends AbstractUpdate {
$siteRootId = 1;
}
$queryBuilder->update($table)->set('pid', $siteRootId)->where('pid = ' . $row['pid'])->execute();
$dbQueries[] = $queryBuilder->getSQL();
}
}
}
$this->markWizardAsDone();
return TRUE;
}
/**
* check if site_root columns actually exist
*
* @param string $table
* @return bool
*/
private function siteRootColumnExists($table) {
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
$result = $connection->getSchemaManager()->listTableColumns($table);
return \array_key_exists('site_root_id', $result);
public function updateNecessary(): bool {
// are there site root columns differing from pids?
foreach ($this->tables as $table) {
if (!$this->siteRootColumnExists($table)) {
continue;
}
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$rowCount = $queryBuilder->select('*')
->from($table)
->where($queryBuilder->expr()->neq('site_root_id', 'pid'))
->execute()->rowCount();
if ($rowCount > 0) {
return TRUE;
}
}
// are the pids not belonging to site root pages ?
foreach ($this->tables as $table) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$result = $queryBuilder->select('pid')->from($table)->groupBy('pid')->execute();
foreach ($result as $row) {
$siteRootId = BackendService::getSiteRoot($row['pid']);
if ($siteRootId !== $row['pid']) {
return TRUE;
}
}
}
return FALSE;
}
/**
* @return array|string[]
*/
public function getPrerequisites(): array {
return [
DatabaseUpdatedPrerequisite::class
];
}
}
......@@ -28,44 +28,46 @@ namespace SGalinski\SgMail\Updates;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Install\Updates\AbstractUpdate;
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;