Skip to content

Commit dab41de

Browse files
committed
format
1 parent 94e8afd commit dab41de

5 files changed

Lines changed: 20 additions & 11 deletions

File tree

app/Actions/Scrape/BulkUpdateOrCreateRawPage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __invoke(Collection $urls, SiteName $siteName, string $html): vo
1919
$now = now();
2020
// Ensure compression since Eloquent's upsert bypasses attribute casts.
2121
$compressed = HtmlCompression::encode($html);
22-
$data = $urls->map(fn(string $url): array => [
22+
$data = $urls->map(fn (string $url): array => [
2323
'url' => $url,
2424
'site_name' => $siteName->value,
2525
'html' => $compressed,

app/Casts/CompressedHtml.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
namespace App\Casts;
66

7+
use App\Support\HtmlCompression;
78
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
89
use Illuminate\Database\Eloquent\Model;
9-
use App\Support\HtmlCompression;
1010

11+
/**
12+
* @implements CastsAttributes<string, string>
13+
*/
1114
final class CompressedHtml implements CastsAttributes
1215
{
1316
/**
@@ -18,6 +21,7 @@ public function get(Model $model, string $key, mixed $value, array $attributes):
1821
if (! is_string($value)) {
1922
return '';
2023
}
24+
2125
return HtmlCompression::decode($value);
2226
}
2327

@@ -27,6 +31,7 @@ public function get(Model $model, string $key, mixed $value, array $attributes):
2731
public function set(Model $model, string $key, mixed $value, array $attributes): string
2832
{
2933
$input = is_string($value) ? $value : '';
34+
3035
return HtmlCompression::encode($input);
3136
}
3237
}

app/Console/Commands/Pages/CompressRawHtmlCommand.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,36 @@ public function handle(): int
3737
RawPage::query()
3838
->select(['id', 'html'])
3939
->orderBy('id')
40-
->chunkById($chunk, function ($chunkRows) use (&$processed, &$skipped, $dryRun, $force) {
41-
foreach ($chunkRows as $rp) {
42-
$raw = $rp->getAttributes()['html'] ?? '';
40+
->chunkById($chunk, function ($chunkRows) use (&$processed, &$skipped, $dryRun, $force): void {
41+
foreach ($chunkRows as $chunkRow) {
42+
$raw = $chunkRow->getAttributes()['html'] ?? '';
4343
// Skip already gzip data unless --force is specified
4444
if (! $force && is_string($raw) && HtmlCompression::isGzip($raw)) {
4545
$skipped++;
46+
4647
continue;
4748
}
4849

4950
if ($dryRun) {
5051
$processed++;
52+
5153
continue;
5254
}
5355

5456
// Reassign to trigger cast compression
5557
// For already compressed data with --force, decode first then re-encode
5658
if ($force && is_string($raw) && HtmlCompression::isGzip($raw)) {
57-
$rp->html = HtmlCompression::decode($raw);
59+
$chunkRow->html = HtmlCompression::decode($raw);
5860
} else {
59-
$rp->html = $rp->html;
6061
}
61-
$rp->saveQuietly();
62+
63+
$chunkRow->saveQuietly();
6264
$processed++;
6365
}
6466
});
6567

6668
$logger->info('Finish backfill: compress raw_pages.html', ['processed' => $processed, 'skipped' => $skipped, 'dry' => $dryRun, 'force' => $force]);
67-
$this->info("processed={$processed} skipped={$skipped} dryRun=" . ($dryRun ? 'yes' : 'no') . ' force=' . ($force ? 'yes' : 'no'));
69+
$this->info(sprintf('processed=%d skipped=%d dryRun=', $processed, $skipped).($dryRun ? 'yes' : 'no').' force='.($force ? 'yes' : 'no'));
6870

6971
return self::SUCCESS;
7072
} catch (\Throwable $throwable) {

app/Support/HtmlCompression.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
final class HtmlCompression
88
{
9-
private const GZIP_MAGIC = "\x1f\x8b";
9+
private const string GZIP_MAGIC = "\x1f\x8b";
1010

1111
public static function isGzip(string $data): bool
1212
{
@@ -28,6 +28,7 @@ public static function encode(string $plain, int $level = 6): string
2828
}
2929

3030
$compressed = gzencode($plain, $level);
31+
3132
return $compressed !== false ? $compressed : $plain;
3233
}
3334

database/migrations/2026_01_12_000000_alter_html_to_longblob_in_raw_pages_table.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
use Illuminate\Database\Migrations\Migration;
66
use Illuminate\Support\Facades\DB;
77

8-
return new class extends Migration {
8+
return new class extends Migration
9+
{
910
public function up(): void
1011
{
1112
DB::statement('ALTER TABLE raw_pages MODIFY html LONGBLOB NOT NULL');

0 commit comments

Comments
 (0)