Skip to content

Commit

Permalink
[FEATURE] Add upgrade wizards for new plugin types
Browse files Browse the repository at this point in the history
Migrates content elements and permissions of be_users from
old list_types to new CTypes
  • Loading branch information
garfieldius committed Jun 6, 2024
1 parent 485b28c commit fdff281
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 0 deletions.
93 changes: 93 additions & 0 deletions Classes/Updates/PluginNameUpdater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace Supseven\Cleverreach\Updates;

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\DatabaseUpdatedPrerequisite;
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;

class PluginNameUpdater implements UpgradeWizardInterface
{
public function getIdentifier(): string
{
return 'cleverreachPluginNameUpdater';
}

public function getTitle(): string
{
return 'EXT:cleverreach: Migrate plugins to CTypes';
}

public function getDescription(): string
{
return 'Update content elements to use the new CTypes for the plugins instead of the old list_types';
}

public function getPrerequisites(): array
{
return [
DatabaseUpdatedPrerequisite::class,
];
}

public function updateNecessary(): bool
{
return count($this->getRecords()) > 0;
}

public function executeUpdate(): bool
{
$records = $this->getRecords();
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool->getQueryBuilderForTable('tt_content');
$queryBuilder->getRestrictions()->removeAll();

$stmt = $queryBuilder
->update('tt_content')
->set('CType', '?', false)
->set('list_type', $queryBuilder->quote(''), false)
->where($queryBuilder->expr()->eq('uid', '?'))
->prepare();

foreach ($records as $record) {
$newType = str_replace(
['_pi1', '_pi2'],
['_optin', 'optout'],
$record['list_type']
);

$stmt->bindValue(1, $newType);
$stmt->bindValue(2, $record['uid']);
$stmt->executeStatement();
}

return true;
}

protected function getRecords(): array
{
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool->getQueryBuilderForTable('tt_content');
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));

return $queryBuilder
->select('uid', 'list_type')
->from('tt_content')
->where(
$queryBuilder->expr()->eq(
'CType',
$queryBuilder->createNamedParameter('list')
),
$queryBuilder->expr()->like(
'list_type',
$queryBuilder->createNamedParameter('cleverreach_pi%')
)
)
->executeQuery()
->fetchAllAssociative();
}
}
123 changes: 123 additions & 0 deletions Classes/Updates/PluginPermissionUpdater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

declare(strict_types=1);

namespace Supseven\Cleverreach\Updates;

use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Install\Updates\BackendModulePermissionMigration;
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;

class PluginPermissionUpdater implements UpgradeWizardInterface
{
public function getIdentifier(): string
{
return 'cleverreachPluginPermissionUpdater';
}

public function getTitle(): string
{
return 'EXT:cleverreach: Migrate plugin permissions';
}

public function getDescription(): string
{
return 'Migrate permissions of be_groups to use new CTypes instead of cleverreach_pi list_types';
}

public function getPrerequisites(): array
{
return [
DatabaseUpdatedPrerequisite::class,
BackendModulePermissionMigration::class,
];
}

public function updateNecessary(): bool
{
return count($this->getRecords()) > 0;
}

public function executeUpdate(): bool
{
$records = $this->getRecords();
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool->getQueryBuilderForTable('be_groups');
$queryBuilder->getRestrictions()->removeAll();
$stmt = $queryBuilder
->update('be_groups')
->set('explicit_allowdeny', '?', false)
->where($queryBuilder->expr()->eq('uid', '?'))
->prepare();

foreach ($records as $record) {
$newList = str_replace(
['list_type:cleverreach_pi1', 'list_type:cleverreach_pi2'],
['CType:cleverreach_optin', 'list_type:cleverreach_optin'],
$record['explicit_allowdeny']
);

$stmt->bindValue(1, $newList);
$stmt->bindValue(2, $record['uid']);
$stmt->executeStatement();
}

return true;
}

protected function getRecords(): array
{
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool->getQueryBuilderForTable('be_groups');
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));

return $queryBuilder
->select('uid', 'explicit_allowdeny')
->from('be_groups')
->where(
$queryBuilder->expr()->like(
'explicit_allowdeny',
$queryBuilder->createNamedParameter('%list_type:cleverreach_pi%')
)
)
->executeQuery()
->fetchAllAssociative();
}

protected function updateRow(array $row): void
{
$default = 'tt_content:CType:news_pi1,tt_content:CType:news_newsliststicky,tt_content:CType:news_newsdetail,tt_content:CType:news_newsdatemenu,tt_content:CType:news_newssearchform,tt_content:CType:news_newssearchresult,tt_content:CType:news_newsselectedlist,tt_content:CType:news_categorylist,tt_content:CType:news_taglist';

if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() >= 12) {
$searchReplace = [
'tt_content:list_type:news_pi1:ALLOW' => $default,
'tt_content:list_type:news_pi1:DENY' => '',
'tt_content:list_type:news_pi1' => $default,
];
} else {
$default .= ',';
$default = str_replace(',', ':ALLOW,', $default);
$searchReplace = [
'tt_content:list_type:news_pi1:ALLOW' => $default,
'tt_content:list_type:news_pi1:DENY' => str_replace($default, 'ALLOW', 'DENY'),
];
}

$newList = str_replace(array_keys($searchReplace), array_values($searchReplace), $row['explicit_allowdeny']);
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_groups');
$queryBuilder->update('be_groups')
->set('explicit_allowdeny', $newList)
->where(
$queryBuilder->expr()->in(
'uid',
$queryBuilder->createNamedParameter($row['uid'], Connection::PARAM_INT)
)
)
->executeStatement();
}
}
6 changes: 6 additions & 0 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@
);

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPageTSConfig('@import \'EXT:cleverreach/Configuration/TsConfig/NewElementWizard.tsconfig\';');

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['cleverreachPluginPermissionUpdater'] =
\Supseven\Cleverreach\Updates\PluginPermissionUpdater::class;

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['cleverreachPluginNameUpdater'] =
\Supseven\Cleverreach\Updates\PluginNameUpdater::class;

0 comments on commit fdff281

Please sign in to comment.