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 * Update a page in the system.
93 public function update(Page $page, array $input): Page
95 // Hold the old details to compare later
96 $oldHtml = $page->html;
97 $oldName = $page->name;
98 $oldMarkdown = $page->markdown;
100 $this->updateTemplateStatusAndContentFromInput($page, $input);
101 $this->baseRepo->update($page, $input);
103 // Update with new details
104 $page->revision_count++;
107 // Remove all update drafts for this user & page.
108 $this->revisionRepo->deleteDraftsForCurrentUser($page);
110 // Save a revision after updating
111 $summary = trim($input['summary'] ?? '');
112 $htmlChanged = isset($input['html']) && $input['html'] !== $oldHtml;
113 $nameChanged = isset($input['name']) && $input['name'] !== $oldName;
114 $markdownChanged = isset($input['markdown']) && $input['markdown'] !== $oldMarkdown;
115 if ($htmlChanged || $nameChanged || $markdownChanged || $summary) {
116 $this->revisionRepo->storeNewForPage($page, $summary);
119 Activity::add(ActivityType::PAGE_UPDATE, $page);
124 protected function updateTemplateStatusAndContentFromInput(Page $page, array $input)
126 if (isset($input['template']) && userCan('templates-manage')) {
127 $page->template = ($input['template'] === 'true');
130 $pageContent = new PageContent($page);
131 $defaultEditor = PageEditorType::getSystemDefault();
132 $currentEditor = PageEditorType::forPage($page) ?: $defaultEditor;
133 $inputEditor = PageEditorType::fromRequestValue($input['editor'] ?? '') ?? $currentEditor;
134 $newEditor = $currentEditor;
136 $haveInput = isset($input['markdown']) || isset($input['html']);
137 $inputEmpty = empty($input['markdown']) && empty($input['html']);
139 if ($haveInput && $inputEmpty) {
140 $pageContent->setNewHTML('', user());
141 } elseif (!empty($input['markdown']) && is_string($input['markdown'])) {
142 $newEditor = PageEditorType::Markdown;
143 $pageContent->setNewMarkdown($input['markdown'], user());
144 } elseif (isset($input['html'])) {
145 $newEditor = ($inputEditor->isHtmlBased() ? $inputEditor : null) ?? ($defaultEditor->isHtmlBased() ? $defaultEditor : null) ?? PageEditorType::WysiwygTinymce;
146 $pageContent->setNewHTML($input['html'], user());
149 if (($newEditor !== $currentEditor || empty($page->editor)) && userCan('editor-change')) {
150 $page->editor = $newEditor->value;
151 } elseif (empty($page->editor)) {
152 $page->editor = $defaultEditor->value;
157 * Save a page update draft.
159 public function updatePageDraft(Page $page, array $input)
161 // If the page itself is a draft simply update that
163 $this->updateTemplateStatusAndContentFromInput($page, $input);
170 // Otherwise, save the data to a revision
171 $draft = $this->revisionRepo->getNewDraftForCurrentUser($page);
172 $draft->fill($input);
174 if (!empty($input['markdown'])) {
175 $draft->markdown = $input['markdown'];
178 $draft->html = $input['html'];
179 $draft->markdown = '';
188 * Destroy a page from the system.
192 public function destroy(Page $page)
194 $this->trashCan->softDestroyPage($page);
195 Activity::add(ActivityType::PAGE_DELETE, $page);
196 $this->trashCan->autoClearOld();
200 * Restores a revision's content back into a page.
202 public function restoreRevision(Page $page, int $revisionId): Page
204 $oldUrl = $page->getUrl();
205 $page->revision_count++;
207 /** @var PageRevision $revision */
208 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
210 $page->fill($revision->toArray());
211 $content = new PageContent($page);
213 if (!empty($revision->markdown)) {
214 $content->setNewMarkdown($revision->markdown, user());
216 $content->setNewHTML($revision->html, user());
219 $page->updated_by = user()->id;
220 $page->refreshSlug();
222 $page->indexForSearch();
223 $this->referenceStore->updateForEntity($page);
225 $summary = trans('entities.pages_revision_restored_from', ['id' => strval($revisionId), 'summary' => $revision->summary]);
226 $this->revisionRepo->storeNewForPage($page, $summary);
228 if ($oldUrl !== $page->getUrl()) {
229 $this->referenceUpdater->updateEntityReferences($page, $oldUrl);
232 Activity::add(ActivityType::PAGE_RESTORE, $page);
233 Activity::add(ActivityType::REVISION_RESTORE, $revision);
239 * Move the given page into a new parent book or chapter.
240 * The $parentIdentifier must be a string of the following format:
241 * 'book:<id>' (book:5).
243 * @throws MoveOperationException
244 * @throws PermissionsException
246 public function move(Page $page, string $parentIdentifier): Entity
248 $parent = $this->entityQueries->findVisibleByStringIdentifier($parentIdentifier);
249 if (!$parent instanceof Chapter && !$parent instanceof Book) {
250 throw new MoveOperationException('Book or chapter to move page into not found');
253 if (!userCan('page-create', $parent)) {
254 throw new PermissionsException('User does not have permission to create a page within the new parent');
257 $page->chapter_id = ($parent instanceof Chapter) ? $parent->id : null;
258 $newBookId = ($parent instanceof Chapter) ? $parent->book->id : $parent->id;
259 $page->changeBook($newBookId);
260 $page->rebuildPermissions();
262 Activity::add(ActivityType::PAGE_MOVE, $page);
268 * Get a new priority for a page.
270 protected function getNewPriority(Page $page): int
272 $parent = $page->getParent();
273 if ($parent instanceof Chapter) {
274 /** @var ?Page $lastPage */
275 $lastPage = $parent->pages('desc')->first();
277 return $lastPage ? $lastPage->priority + 1 : 0;
280 return (new BookContents($page->book))->getLastPriority() + 1;