3 namespace BookStack\Entities\Controllers;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Entities\Models\PageRevision;
7 use BookStack\Entities\Queries\PageQueries;
8 use BookStack\Entities\Repos\PageRepo;
9 use BookStack\Entities\Repos\RevisionRepo;
10 use BookStack\Entities\Tools\PageContent;
11 use BookStack\Exceptions\NotFoundException;
12 use BookStack\Facades\Activity;
13 use BookStack\Http\Controller;
14 use BookStack\Util\SimpleListOptions;
15 use Illuminate\Http\Request;
16 use Ssddanbrown\HtmlDiff\Diff;
18 class PageRevisionController extends Controller
20 public function __construct(
21 protected PageRepo $pageRepo,
22 protected PageQueries $pageQueries,
23 protected RevisionRepo $revisionRepo,
28 * Shows the last revisions for this page.
30 * @throws NotFoundException
32 public function index(Request $request, string $bookSlug, string $pageSlug)
34 $page = $this->pageQueries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
35 $listOptions = SimpleListOptions::fromRequest($request, 'page_revisions', true)->withSortOptions([
36 'id' => trans('entities.pages_revisions_sort_number')
39 $revisions = $page->revisions()->select([
40 'id', 'page_id', 'name', 'created_at', 'created_by', 'updated_at',
41 'type', 'revision_number', 'summary',
43 ->selectRaw("IF(markdown = '', false, true) as is_markdown")
44 ->with(['page.book', 'createdBy'])
45 ->reorder('id', $listOptions->getOrder())
48 $this->setPageTitle(trans('entities.pages_revisions_named', ['pageName' => $page->getShortName()]));
50 return view('pages.revisions', [
51 'revisions' => $revisions,
53 'listOptions' => $listOptions,
54 'oldestRevisionId' => $page->revisions()->min('id'),
59 * Shows a preview of a single revision.
61 * @throws NotFoundException
63 public function show(string $bookSlug, string $pageSlug, int $revisionId)
65 $page = $this->pageQueries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
66 /** @var ?PageRevision $revision */
67 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
68 if ($revision === null) {
69 throw new NotFoundException();
72 $page->fill($revision->toArray());
73 // TODO - Refactor PageContent so we don't need to juggle this
74 $page->html = $revision->html;
75 $page->html = (new PageContent($page))->render();
77 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
79 return view('pages.revision', [
81 'book' => $page->book,
83 'revision' => $revision,
88 * Shows the changes of a single revision.
90 * @throws NotFoundException
92 public function changes(string $bookSlug, string $pageSlug, int $revisionId)
94 $page = $this->pageQueries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
95 /** @var ?PageRevision $revision */
96 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
97 if ($revision === null) {
98 throw new NotFoundException();
101 $prev = $revision->getPrevious();
102 $prevContent = $prev->html ?? '';
103 $diff = Diff::excecute($prevContent, $revision->html);
105 $page->fill($revision->toArray());
106 // TODO - Refactor PageContent so we don't need to juggle this
107 $page->html = $revision->html;
108 $page->html = (new PageContent($page))->render();
109 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
111 return view('pages.revision', [
113 'book' => $page->book,
115 'revision' => $revision,
120 * Restores a page using the content of the specified revision.
122 * @throws NotFoundException
124 public function restore(string $bookSlug, string $pageSlug, int $revisionId)
126 $page = $this->pageQueries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
127 $this->checkOwnablePermission('page-update', $page);
129 $page = $this->pageRepo->restoreRevision($page, $revisionId);
131 return redirect($page->getUrl());
135 * Deletes a revision using the id of the specified revision.
137 * @throws NotFoundException
139 public function destroy(string $bookSlug, string $pageSlug, int $revId)
141 $page = $this->pageQueries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
142 $this->checkOwnablePermission('page-delete', $page);
144 $revision = $page->revisions()->where('id', '=', $revId)->first();
145 if ($revision === null) {
146 throw new NotFoundException("Revision #{$revId} not found");
149 // Check if it's the latest revision, cannot delete the latest revision.
150 if (intval($page->currentRevision->id ?? null) === intval($revId)) {
151 $this->showErrorNotification(trans('entities.revision_cannot_delete_latest'));
153 return redirect($page->getUrl('/revisions'));
157 Activity::add(ActivityType::REVISION_DELETE, $revision);
159 return redirect($page->getUrl('/revisions'));
163 * Destroys existing drafts, belonging to the current user, for the given page.
165 public function destroyUserDraft(string $pageId)
167 $page = $this->pageQueries->findVisibleByIdOrFail($pageId);
168 $this->revisionRepo->deleteDraftsForCurrentUser($page);
170 return response('', 200);