3 namespace BookStack\Entities\Controllers;
5 use BookStack\Activity\Models\View;
6 use BookStack\Activity\Tools\CommentTree;
7 use BookStack\Activity\Tools\UserEntityWatchOptions;
8 use BookStack\Entities\Models\Book;
9 use BookStack\Entities\Models\Chapter;
10 use BookStack\Entities\Queries\EntityQueries;
11 use BookStack\Entities\Queries\PageQueries;
12 use BookStack\Entities\Repos\PageRepo;
13 use BookStack\Entities\Tools\BookContents;
14 use BookStack\Entities\Tools\Cloner;
15 use BookStack\Entities\Tools\NextPreviousContentLocator;
16 use BookStack\Entities\Tools\PageContent;
17 use BookStack\Entities\Tools\PageEditActivity;
18 use BookStack\Entities\Tools\PageEditorData;
19 use BookStack\Exceptions\NotFoundException;
20 use BookStack\Exceptions\NotifyException;
21 use BookStack\Exceptions\PermissionsException;
22 use BookStack\Http\Controller;
23 use BookStack\References\ReferenceFetcher;
25 use Illuminate\Database\Eloquent\Relations\BelongsTo;
26 use Illuminate\Http\Request;
27 use Illuminate\Validation\ValidationException;
30 class PageController extends Controller
32 public function __construct(
33 protected PageRepo $pageRepo,
34 protected PageQueries $queries,
35 protected EntityQueries $entityQueries,
36 protected ReferenceFetcher $referenceFetcher
41 * Show the form for creating a new page.
45 public function create(string $bookSlug, ?string $chapterSlug = null)
48 $parent = $this->entityQueries->chapters->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
50 $parent = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
53 $this->checkOwnablePermission('page-create', $parent);
55 // Redirect to draft edit screen if signed in
56 if ($this->isSignedIn()) {
57 $draft = $this->pageRepo->getNewDraftPage($parent);
59 return redirect($draft->getUrl());
62 // Otherwise show the edit view if they're a guest
63 $this->setPageTitle(trans('entities.pages_new'));
65 return view('pages.guest-create', ['parent' => $parent]);
69 * Create a new page as a guest user.
71 * @throws ValidationException
73 public function createAsGuest(Request $request, string $bookSlug, ?string $chapterSlug = null)
75 $this->validate($request, [
76 'name' => ['required', 'string', 'max:255'],
80 $parent = $this->entityQueries->chapters->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
82 $parent = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
85 $this->checkOwnablePermission('page-create', $parent);
87 $page = $this->pageRepo->getNewDraftPage($parent);
88 $this->pageRepo->publishDraft($page, [
89 'name' => $request->get('name'),
92 return redirect($page->getUrl('/edit'));
96 * Show form to continue editing a draft page.
98 * @throws NotFoundException
100 public function editDraft(Request $request, string $bookSlug, int $pageId)
102 $draft = $this->queries->findVisibleByIdOrFail($pageId);
103 $this->checkOwnablePermission('page-create', $draft->getParent());
105 $editorData = new PageEditorData($draft, $this->entityQueries, $request->query('editor', ''));
106 $this->setPageTitle(trans('entities.pages_edit_draft'));
108 return view('pages.edit', $editorData->getViewData());
112 * Store a new page by changing a draft into a page.
114 * @throws NotFoundException
115 * @throws ValidationException
117 public function store(Request $request, string $bookSlug, int $pageId)
119 $this->validate($request, [
120 'name' => ['required', 'string', 'max:255'],
122 $draftPage = $this->queries->findVisibleByIdOrFail($pageId);
123 $this->checkOwnablePermission('page-create', $draftPage->getParent());
125 $page = $this->pageRepo->publishDraft($draftPage, $request->all());
127 return redirect($page->getUrl());
131 * Display the specified page.
132 * If the page is not found via the slug the revisions are searched for a match.
134 * @throws NotFoundException
136 public function show(string $bookSlug, string $pageSlug)
139 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
140 } catch (NotFoundException $e) {
141 $revision = $this->entityQueries->revisions->findLatestVersionBySlugs($bookSlug, $pageSlug);
142 $page = $revision->page ?? null;
144 if (is_null($page)) {
148 return redirect($page->getUrl());
151 $this->checkOwnablePermission('page-view', $page);
153 $pageContent = (new PageContent($page));
154 $page->html = $pageContent->render();
155 $pageNav = $pageContent->getNavigation($page->html);
157 $sidebarTree = (new BookContents($page->book))->getTree();
158 $commentTree = (new CommentTree($page));
159 $nextPreviousLocator = new NextPreviousContentLocator($page, $sidebarTree);
161 View::incrementFor($page);
162 $this->setPageTitle($page->getShortName());
164 return view('pages.show', [
166 'book' => $page->book,
168 'sidebarTree' => $sidebarTree,
169 'commentTree' => $commentTree,
170 'pageNav' => $pageNav,
171 'watchOptions' => new UserEntityWatchOptions(user(), $page),
172 'next' => $nextPreviousLocator->getNext(),
173 'previous' => $nextPreviousLocator->getPrevious(),
174 'referenceCount' => $this->referenceFetcher->getReferenceCountToEntity($page),
179 * Get page from an ajax request.
181 * @throws NotFoundException
183 public function getPageAjax(int $pageId)
185 $page = $this->queries->findVisibleByIdOrFail($pageId);
186 $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
187 $page->makeHidden(['book']);
189 return response()->json($page);
193 * Show the form for editing the specified page.
195 * @throws NotFoundException
197 public function edit(Request $request, string $bookSlug, string $pageSlug)
199 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
200 $this->checkOwnablePermission('page-update', $page, $page->getUrl());
202 $editorData = new PageEditorData($page, $this->entityQueries, $request->query('editor', ''));
203 if ($editorData->getWarnings()) {
204 $this->showWarningNotification(implode("\n", $editorData->getWarnings()));
207 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
209 return view('pages.edit', $editorData->getViewData());
213 * Update the specified page in storage.
215 * @throws ValidationException
216 * @throws NotFoundException
218 public function update(Request $request, string $bookSlug, string $pageSlug)
220 $this->validate($request, [
221 'name' => ['required', 'string', 'max:255'],
223 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
224 $this->checkOwnablePermission('page-update', $page);
226 $this->pageRepo->update($page, $request->all());
228 return redirect($page->getUrl());
232 * Save a draft update as a revision.
234 * @throws NotFoundException
236 public function saveDraft(Request $request, int $pageId)
238 $page = $this->queries->findVisibleByIdOrFail($pageId);
239 $this->checkOwnablePermission('page-update', $page);
241 if (!$this->isSignedIn()) {
242 return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
245 $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
246 $warnings = (new PageEditActivity($page))->getWarningMessagesForDraft($draft);
248 return response()->json([
249 'status' => 'success',
250 'message' => trans('entities.pages_edit_draft_save_at'),
251 'warning' => implode("\n", $warnings),
252 'timestamp' => $draft->updated_at->timestamp,
257 * Redirect from a special link url which uses the page id rather than the name.
259 * @throws NotFoundException
261 public function redirectFromLink(int $pageId)
263 $page = $this->queries->findVisibleByIdOrFail($pageId);
265 return redirect($page->getUrl());
269 * Show the deletion page for the specified page.
271 * @throws NotFoundException
273 public function showDelete(string $bookSlug, string $pageSlug)
275 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
276 $this->checkOwnablePermission('page-delete', $page);
277 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
279 $this->entityQueries->books->start()->where('default_template_id', '=', $page->id)->count() > 0 ||
280 $this->entityQueries->chapters->start()->where('default_template_id', '=', $page->id)->count() > 0;
282 return view('pages.delete', [
283 'book' => $page->book,
286 'usedAsTemplate' => $usedAsTemplate,
291 * Show the deletion page for the specified page.
293 * @throws NotFoundException
295 public function showDeleteDraft(string $bookSlug, int $pageId)
297 $page = $this->queries->findVisibleByIdOrFail($pageId);
298 $this->checkOwnablePermission('page-update', $page);
299 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
301 $this->entityQueries->books->start()->where('default_template_id', '=', $page->id)->count() > 0 ||
302 $this->entityQueries->chapters->start()->where('default_template_id', '=', $page->id)->count() > 0;
304 return view('pages.delete', [
305 'book' => $page->book,
308 'usedAsTemplate' => $usedAsTemplate,
313 * Remove the specified page from storage.
315 * @throws NotFoundException
318 public function destroy(string $bookSlug, string $pageSlug)
320 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
321 $this->checkOwnablePermission('page-delete', $page);
322 $parent = $page->getParent();
324 $this->pageRepo->destroy($page);
326 return redirect($parent->getUrl());
330 * Remove the specified draft page from storage.
332 * @throws NotFoundException
335 public function destroyDraft(string $bookSlug, int $pageId)
337 $page = $this->queries->findVisibleByIdOrFail($pageId);
339 $chapter = $page->chapter;
340 $this->checkOwnablePermission('page-update', $page);
342 $this->pageRepo->destroy($page);
344 $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
346 if ($chapter && userCan('view', $chapter)) {
347 return redirect($chapter->getUrl());
350 return redirect($book->getUrl());
354 * Show a listing of recently created pages.
356 public function showRecentlyUpdated()
358 $visibleBelongsScope = function (BelongsTo $query) {
359 $query->scopes('visible');
362 $pages = $this->queries->visibleForList()
363 ->addSelect('updated_by')
364 ->with(['updatedBy', 'book' => $visibleBelongsScope, 'chapter' => $visibleBelongsScope])
365 ->orderBy('updated_at', 'desc')
367 ->setPath(url('/pages/recently-updated'));
369 $this->setPageTitle(trans('entities.recently_updated_pages'));
371 return view('common.detailed-listing-paginated', [
372 'title' => trans('entities.recently_updated_pages'),
373 'entities' => $pages,
374 'showUpdatedBy' => true,
380 * Show the view to choose a new parent to move a page into.
382 * @throws NotFoundException
384 public function showMove(string $bookSlug, string $pageSlug)
386 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
387 $this->checkOwnablePermission('page-update', $page);
388 $this->checkOwnablePermission('page-delete', $page);
390 return view('pages.move', [
391 'book' => $page->book,
397 * Does the action of moving the location of a page.
399 * @throws NotFoundException
402 public function move(Request $request, string $bookSlug, string $pageSlug)
404 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
405 $this->checkOwnablePermission('page-update', $page);
406 $this->checkOwnablePermission('page-delete', $page);
408 $entitySelection = $request->get('entity_selection', null);
409 if ($entitySelection === null || $entitySelection === '') {
410 return redirect($page->getUrl());
414 $this->pageRepo->move($page, $entitySelection);
415 } catch (PermissionsException $exception) {
416 $this->showPermissionError();
417 } catch (Exception $exception) {
418 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
420 return redirect($page->getUrl('/move'));
423 return redirect($page->getUrl());
427 * Show the view to copy a page.
429 * @throws NotFoundException
431 public function showCopy(string $bookSlug, string $pageSlug)
433 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
434 $this->checkOwnablePermission('page-view', $page);
435 session()->flashInput(['name' => $page->name]);
437 return view('pages.copy', [
438 'book' => $page->book,
444 * Create a copy of a page within the requested target destination.
446 * @throws NotFoundException
449 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $pageSlug)
451 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
452 $this->checkOwnablePermission('page-view', $page);
454 $entitySelection = $request->get('entity_selection') ?: null;
455 $newParent = $entitySelection ? $this->entityQueries->findVisibleByStringIdentifier($entitySelection) : $page->getParent();
457 if (!$newParent instanceof Book && !$newParent instanceof Chapter) {
458 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
460 return redirect($page->getUrl('/copy'));
463 $this->checkOwnablePermission('page-create', $newParent);
465 $newName = $request->get('name') ?: $page->name;
466 $pageCopy = $cloner->clonePage($page, $newParent, $newName);
467 $this->showSuccessNotification(trans('entities.pages_copy_success'));
469 return redirect($pageCopy->getUrl());