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