3 namespace BookStack\Entities\Controllers;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Entities\Repos\PageRepo;
9 use BookStack\Exceptions\PermissionsException;
10 use BookStack\Http\ApiController;
12 use Illuminate\Http\Request;
14 class PageApiController extends ApiController
18 'book_id' => ['required_without:chapter_id', 'integer'],
19 'chapter_id' => ['required_without:book_id', 'integer'],
20 'name' => ['required', 'string', 'max:255'],
21 'html' => ['required_without:markdown', 'string'],
22 'markdown' => ['required_without:html', 'string'],
26 'book_id' => ['integer'],
27 'chapter_id' => ['integer'],
28 'name' => ['string', 'min:1', 'max:255'],
30 'markdown' => ['string'],
35 public function __construct(
36 protected PageRepo $pageRepo
41 * Get a listing of pages visible to the user.
43 public function list()
45 $pages = Page::visible();
47 return $this->apiListingResponse($pages, [
48 'id', 'book_id', 'chapter_id', 'name', 'slug', 'priority',
50 'created_at', 'updated_at',
51 'created_by', 'updated_by', 'owned_by',
56 * Create a new page in the system.
58 * The ID of a parent book or chapter is required to indicate
59 * where this page should be located.
61 * Any HTML content provided should be kept to a single-block depth of plain HTML
62 * elements to remain compatible with the BookStack front-end and editors.
63 * Any images included via base64 data URIs will be extracted and saved as gallery
64 * images against the page during upload.
66 public function create(Request $request)
68 $this->validate($request, $this->rules['create']);
70 if ($request->has('chapter_id')) {
71 $parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
73 $parent = Book::visible()->findOrFail($request->get('book_id'));
75 $this->checkOwnablePermission('page-create', $parent);
77 $draft = $this->pageRepo->getNewDraftPage($parent);
78 $this->pageRepo->publishDraft($draft, $request->only(array_keys($this->rules['create'])));
80 return response()->json($draft->forJsonDisplay());
84 * View the details of a single page.
86 * Pages will always have HTML content. They may have markdown content
87 * if the markdown editor was used to last update the page.
89 * See the "Content Security" section of these docs for security considerations when using
90 * the page content returned from this endpoint.
92 public function read(string $id)
94 $page = $this->pageRepo->getById($id, []);
96 return response()->json($page->forJsonDisplay());
100 * Update the details of a single page.
102 * See the 'create' action for details on the provided HTML/Markdown.
103 * Providing a 'book_id' or 'chapter_id' property will essentially move
104 * the page into that parent element if you have permissions to do so.
106 public function update(Request $request, string $id)
108 $requestData = $this->validate($request, $this->rules['update']);
110 $page = $this->pageRepo->getById($id, []);
111 $this->checkOwnablePermission('page-update', $page);
114 if ($request->has('chapter_id')) {
115 $parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
116 } elseif ($request->has('book_id')) {
117 $parent = Book::visible()->findOrFail($request->get('book_id'));
120 if ($parent && !$parent->matches($page->getParent())) {
121 $this->checkOwnablePermission('page-delete', $page);
124 $this->pageRepo->move($page, $parent->getType() . ':' . $parent->id);
125 } catch (Exception $exception) {
126 if ($exception instanceof PermissionsException) {
127 $this->showPermissionError();
130 return $this->jsonError(trans('errors.selected_book_chapter_not_found'));
134 $updatedPage = $this->pageRepo->update($page, $requestData);
136 return response()->json($updatedPage->forJsonDisplay());
141 * This will typically send the page to the recycle bin.
143 public function delete(string $id)
145 $page = $this->pageRepo->getById($id, []);
146 $this->checkOwnablePermission('page-delete', $page);
148 $this->pageRepo->destroy($page);
150 return response('', 204);