3 namespace BookStack\Http\Controllers;
5 use BookStack\Actions\View;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Entities\Repos\PageRepo;
8 use BookStack\Entities\Tools\BookContents;
9 use BookStack\Entities\Tools\Cloner;
10 use BookStack\Entities\Tools\NextPreviousContentLocator;
11 use BookStack\Entities\Tools\PageContent;
12 use BookStack\Entities\Tools\PageEditActivity;
13 use BookStack\Entities\Tools\PageEditorData;
14 use BookStack\Exceptions\NotFoundException;
15 use BookStack\Exceptions\PermissionsException;
16 use BookStack\References\ReferenceFetcher;
18 use Illuminate\Database\Eloquent\Relations\BelongsTo;
19 use Illuminate\Http\Request;
20 use Illuminate\Validation\ValidationException;
23 class PageController extends Controller
25 protected PageRepo $pageRepo;
26 protected ReferenceFetcher $referenceFetcher;
29 * PageController constructor.
31 public function __construct(PageRepo $pageRepo, ReferenceFetcher $referenceFetcher)
33 $this->pageRepo = $pageRepo;
34 $this->referenceFetcher = $referenceFetcher;
38 * Show the form for creating a new page.
42 public function create(string $bookSlug, string $chapterSlug = null)
44 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
45 $this->checkOwnablePermission('page-create', $parent);
47 // Redirect to draft edit screen if signed in
48 if ($this->isSignedIn()) {
49 $draft = $this->pageRepo->getNewDraftPage($parent);
51 return redirect($draft->getUrl());
54 // Otherwise show the edit view if they're a guest
55 $this->setPageTitle(trans('entities.pages_new'));
57 return view('pages.guest-create', ['parent' => $parent]);
61 * Create a new page as a guest user.
63 * @throws ValidationException
65 public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
67 $this->validate($request, [
68 'name' => ['required', 'string', 'max:255'],
71 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
72 $this->checkOwnablePermission('page-create', $parent);
74 $page = $this->pageRepo->getNewDraftPage($parent);
75 $this->pageRepo->publishDraft($page, [
76 'name' => $request->get('name'),
80 return redirect($page->getUrl('/edit'));
84 * Show form to continue editing a draft page.
86 * @throws NotFoundException
88 public function editDraft(Request $request, string $bookSlug, int $pageId)
90 $draft = $this->pageRepo->getById($pageId);
91 $this->checkOwnablePermission('page-create', $draft->getParent());
93 $editorData = new PageEditorData($draft, $this->pageRepo, $request->query('editor', ''));
94 $this->setPageTitle(trans('entities.pages_edit_draft'));
96 return view('pages.edit', $editorData->getViewData());
100 * Store a new page by changing a draft into a page.
102 * @throws NotFoundException
103 * @throws ValidationException
105 public function store(Request $request, string $bookSlug, int $pageId)
107 $this->validate($request, [
108 'name' => ['required', 'string', 'max:255'],
110 $draftPage = $this->pageRepo->getById($pageId);
111 $this->checkOwnablePermission('page-create', $draftPage->getParent());
113 $page = $this->pageRepo->publishDraft($draftPage, $request->all());
115 return redirect($page->getUrl());
119 * Display the specified page.
120 * If the page is not found via the slug the revisions are searched for a match.
122 * @throws NotFoundException
124 public function show(string $bookSlug, string $pageSlug)
127 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
128 } catch (NotFoundException $e) {
129 $page = $this->pageRepo->getByOldSlug($bookSlug, $pageSlug);
131 if ($page === null) {
135 return redirect($page->getUrl());
138 $this->checkOwnablePermission('page-view', $page);
140 $pageContent = (new PageContent($page));
141 $page->html = $pageContent->render();
142 $sidebarTree = (new BookContents($page->book))->getTree();
143 $pageNav = $pageContent->getNavigation($page->html);
145 // Check if page comments are enabled
146 $commentsEnabled = !setting('app-disable-comments');
147 if ($commentsEnabled) {
148 $page->load(['comments.createdBy']);
151 $nextPreviousLocator = new NextPreviousContentLocator($page, $sidebarTree);
153 View::incrementFor($page);
154 $this->setPageTitle($page->getShortName());
156 return view('pages.show', [
158 'book' => $page->book,
160 'sidebarTree' => $sidebarTree,
161 'commentsEnabled' => $commentsEnabled,
162 'pageNav' => $pageNav,
163 'next' => $nextPreviousLocator->getNext(),
164 'previous' => $nextPreviousLocator->getPrevious(),
165 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($page),
170 * Get page from an ajax request.
172 * @throws NotFoundException
174 public function getPageAjax(int $pageId)
176 $page = $this->pageRepo->getById($pageId);
177 $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
178 $page->makeHidden(['book']);
180 return response()->json($page);
184 * Show the form for editing the specified page.
186 * @throws NotFoundException
188 public function edit(Request $request, string $bookSlug, string $pageSlug)
190 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
191 $this->checkOwnablePermission('page-update', $page);
193 $editorData = new PageEditorData($page, $this->pageRepo, $request->query('editor', ''));
194 if ($editorData->getWarnings()) {
195 $this->showWarningNotification(implode("\n", $editorData->getWarnings()));
198 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
200 return view('pages.edit', $editorData->getViewData());
204 * Update the specified page in storage.
206 * @throws ValidationException
207 * @throws NotFoundException
209 public function update(Request $request, string $bookSlug, string $pageSlug)
211 $this->validate($request, [
212 'name' => ['required', 'string', 'max:255'],
214 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
215 $this->checkOwnablePermission('page-update', $page);
217 $this->pageRepo->update($page, $request->all());
219 return redirect($page->getUrl());
223 * Save a draft update as a revision.
225 * @throws NotFoundException
227 public function saveDraft(Request $request, int $pageId)
229 $page = $this->pageRepo->getById($pageId);
230 $this->checkOwnablePermission('page-update', $page);
232 if (!$this->isSignedIn()) {
233 return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
236 $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
237 $warnings = (new PageEditActivity($page))->getWarningMessagesForDraft($draft);
239 return response()->json([
240 'status' => 'success',
241 'message' => trans('entities.pages_edit_draft_save_at'),
242 'warning' => implode("\n", $warnings),
243 'timestamp' => $draft->updated_at->timestamp,
248 * Redirect from a special link url which uses the page id rather than the name.
250 * @throws NotFoundException
252 public function redirectFromLink(int $pageId)
254 $page = $this->pageRepo->getById($pageId);
256 return redirect($page->getUrl());
260 * Show the deletion page for the specified page.
262 * @throws NotFoundException
264 public function showDelete(string $bookSlug, string $pageSlug)
266 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
267 $this->checkOwnablePermission('page-delete', $page);
268 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
270 return view('pages.delete', [
271 'book' => $page->book,
278 * Show the deletion page for the specified page.
280 * @throws NotFoundException
282 public function showDeleteDraft(string $bookSlug, int $pageId)
284 $page = $this->pageRepo->getById($pageId);
285 $this->checkOwnablePermission('page-update', $page);
286 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
288 return view('pages.delete', [
289 'book' => $page->book,
296 * Remove the specified page from storage.
298 * @throws NotFoundException
301 public function destroy(string $bookSlug, string $pageSlug)
303 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
304 $this->checkOwnablePermission('page-delete', $page);
305 $parent = $page->getParent();
307 $this->pageRepo->destroy($page);
309 return redirect($parent->getUrl());
313 * Remove the specified draft page from storage.
315 * @throws NotFoundException
318 public function destroyDraft(string $bookSlug, int $pageId)
320 $page = $this->pageRepo->getById($pageId);
322 $chapter = $page->chapter;
323 $this->checkOwnablePermission('page-update', $page);
325 $this->pageRepo->destroy($page);
327 $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
329 if ($chapter && userCan('view', $chapter)) {
330 return redirect($chapter->getUrl());
333 return redirect($book->getUrl());
337 * Show a listing of recently created pages.
339 public function showRecentlyUpdated()
341 $visibleBelongsScope = function (BelongsTo $query) {
342 $query->scopes('visible');
345 $pages = Page::visible()->with(['updatedBy', 'book' => $visibleBelongsScope, 'chapter' => $visibleBelongsScope])
346 ->orderBy('updated_at', 'desc')
348 ->setPath(url('/pages/recently-updated'));
350 $this->setPageTitle(trans('entities.recently_updated_pages'));
352 return view('common.detailed-listing-paginated', [
353 'title' => trans('entities.recently_updated_pages'),
354 'entities' => $pages,
355 'showUpdatedBy' => true,
361 * Show the view to choose a new parent to move a page into.
363 * @throws NotFoundException
365 public function showMove(string $bookSlug, string $pageSlug)
367 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
368 $this->checkOwnablePermission('page-update', $page);
369 $this->checkOwnablePermission('page-delete', $page);
371 return view('pages.move', [
372 'book' => $page->book,
378 * Does the action of moving the location of a page.
380 * @throws NotFoundException
383 public function move(Request $request, string $bookSlug, string $pageSlug)
385 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
386 $this->checkOwnablePermission('page-update', $page);
387 $this->checkOwnablePermission('page-delete', $page);
389 $entitySelection = $request->get('entity_selection', null);
390 if ($entitySelection === null || $entitySelection === '') {
391 return redirect($page->getUrl());
395 $parent = $this->pageRepo->move($page, $entitySelection);
396 } catch (PermissionsException $exception) {
397 $this->showPermissionError();
398 } catch (Exception $exception) {
399 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
401 return redirect()->back();
404 $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
406 return redirect($page->getUrl());
410 * Show the view to copy a page.
412 * @throws NotFoundException
414 public function showCopy(string $bookSlug, string $pageSlug)
416 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
417 $this->checkOwnablePermission('page-view', $page);
418 session()->flashInput(['name' => $page->name]);
420 return view('pages.copy', [
421 'book' => $page->book,
427 * Create a copy of a page within the requested target destination.
429 * @throws NotFoundException
432 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $pageSlug)
434 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
435 $this->checkOwnablePermission('page-view', $page);
437 $entitySelection = $request->get('entity_selection') ?: null;
438 $newParent = $entitySelection ? $this->pageRepo->findParentByIdentifier($entitySelection) : $page->getParent();
440 if (is_null($newParent)) {
441 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
443 return redirect()->back();
446 $this->checkOwnablePermission('page-create', $newParent);
448 $newName = $request->get('name') ?: $page->name;
449 $pageCopy = $cloner->clonePage($page, $newParent, $newName);
450 $this->showSuccessNotification(trans('entities.pages_copy_success'));
452 return redirect($pageCopy->getUrl());