]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/PageRevisionController.php
Revisions: Hid changes link for oldest revision
[bookstack] / app / Entities / Controllers / PageRevisionController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
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;
17
18 class PageRevisionController extends Controller
19 {
20     public function __construct(
21         protected PageRepo $pageRepo,
22         protected PageQueries $pageQueries,
23         protected RevisionRepo $revisionRepo,
24     ) {
25     }
26
27     /**
28      * Shows the last revisions for this page.
29      *
30      * @throws NotFoundException
31      */
32     public function index(Request $request, string $bookSlug, string $pageSlug)
33     {
34         $page = $this->pageQueries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
35         $listOptions = SimpleListOptions::fromRequest($request, 'page_revisions', true)->withSortOptions([
36             'id' => trans('entities.pages_revisions_sort_number')
37         ]);
38
39         $revisions = $page->revisions()->select([
40                 'id', 'page_id', 'name', 'created_at', 'created_by', 'updated_at',
41                 'type', 'revision_number', 'summary',
42             ])
43             ->selectRaw("IF(markdown = '', false, true) as is_markdown")
44             ->with(['page.book', 'createdBy'])
45             ->reorder('id', $listOptions->getOrder())
46             ->paginate(50);
47
48         $this->setPageTitle(trans('entities.pages_revisions_named', ['pageName' => $page->getShortName()]));
49
50         return view('pages.revisions', [
51             'revisions'   => $revisions,
52             'page'        => $page,
53             'listOptions' => $listOptions,
54             'oldestRevisionId' => $page->revisions()->min('id'),
55         ]);
56     }
57
58     /**
59      * Shows a preview of a single revision.
60      *
61      * @throws NotFoundException
62      */
63     public function show(string $bookSlug, string $pageSlug, int $revisionId)
64     {
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();
70         }
71
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();
76
77         $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
78
79         return view('pages.revision', [
80             'page'     => $page,
81             'book'     => $page->book,
82             'diff'     => null,
83             'revision' => $revision,
84         ]);
85     }
86
87     /**
88      * Shows the changes of a single revision.
89      *
90      * @throws NotFoundException
91      */
92     public function changes(string $bookSlug, string $pageSlug, int $revisionId)
93     {
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();
99         }
100
101         $prev = $revision->getPrevious();
102         $prevContent = $prev->html ?? '';
103         $diff = Diff::excecute($prevContent, $revision->html);
104
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()]));
110
111         return view('pages.revision', [
112             'page'     => $page,
113             'book'     => $page->book,
114             'diff'     => $diff,
115             'revision' => $revision,
116         ]);
117     }
118
119     /**
120      * Restores a page using the content of the specified revision.
121      *
122      * @throws NotFoundException
123      */
124     public function restore(string $bookSlug, string $pageSlug, int $revisionId)
125     {
126         $page = $this->pageQueries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
127         $this->checkOwnablePermission('page-update', $page);
128
129         $page = $this->pageRepo->restoreRevision($page, $revisionId);
130
131         return redirect($page->getUrl());
132     }
133
134     /**
135      * Deletes a revision using the id of the specified revision.
136      *
137      * @throws NotFoundException
138      */
139     public function destroy(string $bookSlug, string $pageSlug, int $revId)
140     {
141         $page = $this->pageQueries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
142         $this->checkOwnablePermission('page-delete', $page);
143
144         $revision = $page->revisions()->where('id', '=', $revId)->first();
145         if ($revision === null) {
146             throw new NotFoundException("Revision #{$revId} not found");
147         }
148
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'));
152
153             return redirect($page->getUrl('/revisions'));
154         }
155
156         $revision->delete();
157         Activity::add(ActivityType::REVISION_DELETE, $revision);
158
159         return redirect($page->getUrl('/revisions'));
160     }
161
162     /**
163      * Destroys existing drafts, belonging to the current user, for the given page.
164      */
165     public function destroyUserDraft(string $pageId)
166     {
167         $page = $this->pageQueries->findVisibleByIdOrFail($pageId);
168         $this->revisionRepo->deleteDraftsForCurrentUser($page);
169
170         return response('', 200);
171     }
172 }