1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Console\Commands;
7
8use App\Models\Forum\TopicCover;
9use Illuminate\Console\Command;
10
11class ForumTopicCoversCleanup extends Command
12{
13 /**
14 * The name and signature of the console command.
15 *
16 * @var string
17 */
18 protected $signature = 'forum:topic-cover-cleanup {--maxdays=}';
19
20 /**
21 * The console command description.
22 *
23 * @var string
24 */
25 protected $description = 'Delete unused topic covers.';
26
27 /**
28 * Execute the console command.
29 *
30 * @return mixed
31 */
32 public function handle()
33 {
34 $createdBefore = now()->subDays(get_int($this->option('maxdays')) ?? 30);
35 $this->line("This will delete unused topic covers before {$createdBefore}.");
36
37 if (!$this->option('no-interaction') && !$this->confirm('Proceed?', true)) {
38 $this->error('Aborted.');
39 return;
40 }
41
42 $progress = $this->output->createProgressBar();
43 $deleted = 0;
44
45 TopicCover::whereNull('topic_id')->chunkById(1000, function ($coversChunk) use ($createdBefore, $progress, &$deleted) {
46 foreach ($coversChunk as $cover) {
47 if ($cover->created_at > $createdBefore) {
48 // we're done, exit loop and chunkById
49 return false;
50 }
51
52 $deleted++;
53 $progress->advance();
54 $cover->delete();
55 }
56 });
57
58 $progress->finish();
59 $this->line('');
60 $this->info("Done. Deleted {$deleted} cover(s).");
61 }
62}