diff --git a/migrations/split_title_descriptors.php b/migrations/split_title_descriptors.php new file mode 100644 index 0000000..2a80eae --- /dev/null +++ b/migrations/split_title_descriptors.php @@ -0,0 +1,82 @@ + 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.' +);