38 oeffentliche Firmen tragen eine URL im Namen ("Firma - https://..."),
laufend ueber die API. Nicht ablehnen (traefe die Meldung), sondern
normalisieren: URL raus, Rest bleibt Name, URL ins leere Website-Feld;
besteht der Name nur aus der URL, wird der Host zum Namen. Am API-Eingang
vor der Zuordnung; im Bestand per companies:strip-links-from-names mit
Dry-Run, Slugs bleiben.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
101 lines
3.6 KiB
PHP
101 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\ActivityType;
|
|
use App\Models\Company;
|
|
use App\Scopes\PortalScope;
|
|
use App\Services\ActivityLog;
|
|
use App\Services\Company\CompanyNameNormalizer;
|
|
use App\Support\WebCaches;
|
|
use Illuminate\Console\Command;
|
|
|
|
/**
|
|
* Bestandsbereinigung zu CompanyNameNormalizer (17.09.2026): Firmennamen
|
|
* mit Link („Firma - https://…", „www.beispiel.de") werden einmalig
|
|
* bereinigt, die URL wandert ins leere Website-Feld. Slugs bleiben — die
|
|
* Newsroom-URLs sind draußen verlinkt.
|
|
*/
|
|
class StripLinksFromCompanyNames extends Command
|
|
{
|
|
protected $signature = 'companies:strip-links-from-names
|
|
{--dry-run : Nur Vorher/Nachher zeigen, nichts schreiben}
|
|
{--all : Auch inaktive Firmen ohne veröffentlichte Meldung}';
|
|
|
|
protected $description = 'Zieht Links aus Firmennamen und legt sie ins Website-Feld (Bestandsbereinigung).';
|
|
|
|
public function handle(ActivityLog $activityLog): int
|
|
{
|
|
$query = Company::query()
|
|
->withoutGlobalScope(PortalScope::class)
|
|
->whereNull('gone_at')
|
|
// Grobfilter in SQL (portabel, SQLite kennt kein REGEXP), die
|
|
// eigentliche Erkennung macht der Normalizer in PHP.
|
|
->where(fn ($q) => $q->where('name', 'like', '%http://%')
|
|
->orWhere('name', 'like', '%https://%')
|
|
->orWhere('name', 'like', '%www.%')
|
|
->orWhere('name', 'like', '%/%'))
|
|
->orderBy('id');
|
|
|
|
// Ohne --all nur, was jemand sehen kann: aktive Firmen mit
|
|
// veröffentlichter Meldung. Der Rest ist Datenmüll ohne Newsroom-Seite.
|
|
if (! $this->option('all')) {
|
|
$query->where('is_active', true)
|
|
->whereHas('pressReleases', fn ($q) => $q->withoutGlobalScope(PortalScope::class)->where('status', 'published'));
|
|
}
|
|
|
|
$rows = [];
|
|
$changes = [];
|
|
|
|
foreach ($query->cursor() as $company) {
|
|
if (! CompanyNameNormalizer::containsUrl((string) $company->name)) {
|
|
continue;
|
|
}
|
|
|
|
$normalized = CompanyNameNormalizer::normalize((string) $company->name, $company->website);
|
|
|
|
if ($normalized['removed'] === [] || $normalized['name'] === $company->name) {
|
|
continue;
|
|
}
|
|
|
|
$rows[] = [$company->id, $company->name, $normalized['name'], $normalized['website'] ?? '—'];
|
|
$changes[] = [$company, $normalized];
|
|
}
|
|
|
|
if ($rows === []) {
|
|
$this->info('Keine Firmennamen mit Link gefunden.');
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
$this->table(['ID', 'Vorher', 'Nachher', 'Website'], $rows);
|
|
|
|
if ($this->option('dry-run')) {
|
|
$this->info(sprintf('[Dry-Run] %d Firmennamen würden bereinigt.', count($rows)));
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
foreach ($changes as [$company, $normalized]) {
|
|
$before = $company->name;
|
|
$company->forceFill([
|
|
'name' => $normalized['name'],
|
|
'website' => $normalized['website'],
|
|
])->save();
|
|
|
|
$activityLog->record(
|
|
ActivityType::Correction,
|
|
sprintf('Firma #%d: Link aus dem Namen entfernt („%s“ → „%s“)', $company->getKey(), $before, $company->name),
|
|
null,
|
|
$company,
|
|
['removed' => $normalized['removed']],
|
|
'system',
|
|
);
|
|
}
|
|
|
|
WebCaches::forgetPublicPages();
|
|
$this->info(sprintf('%d Firmennamen bereinigt.', count($changes)));
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|