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\Permissions\Permission;
15 use BookStack\Util\SimpleListOptions;
16 use Illuminate\Http\Request;
17 use Ssddanbrown\HtmlDiff\Diff;
19 class PageRevisionController extends Controller
21 public function __construct(
22 protected PageRepo $pageRepo,
23 protected PageQueries $pageQueries,
24 protected RevisionRepo $revisionRepo,
29 * Shows the last revisions for this page.
31 * @throws NotFoundException
33 public function index(Request $request, string $bookSlug, string $pageSlug)
35 $page = $this->pageQueries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
36 $listOptions = SimpleListOptions::fromRequest($request, 'page_revisions', true)->withSortOptions([
37 'id' => trans('entities.pages_revisions_sort_number')
40 $revisions = $page->revisions()->select([
41 'id', 'page_id', 'name', 'created_at', 'created_by', 'updated_at',
42 'type', 'revision_number', 'summary',
44 ->selectRaw("IF(markdown = '', false, true) as is_markdown")
45 ->with(['page.book', 'createdBy'])
46 ->reorder('id', $listOptions->getOrder())
49 $this->setPageTitle(trans('entities.pages_revisions_named', ['pageName' => $page->getShortName()]));
51 return view('pages.revisions', [
52 'revisions' => $revisions,
54 'listOptions' => $listOptions,
55 'oldestRevisionId' => $page->revisions()->min('id'),
60 * Shows a preview of a single revision.
62 * @throws NotFoundException
64 public function show(string $bookSlug, string $pageSlug, int $revisionId)
66 $page = $this->pageQueries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
67 /** @var ?PageRevision $revision */
68 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
69 if ($revision === null) {
70 throw new NotFoundException();
73 $page->fill($revision->toArray());
74 // TODO - Refactor PageContent so we don't need to juggle this
75 $page->html = $revision->html;
76 $page->html = (new PageContent($page))->render();
78 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
80 return view('pages.revision', [
82 'book' => $page->book,
84 'revision' => $revision,
89 * Shows the changes of a single revision.
91 * @throws NotFoundException
93 public function changes(string $bookSlug, string $pageSlug, int $revisionId)
95 $page = $this->pageQueries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
96 /** @var ?PageRevision $revision */
97 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
98 if ($revision === null) {
99 throw new NotFoundException();
102 $prev = $revision->getPreviousRevision();
103 $prevContent = $prev->html ?? '';
104 $diff = Diff::excecute($prevContent, $revision->html);
106 $page->fill($revision->toArray());
107 // TODO - Refactor PageContent so we don't need to juggle this
108 $page->html = $revision->html;
109 $page->html = (new PageContent($page))->render();
110 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
112 return view('pages.revision', [
114 'book' => $page->book,
116 'revision' => $revision,
121 * Restores a page using the content of the specified revision.
123 * @throws NotFoundException
125 public function restore(string $bookSlug, string $pageSlug, int $revisionId)
127 $page = $this->pageQueries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
128 $this->checkOwnablePermission(Permission::PageUpdate, $page);
130 $page = $this->pageRepo->restoreRevision($page, $revisionId);
132 return redirect($page->getUrl());
136 * Deletes a revision using the id of the specified revision.
138 * @throws NotFoundException
140 public function destroy(string $bookSlug, string $pageSlug, int $revId)
142 $page = $this->pageQueries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
143 $this->checkOwnablePermission(Permission::PageDelete, $page);
145 $revision = $page->revisions()->where('id', '=', $revId)->first();
146 if ($revision === null) {
147 throw new NotFoundException("Revision #{$revId} not found");
150 // Check if it's the latest revision, cannot delete the latest revision.
151 if (intval($page->currentRevision->id ?? null) === intval($revId)) {
152 $this->showErrorNotification(trans('entities.revision_cannot_delete_latest'));
154 return redirect($page->getUrl('/revisions'));
158 Activity::add(ActivityType::REVISION_DELETE, $revision);
160 return redirect($page->getUrl('/revisions'));
164 * Destroys existing drafts, belonging to the current user, for the given page.
166 public function destroyUserDraft(string $pageId)
168 $page = $this->pageQueries->findVisibleByIdOrFail($pageId);
169 $this->revisionRepo->deleteDraftsForCurrentUser($page);
171 return response('', 200);