3 namespace BookStack\Http\Controllers;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Entities\Models\PageRevision;
7 use BookStack\Entities\Repos\PageRepo;
8 use BookStack\Entities\Tools\PageContent;
9 use BookStack\Exceptions\NotFoundException;
10 use BookStack\Facades\Activity;
11 use BookStack\Util\SimpleListOptions;
12 use Illuminate\Http\Request;
13 use Ssddanbrown\HtmlDiff\Diff;
15 class PageRevisionController extends Controller
17 protected PageRepo $pageRepo;
19 public function __construct(PageRepo $pageRepo)
21 $this->pageRepo = $pageRepo;
25 * Shows the last revisions for this page.
27 * @throws NotFoundException
29 public function index(Request $request, string $bookSlug, string $pageSlug)
31 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
32 $listOptions = SimpleListOptions::fromRequest($request, 'page_revisions', true)->withSortOptions([
33 'id' => trans('entities.pages_revisions_sort_number')
36 $revisions = $page->revisions()->select([
37 'id', 'page_id', 'name', 'created_at', 'created_by', 'updated_at',
38 'type', 'revision_number', 'summary',
40 ->selectRaw("IF(markdown = '', false, true) as is_markdown")
41 ->with(['page.book', 'createdBy'])
42 ->reorder('id', $listOptions->getOrder())
43 ->reorder('created_at', $listOptions->getOrder())
46 $this->setPageTitle(trans('entities.pages_revisions_named', ['pageName' => $page->getShortName()]));
48 return view('pages.revisions', [
49 'revisions' => $revisions,
51 'listOptions' => $listOptions,
56 * Shows a preview of a single revision.
58 * @throws NotFoundException
60 public function show(string $bookSlug, string $pageSlug, int $revisionId)
62 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
63 /** @var ?PageRevision $revision */
64 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
65 if ($revision === null) {
66 throw new NotFoundException();
69 $page->fill($revision->toArray());
70 // TODO - Refactor PageContent so we don't need to juggle this
71 $page->html = $revision->html;
72 $page->html = (new PageContent($page))->render();
74 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
76 return view('pages.revision', [
78 'book' => $page->book,
80 'revision' => $revision,
85 * Shows the changes of a single revision.
87 * @throws NotFoundException
89 public function changes(string $bookSlug, string $pageSlug, int $revisionId)
91 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
92 /** @var ?PageRevision $revision */
93 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
94 if ($revision === null) {
95 throw new NotFoundException();
98 $prev = $revision->getPrevious();
99 $prevContent = $prev->html ?? '';
100 $diff = Diff::excecute($prevContent, $revision->html);
102 $page->fill($revision->toArray());
103 // TODO - Refactor PageContent so we don't need to juggle this
104 $page->html = $revision->html;
105 $page->html = (new PageContent($page))->render();
106 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
108 return view('pages.revision', [
110 'book' => $page->book,
112 'revision' => $revision,
117 * Restores a page using the content of the specified revision.
119 * @throws NotFoundException
121 public function restore(string $bookSlug, string $pageSlug, int $revisionId)
123 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
124 $this->checkOwnablePermission('page-update', $page);
126 $page = $this->pageRepo->restoreRevision($page, $revisionId);
128 return redirect($page->getUrl());
132 * Deletes a revision using the id of the specified revision.
134 * @throws NotFoundException
136 public function destroy(string $bookSlug, string $pageSlug, int $revId)
138 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
139 $this->checkOwnablePermission('page-delete', $page);
141 $revision = $page->revisions()->where('id', '=', $revId)->first();
142 if ($revision === null) {
143 throw new NotFoundException("Revision #{$revId} not found");
146 // Check if it's the latest revision, cannot delete the latest revision.
147 if (intval($page->currentRevision->id ?? null) === intval($revId)) {
148 $this->showErrorNotification(trans('entities.revision_cannot_delete_latest'));
150 return redirect($page->getUrl('/revisions'));
154 Activity::add(ActivityType::REVISION_DELETE, $revision);
155 $this->showSuccessNotification(trans('entities.revision_delete_success'));
157 return redirect($page->getUrl('/revisions'));