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\Controllers\ApiController;
12 use Illuminate\Http\Request;
14 class PageApiController extends ApiController
16 protected PageRepo $pageRepo;
20 'book_id' => ['required_without:chapter_id', 'integer'],
21 'chapter_id' => ['required_without:book_id', 'integer'],
22 'name' => ['required', 'string', 'max:255'],
23 'html' => ['required_without:markdown', 'string'],
24 'markdown' => ['required_without:html', 'string'],
28 'book_id' => ['integer'],
29 'chapter_id' => ['integer'],
30 'name' => ['string', 'min:1', 'max:255'],
32 'markdown' => ['string'],
37 public function __construct(PageRepo $pageRepo)
39 $this->pageRepo = $pageRepo;
43 * Get a listing of pages visible to the user.
45 public function list()
47 $pages = Page::visible();
49 return $this->apiListingResponse($pages, [
50 'id', 'book_id', 'chapter_id', 'name', 'slug', 'priority',
52 'created_at', 'updated_at',
53 'created_by', 'updated_by', 'owned_by',
58 * Create a new page in the system.
60 * The ID of a parent book or chapter is required to indicate
61 * where this page should be located.
63 * Any HTML content provided should be kept to a single-block depth of plain HTML
64 * elements to remain compatible with the BookStack front-end and editors.
65 * Any images included via base64 data URIs will be extracted and saved as gallery
66 * images against the page during upload.
68 public function create(Request $request)
70 $this->validate($request, $this->rules['create']);
72 if ($request->has('chapter_id')) {
73 $parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
75 $parent = Book::visible()->findOrFail($request->get('book_id'));
77 $this->checkOwnablePermission('page-create', $parent);
79 $draft = $this->pageRepo->getNewDraftPage($parent);
80 $this->pageRepo->publishDraft($draft, $request->only(array_keys($this->rules['create'])));
82 return response()->json($draft->forJsonDisplay());
86 * View the details of a single page.
88 * Pages will always have HTML content. They may have markdown content
89 * if the markdown editor was used to last update the page.
91 * See the "Content Security" section of these docs for security considerations when using
92 * the page content returned from this endpoint.
94 public function read(string $id)
96 $page = $this->pageRepo->getById($id, []);
98 return response()->json($page->forJsonDisplay());
102 * Update the details of a single page.
104 * See the 'create' action for details on the provided HTML/Markdown.
105 * Providing a 'book_id' or 'chapter_id' property will essentially move
106 * the page into that parent element if you have permissions to do so.
108 public function update(Request $request, string $id)
110 $requestData = $this->validate($request, $this->rules['update']);
112 $page = $this->pageRepo->getById($id, []);
113 $this->checkOwnablePermission('page-update', $page);
116 if ($request->has('chapter_id')) {
117 $parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
118 } elseif ($request->has('book_id')) {
119 $parent = Book::visible()->findOrFail($request->get('book_id'));
122 if ($parent && !$parent->matches($page->getParent())) {
123 $this->checkOwnablePermission('page-delete', $page);
126 $this->pageRepo->move($page, $parent->getType() . ':' . $parent->id);
127 } catch (Exception $exception) {
128 if ($exception instanceof PermissionsException) {
129 $this->showPermissionError();
132 return $this->jsonError(trans('errors.selected_book_chapter_not_found'));
136 $updatedPage = $this->pageRepo->update($page, $requestData);
138 return response()->json($updatedPage->forJsonDisplay());
143 * This will typically send the page to the recycle bin.
145 public function delete(string $id)
147 $page = $this->pageRepo->getById($id, []);
148 $this->checkOwnablePermission('page-delete', $page);
150 $this->pageRepo->destroy($page);
152 return response('', 204);