1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Entities\Repos\PageRepo;
4 use BookStack\Exceptions\NotFoundException;
5 use BookStack\Facades\Activity;
6 use GatherContent\Htmldiff\Htmldiff;
8 class PageRevisionController extends Controller
14 * PageRevisionController constructor.
16 public function __construct(PageRepo $pageRepo)
18 $this->pageRepo = $pageRepo;
19 parent::__construct();
23 * Shows the last revisions for this page.
24 * @throws NotFoundException
26 public function index(string $bookSlug, string $pageSlug)
28 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
29 $this->setPageTitle(trans('entities.pages_revisions_named', ['pageName'=>$page->getShortName()]));
30 return view('pages.revisions', [
37 * Shows a preview of a single revision.
38 * @throws NotFoundException
40 public function show(string $bookSlug, string $pageSlug, int $revisionId)
42 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
43 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
44 if ($revision === null) {
45 throw new NotFoundException();
48 $page->fill($revision->toArray());
50 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
51 return view('pages.revision', [
53 'book' => $page->book,
55 'revision' => $revision
60 * Shows the changes of a single revision.
61 * @throws NotFoundException
63 public function changes(string $bookSlug, string $pageSlug, int $revisionId)
65 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
66 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
67 if ($revision === null) {
68 throw new NotFoundException();
71 $prev = $revision->getPrevious();
72 $prevContent = $prev->html ?? '';
73 $diff = (new Htmldiff)->diff($prevContent, $revision->html);
75 $page->fill($revision->toArray());
76 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName'=>$page->getShortName()]));
78 return view('pages.revision', [
80 'book' => $page->book,
82 'revision' => $revision
87 * Restores a page using the content of the specified revision.
88 * @throws NotFoundException
90 public function restore(string $bookSlug, string $pageSlug, int $revisionId)
92 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
93 $this->checkOwnablePermission('page-update', $page);
95 $page = $this->pageRepo->restoreRevision($page, $revisionId);
97 Activity::add($page, 'page_restore', $page->book->id);
98 return redirect($page->getUrl());
102 * Deletes a revision using the id of the specified revision.
103 * @throws NotFoundException
105 public function destroy(string $bookSlug, string $pageSlug, int $revId)
107 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
108 $this->checkOwnablePermission('page-delete', $page);
110 $revision = $page->revisions()->where('id', '=', $revId)->first();
111 if ($revision === null) {
112 throw new NotFoundException("Revision #{$revId} not found");
115 // Get the current revision for the page
116 $currentRevision = $page->getCurrentRevision();
118 // Check if its the latest revision, cannot delete latest revision.
119 if (intval($currentRevision->id) === intval($revId)) {
120 $this->showErrorNotification(trans('entities.revision_cannot_delete_latest'));
121 return redirect($page->getUrl('/revisions'));
125 $this->showSuccessNotification(trans('entities.revision_delete_success'));
126 return redirect($page->getUrl('/revisions'));