Files
RomhackPlaza/app/Console/Commands/FixEntriesEncodedDescription.php
2026-07-01 15:11:18 +02:00

66 lines
2.1 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Helpers\MigrationHelpers;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Command;
use App\Models\Entry;
use Illuminate\Support\Facades\DB;
use League\HTMLToMarkdown\HtmlConverter;
#[Signature('fix:entries-encoded-description {--dry-run}')]
#[Description('Remove coded specials chars in description')]
class FixEntriesEncodedDescription extends Command
{
public function handle(): int
{
$dryRun = $this->option('dry-run');
$fixed = 0;
Entry::withTrashed()
->withoutGlobalScopes()
->chunkById(100, function ($entries) use (&$fixed, $dryRun) {
foreach ($entries as $entry) {
$original = $entry->description;
if ($original === null || $original === '') {
continue;
}
$decoded = $original;
for ($i = 0; $i < 20; $i++) {
$next = html_entity_decode($decoded, ENT_QUOTES | ENT_HTML5);
if ($next === $decoded) {
break;
}
$decoded = $next;
}
$decoded = stripslashes($decoded);
if ($decoded !== $original) {
$fixed++;
$this->line("Entry #{$entry->id}: " . $this->preview($original) . ' => ' . $this->preview($decoded));
if (!$dryRun) {
$entry->description = $decoded;
$entry->saveQuietly();
}
}
}
});
$this->info(($dryRun ? '[DRY RUN] ' : '') . "{$fixed} entries fixed.).");
return self::SUCCESS;
}
private function preview(string $text, int $len = 60): string
{
$text = str_replace(["\n", "\r"], ' ', $text);
return mb_strlen($text) > $len ? mb_substr($text, 0, $len) . '...' : $text;
}
}