]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/RegenerateCommentContent.php
Played around with a new app structure
[bookstack] / app / Console / Commands / RegenerateCommentContent.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 RegenerateCommentContent 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 {--database= : The database connection to use.}';
18
19     /**
20      * The console command description.
21      *
22      * @var string
23      */
24     protected $description = 'Regenerate the stored HTML of all comments';
25
26     /**
27      * @var CommentRepo
28      */
29     protected $commentRepo;
30
31     /**
32      * Create a new command instance.
33      */
34     public function __construct(CommentRepo $commentRepo)
35     {
36         $this->commentRepo = $commentRepo;
37         parent::__construct();
38     }
39
40     /**
41      * Execute the console command.
42      *
43      * @return mixed
44      */
45     public function handle()
46     {
47         $connection = DB::getDefaultConnection();
48         if ($this->option('database') !== null) {
49             DB::setDefaultConnection($this->option('database'));
50         }
51
52         Comment::query()->chunk(100, function ($comments) {
53             foreach ($comments as $comment) {
54                 $comment->html = $this->commentRepo->commentToHtml($comment->text);
55                 $comment->save();
56             }
57         });
58
59         DB::setDefaultConnection($connection);
60         $this->comment('Comment HTML content has been regenerated');
61
62         return 0;
63     }
64 }