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\Permissions\Permission;
20 use BookStack\References\ReferenceStore;
21 use BookStack\References\ReferenceUpdater;
22 use BookStack\Util\DatabaseTransaction;
27 public function __construct(
28 protected BaseRepo $baseRepo,
29 protected RevisionRepo $revisionRepo,
30 protected EntityQueries $entityQueries,
31 protected ReferenceStore $referenceStore,
32 protected ReferenceUpdater $referenceUpdater,
33 protected TrashCan $trashCan,
38 * Get a new draft page belonging to the given parent entity.
40 public function getNewDraftPage(Entity $parent)
42 $page = (new Page())->forceFill([
43 'name' => trans('entities.pages_initial_name'),
44 'created_by' => user()->id,
45 'owned_by' => user()->id,
46 'updated_by' => user()->id,
48 'editor' => PageEditorType::getSystemDefault()->value,
51 if ($parent instanceof Chapter) {
52 $page->chapter_id = $parent->id;
53 $page->book_id = $parent->book_id;
55 $page->book_id = $parent->id;
58 $defaultTemplate = $page->chapter->defaultTemplate ?? $page->book->defaultTemplate;
59 if ($defaultTemplate && userCan(Permission::PageView, $defaultTemplate)) {
61 'html' => $defaultTemplate->html,
62 'markdown' => $defaultTemplate->markdown,
66 (new DatabaseTransaction(function () use ($page) {
68 $page->refresh()->rebuildPermissions();
75 * Publish a draft page to make it a live, non-draft page.
77 public function publishDraft(Page $draft, array $input): Page
79 return (new DatabaseTransaction(function () use ($draft, $input) {
80 $draft->draft = false;
81 $draft->revision_count = 1;
82 $draft->priority = $this->getNewPriority($draft);
83 $this->updateTemplateStatusAndContentFromInput($draft, $input);
84 $this->baseRepo->update($draft, $input);
85 $draft->rebuildPermissions();
87 $summary = trim($input['summary'] ?? '') ?: trans('entities.pages_initial_revision');
88 $this->revisionRepo->storeNewForPage($draft, $summary);
91 Activity::add(ActivityType::PAGE_CREATE, $draft);
92 $this->baseRepo->sortParent($draft);
99 * Directly update the content for the given page from the provided input.
100 * Used for direct content access in a way that performs required changes
101 * (Search index and reference regen) without performing an official update.
103 public function setContentFromInput(Page $page, array $input): void
105 $this->updateTemplateStatusAndContentFromInput($page, $input);
106 $this->baseRepo->update($page, []);
110 * Update a page in the system.
112 public function update(Page $page, array $input): Page
114 // Hold the old details to compare later
115 $oldHtml = $page->html;
116 $oldName = $page->name;
117 $oldMarkdown = $page->markdown;
119 $this->updateTemplateStatusAndContentFromInput($page, $input);
120 $this->baseRepo->update($page, $input);
122 // Update with new details
123 $page->revision_count++;
126 // Remove all update drafts for this user and page.
127 $this->revisionRepo->deleteDraftsForCurrentUser($page);
129 // Save a revision after updating
130 $summary = trim($input['summary'] ?? '');
131 $htmlChanged = isset($input['html']) && $input['html'] !== $oldHtml;
132 $nameChanged = isset($input['name']) && $input['name'] !== $oldName;
133 $markdownChanged = isset($input['markdown']) && $input['markdown'] !== $oldMarkdown;
134 if ($htmlChanged || $nameChanged || $markdownChanged || $summary) {
135 $this->revisionRepo->storeNewForPage($page, $summary);
138 Activity::add(ActivityType::PAGE_UPDATE, $page);
139 $this->baseRepo->sortParent($page);
144 protected function updateTemplateStatusAndContentFromInput(Page $page, array $input): void
146 if (isset($input['template']) && userCan(Permission::TemplatesManage)) {
147 $page->template = ($input['template'] === 'true');
150 $pageContent = new PageContent($page);
151 $defaultEditor = PageEditorType::getSystemDefault();
152 $currentEditor = PageEditorType::forPage($page) ?: $defaultEditor;
153 $inputEditor = PageEditorType::fromRequestValue($input['editor'] ?? '') ?? $currentEditor;
154 $newEditor = $currentEditor;
156 $haveInput = isset($input['markdown']) || isset($input['html']);
157 $inputEmpty = empty($input['markdown']) && empty($input['html']);
159 if ($haveInput && $inputEmpty) {
160 $pageContent->setNewHTML('', user());
161 } elseif (!empty($input['markdown']) && is_string($input['markdown'])) {
162 $newEditor = PageEditorType::Markdown;
163 $pageContent->setNewMarkdown($input['markdown'], user());
164 } elseif (isset($input['html'])) {
165 $newEditor = ($inputEditor->isHtmlBased() ? $inputEditor : null) ?? ($defaultEditor->isHtmlBased() ? $defaultEditor : null) ?? PageEditorType::WysiwygTinymce;
166 $pageContent->setNewHTML($input['html'], user());
169 if (($newEditor !== $currentEditor || empty($page->editor)) && userCan(Permission::EditorChange)) {
170 $page->editor = $newEditor->value;
171 } elseif (empty($page->editor)) {
172 $page->editor = $defaultEditor->value;
177 * Save a page update draft.
179 public function updatePageDraft(Page $page, array $input)
181 // If the page itself is a draft simply update that
183 $this->updateTemplateStatusAndContentFromInput($page, $input);
190 // Otherwise, save the data to a revision
191 $draft = $this->revisionRepo->getNewDraftForCurrentUser($page);
192 $draft->fill($input);
194 if (!empty($input['markdown'])) {
195 $draft->markdown = $input['markdown'];
198 $draft->html = $input['html'];
199 $draft->markdown = '';
208 * Destroy a page from the system.
212 public function destroy(Page $page)
214 $this->trashCan->softDestroyPage($page);
215 Activity::add(ActivityType::PAGE_DELETE, $page);
216 $this->trashCan->autoClearOld();
220 * Restores a revision's content back into a page.
222 public function restoreRevision(Page $page, int $revisionId): Page
224 $oldUrl = $page->getUrl();
225 $page->revision_count++;
227 /** @var PageRevision $revision */
228 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
230 $page->fill($revision->toArray());
231 $content = new PageContent($page);
233 if (!empty($revision->markdown)) {
234 $content->setNewMarkdown($revision->markdown, user());
236 $content->setNewHTML($revision->html, user());
239 $page->updated_by = user()->id;
240 $page->refreshSlug();
242 $page->indexForSearch();
243 $this->referenceStore->updateForEntity($page);
245 $summary = trans('entities.pages_revision_restored_from', ['id' => strval($revisionId), 'summary' => $revision->summary]);
246 $this->revisionRepo->storeNewForPage($page, $summary);
248 if ($oldUrl !== $page->getUrl()) {
249 $this->referenceUpdater->updateEntityReferences($page, $oldUrl);
252 Activity::add(ActivityType::PAGE_RESTORE, $page);
253 Activity::add(ActivityType::REVISION_RESTORE, $revision);
255 $this->baseRepo->sortParent($page);
261 * Move the given page into a new parent book or chapter.
262 * The $parentIdentifier must be a string of the following format:
263 * 'book:<id>' (book:5).
265 * @throws MoveOperationException
266 * @throws PermissionsException
268 public function move(Page $page, string $parentIdentifier): Entity
270 $parent = $this->entityQueries->findVisibleByStringIdentifier($parentIdentifier);
271 if (!$parent instanceof Chapter && !$parent instanceof Book) {
272 throw new MoveOperationException('Book or chapter to move page into not found');
275 if (!userCan(Permission::PageCreate, $parent)) {
276 throw new PermissionsException('User does not have permission to create a page within the new parent');
279 return (new DatabaseTransaction(function () use ($page, $parent) {
280 $page->chapter_id = ($parent instanceof Chapter) ? $parent->id : null;
281 $newBookId = ($parent instanceof Chapter) ? $parent->book->id : $parent->id;
282 $page->changeBook($newBookId);
283 $page->rebuildPermissions();
285 Activity::add(ActivityType::PAGE_MOVE, $page);
287 $this->baseRepo->sortParent($page);
294 * Get a new priority for a page.
296 protected function getNewPriority(Page $page): int
298 $parent = $page->getParent();
299 if ($parent instanceof Chapter) {
300 /** @var ?Page $lastPage */
301 $lastPage = $parent->pages('desc')->first();
303 return $lastPage ? $lastPage->priority + 1 : 0;
306 return (new BookContents($page->book))->getLastPriority() + 1;