Ordner mit genau einer Uebersichtsseite leiten auf diese (RF Gateway, AV over IP & KVM)
Nachtrag zu 1b72e65 (Vorschlag Content-Import-Session): statt /products
zeigen die Redirects der Ordner rf-gateway und av-over-ip-and-kvm samt
ihrer Alt-URL-Redirects auf /products/avedia-modular-system bzw.
/products/aligo. Migration folder_redirect_targets_20261111 (angewendet).
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
1b72e65a1d
commit
cda26947f2
1 file changed
+55
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Follow-up to folder_pages_redirects_20261111: a product folder that holds
|
||||
* exactly one visible overview page sends its visitors to that page instead
|
||||
* of /products (RF Gateway -> Avedia Modular System, AV over IP & KVM -> Aligo).
|
||||
* Applies to the folder's own redirects and to older redirects that were
|
||||
* pointed at /products in the previous step because they targeted the folder.
|
||||
*
|
||||
* Dry run: php migrations/folder_redirect_targets_20261111.php
|
||||
* Write: php migrations/folder_redirect_targets_20261111.php --apply
|
||||
*/
|
||||
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', '1');
|
||||
|
||||
$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("no project root\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]
|
||||
);
|
||||
|
||||
echo $apply ? "== APPLY ==\n" : "== DRY RUN (add --apply to write) ==\n";
|
||||
|
||||
// Older redirects (sys_redirect uid) that targeted the folder before the previous step.
|
||||
$legacyByFolder = [566 => [34, 124, 125], 590 => [63, 66]];
|
||||
$n = 0;
|
||||
foreach ($pdo->query("SELECT uid, slug, title FROM pages WHERE deleted = 0 AND hidden = 0 AND doktype = 254 AND slug LIKE '/products/%' ORDER BY slug") as $folder) {
|
||||
$children = $pdo->query("SELECT uid, slug, title FROM pages WHERE deleted = 0 AND hidden = 0 AND doktype = 1 AND pid = {$folder['uid']}")->fetchAll();
|
||||
if (count($children) !== 1) { continue; }
|
||||
$target = $children[0]['slug'];
|
||||
printf(" Ordner #%d %s -> einzige Seite #%d %s\n", $folder['uid'], $folder['slug'], $children[0]['uid'], $target);
|
||||
$uids = $legacyByFolder[(int)$folder['uid']] ?? [];
|
||||
$st = $pdo->prepare("SELECT uid, source_path, target FROM sys_redirect WHERE deleted = 0 AND target = '/products' AND (source_path = ? OR source_path = ?" . ($uids ? ' OR uid IN (' . implode(',', array_map('intval', $uids)) . ')' : '') . ")");
|
||||
$st->execute([$folder['slug'], $folder['slug'] . '/']);
|
||||
foreach ($st->fetchAll() as $r) {
|
||||
printf(" redirect #%-4d %-55s /products -> %s\n", $r['uid'], $r['source_path'], $target);
|
||||
$n++;
|
||||
if ($apply) { $pdo->prepare("UPDATE sys_redirect SET target = ?, updatedon = UNIX_TIMESTAMP() WHERE uid = ?")->execute([$target, (int)$r['uid']]); }
|
||||
}
|
||||
}
|
||||
echo "\n$n Redirect(s) " . ($apply ? 'umgebogen' : 'wuerden umgebogen') . ".\n";
|
||||
Reference in new issue
Block a user