Entscheidung Harry: Link entfernen, Text behalten. Hosts: Queen's Awards Magazine, Sam Minton, EET Group, Tecnomultimedia, Sentry Video Walls, Exertis, InfoComm SEA, SurveyMonkey, Exterity, Eureka Network. Migration unlink_dead_external_20261110 (angewendet). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
77 lines
3.0 KiB
PHP
77 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Old Datapath/VITEC articles link to partner sites that no longer exist
|
|
* (Seobility "Probleme mit externen Links", decision Harry 2026-11-10:
|
|
* remove the link, keep the text). The anchor is replaced by its text.
|
|
*
|
|
* Dry run: php migrations/unlink_dead_external_20261110.php
|
|
* Write: php migrations/unlink_dead_external_20261110.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";
|
|
$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));
|
|
// <a ... href="...host..." ...>text</a> -> text
|
|
$pattern = '~<a\b[^>]*\bhref="[^"]*(?:' . $hostAlt . ')[^"]*"[^>]*>(.*?)</a>~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 <a>-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";
|