1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Entities\Tools\PageContent;
4 use BookStack\Entities\Repos\PageRepo;
5 use BookStack\Exceptions\NotFoundException;
6 use Ssddanbrown\HtmlDiff\Diff;
8 class PageRevisionController extends Controller
14 * PageRevisionController constructor.
16 public function __construct(PageRepo $pageRepo)
18 $this->pageRepo = $pageRepo;
22 * Shows the last revisions for this page.
23 * @throws NotFoundException
25 public function index(string $bookSlug, string $pageSlug)
27 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
28 $this->setPageTitle(trans('entities.pages_revisions_named', ['pageName'=>$page->getShortName()]));
29 return view('pages.revisions', [
36 * Shows a preview of a single revision.
37 * @throws NotFoundException
39 public function show(string $bookSlug, string $pageSlug, int $revisionId)
41 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
42 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
43 if ($revision === null) {
44 throw new NotFoundException();
47 $page->fill($revision->toArray());
48 // TODO - Refactor PageContent so we don't need to juggle this
49 $page->html = $revision->html;
50 $page->html = (new PageContent($page))->render();
52 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
53 return view('pages.revision', [
55 'book' => $page->book,
57 'revision' => $revision
62 * Shows the changes of a single revision.
63 * @throws NotFoundException
65 public function changes(string $bookSlug, string $pageSlug, int $revisionId)
67 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
68 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
69 if ($revision === null) {
70 throw new NotFoundException();
73 $prev = $revision->getPrevious();
74 $prevContent = $prev->html ?? '';
75 $diff = Diff::excecute($prevContent, $revision->html);
77 $page->fill($revision->toArray());
78 // TODO - Refactor PageContent so we don't need to juggle this
79 $page->html = $revision->html;
80 $page->html = (new PageContent($page))->render();
81 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName'=>$page->getShortName()]));
83 return view('pages.revision', [
85 'book' => $page->book,
87 'revision' => $revision
92 * Restores a page using the content of the specified revision.
93 * @throws NotFoundException
95 public function restore(string $bookSlug, string $pageSlug, int $revisionId)
97 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
98 $this->checkOwnablePermission('page-update', $page);
100 $page = $this->pageRepo->restoreRevision($page, $revisionId);
102 return redirect($page->getUrl());
106 * Deletes a revision using the id of the specified revision.
107 * @throws NotFoundException
109 public function destroy(string $bookSlug, string $pageSlug, int $revId)
111 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
112 $this->checkOwnablePermission('page-delete', $page);
114 $revision = $page->revisions()->where('id', '=', $revId)->first();
115 if ($revision === null) {
116 throw new NotFoundException("Revision #{$revId} not found");
119 // Get the current revision for the page
120 $currentRevision = $page->getCurrentRevision();
122 // Check if its the latest revision, cannot delete latest revision.
123 if (intval($currentRevision->id) === intval($revId)) {
124 $this->showErrorNotification(trans('entities.revision_cannot_delete_latest'));
125 return redirect($page->getUrl('/revisions'));
129 $this->showSuccessNotification(trans('entities.revision_delete_success'));
130 return redirect($page->getUrl('/revisions'));