]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/RegenerateSearchCommand.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / app / Console / Commands / RegenerateSearchCommand.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 RegenerateSearchCommand extends Command
11 {
12     /**
13      * The name and signature of the console command.
14      *
15      * @var string
16      */
17     protected $signature = 'bookstack:regenerate-search 
18                             {--database= : The database connection to use}';
19
20     /**
21      * The console command description.
22      *
23      * @var string
24      */
25     protected $description = 'Re-index all content for searching';
26
27     /**
28      * Execute the console command.
29      */
30     public function handle(SearchIndex $searchIndex): int
31     {
32         $connection = DB::getDefaultConnection();
33         if ($this->option('database') !== null) {
34             DB::setDefaultConnection($this->option('database'));
35         }
36
37         $searchIndex->indexAllEntities(function (Entity $model, int $processed, int $total): void {
38             $this->info('Indexed ' . class_basename($model) . ' entries (' . $processed . '/' . $total . ')');
39         });
40
41         DB::setDefaultConnection($connection);
42         $this->line('Search index regenerated!');
43
44         return static::SUCCESS;
45     }
46 }