| 
									
										
										
										
											2025-02-10 01:44:24 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Console\Commands; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use BookStack\Entities\Models\Book; | 
					
						
							|  |  |  | use BookStack\Sorting\BookSorter; | 
					
						
							| 
									
										
										
										
											2025-02-11 22:36:25 +08:00
										 |  |  | use BookStack\Sorting\SortRule; | 
					
						
							| 
									
										
										
										
											2025-02-10 01:44:24 +08:00
										 |  |  | use Illuminate\Console\Command; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-11 23:29:16 +08:00
										 |  |  | class AssignSortRuleCommand extends Command | 
					
						
							| 
									
										
										
										
											2025-02-10 01:44:24 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * The name and signature of the console command. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-02-11 23:29:16 +08:00
										 |  |  |     protected $signature = 'bookstack:assign-sort-rule | 
					
						
							|  |  |  |                             {sort-rule=0: ID of the sort rule to apply} | 
					
						
							| 
									
										
										
										
											2025-02-10 01:44:24 +08:00
										 |  |  |                             {--all-books : Apply to all books in the system} | 
					
						
							| 
									
										
										
										
											2025-02-11 23:29:16 +08:00
										 |  |  |                             {--books-without-sort : Apply to only books without a sort rule already assigned} | 
					
						
							|  |  |  |                             {--books-with-sort= : Apply to only books with the sort rule of given id}'; | 
					
						
							| 
									
										
										
										
											2025-02-10 01:44:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * The console command description. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-02-11 23:29:16 +08:00
										 |  |  |     protected $description = 'Assign a sort rule to content in the system'; | 
					
						
							| 
									
										
										
										
											2025-02-10 01:44:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Execute the console command. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function handle(BookSorter $sorter): int | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-02-11 23:29:16 +08:00
										 |  |  |         $sortRuleId = intval($this->argument('sort-rule')) ?? 0; | 
					
						
							|  |  |  |         if ($sortRuleId === 0) { | 
					
						
							|  |  |  |             return $this->listSortRules(); | 
					
						
							| 
									
										
										
										
											2025-02-10 01:44:24 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-11 23:29:16 +08:00
										 |  |  |         $rule = SortRule::query()->find($sortRuleId); | 
					
						
							| 
									
										
										
										
											2025-02-10 01:44:24 +08:00
										 |  |  |         if ($this->option('all-books')) { | 
					
						
							|  |  |  |             $query = Book::query(); | 
					
						
							|  |  |  |         } else if ($this->option('books-without-sort')) { | 
					
						
							| 
									
										
										
										
											2025-02-11 23:29:16 +08:00
										 |  |  |             $query = Book::query()->whereNull('sort_rule_id'); | 
					
						
							| 
									
										
										
										
											2025-02-10 01:44:24 +08:00
										 |  |  |         } else if ($this->option('books-with-sort')) { | 
					
						
							|  |  |  |             $sortId = intval($this->option('books-with-sort')) ?: 0; | 
					
						
							|  |  |  |             if (!$sortId) { | 
					
						
							|  |  |  |                 $this->error("Provided --books-with-sort option value is invalid"); | 
					
						
							|  |  |  |                 return 1; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-02-11 23:29:16 +08:00
										 |  |  |             $query = Book::query()->where('sort_rule_id', $sortId); | 
					
						
							| 
									
										
										
										
											2025-02-10 01:44:24 +08:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2025-02-10 07:24:36 +08:00
										 |  |  |             $this->error("No option provided to specify target. Run with the -h option to see all available options."); | 
					
						
							| 
									
										
										
										
											2025-02-10 01:44:24 +08:00
										 |  |  |             return 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-11 23:29:16 +08:00
										 |  |  |         if (!$rule) { | 
					
						
							|  |  |  |             $this->error("Sort rule of provided id {$sortRuleId} not found!"); | 
					
						
							| 
									
										
										
										
											2025-02-10 01:44:24 +08:00
										 |  |  |             return 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $count = $query->clone()->count(); | 
					
						
							| 
									
										
										
										
											2025-02-11 23:29:16 +08:00
										 |  |  |         $this->warn("This will apply sort rule [{$rule->id}: {$rule->name}] to {$count} book(s) and run the sort on each."); | 
					
						
							| 
									
										
										
										
											2025-02-10 01:44:24 +08:00
										 |  |  |         $confirmed = $this->confirm("Are you sure you want to continue?"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$confirmed) { | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $processed = 0; | 
					
						
							| 
									
										
										
										
											2025-02-11 23:29:16 +08:00
										 |  |  |         $query->chunkById(10, function ($books) use ($rule, $sorter, $count, &$processed) { | 
					
						
							| 
									
										
										
										
											2025-02-10 01:44:24 +08:00
										 |  |  |             $max = min($count, ($processed + 10)); | 
					
						
							|  |  |  |             $this->info("Applying to {$processed}-{$max} of {$count} books"); | 
					
						
							|  |  |  |             foreach ($books as $book) { | 
					
						
							| 
									
										
										
										
											2025-02-11 23:29:16 +08:00
										 |  |  |                 $book->sort_rule_id = $rule->id; | 
					
						
							| 
									
										
										
										
											2025-02-10 01:44:24 +08:00
										 |  |  |                 $book->save(); | 
					
						
							|  |  |  |                 $sorter->runBookAutoSort($book); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $processed = $max; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-10 07:24:36 +08:00
										 |  |  |         $this->info("Sort applied to {$processed} book(s)!"); | 
					
						
							| 
									
										
										
										
											2025-02-10 01:44:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-11 23:29:16 +08:00
										 |  |  |     protected function listSortRules(): int | 
					
						
							| 
									
										
										
										
											2025-02-10 01:44:24 +08:00
										 |  |  |     { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-11 23:29:16 +08:00
										 |  |  |         $rules = SortRule::query()->orderBy('id', 'asc')->get(); | 
					
						
							|  |  |  |         $this->error("Sort rule ID required!"); | 
					
						
							|  |  |  |         $this->warn("\nAvailable sort rules:"); | 
					
						
							|  |  |  |         foreach ($rules as $rule) { | 
					
						
							|  |  |  |             $this->info("{$rule->id}: {$rule->name}"); | 
					
						
							| 
									
										
										
										
											2025-02-10 01:44:24 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |