]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageRevisionController.php
Added force option for update-url command
[bookstack] / app / Http / Controllers / PageRevisionController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
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;
14
15 class PageRevisionController extends Controller
16 {
17     protected PageRepo $pageRepo;
18
19     public function __construct(PageRepo $pageRepo)
20     {
21         $this->pageRepo = $pageRepo;
22     }
23
24     /**
25      * Shows the last revisions for this page.
26      *
27      * @throws NotFoundException
28      */
29     public function index(Request $request, string $bookSlug, string $pageSlug)
30     {
31         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
32         $listOptions = SimpleListOptions::fromRequest($request, 'page_revisions', true)->withSortOptions([
33             'id' => trans('entities.pages_revisions_sort_number')
34         ]);
35
36         $revisions = $page->revisions()->select([
37                 'id', 'page_id', 'name', 'created_at', 'created_by', 'updated_at',
38                 'type', 'revision_number', 'summary',
39             ])
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())
44             ->paginate(50);
45
46         $this->setPageTitle(trans('entities.pages_revisions_named', ['pageName' => $page->getShortName()]));
47
48         return view('pages.revisions', [
49             'revisions'   => $revisions,
50             'page'        => $page,
51             'listOptions' => $listOptions,
52         ]);
53     }
54
55     /**
56      * Shows a preview of a single revision.
57      *
58      * @throws NotFoundException
59      */
60     public function show(string $bookSlug, string $pageSlug, int $revisionId)
61     {
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();
67         }
68
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();
73
74         $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
75
76         return view('pages.revision', [
77             'page'     => $page,
78             'book'     => $page->book,
79             'diff'     => null,
80             'revision' => $revision,
81         ]);
82     }
83
84     /**
85      * Shows the changes of a single revision.
86      *
87      * @throws NotFoundException
88      */
89     public function changes(string $bookSlug, string $pageSlug, int $revisionId)
90     {
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();
96         }
97
98         $prev = $revision->getPrevious();
99         $prevContent = $prev->html ?? '';
100         $diff = Diff::excecute($prevContent, $revision->html);
101
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()]));
107
108         return view('pages.revision', [
109             'page'     => $page,
110             'book'     => $page->book,
111             'diff'     => $diff,
112             'revision' => $revision,
113         ]);
114     }
115
116     /**
117      * Restores a page using the content of the specified revision.
118      *
119      * @throws NotFoundException
120      */
121     public function restore(string $bookSlug, string $pageSlug, int $revisionId)
122     {
123         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
124         $this->checkOwnablePermission('page-update', $page);
125
126         $page = $this->pageRepo->restoreRevision($page, $revisionId);
127
128         return redirect($page->getUrl());
129     }
130
131     /**
132      * Deletes a revision using the id of the specified revision.
133      *
134      * @throws NotFoundException
135      */
136     public function destroy(string $bookSlug, string $pageSlug, int $revId)
137     {
138         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
139         $this->checkOwnablePermission('page-delete', $page);
140
141         $revision = $page->revisions()->where('id', '=', $revId)->first();
142         if ($revision === null) {
143             throw new NotFoundException("Revision #{$revId} not found");
144         }
145
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'));
149
150             return redirect($page->getUrl('/revisions'));
151         }
152
153         $revision->delete();
154         Activity::add(ActivityType::REVISION_DELETE, $revision);
155         $this->showSuccessNotification(trans('entities.revision_delete_success'));
156
157         return redirect($page->getUrl('/revisions'));
158     }
159 }