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