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