2017-03-19 13:48:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Console\Commands;
|
|
|
|
|
2021-11-11 15:10:11 +01:00
|
|
|
use BookStack\Entities\Models\Entity;
|
2022-08-16 12:27:22 +02:00
|
|
|
use BookStack\Search\SearchIndex;
|
2017-03-19 13:48:44 +01:00
|
|
|
use Illuminate\Console\Command;
|
2021-09-26 16:48:22 +02:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2017-03-19 13:48:44 +01:00
|
|
|
|
2023-05-24 14:21:46 +02:00
|
|
|
class RegenerateSearchCommand extends Command
|
2017-03-19 13:48:44 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2023-05-24 13:59:50 +02:00
|
|
|
protected $signature = 'bookstack:regenerate-search
|
|
|
|
{--database= : The database connection to use}';
|
2017-03-19 13:48:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-10-01 18:51:59 +02:00
|
|
|
protected $description = 'Re-index all content for searching';
|
2017-03-19 13:48:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*/
|
2023-05-24 13:59:50 +02:00
|
|
|
public function handle(SearchIndex $searchIndex): int
|
2017-03-19 13:48:44 +01:00
|
|
|
{
|
2020-04-09 17:58:40 +02:00
|
|
|
$connection = DB::getDefaultConnection();
|
2017-03-26 20:24:57 +02:00
|
|
|
if ($this->option('database') !== null) {
|
2020-04-09 17:58:40 +02:00
|
|
|
DB::setDefaultConnection($this->option('database'));
|
2017-03-26 20:24:57 +02:00
|
|
|
}
|
|
|
|
|
2023-05-24 13:59:50 +02:00
|
|
|
$searchIndex->indexAllEntities(function (Entity $model, int $processed, int $total): void {
|
2021-11-11 15:10:11 +01:00
|
|
|
$this->info('Indexed ' . class_basename($model) . ' entries (' . $processed . '/' . $total . ')');
|
|
|
|
});
|
|
|
|
|
2020-04-09 17:58:40 +02:00
|
|
|
DB::setDefaultConnection($connection);
|
2021-11-11 15:10:11 +01:00
|
|
|
$this->line('Search index regenerated!');
|
|
|
|
|
|
|
|
return static::SUCCESS;
|
2017-03-19 13:48:44 +01:00
|
|
|
}
|
|
|
|
}
|