3 use BookStack\Entities\Models\Page;
4 use BookStack\Facades\Theme;
5 use Illuminate\Console\Command;
6 use Illuminate\Database\Eloquent\Builder;
8 // Define the command we want to add to BookStack
9 class PruneRevisionsCommand extends Command
11 // Specify how the command should be called, and the description that'll show for it
12 protected $signature = 'bookstack:prune-revisions {bookslug} {pageslug}';
13 protected $description = 'Remove revisions without changelog and reset numbers for a specific page';
15 // Define the function that'll be called when the command is performed
16 public function handle(): int
18 $pageSlug = $this->argument('pageslug');
19 $bookSlug = $this->argument('bookslug');
21 // Find the provided page using slugs
22 $page = Page::query()->whereHas('book', function (Builder $query) use ($bookSlug) {
23 $query->where('slug', '=', $bookSlug);
24 })->where('slug', '=', $pageSlug)->first();
26 // Return as an error if the page is not found
28 $this->error("Page of slugs {$bookSlug}:{$pageSlug} not found");
32 // Delete revisions without changelog, except if it's the current revision
33 $currentRevision = $page->currentRevision()->first();
34 $deleteCount = $page->allRevisions()
35 ->where('type', '=', 'version')
36 ->where('summary', '=', '')
37 ->where('id', '!=', $currentRevision->id)
40 // Reset numbers on the revisions
41 $revisions = $page->allRevisions()->where('type', '=', 'version')
42 ->orderBy('created_at', 'asc')
45 foreach ($revisions as $index => $revision) {
46 $revision->revision_number = $index + 1;
50 // Update page revision count
51 $page->revision_count = count($revisions);
54 $this->line("Revisions pruned and re-numbered for page [{$page->name}] with {$deleteCount} revisions deleted");
60 // Register our command via the theme system
61 Theme::registerCommand(new PruneRevisionsCommand());