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