3 namespace BookStack\Entities\Controllers;
5 use BookStack\Activity\Tools\CommentTree;
6 use BookStack\Entities\Queries\EntityQueries;
7 use BookStack\Entities\Queries\PageQueries;
8 use BookStack\Entities\Repos\PageRepo;
9 use BookStack\Exceptions\PermissionsException;
10 use BookStack\Http\ApiController;
11 use BookStack\Permissions\Permission;
13 use Illuminate\Http\Request;
15 class PageApiController extends ApiController
17 protected array $rules = [
19 'book_id' => ['required_without:chapter_id', 'integer'],
20 'chapter_id' => ['required_without:book_id', 'integer'],
21 'name' => ['required', 'string', 'max:255'],
22 'html' => ['required_without:markdown', 'string'],
23 'markdown' => ['required_without:html', 'string'],
25 'priority' => ['integer'],
28 'book_id' => ['integer'],
29 'chapter_id' => ['integer'],
30 'name' => ['string', 'min:1', 'max:255'],
32 'markdown' => ['string'],
34 'priority' => ['integer'],
38 public function __construct(
39 protected PageRepo $pageRepo,
40 protected PageQueries $queries,
41 protected EntityQueries $entityQueries,
46 * Get a listing of pages visible to the user.
48 public function list()
50 $pages = $this->queries->visibleForList()
51 ->addSelect(['created_by', 'updated_by', 'revision_count', 'editor']);
53 return $this->apiListingResponse($pages, [
54 'id', 'book_id', 'chapter_id', 'name', 'slug', 'priority',
56 'created_at', 'updated_at',
57 'created_by', 'updated_by', 'owned_by',
62 * Create a new page in the system.
64 * The ID of a parent book or chapter is required to indicate
65 * where this page should be located.
67 * Any HTML content provided should be kept to a single-block depth of plain HTML
68 * elements to remain compatible with the BookStack front-end and editors.
69 * Any images included via base64 data URIs will be extracted and saved as gallery
70 * images against the page during upload.
72 public function create(Request $request)
74 $this->validate($request, $this->rules['create']);
76 if ($request->has('chapter_id')) {
77 $parent = $this->entityQueries->chapters->findVisibleByIdOrFail(intval($request->get('chapter_id')));
79 $parent = $this->entityQueries->books->findVisibleByIdOrFail(intval($request->get('book_id')));
81 $this->checkOwnablePermission(Permission::PageCreate, $parent);
83 $draft = $this->pageRepo->getNewDraftPage($parent);
84 $this->pageRepo->publishDraft($draft, $request->only(array_keys($this->rules['create'])));
86 return response()->json($draft->forJsonDisplay());
90 * View the details of a single page.
91 * Pages will always have HTML content. They may have markdown content
92 * if the Markdown editor was used to last update the page.
94 * The 'html' property is the fully rendered and escaped HTML content that BookStack
95 * would show on page view, with page includes handled.
96 * The 'raw_html' property is the direct database stored HTML content, which would be
97 * what BookStack shows on page edit.
99 * See the "Content Security" section of these docs for security considerations when using
100 * the page content returned from this endpoint.
102 * Comments for the page are provided in a tree-structure representing the hierarchy of top-level
103 * comments and replies, for both archived and active comments.
105 public function read(string $id)
107 $page = $this->queries->findVisibleByIdOrFail($id);
109 $page = $page->forJsonDisplay();
110 $commentTree = (new CommentTree($page));
111 $commentTree->loadVisibleHtml();
112 $page->setAttribute('comments', [
113 'active' => $commentTree->getActive(),
114 'archived' => $commentTree->getArchived(),
117 return response()->json($page);
121 * Update the details of a single page.
123 * See the 'create' action for details on the provided HTML/Markdown.
124 * Providing a 'book_id' or 'chapter_id' property will essentially move
125 * the page into that parent element if you have permissions to do so.
127 public function update(Request $request, string $id)
129 $requestData = $this->validate($request, $this->rules['update']);
131 $page = $this->queries->findVisibleByIdOrFail($id);
132 $this->checkOwnablePermission(Permission::PageUpdate, $page);
135 if ($request->has('chapter_id')) {
136 $parent = $this->entityQueries->chapters->findVisibleByIdOrFail(intval($request->get('chapter_id')));
137 } elseif ($request->has('book_id')) {
138 $parent = $this->entityQueries->books->findVisibleByIdOrFail(intval($request->get('book_id')));
141 if ($parent && !$parent->matches($page->getParent())) {
142 $this->checkOwnablePermission(Permission::PageDelete, $page);
145 $this->pageRepo->move($page, $parent->getType() . ':' . $parent->id);
146 } catch (Exception $exception) {
147 if ($exception instanceof PermissionsException) {
148 $this->showPermissionError();
151 return $this->jsonError(trans('errors.selected_book_chapter_not_found'));
155 $updatedPage = $this->pageRepo->update($page, $requestData);
157 return response()->json($updatedPage->forJsonDisplay());
162 * This will typically send the page to the recycle bin.
164 public function delete(string $id)
166 $page = $this->queries->findVisibleByIdOrFail($id);
167 $this->checkOwnablePermission(Permission::PageDelete, $page);
169 $this->pageRepo->destroy($page);
171 return response('', 204);