]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/PageApiController.php
Queries: Updated all app book static query uses
[bookstack] / app / Entities / Controllers / PageApiController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
5 use BookStack\Entities\Queries\EntityQueries;
6 use BookStack\Entities\Queries\PageQueries;
7 use BookStack\Entities\Repos\PageRepo;
8 use BookStack\Exceptions\PermissionsException;
9 use BookStack\Http\ApiController;
10 use Exception;
11 use Illuminate\Http\Request;
12
13 class PageApiController extends ApiController
14 {
15     protected $rules = [
16         'create' => [
17             'book_id'    => ['required_without:chapter_id', 'integer'],
18             'chapter_id' => ['required_without:book_id', 'integer'],
19             'name'       => ['required', 'string', 'max:255'],
20             'html'       => ['required_without:markdown', 'string'],
21             'markdown'   => ['required_without:html', 'string'],
22             'tags'       => ['array'],
23             'priority'   => ['integer'],
24         ],
25         'update' => [
26             'book_id'    => ['integer'],
27             'chapter_id' => ['integer'],
28             'name'       => ['string', 'min:1', 'max:255'],
29             'html'       => ['string'],
30             'markdown'   => ['string'],
31             'tags'       => ['array'],
32             'priority'   => ['integer'],
33         ],
34     ];
35
36     public function __construct(
37         protected PageRepo $pageRepo,
38         protected PageQueries $queries,
39         protected EntityQueries $entityQueries,
40     ) {
41     }
42
43     /**
44      * Get a listing of pages visible to the user.
45      */
46     public function list()
47     {
48         $pages = $this->queries->visibleForList();
49
50         return $this->apiListingResponse($pages, [
51             'id', 'book_id', 'chapter_id', 'name', 'slug', 'priority',
52             'draft', 'template',
53             'created_at', 'updated_at',
54             'created_by', 'updated_by', 'owned_by',
55         ]);
56     }
57
58     /**
59      * Create a new page in the system.
60      *
61      * The ID of a parent book or chapter is required to indicate
62      * where this page should be located.
63      *
64      * Any HTML content provided should be kept to a single-block depth of plain HTML
65      * elements to remain compatible with the BookStack front-end and editors.
66      * Any images included via base64 data URIs will be extracted and saved as gallery
67      * images against the page during upload.
68      */
69     public function create(Request $request)
70     {
71         $this->validate($request, $this->rules['create']);
72
73         if ($request->has('chapter_id')) {
74             $parent = $this->entityQueries->chapters->findVisibleByIdOrFail(intval($request->get('chapter_id')));
75         } else {
76             $parent = $this->entityQueries->books->findVisibleByIdOrFail(intval($request->get('book_id')));
77         }
78         $this->checkOwnablePermission('page-create', $parent);
79
80         $draft = $this->pageRepo->getNewDraftPage($parent);
81         $this->pageRepo->publishDraft($draft, $request->only(array_keys($this->rules['create'])));
82
83         return response()->json($draft->forJsonDisplay());
84     }
85
86     /**
87      * 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.
90      *
91      * The 'html' property is the fully rendered & escaped HTML content that BookStack
92      * would show on page view, with page includes handled.
93      * The 'raw_html' property is the direct database stored HTML content, which would be
94      * what BookStack shows on page edit.
95      *
96      * See the "Content Security" section of these docs for security considerations when using
97      * the page content returned from this endpoint.
98      */
99     public function read(string $id)
100     {
101         $page = $this->queries->findVisibleByIdOrFail($id);
102
103         return response()->json($page->forJsonDisplay());
104     }
105
106     /**
107      * Update the details of a single page.
108      *
109      * See the 'create' action for details on the provided HTML/Markdown.
110      * Providing a 'book_id' or 'chapter_id' property will essentially move
111      * the page into that parent element if you have permissions to do so.
112      */
113     public function update(Request $request, string $id)
114     {
115         $requestData = $this->validate($request, $this->rules['update']);
116
117         $page = $this->queries->findVisibleByIdOrFail($id);
118         $this->checkOwnablePermission('page-update', $page);
119
120         $parent = null;
121         if ($request->has('chapter_id')) {
122             $parent = $this->entityQueries->chapters->findVisibleByIdOrFail(intval($request->get('chapter_id')));
123         } elseif ($request->has('book_id')) {
124             $parent = $this->entityQueries->books->findVisibleByIdOrFail(intval($request->get('book_id')));
125         }
126
127         if ($parent && !$parent->matches($page->getParent())) {
128             $this->checkOwnablePermission('page-delete', $page);
129
130             try {
131                 $this->pageRepo->move($page, $parent->getType() . ':' . $parent->id);
132             } catch (Exception $exception) {
133                 if ($exception instanceof  PermissionsException) {
134                     $this->showPermissionError();
135                 }
136
137                 return $this->jsonError(trans('errors.selected_book_chapter_not_found'));
138             }
139         }
140
141         $updatedPage = $this->pageRepo->update($page, $requestData);
142
143         return response()->json($updatedPage->forJsonDisplay());
144     }
145
146     /**
147      * Delete a page.
148      * This will typically send the page to the recycle bin.
149      */
150     public function delete(string $id)
151     {
152         $page = $this->queries->findVisibleByIdOrFail($id);
153         $this->checkOwnablePermission('page-delete', $page);
154
155         $this->pageRepo->destroy($page);
156
157         return response('', 204);
158     }
159 }