diff --git a/migrations/unlink_dead_external_20261110.php b/migrations/unlink_dead_external_20261110.php new file mode 100644 index 0000000..7ec07be --- /dev/null +++ b/migrations/unlink_dead_external_20261110.php @@ -0,0 +1,76 @@ + PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC] +); + +echo $apply ? "== APPLY ==\n" : "== DRY RUN (add --apply to write) ==\n"; +$now = time(); + +$hosts = [ + 'queensawardsmagazine.com', + 'samminton.com', + 'eetgroup.com', + 'broadcast-asia.com', + 'tecnomultimedia.com', + 'sentryvideowalls.com', + 'exertis.co.uk', + 'infocomm-sea.com', + 'surveymonkey.co.uk', + 'exterity.com', + 'eurekanetwork.org', +]; +$hostAlt = implode('|', array_map(fn($h) => preg_quote($h, '~'), $hosts)); +// text -> text +$pattern = '~]*\bhref="[^"]*(?:' . $hostAlt . ')[^"]*"[^>]*>(.*?)~is'; +$purge = []; +$total = 0; + +foreach (['tx_news_domain_model_news' => 'path_segment', 'tt_content' => 'pid'] as $table => $ref) { + $like = implode(' OR ', array_map(fn($h) => "bodytext LIKE '%$h%'", $hosts)); + foreach ($pdo->query("SELECT uid, $ref AS ref, bodytext FROM $table WHERE deleted = 0 AND ($like)") as $r) { + $new = (string)preg_replace_callback($pattern, static function (array $m): string { + return $m[1]; + }, (string)$r['bodytext'], -1, $count); + if ($count === 0 || $new === (string)$r['bodytext']) { + echo " $table #{$r['uid']}: Host im Text, aber kein -Tag erkannt (manuell pruefen)\n"; + continue; + } + preg_match_all('~https?://[^"\s<]*(?:' . $hostAlt . ')[^"\s<]*~i', (string)$r['bodytext'], $urls); + printf(" %s #%-4d (%s): %d Link(s) entfernt: %s\n", $table, $r['uid'], $r['ref'], $count, implode(', ', array_unique($urls[0]))); + $total += $count; + if ($table === 'tx_news_domain_model_news') { $purge[] = '/news/article/' . $r['ref']; } + if ($apply) { + $pdo->prepare("UPDATE $table SET bodytext = ?, tstamp = ? WHERE uid = ?")->execute([$new, $now, (int)$r['uid']]); + } + } +} +echo "\n$total Link(s) in " . count($purge) . " News" . ($apply ? ' entfernt' : ' wuerden entfernt') . ".\n"; +echo "SSR-Pfade:\n " . implode("\n ", $purge) . "\n";