]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/RegenerateSearch.php
Fixed role entity permissions ignoring inheritance
[bookstack] / app / Console / Commands / RegenerateSearch.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use BookStack\Entities\Models\Entity;
6 use BookStack\Search\SearchIndex;
7 use Illuminate\Console\Command;
8 use Illuminate\Support\Facades\DB;
9
10 class RegenerateSearch extends Command
11 {
12     /**
13      * The name and signature of the console command.
14      *
15      * @var string
16      */
17     protected $signature = 'bookstack:regenerate-search {--database= : The database connection to use.}';
18
19     /**
20      * The console command description.
21      *
22      * @var string
23      */
24     protected $description = 'Re-index all content for searching';
25
26     /**
27      * @var SearchIndex
28      */
29     protected $searchIndex;
30
31     /**
32      * Create a new command instance.
33      */
34     public function __construct(SearchIndex $searchIndex)
35     {
36         parent::__construct();
37         $this->searchIndex = $searchIndex;
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         $this->searchIndex->indexAllEntities(function (Entity $model, int $processed, int $total): void {
53             $this->info('Indexed ' . class_basename($model) . ' entries (' . $processed . '/' . $total . ')');
54         });
55
56         DB::setDefaultConnection($connection);
57         $this->line('Search index regenerated!');
58
59         return static::SUCCESS;
60     }
61 }