3 namespace BookStack\Http\Controllers\Api;
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;
11 use Illuminate\Http\Request;
13 class PageApiController extends ApiController
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',
27 'book_id' => 'required|integer',
28 'chapter_id' => 'required|integer',
29 'name' => 'string|min:1|max:255',
31 'markdown' => 'string',
36 public function __construct(PageRepo $pageRepo)
38 $this->pageRepo = $pageRepo;
42 * Get a listing of pages visible to the user.
44 public function list()
46 $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 public function read(string $id)
91 $page = $this->pageRepo->getById($id, []);
92 return response()->json($page->forJsonDisplay());
96 * Update the details of a single page.
98 * See the 'create' action for details on the provided HTML/Markdown.
99 * Providing a 'book_id' or 'chapter_id' property will essentially move
100 * the page into that parent element if you have permissions to do so.
102 public function update(Request $request, string $id)
104 $page = $this->pageRepo->getById($id, []);
105 $this->checkOwnablePermission('page-update', $page);
108 if ($request->has('chapter_id')) {
109 $parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
110 } else if ($request->has('book_id')) {
111 $parent = Book::visible()->findOrFail($request->get('book_id'));
114 if ($parent && !$parent->matches($page->getParent())) {
115 $this->checkOwnablePermission('page-delete', $page);
117 $this->pageRepo->move($page, $parent->getType() . ':' . $parent->id);
118 } catch (Exception $exception) {
119 if ($exception instanceof PermissionsException) {
120 $this->showPermissionError();
123 return $this->jsonError(trans('errors.selected_book_chapter_not_found'));
127 $updatedPage = $this->pageRepo->update($page, $request->all());
128 return response()->json($updatedPage->forJsonDisplay());
133 * This will typically send the page to the recycle bin.
135 public function delete(string $id)
137 $page = $this->pageRepo->getById($id, []);
138 $this->checkOwnablePermission('page-delete', $page);
140 $this->pageRepo->destroy($page);
141 return response('', 204);