Files
Alexandre Gain dce849610c Merge the Smartlinks plugin (SmartlinksBundle 0.6.0) into the bundle as the Smart Links domain (0.18.0)
Two domains, one MT3S bundle: Marketing Programs (unchanged) and the
imported Smart Links (smart links + UTM taxonomies), each keeping its
own menu, tables, routes and translations.

- Code moved from MauticPlugin\SmartlinksBundle\ to
  MauticPlugin\MT3SMarketingProgramBundle\; template namespace
  @Smartlinks/ -> @MT3SMarketingProgram/; smartlinks.js ->
  Assets/js/mt3s-mp-smartlinks.js; icon -> Assets/img/smartlinks-icon.svg.
- Incoming migrations renamed Version_0_X_0_SmartLinks.php to avoid
  the class-name collision; schema untouched, data intact.
- Permissions folded into MT3SMarketingProgramPermissions:
  smart_links + taxonomies under the mt3smarketingprogram: base
  (roles that had the old smartlinks: grants must be re-granted).
- config.php: Smart Links routes + menu group, public /l/{slug},
  SmartlinksIntegration services, smartlinks_* parameters (legacy
  utm_manager_* keys still readable), version 0.18.0. The 0.17.0
  placeholder /s/marketing-program-utm-taxonomies is replaced by the
  real /s/utm-taxonomies.
- Translations merged (en_US) and added (fr_FR); Tests/ + phpunit moved
  in; readme documents both domains and the 0.18.0 merge history.

Verified live: menus, both config tabs, both lists + forms, program
pages, public redirect with taxonomy UTM values, host + unknown-slug
404s, 90/90 unit tests, full php -l, all ini catalogs parse.
2026-11-16 13:48:13 +01:00

