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'),
79 return redirect($page->getUrl('/edit'));
83 * Show form to continue editing a draft page.
85 * @throws NotFoundException
87 public function editDraft(Request $request, string $bookSlug, int $pageId)
89 $draft = $this->pageRepo->getById($pageId);
90 $this->checkOwnablePermission('page-create', $draft->getParent());
92 $editorData = new PageEditorData($draft, $this->pageRepo, $request->query('editor', ''));
93 $this->setPageTitle(trans('entities.pages_edit_draft'));
95 return view('pages.edit', $editorData->getViewData());
99 * Store a new page by changing a draft into a page.
101 * @throws NotFoundException
102 * @throws ValidationException
104 public function store(Request $request, string $bookSlug, int $pageId)
106 $this->validate($request, [
107 'name' => ['required', 'string', 'max:255'],
109 $draftPage = $this->pageRepo->getById($pageId);
110 $this->checkOwnablePermission('page-create', $draftPage->getParent());
112 $page = $this->pageRepo->publishDraft($draftPage, $request->all());
114 return redirect($page->getUrl());
118 * Display the specified page.
119 * If the page is not found via the slug the revisions are searched for a match.
121 * @throws NotFoundException
123 public function show(string $bookSlug, string $pageSlug)
126 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
127 } catch (NotFoundException $e) {
128 $page = $this->pageRepo->getByOldSlug($bookSlug, $pageSlug);
130 if ($page === null) {
134 return redirect($page->getUrl());
137 $this->checkOwnablePermission('page-view', $page);
139 $pageContent = (new PageContent($page));
140 $page->html = $pageContent->render();
141 $sidebarTree = (new BookContents($page->book))->getTree();
142 $pageNav = $pageContent->getNavigation($page->html);
144 // Check if page comments are enabled
145 $commentsEnabled = !setting('app-disable-comments');
146 if ($commentsEnabled) {
147 $page->load(['comments.createdBy']);
150 $nextPreviousLocator = new NextPreviousContentLocator($page, $sidebarTree);
152 View::incrementFor($page);
153 $this->setPageTitle($page->getShortName());
155 return view('pages.show', [
157 'book' => $page->book,
159 'sidebarTree' => $sidebarTree,
160 'commentsEnabled' => $commentsEnabled,
161 'pageNav' => $pageNav,
162 'next' => $nextPreviousLocator->getNext(),
163 'previous' => $nextPreviousLocator->getPrevious(),
164 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($page),
169 * Get page from an ajax request.
171 * @throws NotFoundException
173 public function getPageAjax(int $pageId)
175 $page = $this->pageRepo->getById($pageId);
176 $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
177 $page->makeHidden(['book']);
179 return response()->json($page);
183 * Show the form for editing the specified page.
185 * @throws NotFoundException
187 public function edit(Request $request, string $bookSlug, string $pageSlug)
189 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
190 $this->checkOwnablePermission('page-update', $page);
192 $editorData = new PageEditorData($page, $this->pageRepo, $request->query('editor', ''));
193 if ($editorData->getWarnings()) {
194 $this->showWarningNotification(implode("\n", $editorData->getWarnings()));
197 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
199 return view('pages.edit', $editorData->getViewData());
203 * Update the specified page in storage.
205 * @throws ValidationException
206 * @throws NotFoundException
208 public function update(Request $request, string $bookSlug, string $pageSlug)
210 $this->validate($request, [
211 'name' => ['required', 'string', 'max:255'],
213 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
214 $this->checkOwnablePermission('page-update', $page);
216 $this->pageRepo->update($page, $request->all());
218 return redirect($page->getUrl());
222 * Save a draft update as a revision.
224 * @throws NotFoundException
226 public function saveDraft(Request $request, int $pageId)
228 $page = $this->pageRepo->getById($pageId);
229 $this->checkOwnablePermission('page-update', $page);
231 if (!$this->isSignedIn()) {
232 return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
235 $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
236 $warnings = (new PageEditActivity($page))->getWarningMessagesForDraft($draft);
238 return response()->json([
239 'status' => 'success',
240 'message' => trans('entities.pages_edit_draft_save_at'),
241 'warning' => implode("\n", $warnings),
242 'timestamp' => $draft->updated_at->timestamp,
247 * Redirect from a special link url which uses the page id rather than the name.
249 * @throws NotFoundException
251 public function redirectFromLink(int $pageId)
253 $page = $this->pageRepo->getById($pageId);
255 return redirect($page->getUrl());
259 * Show the deletion page for the specified page.
261 * @throws NotFoundException
263 public function showDelete(string $bookSlug, string $pageSlug)
265 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
266 $this->checkOwnablePermission('page-delete', $page);
267 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
269 return view('pages.delete', [
270 'book' => $page->book,
277 * Show the deletion page for the specified page.
279 * @throws NotFoundException
281 public function showDeleteDraft(string $bookSlug, int $pageId)
283 $page = $this->pageRepo->getById($pageId);
284 $this->checkOwnablePermission('page-update', $page);
285 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
287 return view('pages.delete', [
288 'book' => $page->book,
295 * Remove the specified page from storage.
297 * @throws NotFoundException
300 public function destroy(string $bookSlug, string $pageSlug)
302 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
303 $this->checkOwnablePermission('page-delete', $page);
304 $parent = $page->getParent();
306 $this->pageRepo->destroy($page);
308 return redirect($parent->getUrl());
312 * Remove the specified draft page from storage.
314 * @throws NotFoundException
317 public function destroyDraft(string $bookSlug, int $pageId)
319 $page = $this->pageRepo->getById($pageId);
321 $chapter = $page->chapter;
322 $this->checkOwnablePermission('page-update', $page);
324 $this->pageRepo->destroy($page);
326 $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
328 if ($chapter && userCan('view', $chapter)) {
329 return redirect($chapter->getUrl());
332 return redirect($book->getUrl());
336 * Show a listing of recently created pages.
338 public function showRecentlyUpdated()
340 $visibleBelongsScope = function (BelongsTo $query) {
341 $query->scopes('visible');
344 $pages = Page::visible()->with(['updatedBy', 'book' => $visibleBelongsScope, 'chapter' => $visibleBelongsScope])
345 ->orderBy('updated_at', 'desc')
347 ->setPath(url('/pages/recently-updated'));
349 $this->setPageTitle(trans('entities.recently_updated_pages'));
351 return view('common.detailed-listing-paginated', [
352 'title' => trans('entities.recently_updated_pages'),
353 'entities' => $pages,
354 'showUpdatedBy' => true,
360 * Show the view to choose a new parent to move a page into.
362 * @throws NotFoundException
364 public function showMove(string $bookSlug, string $pageSlug)
366 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
367 $this->checkOwnablePermission('page-update', $page);
368 $this->checkOwnablePermission('page-delete', $page);
370 return view('pages.move', [
371 'book' => $page->book,
377 * Does the action of moving the location of a page.
379 * @throws NotFoundException
382 public function move(Request $request, string $bookSlug, string $pageSlug)
384 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
385 $this->checkOwnablePermission('page-update', $page);
386 $this->checkOwnablePermission('page-delete', $page);
388 $entitySelection = $request->get('entity_selection', null);
389 if ($entitySelection === null || $entitySelection === '') {
390 return redirect($page->getUrl());
394 $parent = $this->pageRepo->move($page, $entitySelection);
395 } catch (PermissionsException $exception) {
396 $this->showPermissionError();
397 } catch (Exception $exception) {
398 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
400 return redirect()->back();
403 $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
405 return redirect($page->getUrl());
409 * Show the view to copy a page.
411 * @throws NotFoundException
413 public function showCopy(string $bookSlug, string $pageSlug)
415 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
416 $this->checkOwnablePermission('page-view', $page);
417 session()->flashInput(['name' => $page->name]);
419 return view('pages.copy', [
420 'book' => $page->book,
426 * Create a copy of a page within the requested target destination.
428 * @throws NotFoundException
431 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $pageSlug)
433 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
434 $this->checkOwnablePermission('page-view', $page);
436 $entitySelection = $request->get('entity_selection') ?: null;
437 $newParent = $entitySelection ? $this->pageRepo->findParentByIdentifier($entitySelection) : $page->getParent();
439 if (is_null($newParent)) {
440 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
442 return redirect()->back();
445 $this->checkOwnablePermission('page-create', $newParent);
447 $newName = $request->get('name') ?: $page->name;
448 $pageCopy = $cloner->clonePage($page, $newParent, $newName);
449 $this->showSuccessNotification(trans('entities.pages_copy_success'));
451 return redirect($pageCopy->getUrl());