Files
BookStack/app/Entities/Controllers/PageController.php

471 lines
16 KiB
PHP
Raw Normal View History

2021-06-26 15:23:15 +00:00
<?php
2023-05-17 17:56:55 +01:00
namespace BookStack\Entities\Controllers;
2015-07-12 20:01:42 +01:00
2023-05-17 17:56:55 +01:00
use BookStack\Activity\Models\View;
use BookStack\Activity\Tools\CommentTree;
use BookStack\Activity\Tools\UserEntityWatchOptions;
use BookStack\Entities\Models\Book;
2024-01-01 21:58:49 +01:00
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Queries\EntityQueries;
use BookStack\Entities\Queries\PageQueries;
2021-06-26 15:23:15 +00:00
use BookStack\Entities\Repos\PageRepo;
use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Tools\Cloner;
use BookStack\Entities\Tools\NextPreviousContentLocator;
use BookStack\Entities\Tools\PageContent;
use BookStack\Entities\Tools\PageEditActivity;
use BookStack\Entities\Tools\PageEditorData;
use BookStack\Exceptions\NotFoundException;
use BookStack\Exceptions\NotifyException;
use BookStack\Exceptions\PermissionsException;
use BookStack\Http\Controller;
use BookStack\Permissions\Permission;
use BookStack\References\ReferenceFetcher;
use Exception;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2015-07-12 20:01:42 +01:00
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Throwable;
2015-07-12 20:01:42 +01:00
class PageController extends Controller
{
public function __construct(
protected PageRepo $pageRepo,
protected PageQueries $queries,
protected EntityQueries $entityQueries,
protected ReferenceFetcher $referenceFetcher
) {
2015-07-12 21:31:15 +01:00
}
2015-07-12 20:01:42 +01:00
/**
2015-08-09 12:06:52 +01:00
* Show the form for creating a new page.
2021-06-26 15:23:15 +00:00
*
* @throws Throwable
2015-07-12 20:01:42 +01:00
*/
public function create(string $bookSlug, ?string $chapterSlug = null)
2015-07-12 20:01:42 +01:00
{
if ($chapterSlug) {
$parent = $this->entityQueries->chapters->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
} else {
$parent = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
}
$this->checkOwnablePermission(Permission::PageCreate, $parent);
// Redirect to draft edit screen if signed in
if ($this->isSignedIn()) {
$draft = $this->pageRepo->getNewDraftPage($parent);
2021-06-26 15:23:15 +00:00
return redirect($draft->getUrl());
}
// Otherwise show the edit view if they're a guest
2016-12-04 16:51:39 +00:00
$this->setPageTitle(trans('entities.pages_new'));
2021-06-26 15:23:15 +00:00
return view('pages.guest-create', ['parent' => $parent]);
}
/**
* Create a new page as a guest user.
2021-06-26 15:23:15 +00:00
*
* @throws ValidationException
*/
public function createAsGuest(Request $request, string $bookSlug, ?string $chapterSlug = null)
{
$this->validate($request, [
'name' => ['required', 'string', 'max:255'],
]);
if ($chapterSlug) {
$parent = $this->entityQueries->chapters->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
} else {
$parent = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
}
$this->checkOwnablePermission(Permission::PageCreate, $parent);
$page = $this->pageRepo->getNewDraftPage($parent);
$this->pageRepo->publishDraft($page, [
'name' => $request->get('name'),
]);
return redirect($page->getUrl('/edit'));
}
/**
* Show form to continue editing a draft page.
2021-06-26 15:23:15 +00:00
*
* @throws NotFoundException
*/
public function editDraft(Request $request, string $bookSlug, int $pageId)
{
$draft = $this->queries->findVisibleByIdOrFail($pageId);
$this->checkOwnablePermission(Permission::PageCreate, $draft->getParent());
$editorData = new PageEditorData($draft, $this->entityQueries, $request->query('editor', ''));
$this->setPageTitle(trans('entities.pages_edit_draft'));
return view('pages.edit', $editorData->getViewData());
2015-07-12 20:01:42 +01:00
}
/**
* Store a new page by changing a draft into a page.
2021-06-26 15:23:15 +00:00
*
* @throws NotFoundException
* @throws ValidationException
2015-07-12 20:01:42 +01:00
*/
public function store(Request $request, string $bookSlug, int $pageId)
2015-07-12 20:01:42 +01:00
{
2015-07-12 21:31:15 +01:00
$this->validate($request, [
'name' => ['required', 'string', 'max:255'],
2015-07-12 21:31:15 +01:00
]);
DB: Aligned entity structure to a common table As per PR #5800 * DB: Planned out new entity table format via migrations * DB: Created entity migration logic Made some other tweaks/fixes while testing. * DB: Added change of entity relation columns to suit new entities table * DB: Got most view queries working for new structure * Entities: Started logic change to new structure Updated base entity class, and worked through BaseRepo. Need to go through other repos next. Removed a couple of redundant interfaces as part of this since we can move the logic onto the shared ContainerData model as needed. * Entities: Been through repos to update for new format * Entities: Updated repos to act on refreshed clones Changes to core entity models are now done on clones to ensure clean state before save, and those clones are returned back if changes are needed after that action. * Entities: Updated model classes & relations for changes * Entities: Changed from *Data to a common "contents" system Added smart loading from builder instances which should hydrate with "contents()" loaded via join, while keeping the core model original. * Entities: Moved entity description/covers to own non-model classes Added back some interfaces. * Entities: Removed use of contents system for data access * Entities: Got most queries back to working order * Entities: Reverted back to data from contents, fixed various issues * Entities: Started addressing issues from tests * Entities: Addressed further tests/issues * Entities: Been through tests to get all passing in dev Fixed issues and needed test changes along the way. * Entities: Addressed phpstan errors * Entities: Reviewed TODO notes * Entities: Ensured book/shelf relation data removed on destroy * Entities: Been through API responses & adjusted field visibility * Entities: Added type index to massively improve query speed
2025-10-18 13:14:30 +01:00
$draftPage = $this->queries->findVisibleByIdOrFail($pageId);
$this->checkOwnablePermission(Permission::PageCreate, $draftPage->getParent());
$page = $this->pageRepo->publishDraft($draftPage, $request->all());
2015-07-20 22:05:26 +01:00
2015-07-12 21:31:15 +01:00
return redirect($page->getUrl());
2015-07-12 20:01:42 +01:00
}
/**
2015-08-09 12:06:52 +01:00
* Display the specified page.
2017-01-01 16:05:44 +00:00
* If the page is not found via the slug the revisions are searched for a match.
2021-06-26 15:23:15 +00:00
*
* @throws NotFoundException
2015-07-12 20:01:42 +01:00
*/
public function show(string $bookSlug, string $pageSlug)
2015-07-12 20:01:42 +01:00
{
try {
$page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
} catch (NotFoundException $e) {
$revision = $this->entityQueries->revisions->findLatestVersionBySlugs($bookSlug, $pageSlug);
$page = $revision->page ?? null;
if (is_null($page)) {
throw $e;
}
return redirect($page->getUrl());
}
$pageContent = (new PageContent($page));
$page->html = $pageContent->render();
$pageNav = $pageContent->getNavigation($page->html);
$sidebarTree = (new BookContents($page->book))->getTree();
$commentTree = (new CommentTree($page));
$nextPreviousLocator = new NextPreviousContentLocator($page, $sidebarTree);
View::incrementFor($page);
$this->setPageTitle($page->getShortName());
2021-06-26 15:23:15 +00:00
return view('pages.show', [
2021-06-26 15:23:15 +00:00
'page' => $page,
'book' => $page->book,
'current' => $page,
'sidebarTree' => $sidebarTree,
'commentTree' => $commentTree,
2021-06-26 15:23:15 +00:00
'pageNav' => $pageNav,
'watchOptions' => new UserEntityWatchOptions(user(), $page),
2021-06-26 15:23:15 +00:00
'next' => $nextPreviousLocator->getNext(),
'previous' => $nextPreviousLocator->getPrevious(),
'referenceCount' => $this->referenceFetcher->getReferenceCountToEntity($page),
]);
2015-07-12 20:01:42 +01:00
}
2016-03-12 15:52:19 +00:00
/**
* Get page from an ajax request.
2021-06-26 15:23:15 +00:00
*
* @throws NotFoundException
2016-03-12 15:52:19 +00:00
*/
public function getPageAjax(int $pageId)
2016-03-12 15:52:19 +00:00
{
$page = $this->queries->findVisibleByIdOrFail($pageId);
$page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
$page->makeHidden(['book']);
2021-06-26 15:23:15 +00:00
2016-03-12 15:52:19 +00:00
return response()->json($page);
}
2015-07-12 20:01:42 +01:00
/**
2015-08-09 12:06:52 +01:00
* Show the form for editing the specified page.
2021-06-26 15:23:15 +00:00
*
* @throws NotFoundException
2015-07-12 20:01:42 +01:00
*/
public function edit(Request $request, string $bookSlug, string $pageSlug)
2015-07-12 20:01:42 +01:00
{
$page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
$this->checkOwnablePermission(Permission::PageUpdate, $page, $page->getUrl());
$editorData = new PageEditorData($page, $this->entityQueries, $request->query('editor', ''));
if ($editorData->getWarnings()) {
$this->showWarningNotification(implode("\n", $editorData->getWarnings()));
2016-03-12 15:52:19 +00:00
}
$this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
2021-06-26 15:23:15 +00:00
return view('pages.edit', $editorData->getViewData());
2015-07-12 20:01:42 +01:00
}
/**
2015-08-09 12:06:52 +01:00
* Update the specified page in storage.
2021-06-26 15:23:15 +00:00
*
* @throws ValidationException
* @throws NotFoundException
2015-07-12 20:01:42 +01:00
*/
public function update(Request $request, string $bookSlug, string $pageSlug)
2015-07-12 20:01:42 +01:00
{
$this->validate($request, [
'name' => ['required', 'string', 'max:255'],
]);
$page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
$this->checkOwnablePermission(Permission::PageUpdate, $page);
$this->pageRepo->update($page, $request->all());
2015-07-12 21:31:15 +01:00
return redirect($page->getUrl());
2015-07-12 20:01:42 +01:00
}
/**
* Save a draft update as a revision.
2021-06-26 15:23:15 +00:00
*
* @throws NotFoundException
*/
public function saveDraft(Request $request, int $pageId)
{
$page = $this->queries->findVisibleByIdOrFail($pageId);
$this->checkOwnablePermission(Permission::PageUpdate, $page);
if (!$this->isSignedIn()) {
return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
}
$draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
$warnings = (new PageEditActivity($page))->getWarningMessagesForDraft($draft);
2021-06-26 15:23:15 +00:00
return response()->json([
2021-06-26 15:23:15 +00:00
'status' => 'success',
'message' => trans('entities.pages_edit_draft_save_at'),
'warning' => implode("\n", $warnings),
'timestamp' => $draft->updated_at->timestamp,
]);
}
2015-07-21 20:13:29 +01:00
/**
* Redirect from a special link url which uses the page id rather than the name.
2021-06-26 15:23:15 +00:00
*
* @throws NotFoundException
2015-07-21 20:13:29 +01:00
*/
public function redirectFromLink(int $pageId)
2015-07-16 19:15:22 +01:00
{
$page = $this->queries->findVisibleByIdOrFail($pageId);
2021-06-26 15:23:15 +00:00
2015-07-16 19:15:22 +01:00
return redirect($page->getUrl());
}
2015-08-09 12:06:52 +01:00
/**
* Show the deletion page for the specified page.
2021-06-26 15:23:15 +00:00
*
* @throws NotFoundException
2015-08-09 12:06:52 +01:00
*/
public function showDelete(string $bookSlug, string $pageSlug)
{
$page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
$this->checkOwnablePermission(Permission::PageDelete, $page);
$this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
2024-01-23 22:37:15 +01:00
$usedAsTemplate =
$this->entityQueries->books->start()->where('default_template_id', '=', $page->id)->count() > 0 ||
$this->entityQueries->chapters->start()->where('default_template_id', '=', $page->id)->count() > 0;
2021-06-26 15:23:15 +00:00
return view('pages.delete', [
2021-06-26 15:23:15 +00:00
'book' => $page->book,
'page' => $page,
'current' => $page,
'usedAsTemplate' => $usedAsTemplate,
]);
}
/**
* Show the deletion page for the specified page.
2021-06-26 15:23:15 +00:00
*
* @throws NotFoundException
*/
public function showDeleteDraft(string $bookSlug, int $pageId)
{
$page = $this->queries->findVisibleByIdOrFail($pageId);
$this->checkOwnablePermission(Permission::PageUpdate, $page);
$this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
2024-01-23 22:37:15 +01:00
$usedAsTemplate =
$this->entityQueries->books->start()->where('default_template_id', '=', $page->id)->count() > 0 ||
$this->entityQueries->chapters->start()->where('default_template_id', '=', $page->id)->count() > 0;
2021-06-26 15:23:15 +00:00
return view('pages.delete', [
2021-06-26 15:23:15 +00:00
'book' => $page->book,
'page' => $page,
'current' => $page,
'usedAsTemplate' => $usedAsTemplate,
]);
}
2015-07-12 20:01:42 +01:00
/**
2015-08-09 12:06:52 +01:00
* Remove the specified page from storage.
2021-06-26 15:23:15 +00:00
*
* @throws NotFoundException
* @throws Throwable
2015-07-12 20:01:42 +01:00
*/
public function destroy(string $bookSlug, string $pageSlug)
2015-07-12 20:01:42 +01:00
{
$page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
$this->checkOwnablePermission(Permission::PageDelete, $page);
$parent = $page->getParent();
$this->pageRepo->destroy($page);
return redirect($parent->getUrl());
}
/**
* Remove the specified draft page from storage.
2021-06-26 15:23:15 +00:00
*
* @throws NotFoundException
* @throws Throwable
*/
public function destroyDraft(string $bookSlug, int $pageId)
{
$page = $this->queries->findVisibleByIdOrFail($pageId);
2017-01-01 16:05:44 +00:00
$book = $page->book;
$chapter = $page->chapter;
$this->checkOwnablePermission(Permission::PageUpdate, $page);
2015-08-09 12:06:52 +01:00
$this->pageRepo->destroy($page);
2015-08-09 12:06:52 +01:00
$this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
2016-07-07 19:42:21 +02:00
if ($chapter && userCan(Permission::ChapterView, $chapter)) {
return redirect($chapter->getUrl());
}
2021-06-26 15:23:15 +00:00
return redirect($book->getUrl());
}
/**
* Show a listing of recently created pages.
*/
public function showRecentlyUpdated()
{
$visibleBelongsScope = function (BelongsTo $query) {
$query->scopes('visible');
};
$pages = $this->queries->visibleForList()
->addSelect('updated_by')
->with(['updatedBy', 'book' => $visibleBelongsScope, 'chapter' => $visibleBelongsScope])
->orderBy('updated_at', 'desc')
->paginate(20)
->setPath(url('/pages/recently-updated'));
$this->setPageTitle(trans('entities.recently_updated_pages'));
return view('common.detailed-listing-paginated', [
'title' => trans('entities.recently_updated_pages'),
'entities' => $pages,
'showUpdatedBy' => true,
'showPath' => true,
]);
}
/**
* Show the view to choose a new parent to move a page into.
2021-06-26 15:23:15 +00:00
*
* @throws NotFoundException
*/
public function showMove(string $bookSlug, string $pageSlug)
{
$page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
$this->checkOwnablePermission(Permission::PageUpdate, $page);
$this->checkOwnablePermission(Permission::PageDelete, $page);
2021-06-26 15:23:15 +00:00
return view('pages.move', [
2017-01-01 16:05:44 +00:00
'book' => $page->book,
2021-06-26 15:23:15 +00:00
'page' => $page,
]);
}
2016-06-25 15:31:38 +01:00
/**
* Does the action of moving the location of a page.
2021-06-26 15:23:15 +00:00
*
2016-06-25 15:31:38 +01:00
* @throws NotFoundException
* @throws Throwable
2016-06-25 15:31:38 +01:00
*/
public function move(Request $request, string $bookSlug, string $pageSlug)
2016-06-12 12:14:14 +01:00
{
$page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
$this->checkOwnablePermission(Permission::PageUpdate, $page);
$this->checkOwnablePermission(Permission::PageDelete, $page);
2016-06-12 12:14:14 +01:00
$entitySelection = $request->get('entity_selection', null);
if ($entitySelection === null || $entitySelection === '') {
return redirect($page->getUrl());
}
2017-01-01 16:05:44 +00:00
try {
$this->pageRepo->move($page, $entitySelection);
} catch (PermissionsException $exception) {
$this->showPermissionError();
} catch (Exception $exception) {
$this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
2021-06-26 15:23:15 +00:00
return redirect($page->getUrl('/move'));
2016-06-12 12:14:14 +01:00
}
return redirect($page->getUrl());
}
/**
* Show the view to copy a page.
2021-06-26 15:23:15 +00:00
*
* @throws NotFoundException
*/
public function showCopy(string $bookSlug, string $pageSlug)
{
$page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
session()->flashInput(['name' => $page->name]);
2021-06-26 15:23:15 +00:00
return view('pages.copy', [
'book' => $page->book,
2021-06-26 15:23:15 +00:00
'page' => $page,
]);
}
/**
* Create a copy of a page within the requested target destination.
2021-06-26 15:23:15 +00:00
*
* @throws NotFoundException
* @throws Throwable
*/
public function copy(Request $request, Cloner $cloner, string $bookSlug, string $pageSlug)
{
$page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
$this->checkOwnablePermission(Permission::PageView, $page);
$entitySelection = $request->get('entity_selection') ?: null;
$newParent = $entitySelection ? $this->entityQueries->findVisibleByStringIdentifier($entitySelection) : $page->getParent();
if (!$newParent instanceof Book && !$newParent instanceof Chapter) {
$this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
2021-12-20 17:40:27 +00:00
return redirect($page->getUrl('/copy'));
}
$this->checkOwnablePermission(Permission::PageCreate, $newParent);
$newName = $request->get('name') ?: $page->name;
$pageCopy = $cloner->clonePage($page, $newParent, $newName);
$this->showSuccessNotification(trans('entities.pages_copy_success'));
2021-06-26 15:23:15 +00:00
return redirect($pageCopy->getUrl());
}
2015-07-12 20:01:42 +01:00
}