3 namespace BookStack\Entities\Repos;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Entity;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Entities\Models\PageRevision;
11 use BookStack\Entities\Queries\EntityQueries;
12 use BookStack\Entities\Tools\BookContents;
13 use BookStack\Entities\Tools\PageContent;
14 use BookStack\Entities\Tools\PageEditorType;
15 use BookStack\Entities\Tools\TrashCan;
16 use BookStack\Exceptions\MoveOperationException;
17 use BookStack\Exceptions\PermissionsException;
18 use BookStack\Facades\Activity;
19 use BookStack\References\ReferenceStore;
20 use BookStack\References\ReferenceUpdater;
25 public function __construct(
26 protected BaseRepo $baseRepo,
27 protected RevisionRepo $revisionRepo,
28 protected EntityQueries $entityQueries,
29 protected ReferenceStore $referenceStore,
30 protected ReferenceUpdater $referenceUpdater,
31 protected TrashCan $trashCan,
36 * Get a new draft page belonging to the given parent entity.
38 public function getNewDraftPage(Entity $parent)
40 $page = (new Page())->forceFill([
41 'name' => trans('entities.pages_initial_name'),
42 'created_by' => user()->id,
43 'owned_by' => user()->id,
44 'updated_by' => user()->id,
46 'editor' => PageEditorType::getSystemDefault()->value,
49 if ($parent instanceof Chapter) {
50 $page->chapter_id = $parent->id;
51 $page->book_id = $parent->book_id;
53 $page->book_id = $parent->id;
56 $defaultTemplate = $page->chapter->defaultTemplate ?? $page->book->defaultTemplate;
57 if ($defaultTemplate && userCan('view', $defaultTemplate)) {
59 'html' => $defaultTemplate->html,
60 'markdown' => $defaultTemplate->markdown,
65 $page->refresh()->rebuildPermissions();
71 * Publish a draft page to make it a live, non-draft page.
73 public function publishDraft(Page $draft, array $input): Page
75 $draft->draft = false;
76 $draft->revision_count = 1;
77 $draft->priority = $this->getNewPriority($draft);
78 $this->updateTemplateStatusAndContentFromInput($draft, $input);
79 $this->baseRepo->update($draft, $input);
81 $summary = trim($input['summary'] ?? '') ?: trans('entities.pages_initial_revision');
82 $this->revisionRepo->storeNewForPage($draft, $summary);
85 Activity::add(ActivityType::PAGE_CREATE, $draft);
91 * Directly update the content for the given page from the provided input.
92 * Used for direct content access in a way that performs required changes
93 * (Search index & reference regen) without performing an official update.
95 public function setContentFromInput(Page $page, array $input): void
97 $this->updateTemplateStatusAndContentFromInput($page, $input);
98 $this->baseRepo->update($page, []);
102 * Update a page in the system.
104 public function update(Page $page, array $input): Page
106 // Hold the old details to compare later
107 $oldHtml = $page->html;
108 $oldName = $page->name;
109 $oldMarkdown = $page->markdown;
111 $this->updateTemplateStatusAndContentFromInput($page, $input);
112 $this->baseRepo->update($page, $input);
114 // Update with new details
115 $page->revision_count++;
118 // Remove all update drafts for this user & page.
119 $this->revisionRepo->deleteDraftsForCurrentUser($page);
121 // Save a revision after updating
122 $summary = trim($input['summary'] ?? '');
123 $htmlChanged = isset($input['html']) && $input['html'] !== $oldHtml;
124 $nameChanged = isset($input['name']) && $input['name'] !== $oldName;
125 $markdownChanged = isset($input['markdown']) && $input['markdown'] !== $oldMarkdown;
126 if ($htmlChanged || $nameChanged || $markdownChanged || $summary) {
127 $this->revisionRepo->storeNewForPage($page, $summary);
130 Activity::add(ActivityType::PAGE_UPDATE, $page);
135 protected function updateTemplateStatusAndContentFromInput(Page $page, array $input): void
137 if (isset($input['template']) && userCan('templates-manage')) {
138 $page->template = ($input['template'] === 'true');
141 $pageContent = new PageContent($page);
142 $defaultEditor = PageEditorType::getSystemDefault();
143 $currentEditor = PageEditorType::forPage($page) ?: $defaultEditor;
144 $inputEditor = PageEditorType::fromRequestValue($input['editor'] ?? '') ?? $currentEditor;
145 $newEditor = $currentEditor;
147 $haveInput = isset($input['markdown']) || isset($input['html']);
148 $inputEmpty = empty($input['markdown']) && empty($input['html']);
150 if ($haveInput && $inputEmpty) {
151 $pageContent->setNewHTML('', user());
152 } elseif (!empty($input['markdown']) && is_string($input['markdown'])) {
153 $newEditor = PageEditorType::Markdown;
154 $pageContent->setNewMarkdown($input['markdown'], user());
155 } elseif (isset($input['html'])) {
156 $newEditor = ($inputEditor->isHtmlBased() ? $inputEditor : null) ?? ($defaultEditor->isHtmlBased() ? $defaultEditor : null) ?? PageEditorType::WysiwygTinymce;
157 $pageContent->setNewHTML($input['html'], user());
160 if (($newEditor !== $currentEditor || empty($page->editor)) && userCan('editor-change')) {
161 $page->editor = $newEditor->value;
162 } elseif (empty($page->editor)) {
163 $page->editor = $defaultEditor->value;
168 * Save a page update draft.
170 public function updatePageDraft(Page $page, array $input)
172 // If the page itself is a draft simply update that
174 $this->updateTemplateStatusAndContentFromInput($page, $input);
181 // Otherwise, save the data to a revision
182 $draft = $this->revisionRepo->getNewDraftForCurrentUser($page);
183 $draft->fill($input);
185 if (!empty($input['markdown'])) {
186 $draft->markdown = $input['markdown'];
189 $draft->html = $input['html'];
190 $draft->markdown = '';
199 * Destroy a page from the system.
203 public function destroy(Page $page)
205 $this->trashCan->softDestroyPage($page);
206 Activity::add(ActivityType::PAGE_DELETE, $page);
207 $this->trashCan->autoClearOld();
211 * Restores a revision's content back into a page.
213 public function restoreRevision(Page $page, int $revisionId): Page
215 $oldUrl = $page->getUrl();
216 $page->revision_count++;
218 /** @var PageRevision $revision */
219 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
221 $page->fill($revision->toArray());
222 $content = new PageContent($page);
224 if (!empty($revision->markdown)) {
225 $content->setNewMarkdown($revision->markdown, user());
227 $content->setNewHTML($revision->html, user());
230 $page->updated_by = user()->id;
231 $page->refreshSlug();
233 $page->indexForSearch();
234 $this->referenceStore->updateForEntity($page);
236 $summary = trans('entities.pages_revision_restored_from', ['id' => strval($revisionId), 'summary' => $revision->summary]);
237 $this->revisionRepo->storeNewForPage($page, $summary);
239 if ($oldUrl !== $page->getUrl()) {
240 $this->referenceUpdater->updateEntityReferences($page, $oldUrl);
243 Activity::add(ActivityType::PAGE_RESTORE, $page);
244 Activity::add(ActivityType::REVISION_RESTORE, $revision);
250 * Move the given page into a new parent book or chapter.
251 * The $parentIdentifier must be a string of the following format:
252 * 'book:<id>' (book:5).
254 * @throws MoveOperationException
255 * @throws PermissionsException
257 public function move(Page $page, string $parentIdentifier): Entity
259 $parent = $this->entityQueries->findVisibleByStringIdentifier($parentIdentifier);
260 if (!$parent instanceof Chapter && !$parent instanceof Book) {
261 throw new MoveOperationException('Book or chapter to move page into not found');
264 if (!userCan('page-create', $parent)) {
265 throw new PermissionsException('User does not have permission to create a page within the new parent');
268 $page->chapter_id = ($parent instanceof Chapter) ? $parent->id : null;
269 $newBookId = ($parent instanceof Chapter) ? $parent->book->id : $parent->id;
270 $page->changeBook($newBookId);
271 $page->rebuildPermissions();
273 Activity::add(ActivityType::PAGE_MOVE, $page);
279 * Get a new priority for a page.
281 protected function getNewPriority(Page $page): int
283 $parent = $page->getParent();
284 if ($parent instanceof Chapter) {
285 /** @var ?Page $lastPage */
286 $lastPage = $parent->pages('desc')->first();
288 return $lastPage ? $lastPage->priority + 1 : 0;
291 return (new BookContents($page->book))->getLastPriority() + 1;