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;
21 use BookStack\Util\DatabaseTransaction;
26 public function __construct(
27 protected BaseRepo $baseRepo,
28 protected RevisionRepo $revisionRepo,
29 protected EntityQueries $entityQueries,
30 protected ReferenceStore $referenceStore,
31 protected ReferenceUpdater $referenceUpdater,
32 protected TrashCan $trashCan,
37 * Get a new draft page belonging to the given parent entity.
39 public function getNewDraftPage(Entity $parent)
41 $page = (new Page())->forceFill([
42 'name' => trans('entities.pages_initial_name'),
43 'created_by' => user()->id,
44 'owned_by' => user()->id,
45 'updated_by' => user()->id,
47 'editor' => PageEditorType::getSystemDefault()->value,
50 if ($parent instanceof Chapter) {
51 $page->chapter_id = $parent->id;
52 $page->book_id = $parent->book_id;
54 $page->book_id = $parent->id;
57 $defaultTemplate = $page->chapter->defaultTemplate ?? $page->book->defaultTemplate;
58 if ($defaultTemplate && userCan('view', $defaultTemplate)) {
60 'html' => $defaultTemplate->html,
61 'markdown' => $defaultTemplate->markdown,
65 (new DatabaseTransaction(function () use ($page) {
67 $page->refresh()->rebuildPermissions();
74 * Publish a draft page to make it a live, non-draft page.
76 public function publishDraft(Page $draft, array $input): Page
78 return (new DatabaseTransaction(function () use ($draft, $input) {
79 $draft->draft = false;
80 $draft->revision_count = 1;
81 $draft->priority = $this->getNewPriority($draft);
82 $this->updateTemplateStatusAndContentFromInput($draft, $input);
83 $this->baseRepo->update($draft, $input);
84 $draft->rebuildPermissions();
86 $summary = trim($input['summary'] ?? '') ?: trans('entities.pages_initial_revision');
87 $this->revisionRepo->storeNewForPage($draft, $summary);
90 Activity::add(ActivityType::PAGE_CREATE, $draft);
91 $this->baseRepo->sortParent($draft);
98 * Directly update the content for the given page from the provided input.
99 * Used for direct content access in a way that performs required changes
100 * (Search index and reference regen) without performing an official update.
102 public function setContentFromInput(Page $page, array $input): void
104 $this->updateTemplateStatusAndContentFromInput($page, $input);
105 $this->baseRepo->update($page, []);
109 * Update a page in the system.
111 public function update(Page $page, array $input): Page
113 // Hold the old details to compare later
114 $oldHtml = $page->html;
115 $oldName = $page->name;
116 $oldMarkdown = $page->markdown;
118 $this->updateTemplateStatusAndContentFromInput($page, $input);
119 $this->baseRepo->update($page, $input);
121 // Update with new details
122 $page->revision_count++;
125 // Remove all update drafts for this user and page.
126 $this->revisionRepo->deleteDraftsForCurrentUser($page);
128 // Save a revision after updating
129 $summary = trim($input['summary'] ?? '');
130 $htmlChanged = isset($input['html']) && $input['html'] !== $oldHtml;
131 $nameChanged = isset($input['name']) && $input['name'] !== $oldName;
132 $markdownChanged = isset($input['markdown']) && $input['markdown'] !== $oldMarkdown;
133 if ($htmlChanged || $nameChanged || $markdownChanged || $summary) {
134 $this->revisionRepo->storeNewForPage($page, $summary);
137 Activity::add(ActivityType::PAGE_UPDATE, $page);
138 $this->baseRepo->sortParent($page);
143 protected function updateTemplateStatusAndContentFromInput(Page $page, array $input): void
145 if (isset($input['template']) && userCan('templates-manage')) {
146 $page->template = ($input['template'] === 'true');
149 $pageContent = new PageContent($page);
150 $defaultEditor = PageEditorType::getSystemDefault();
151 $currentEditor = PageEditorType::forPage($page) ?: $defaultEditor;
152 $inputEditor = PageEditorType::fromRequestValue($input['editor'] ?? '') ?? $currentEditor;
153 $newEditor = $currentEditor;
155 $haveInput = isset($input['markdown']) || isset($input['html']);
156 $inputEmpty = empty($input['markdown']) && empty($input['html']);
158 if ($haveInput && $inputEmpty) {
159 $pageContent->setNewHTML('', user());
160 } elseif (!empty($input['markdown']) && is_string($input['markdown'])) {
161 $newEditor = PageEditorType::Markdown;
162 $pageContent->setNewMarkdown($input['markdown'], user());
163 } elseif (isset($input['html'])) {
164 $newEditor = ($inputEditor->isHtmlBased() ? $inputEditor : null) ?? ($defaultEditor->isHtmlBased() ? $defaultEditor : null) ?? PageEditorType::WysiwygTinymce;
165 $pageContent->setNewHTML($input['html'], user());
168 if (($newEditor !== $currentEditor || empty($page->editor)) && userCan('editor-change')) {
169 $page->editor = $newEditor->value;
170 } elseif (empty($page->editor)) {
171 $page->editor = $defaultEditor->value;
176 * Save a page update draft.
178 public function updatePageDraft(Page $page, array $input)
180 // If the page itself is a draft simply update that
182 $this->updateTemplateStatusAndContentFromInput($page, $input);
189 // Otherwise, save the data to a revision
190 $draft = $this->revisionRepo->getNewDraftForCurrentUser($page);
191 $draft->fill($input);
193 if (!empty($input['markdown'])) {
194 $draft->markdown = $input['markdown'];
197 $draft->html = $input['html'];
198 $draft->markdown = '';
207 * Destroy a page from the system.
211 public function destroy(Page $page)
213 $this->trashCan->softDestroyPage($page);
214 Activity::add(ActivityType::PAGE_DELETE, $page);
215 $this->trashCan->autoClearOld();
219 * Restores a revision's content back into a page.
221 public function restoreRevision(Page $page, int $revisionId): Page
223 $oldUrl = $page->getUrl();
224 $page->revision_count++;
226 /** @var PageRevision $revision */
227 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
229 $page->fill($revision->toArray());
230 $content = new PageContent($page);
232 if (!empty($revision->markdown)) {
233 $content->setNewMarkdown($revision->markdown, user());
235 $content->setNewHTML($revision->html, user());
238 $page->updated_by = user()->id;
239 $page->refreshSlug();
241 $page->indexForSearch();
242 $this->referenceStore->updateForEntity($page);
244 $summary = trans('entities.pages_revision_restored_from', ['id' => strval($revisionId), 'summary' => $revision->summary]);
245 $this->revisionRepo->storeNewForPage($page, $summary);
247 if ($oldUrl !== $page->getUrl()) {
248 $this->referenceUpdater->updateEntityReferences($page, $oldUrl);
251 Activity::add(ActivityType::PAGE_RESTORE, $page);
252 Activity::add(ActivityType::REVISION_RESTORE, $revision);
254 $this->baseRepo->sortParent($page);
260 * Move the given page into a new parent book or chapter.
261 * The $parentIdentifier must be a string of the following format:
262 * 'book:<id>' (book:5).
264 * @throws MoveOperationException
265 * @throws PermissionsException
267 public function move(Page $page, string $parentIdentifier): Entity
269 $parent = $this->entityQueries->findVisibleByStringIdentifier($parentIdentifier);
270 if (!$parent instanceof Chapter && !$parent instanceof Book) {
271 throw new MoveOperationException('Book or chapter to move page into not found');
274 if (!userCan('page-create', $parent)) {
275 throw new PermissionsException('User does not have permission to create a page within the new parent');
278 return (new DatabaseTransaction(function () use ($page, $parent) {
279 $page->chapter_id = ($parent instanceof Chapter) ? $parent->id : null;
280 $newBookId = ($parent instanceof Chapter) ? $parent->book->id : $parent->id;
281 $page->changeBook($newBookId);
282 $page->rebuildPermissions();
284 Activity::add(ActivityType::PAGE_MOVE, $page);
286 $this->baseRepo->sortParent($page);
293 * Get a new priority for a page.
295 protected function getNewPriority(Page $page): int
297 $parent = $page->getParent();
298 if ($parent instanceof Chapter) {
299 /** @var ?Page $lastPage */
300 $lastPage = $parent->pages('desc')->first();
302 return $lastPage ? $lastPage->priority + 1 : 0;
305 return (new BookContents($page->book))->getLastPriority() + 1;