]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageRevisionController.php
Added a custom link context toolbar
[bookstack] / app / Http / Controllers / PageRevisionController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Entities\Repos\PageRepo;
6 use BookStack\Entities\Tools\PageContent;
7 use BookStack\Exceptions\NotFoundException;
8 use Ssddanbrown\HtmlDiff\Diff;
9
10 class PageRevisionController extends Controller
11 {
12     protected $pageRepo;
13
14     /**
15      * PageRevisionController constructor.
16      */
17     public function __construct(PageRepo $pageRepo)
18     {
19         $this->pageRepo = $pageRepo;
20     }
21
22     /**
23      * Shows the last revisions for this page.
24      *
25      * @throws NotFoundException
26      */
27     public function index(string $bookSlug, string $pageSlug)
28     {
29         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
30         $this->setPageTitle(trans('entities.pages_revisions_named', ['pageName'=>$page->getShortName()]));
31
32         return view('pages.revisions', [
33             'page'    => $page,
34             'current' => $page,
35         ]);
36     }
37
38     /**
39      * Shows a preview of a single revision.
40      *
41      * @throws NotFoundException
42      */
43     public function show(string $bookSlug, string $pageSlug, int $revisionId)
44     {
45         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
46         $revision = $page->revisions()->where('id', '=', $revisionId)->first();
47         if ($revision === null) {
48             throw new NotFoundException();
49         }
50
51         $page->fill($revision->toArray());
52         // TODO - Refactor PageContent so we don't need to juggle this
53         $page->html = $revision->html;
54         $page->html = (new PageContent($page))->render();
55
56         $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
57
58         return view('pages.revision', [
59             'page'     => $page,
60             'book'     => $page->book,
61             'diff'     => null,
62             'revision' => $revision,
63         ]);
64     }
65
66     /**
67      * Shows the changes of a single revision.
68      *
69      * @throws NotFoundException
70      */
71     public function changes(string $bookSlug, string $pageSlug, int $revisionId)
72     {
73         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
74         $revision = $page->revisions()->where('id', '=', $revisionId)->first();
75         if ($revision === null) {
76             throw new NotFoundException();
77         }
78
79         $prev = $revision->getPrevious();
80         $prevContent = $prev->html ?? '';
81         $diff = Diff::excecute($prevContent, $revision->html);
82
83         $page->fill($revision->toArray());
84         // TODO - Refactor PageContent so we don't need to juggle this
85         $page->html = $revision->html;
86         $page->html = (new PageContent($page))->render();
87         $this->setPageTitle(trans('entities.pages_revision_named', ['pageName'=>$page->getShortName()]));
88
89         return view('pages.revision', [
90             'page'     => $page,
91             'book'     => $page->book,
92             'diff'     => $diff,
93             'revision' => $revision,
94         ]);
95     }
96
97     /**
98      * Restores a page using the content of the specified revision.
99      *
100      * @throws NotFoundException
101      */
102     public function restore(string $bookSlug, string $pageSlug, int $revisionId)
103     {
104         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
105         $this->checkOwnablePermission('page-update', $page);
106
107         $page = $this->pageRepo->restoreRevision($page, $revisionId);
108
109         return redirect($page->getUrl());
110     }
111
112     /**
113      * Deletes a revision using the id of the specified revision.
114      *
115      * @throws NotFoundException
116      */
117     public function destroy(string $bookSlug, string $pageSlug, int $revId)
118     {
119         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
120         $this->checkOwnablePermission('page-delete', $page);
121
122         $revision = $page->revisions()->where('id', '=', $revId)->first();
123         if ($revision === null) {
124             throw new NotFoundException("Revision #{$revId} not found");
125         }
126
127         // Get the current revision for the page
128         $currentRevision = $page->getCurrentRevision();
129
130         // Check if its the latest revision, cannot delete latest revision.
131         if (intval($currentRevision->id) === intval($revId)) {
132             $this->showErrorNotification(trans('entities.revision_cannot_delete_latest'));
133
134             return redirect($page->getUrl('/revisions'));
135         }
136
137         $revision->delete();
138         $this->showSuccessNotification(trans('entities.revision_delete_success'));
139
140         return redirect($page->getUrl('/revisions'));
141     }
142 }