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\Entities\Queries\EntityQueries;
14 use BookStack\Exceptions\NotifyException;
15 use BookStack\Facades\Activity;
16 use BookStack\Uploads\AttachmentService;
17 use BookStack\Uploads\ImageService;
19 use Illuminate\Database\Eloquent\Builder;
20 use Illuminate\Support\Carbon;
24 public function __construct(
25 protected EntityQueries $queries,
30 * Send a shelf to the recycle bin.
32 * @throws NotifyException
34 public function softDestroyShelf(Bookshelf $shelf)
36 $this->ensureDeletable($shelf);
37 Deletion::createForEntity($shelf);
42 * Send a book to the recycle bin.
46 public function softDestroyBook(Book $book)
48 $this->ensureDeletable($book);
49 Deletion::createForEntity($book);
51 foreach ($book->pages as $page) {
52 $this->softDestroyPage($page, false);
55 foreach ($book->chapters as $chapter) {
56 $this->softDestroyChapter($chapter, false);
63 * Send a chapter to the recycle bin.
67 public function softDestroyChapter(Chapter $chapter, bool $recordDelete = true)
70 $this->ensureDeletable($chapter);
71 Deletion::createForEntity($chapter);
74 if (count($chapter->pages) > 0) {
75 foreach ($chapter->pages as $page) {
76 $this->softDestroyPage($page, false);
84 * Send a page to the recycle bin.
88 public function softDestroyPage(Page $page, bool $recordDelete = true)
91 $this->ensureDeletable($page);
92 Deletion::createForEntity($page);
99 * Ensure the given entity is deletable.
100 * Is not for permissions, but logical conditions within the application.
101 * Will throw if not deletable.
103 * @throws NotifyException
105 protected function ensureDeletable(Entity $entity): void
107 $customHomeId = intval(explode(':', setting('app-homepage', '0:'))[0]);
108 $customHomeActive = setting('app-homepage-type') === 'page';
109 $removeCustomHome = false;
111 // Check custom homepage usage for pages
112 if ($entity instanceof Page && $entity->id === $customHomeId) {
113 if ($customHomeActive) {
114 throw new NotifyException(trans('errors.page_custom_home_deletion'), $entity->getUrl());
116 $removeCustomHome = true;
119 // Check custom homepage usage within chapters or books
120 if ($entity instanceof Chapter || $entity instanceof Book) {
121 if ($entity->pages()->where('id', '=', $customHomeId)->exists()) {
122 if ($customHomeActive) {
123 throw new NotifyException(trans('errors.page_custom_home_deletion'), $entity->getUrl());
125 $removeCustomHome = true;
129 if ($removeCustomHome) {
130 setting()->remove('app-homepage');
135 * Remove a bookshelf from the system.
139 protected function destroyShelf(Bookshelf $shelf): int
141 $this->destroyCommonRelations($shelf);
142 $shelf->forceDelete();
148 * Remove a book from the system.
149 * Destroys any child chapters and pages.
153 protected function destroyBook(Book $book): int
156 $pages = $book->pages()->withTrashed()->get();
157 foreach ($pages as $page) {
158 $this->destroyPage($page);
162 $chapters = $book->chapters()->withTrashed()->get();
163 foreach ($chapters as $chapter) {
164 $this->destroyChapter($chapter);
168 $this->destroyCommonRelations($book);
169 $book->forceDelete();
175 * Remove a chapter from the system.
176 * Destroys all pages within.
180 protected function destroyChapter(Chapter $chapter): int
183 $pages = $chapter->pages()->withTrashed()->get();
184 foreach ($pages as $page) {
185 $this->destroyPage($page);
189 $this->destroyCommonRelations($chapter);
190 $chapter->forceDelete();
196 * Remove a page from the system.
200 protected function destroyPage(Page $page): int
202 $this->destroyCommonRelations($page);
203 $page->allRevisions()->delete();
205 // Delete Attached Files
206 $attachmentService = app()->make(AttachmentService::class);
207 foreach ($page->attachments as $attachment) {
208 $attachmentService->deleteFile($attachment);
211 // Remove book template usages
212 $this->queries->books->start()
213 ->where('default_template_id', '=', $page->id)
214 ->update(['default_template_id' => null]);
216 // Remove chapter template usages
217 $this->queries->chapters->start()
218 ->where('default_template_id', '=', $page->id)
219 ->update(['default_template_id' => null]);
221 $page->forceDelete();
227 * Get the total counts of those that have been trashed
228 * but not yet fully deleted (In recycle bin).
230 public function getTrashedCounts(): array
234 foreach ((new EntityProvider())->all() as $key => $instance) {
235 /** @var Builder<Entity> $query */
236 $query = $instance->newQuery();
237 $counts[$key] = $query->onlyTrashed()->count();
244 * Destroy all items that have pending deletions.
248 public function empty(): int
250 $deletions = Deletion::all();
252 foreach ($deletions as $deletion) {
253 $deleteCount += $this->destroyFromDeletion($deletion);
260 * Destroy an element from the given deletion model.
264 public function destroyFromDeletion(Deletion $deletion): int
266 // We directly load the deletable element here just to ensure it still
267 // exists in the event it has already been destroyed during this request.
268 $entity = $deletion->deletable()->first();
271 $count = $this->destroyEntity($deletion->deletable);
279 * Restore the content within the given deletion.
283 public function restoreFromDeletion(Deletion $deletion): int
285 $shouldRestore = true;
288 if ($deletion->deletable instanceof Entity) {
289 $parent = $deletion->deletable->getParent();
290 if ($parent && $parent->trashed()) {
291 $shouldRestore = false;
295 if ($deletion->deletable instanceof Entity && $shouldRestore) {
296 $restoreCount = $this->restoreEntity($deletion->deletable);
301 return $restoreCount;
305 * Automatically clear old content from the recycle bin
306 * depending on the configured lifetime.
307 * Returns the total number of deleted elements.
311 public function autoClearOld(): int
313 $lifetime = intval(config('app.recycle_bin_lifetime'));
318 $clearBeforeDate = Carbon::now()->addSeconds(10)->subDays($lifetime);
321 $deletionsToRemove = Deletion::query()->where('created_at', '<', $clearBeforeDate)->get();
322 foreach ($deletionsToRemove as $deletion) {
323 $deleteCount += $this->destroyFromDeletion($deletion);
330 * Restore an entity so it is essentially un-deleted.
331 * Deletions on restored child elements will be removed during this restoration.
333 protected function restoreEntity(Entity $entity): int
338 $restoreAction = function ($entity) use (&$count) {
339 if ($entity->deletions_count > 0) {
340 $entity->deletions()->delete();
347 if ($entity instanceof Chapter || $entity instanceof Book) {
348 $entity->pages()->withTrashed()->withCount('deletions')->get()->each($restoreAction);
351 if ($entity instanceof Book) {
352 $entity->chapters()->withTrashed()->withCount('deletions')->get()->each($restoreAction);
359 * Destroy the given entity.
363 public function destroyEntity(Entity $entity): int
365 if ($entity instanceof Page) {
366 return $this->destroyPage($entity);
368 if ($entity instanceof Chapter) {
369 return $this->destroyChapter($entity);
371 if ($entity instanceof Book) {
372 return $this->destroyBook($entity);
374 if ($entity instanceof Bookshelf) {
375 return $this->destroyShelf($entity);
382 * Update entity relations to remove or update outstanding connections.
384 protected function destroyCommonRelations(Entity $entity)
386 Activity::removeEntity($entity);
387 $entity->views()->delete();
388 $entity->permissions()->delete();
389 $entity->tags()->delete();
390 $entity->comments()->delete();
391 $entity->jointPermissions()->delete();
392 $entity->searchTerms()->delete();
393 $entity->deletions()->delete();
394 $entity->favourites()->delete();
395 $entity->watches()->delete();
396 $entity->referencesTo()->delete();
397 $entity->referencesFrom()->delete();
399 if ($entity instanceof HasCoverImage && $entity->cover()->exists()) {
400 $imageService = app()->make(ImageService::class);
401 $imageService->destroy($entity->cover()->first());