3 namespace BookStack\Entities\Tools;
5 use BookStack\Entities\EntityProvider;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Models\Chapter;
9 use BookStack\Entities\Models\Deletion;
10 use BookStack\Entities\Models\Entity;
11 use BookStack\Entities\Models\HasCoverImage;
12 use BookStack\Entities\Models\Page;
13 use BookStack\Exceptions\NotifyException;
14 use BookStack\Facades\Activity;
15 use BookStack\Uploads\AttachmentService;
16 use BookStack\Uploads\ImageService;
18 use Illuminate\Database\Eloquent\Builder;
19 use Illuminate\Support\Carbon;
24 * Send a shelf to the recycle bin.
26 * @throws NotifyException
28 public function softDestroyShelf(Bookshelf $shelf)
30 $this->ensureDeletable($shelf);
31 Deletion::createForEntity($shelf);
36 * Send a book to the recycle bin.
40 public function softDestroyBook(Book $book)
42 $this->ensureDeletable($book);
43 Deletion::createForEntity($book);
45 foreach ($book->pages as $page) {
46 $this->softDestroyPage($page, false);
49 foreach ($book->chapters as $chapter) {
50 $this->softDestroyChapter($chapter, false);
57 * Send a chapter to the recycle bin.
61 public function softDestroyChapter(Chapter $chapter, bool $recordDelete = true)
64 $this->ensureDeletable($chapter);
65 Deletion::createForEntity($chapter);
68 if (count($chapter->pages) > 0) {
69 foreach ($chapter->pages as $page) {
70 $this->softDestroyPage($page, false);
78 * Send a page to the recycle bin.
82 public function softDestroyPage(Page $page, bool $recordDelete = true)
85 $this->ensureDeletable($page);
86 Deletion::createForEntity($page);
93 * Ensure the given entity is deletable.
94 * Is not for permissions, but logical conditions within the application.
95 * Will throw if not deletable.
97 * @throws NotifyException
99 protected function ensureDeletable(Entity $entity): void
101 $customHomeId = intval(explode(':', setting('app-homepage', '0:'))[0]);
102 $customHomeActive = setting('app-homepage-type') === 'page';
103 $removeCustomHome = false;
105 // Check custom homepage usage for pages
106 if ($entity instanceof Page && $entity->id === $customHomeId) {
107 if ($customHomeActive) {
108 throw new NotifyException(trans('errors.page_custom_home_deletion'), $entity->getUrl());
110 $removeCustomHome = true;
113 // Check custom homepage usage within chapters or books
114 if ($entity instanceof Chapter || $entity instanceof Book) {
115 if ($entity->pages()->where('id', '=', $customHomeId)->exists()) {
116 if ($customHomeActive) {
117 throw new NotifyException(trans('errors.page_custom_home_deletion'), $entity->getUrl());
119 $removeCustomHome = true;
123 if ($removeCustomHome) {
124 setting()->remove('app-homepage');
129 * Remove a bookshelf from the system.
133 protected function destroyShelf(Bookshelf $shelf): int
135 $this->destroyCommonRelations($shelf);
136 $shelf->forceDelete();
142 * Remove a book from the system.
143 * Destroys any child chapters and pages.
147 protected function destroyBook(Book $book): int
150 $pages = $book->pages()->withTrashed()->get();
151 foreach ($pages as $page) {
152 $this->destroyPage($page);
156 $chapters = $book->chapters()->withTrashed()->get();
157 foreach ($chapters as $chapter) {
158 $this->destroyChapter($chapter);
162 $this->destroyCommonRelations($book);
163 $book->forceDelete();
169 * Remove a chapter from the system.
170 * Destroys all pages within.
174 protected function destroyChapter(Chapter $chapter): int
177 $pages = $chapter->pages()->withTrashed()->get();
178 foreach ($pages as $page) {
179 $this->destroyPage($page);
183 $this->destroyCommonRelations($chapter);
184 $chapter->forceDelete();
190 * Remove a page from the system.
194 protected function destroyPage(Page $page): int
196 $this->destroyCommonRelations($page);
197 $page->allRevisions()->delete();
199 // Delete Attached Files
200 $attachmentService = app()->make(AttachmentService::class);
201 foreach ($page->attachments as $attachment) {
202 $attachmentService->deleteFile($attachment);
205 // Remove book template usages
206 Book::query()->where('default_template_id', '=', $page->id)
207 ->update(['default_template_id' => null]);
209 // Remove chapter template usages
210 Chapter::query()->where('default_template_id', '=', $page->id)
211 ->update(['default_template_id' => null]);
213 $page->forceDelete();
219 * Get the total counts of those that have been trashed
220 * but not yet fully deleted (In recycle bin).
222 public function getTrashedCounts(): array
226 foreach ((new EntityProvider())->all() as $key => $instance) {
227 /** @var Builder<Entity> $query */
228 $query = $instance->newQuery();
229 $counts[$key] = $query->onlyTrashed()->count();
236 * Destroy all items that have pending deletions.
240 public function empty(): int
242 $deletions = Deletion::all();
244 foreach ($deletions as $deletion) {
245 $deleteCount += $this->destroyFromDeletion($deletion);
252 * Destroy an element from the given deletion model.
256 public function destroyFromDeletion(Deletion $deletion): int
258 // We directly load the deletable element here just to ensure it still
259 // exists in the event it has already been destroyed during this request.
260 $entity = $deletion->deletable()->first();
263 $count = $this->destroyEntity($deletion->deletable);
271 * Restore the content within the given deletion.
275 public function restoreFromDeletion(Deletion $deletion): int
277 $shouldRestore = true;
280 if ($deletion->deletable instanceof Entity) {
281 $parent = $deletion->deletable->getParent();
282 if ($parent && $parent->trashed()) {
283 $shouldRestore = false;
287 if ($deletion->deletable instanceof Entity && $shouldRestore) {
288 $restoreCount = $this->restoreEntity($deletion->deletable);
293 return $restoreCount;
297 * Automatically clear old content from the recycle bin
298 * depending on the configured lifetime.
299 * Returns the total number of deleted elements.
303 public function autoClearOld(): int
305 $lifetime = intval(config('app.recycle_bin_lifetime'));
310 $clearBeforeDate = Carbon::now()->addSeconds(10)->subDays($lifetime);
313 $deletionsToRemove = Deletion::query()->where('created_at', '<', $clearBeforeDate)->get();
314 foreach ($deletionsToRemove as $deletion) {
315 $deleteCount += $this->destroyFromDeletion($deletion);
322 * Restore an entity so it is essentially un-deleted.
323 * Deletions on restored child elements will be removed during this restoration.
325 protected function restoreEntity(Entity $entity): int
330 $restoreAction = function ($entity) use (&$count) {
331 if ($entity->deletions_count > 0) {
332 $entity->deletions()->delete();
339 if ($entity instanceof Chapter || $entity instanceof Book) {
340 $entity->pages()->withTrashed()->withCount('deletions')->get()->each($restoreAction);
343 if ($entity instanceof Book) {
344 $entity->chapters()->withTrashed()->withCount('deletions')->get()->each($restoreAction);
351 * Destroy the given entity.
355 public function destroyEntity(Entity $entity): int
357 if ($entity instanceof Page) {
358 return $this->destroyPage($entity);
360 if ($entity instanceof Chapter) {
361 return $this->destroyChapter($entity);
363 if ($entity instanceof Book) {
364 return $this->destroyBook($entity);
366 if ($entity instanceof Bookshelf) {
367 return $this->destroyShelf($entity);
374 * Update entity relations to remove or update outstanding connections.
376 protected function destroyCommonRelations(Entity $entity)
378 Activity::removeEntity($entity);
379 $entity->views()->delete();
380 $entity->permissions()->delete();
381 $entity->tags()->delete();
382 $entity->comments()->delete();
383 $entity->jointPermissions()->delete();
384 $entity->searchTerms()->delete();
385 $entity->deletions()->delete();
386 $entity->favourites()->delete();
387 $entity->watches()->delete();
388 $entity->referencesTo()->delete();
389 $entity->referencesFrom()->delete();
391 if ($entity instanceof HasCoverImage && $entity->cover()->exists()) {
392 $imageService = app()->make(ImageService::class);
393 $imageService->destroy($entity->cover()->first());