209 lines
7.4 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Migrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\SmartLink;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\SmartLinkHit;
/**
* The 0.5.0 rename of the plugin (UTM Manager -> Smartlinks) also renames its tables and their
* indexes: utm_smart_links -> smart_links and utm_smart_link_hits -> smart_link_hits.
*
* Mautic syncs the plugin schema before running the migrations, so the new tables usually
* already exist (empty, straight from the entity metadata) when this runs: the rows are copied
* from the old tables, which are then dropped (hits first: they still reference the old links
* table). When the new tables don't exist yet (a plugin update without the schema sync) the old
* tables are renamed instead, which keeps the rows, the foreign keys and the indexes.
*
* Installs updated from 0.3.0 skipped the Mautic destinations of Version_0_4_0, which looks for
* the table under its new name: the page_id / form_id columns and their foreign keys are
* re-checked here so every install converges to the same schema.
*
* Idempotent: runs on plugin update (core's migration engine) and right after a fresh install
* (PluginInstallSubscriber), and stops when nothing is left to rename, copy or add.
*/
class Version_0_5_0_SmartLinks extends AbstractMigration
{
private const RENAMED_TABLES = [
'utm_smart_links' => 'smart_links',
'utm_smart_link_hits' => 'smart_link_hits',
];
private const RENAMED_INDEXES = [
'utm_smart_link_slug' => 'smart_link_slug',
'utm_smart_link_hit_link' => 'smart_link_hit_link',
];
/** Local column => referenced core table (the Mautic destinations of Version_0_4_0). */
private const MAUTIC_DESTINATIONS = [
'page_id' => 'pages',
'form_id' => 'forms',
];
protected function isApplicable(Schema $schema): bool
{
foreach (array_keys(self::RENAMED_TABLES) as $old) {
if ($schema->hasTable($this->concatPrefix($old))) {
return true;
}
}
$links = $this->findLinksTable($schema);
if (null !== $links) {
foreach (array_keys(self::MAUTIC_DESTINATIONS) as $column) {
if (!$links->hasColumn($column)) {
return true;
}
}
if ([] !== $this->getOldIndexes($links) || [] !== $this->getMissingForeignKeys($links)) {
return true;
}
}
$hits = $this->findHitsTable($schema);
return null !== $hits && [] !== $this->getOldIndexes($hits);
}
protected function up(): void
{
$schema = $this->entityManager->getConnection()->createSchemaManager()->introspectSchema();
$emptied = [];
foreach (self::RENAMED_TABLES as $old => $new) {
$oldTable = $this->findTable($schema, $old);
$newTable = $this->findTable($schema, $new);
if (null === $oldTable) {
continue; // nothing left under the old name
}
if (null === $newTable) {
// No schema sync ran: rename keeps the rows, the foreign keys and the indexes.
$this->addSql(sprintf('RENAME TABLE %s TO %s', $this->concatPrefix($old), $this->concatPrefix($new)));
continue;
}
// The schema sync already created the new table (empty): copy the rows over, then
// drop the old table. Hits reference links, so everything is copied before the
// first DROP (see below).
$columns = implode(', ', array_intersect(
array_keys($newTable->getColumns()),
array_keys($oldTable->getColumns())
));
$this->addSql(sprintf(
'INSERT INTO %s (%s) SELECT %s FROM %s',
$this->concatPrefix($new),
$columns,
$columns,
$this->concatPrefix($old)
));
$emptied[] = $old;
}
// Drop the copied-from tables, hits first: their foreign key still references the old links table.
foreach (array_reverse($emptied) as $old) {
$this->addSql(sprintf('DROP TABLE %s', $this->concatPrefix($old)));
}
$links = $this->findLinksTable($schema);
$hits = $this->findHitsTable($schema);
if (null === $links) {
return;
}
// The statements below run after the RENAME TABLE ones, so they target the new names.
foreach ([[SmartLink::TABLE_NAME, $links], [SmartLinkHit::TABLE_NAME, $hits]] as [$name, $table]) {
if (null === $table) {
continue;
}
foreach ($this->getOldIndexes($table) as $old) {
$this->addSql(sprintf(
'ALTER TABLE %s DROP INDEX %s, ADD INDEX %s (%s)',
$this->concatPrefix($name),
$old,
self::RENAMED_INDEXES[$old],
implode(', ', $table->getIndex($old)->getColumns())
));
}
}
foreach (array_keys(self::MAUTIC_DESTINATIONS) as $column) {
if (!$links->hasColumn($column)) {
$this->addSql(sprintf(
'ALTER TABLE %s ADD %s INT UNSIGNED DEFAULT NULL',
$this->concatPrefix(SmartLink::TABLE_NAME),
$column
));
}
}
foreach ($this->getMissingForeignKeys($links) as $column => $referenceTable) {
$this->addSql($this->generateAlterTableForeignKeyStatement(
SmartLink::TABLE_NAME,
[$column],
$referenceTable,
['id'],
'ON DELETE SET NULL'
));
}
}
/**
* The links table under the name it has right now: new on fresh installs, old on installs
* that wait for the RENAME TABLE statements queued above.
*/
private function findLinksTable(Schema $schema): ?Table
{
return $this->findTable($schema, SmartLink::TABLE_NAME) ?? $this->findTable($schema, 'utm_smart_links');
}
private function findHitsTable(Schema $schema): ?Table
{
return $this->findTable($schema, SmartLinkHit::TABLE_NAME) ?? $this->findTable($schema, 'utm_smart_link_hits');
}
private function findTable(Schema $schema, string $name): ?Table
{
$name = $this->concatPrefix($name);
return $schema->hasTable($name) ? $schema->getTable($name) : null;
}
/**
* @return list<string> Indexes still carrying their pre-rename name
*/
private function getOldIndexes(Table $table): array
{
return array_values(array_filter(
array_keys(self::RENAMED_INDEXES),
fn (string $index): bool => $table->hasIndex($index)
));
}
/**
* @return array<string, string>
*/
private function getMissingForeignKeys(Table $table): array
{
$missing = self::MAUTIC_DESTINATIONS;
foreach ($table->getForeignKeys() as $foreignKey) {
foreach ($foreignKey->getLocalColumns() as $column) {
unset($missing[$column]);
}
}
return $missing;
}
}