]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/RegenerateCommentContentCommand.php
f7ec426209f5eb29cf6aadf2eff69c9920cbcd2b
[bookstack] / app / Console / Commands / RegenerateCommentContentCommand.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use BookStack\Activity\CommentRepo;
6 use BookStack\Activity\Models\Comment;
7 use Illuminate\Console\Command;
8 use Illuminate\Support\Facades\DB;
9
10 class RegenerateCommentContentCommand extends Command
11 {
12     /**
13      * The name and signature of the console command.
14      *
15      * @var string
16      */
17     protected $signature = 'bookstack:regenerate-comment-content
18                             {--database= : The database connection to use}';
19
20     /**
21      * The console command description.
22      *
23      * @var string
24      */
25     protected $description = 'Regenerate the stored HTML of all comments';
26
27     /**
28      * Execute the console command.
29      */
30     public function handle(CommentRepo $commentRepo): int
31     {
32         $connection = DB::getDefaultConnection();
33         if ($this->option('database') !== null) {
34             DB::setDefaultConnection($this->option('database'));
35         }
36
37         Comment::query()->chunk(100, function ($comments) use ($commentRepo) {
38             foreach ($comments as $comment) {
39                 $comment->html = $commentRepo->commentToHtml($comment->text);
40                 $comment->save();
41             }
42         });
43
44         DB::setDefaultConnection($connection);
45         $this->comment('Comment HTML content has been regenerated');
46
47         return 0;
48     }
49 }