Move parenthetical descriptors from product titles into subtitle

migrations/split_title_descriptors.php (dry run by default, --apply writes):
only titles ending in a parenthetical are touched, slugs stay unchanged,
non-empty subtitles are never overwritten. Applied to 23 records - titles
now carry the bare V2 product name, the descriptor lives in subtitle."
This commit is contained in:
2026-09-04 13:32:52 +02:00
parent 5aad13e635
commit 4a49196bcc

View File

@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
/**
* Move the parenthetical descriptor out of product titles into `subtitle`
* (decision 2026-09-04): "MGW Diamond-H (HDMI encoder)" becomes
* title "MGW Diamond-H" + subtitle "HDMI encoder".
*
* Rules:
* - only titles ENDING in "(...)" are touched; slugs stay as they are
* - subtitle empty -> descriptor moves there, title is stripped
* - subtitle == descriptor -> title is stripped (subtitle already right)
* - subtitle holds other text -> SKIPPED and reported, nothing changes
*
* Dry run by default: php migrations/split_title_descriptors.php
* Write: php migrations/split_title_descriptors.php --apply
*/
$apply = in_array('--apply', $argv, true);
$root = null;
$dir = __DIR__;
for ($i = 0; $i < 5; $i++) {
if (is_file($dir . '/config/system/settings.php')) {
$root = $dir;
break;
}
$dir = dirname($dir);
}
if ($root === null) {
exit("Could not locate project root above " . __DIR__ . "\n");
}
$settings = require $root . '/config/system/settings.php';
$db = $settings['DB']['Connections']['Default'];
$pdo = new PDO(
sprintf('mysql:host=%s;port=%s;dbname=%s;charset=utf8mb4', $db['host'] ?? 'localhost', $db['port'] ?? 3306, $db['dbname'] ?? ''),
$db['user'] ?? '',
$db['password'] ?? '',
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]
);
$products = $pdo->query(
"SELECT uid, title, subtitle FROM tx_vitec_domain_model_product WHERE deleted = 0 ORDER BY title"
)->fetchAll();
$stmt = $pdo->prepare(
"UPDATE tx_vitec_domain_model_product SET title = ?, subtitle = ?, tstamp = ? WHERE uid = ?"
);
$changed = 0;
$skipped = 0;
foreach ($products as $product) {
if (!preg_match('/^(.*\S)\s*\((.+)\)$/u', trim((string)$product['title']), $m)) {
continue;
}
$newTitle = $m[1];
$descriptor = trim($m[2]);
$subtitle = trim((string)$product['subtitle']);
if ($subtitle !== '' && $subtitle !== $descriptor) {
printf("SKIP %3d | %s -- subtitle belegt: \"%s\" (Deskriptor waere: \"%s\")\n",
$product['uid'], $product['title'], $subtitle, $descriptor);
$skipped++;
continue;
}
printf("%s %3d | \"%s\" -> title \"%s\" | subtitle \"%s\"\n",
$apply ? 'WRITE' : 'plan ', $product['uid'], $product['title'], $newTitle, $descriptor);
if ($apply) {
$stmt->execute([$newTitle, $descriptor, time(), (int)$product['uid']]);
}
$changed++;
}
printf("\n%d %s, %d skipped.%s\n",
$changed,
$apply ? 'written' : 'planned',
$skipped,
$apply ? ' Flush the cache: vendor/bin/typo3 cache:flush' : ' Run again with --apply to write.'
);