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