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