X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/f2ee95ca03a2bc1c0f80b5d775a340d920784bdb..refs/pull/3406/head:/app/Entities/Repos/PageRepo.php diff --git a/app/Entities/Repos/PageRepo.php b/app/Entities/Repos/PageRepo.php index 98fe4ef55..c106d2fd3 100644 --- a/app/Entities/Repos/PageRepo.php +++ b/app/Entities/Repos/PageRepo.php @@ -10,6 +10,7 @@ use BookStack\Entities\Models\Page; use BookStack\Entities\Models\PageRevision; use BookStack\Entities\Tools\BookContents; use BookStack\Entities\Tools\PageContent; +use BookStack\Entities\Tools\PageEditorData; use BookStack\Entities\Tools\TrashCan; use BookStack\Exceptions\MoveOperationException; use BookStack\Exceptions\NotFoundException; @@ -69,9 +70,10 @@ class PageRepo */ public function getByOldSlug(string $bookSlug, string $pageSlug): ?Page { + /** @var ?PageRevision $revision */ $revision = PageRevision::query() ->whereHas('page', function (Builder $query) { - $query->visible(); + $query->scopes('visible'); }) ->where('slug', '=', $pageSlug) ->where('type', '=', 'version') @@ -80,7 +82,7 @@ class PageRepo ->with('page') ->first(); - return $revision ? $revision->page : null; + return $revision->page ?? null; } /** @@ -170,7 +172,7 @@ class PageRepo $draft->indexForSearch(); $draft->refresh(); - Activity::addForEntity($draft, ActivityType::PAGE_CREATE); + Activity::add(ActivityType::PAGE_CREATE, $draft); return $draft; } @@ -204,7 +206,7 @@ class PageRepo $this->savePageRevision($page, $summary); } - Activity::addForEntity($page, ActivityType::PAGE_UPDATE); + Activity::add(ActivityType::PAGE_UPDATE, $page); return $page; } @@ -216,11 +218,25 @@ class PageRepo } $pageContent = new PageContent($page); - if (!empty($input['markdown'] ?? '')) { + $currentEditor = $page->editor ?: PageEditorData::getSystemDefaultEditor(); + $newEditor = $currentEditor; + + $haveInput = isset($input['markdown']) || isset($input['html']); + $inputEmpty = empty($input['markdown']) && empty($input['html']); + + if ($haveInput && $inputEmpty) { + $pageContent->setNewHTML(''); + } elseif (!empty($input['markdown']) && is_string($input['markdown'])) { + $newEditor = 'markdown'; $pageContent->setNewMarkdown($input['markdown']); } elseif (isset($input['html'])) { + $newEditor = 'wysiwyg'; $pageContent->setNewHTML($input['html']); } + + if ($newEditor !== $currentEditor && userCan('editor-change')) { + $page->editor = $newEditor; + } } /** @@ -228,8 +244,12 @@ class PageRepo */ protected function savePageRevision(Page $page, string $summary = null): PageRevision { - $revision = new PageRevision($page->getAttributes()); + $revision = new PageRevision(); + $revision->name = $page->name; + $revision->html = $page->html; + $revision->markdown = $page->markdown; + $revision->text = $page->text; $revision->page_id = $page->id; $revision->slug = $page->slug; $revision->book_slug = $page->book->slug; @@ -252,19 +272,22 @@ class PageRepo { // If the page itself is a draft simply update that if ($page->draft) { - if (isset($input['html'])) { - (new PageContent($page))->setNewHTML($input['html']); - } + $this->updateTemplateStatusAndContentFromInput($page, $input); $page->fill($input); $page->save(); return $page; } - // Otherwise save the data to a revision + // Otherwise, save the data to a revision $draft = $this->getPageRevisionToUpdate($page); $draft->fill($input); - if (setting('app-editor') !== 'markdown') { + + if (!empty($input['markdown'])) { + $draft->markdown = $input['markdown']; + $draft->html = ''; + } else { + $draft->html = $input['html']; $draft->markdown = ''; } @@ -282,7 +305,7 @@ class PageRepo { $trashCan = new TrashCan(); $trashCan->softDestroyPage($page); - Activity::addForEntity($page, ActivityType::PAGE_DELETE); + Activity::add(ActivityType::PAGE_DELETE, $page); $trashCan->autoClearOld(); } @@ -292,6 +315,8 @@ class PageRepo public function restoreRevision(Page $page, int $revisionId): Page { $page->revision_count++; + + /** @var PageRevision $revision */ $revision = $page->revisions()->where('id', '=', $revisionId)->first(); $page->fill($revision->toArray()); @@ -311,7 +336,7 @@ class PageRepo $summary = trans('entities.pages_revision_restored_from', ['id' => strval($revisionId), 'summary' => $revision->summary]); $this->savePageRevision($page, $summary); - Activity::addForEntity($page, ActivityType::PAGE_RESTORE); + Activity::add(ActivityType::PAGE_RESTORE, $page); return $page; } @@ -327,7 +352,7 @@ class PageRepo public function move(Page $page, string $parentIdentifier): Entity { $parent = $this->findParentByIdentifier($parentIdentifier); - if ($parent === null) { + if (is_null($parent)) { throw new MoveOperationException('Book or chapter to move page into not found'); } @@ -336,59 +361,23 @@ class PageRepo } $page->chapter_id = ($parent instanceof Chapter) ? $parent->id : null; - $page->changeBook($parent instanceof Book ? $parent->id : $parent->book->id); + $newBookId = ($parent instanceof Chapter) ? $parent->book->id : $parent->id; + $page->changeBook($newBookId); $page->rebuildPermissions(); - Activity::addForEntity($page, ActivityType::PAGE_MOVE); + Activity::add(ActivityType::PAGE_MOVE, $page); return $parent; } /** - * Copy an existing page in the system. - * Optionally providing a new parent via string identifier and a new name. - * - * @throws MoveOperationException - * @throws PermissionsException - */ - public function copy(Page $page, string $parentIdentifier = null, string $newName = null): Page - { - $parent = $parentIdentifier ? $this->findParentByIdentifier($parentIdentifier) : $page->getParent(); - if ($parent === null) { - throw new MoveOperationException('Book or chapter to move page into not found'); - } - - if (!userCan('page-create', $parent)) { - throw new PermissionsException('User does not have permission to create a page within the new parent'); - } - - $copyPage = $this->getNewDraftPage($parent); - $pageData = $page->getAttributes(); - - // Update name - if (!empty($newName)) { - $pageData['name'] = $newName; - } - - // Copy tags from previous page if set - if ($page->tags) { - $pageData['tags'] = []; - foreach ($page->tags as $tag) { - $pageData['tags'][] = ['name' => $tag->name, 'value' => $tag->value]; - } - } - - return $this->publishDraft($copyPage, $pageData); - } - - /** - * Find a page parent entity via a identifier string in the format: + * Find a page parent entity via an identifier string in the format: * {type}:{id} * Example: (book:5). * * @throws MoveOperationException */ - protected function findParentByIdentifier(string $identifier): ?Entity + public function findParentByIdentifier(string $identifier): ?Entity { $stringExploded = explode(':', $identifier); $entityType = $stringExploded[0]; @@ -408,7 +397,7 @@ class PageRepo */ protected function changeParent(Page $page, Entity $parent) { - $book = ($parent instanceof Book) ? $parent : $parent->book; + $book = ($parent instanceof Chapter) ? $parent->book : $parent; $page->chapter_id = ($parent instanceof Chapter) ? $parent->id : 0; $page->save(); @@ -469,6 +458,7 @@ class PageRepo { $parent = $page->getParent(); if ($parent instanceof Chapter) { + /** @var ?Page $lastPage */ $lastPage = $parent->pages('desc')->first(); return $lastPage ? $lastPage->priority + 1 : 0;