1 <?php namespace BookStack\Entities\Tools;
3 use BookStack\Entities\Models\Book;
4 use BookStack\Entities\Models\Bookshelf;
5 use BookStack\Entities\Models\Chapter;
6 use BookStack\Entities\Models\Deletion;
7 use BookStack\Entities\Models\Entity;
8 use BookStack\Entities\EntityProvider;
9 use BookStack\Entities\Models\HasCoverImage;
10 use BookStack\Entities\Models\Page;
11 use BookStack\Exceptions\NotifyException;
12 use BookStack\Facades\Activity;
13 use BookStack\Uploads\AttachmentService;
14 use BookStack\Uploads\ImageService;
16 use Illuminate\Support\Carbon;
22 * Send a shelf to the recycle bin.
24 public function softDestroyShelf(Bookshelf $shelf)
26 Deletion::createForEntity($shelf);
31 * Send a book to the recycle bin.
34 public function softDestroyBook(Book $book)
36 Deletion::createForEntity($book);
38 foreach ($book->pages as $page) {
39 $this->softDestroyPage($page, false);
42 foreach ($book->chapters as $chapter) {
43 $this->softDestroyChapter($chapter, false);
50 * Send a chapter to the recycle bin.
53 public function softDestroyChapter(Chapter $chapter, bool $recordDelete = true)
56 Deletion::createForEntity($chapter);
59 if (count($chapter->pages) > 0) {
60 foreach ($chapter->pages as $page) {
61 $this->softDestroyPage($page, false);
69 * Send a page to the recycle bin.
72 public function softDestroyPage(Page $page, bool $recordDelete = true)
75 Deletion::createForEntity($page);
78 // Check if set as custom homepage & remove setting if not used or throw error if active
79 $customHome = setting('app-homepage', '0:');
80 if (intval($page->id) === intval(explode(':', $customHome)[0])) {
81 if (setting('app-homepage-type') === 'page') {
82 throw new NotifyException(trans('errors.page_custom_home_deletion'), $page->getUrl());
84 setting()->remove('app-homepage');
91 * Remove a bookshelf from the system.
94 protected function destroyShelf(Bookshelf $shelf): int
96 $this->destroyCommonRelations($shelf);
97 $shelf->forceDelete();
102 * Remove a book from the system.
103 * Destroys any child chapters and pages.
106 protected function destroyBook(Book $book): int
109 $pages = $book->pages()->withTrashed()->get();
110 foreach ($pages as $page) {
111 $this->destroyPage($page);
115 $chapters = $book->chapters()->withTrashed()->get();
116 foreach ($chapters as $chapter) {
117 $this->destroyChapter($chapter);
121 $this->destroyCommonRelations($book);
122 $book->forceDelete();
127 * Remove a chapter from the system.
128 * Destroys all pages within.
131 protected function destroyChapter(Chapter $chapter): int
134 $pages = $chapter->pages()->withTrashed()->get();
136 foreach ($pages as $page) {
137 $this->destroyPage($page);
142 $this->destroyCommonRelations($chapter);
143 $chapter->forceDelete();
148 * Remove a page from the system.
151 protected function destroyPage(Page $page): int
153 $this->destroyCommonRelations($page);
155 // Delete Attached Files
156 $attachmentService = app(AttachmentService::class);
157 foreach ($page->attachments as $attachment) {
158 $attachmentService->deleteFile($attachment);
161 $page->forceDelete();
166 * Get the total counts of those that have been trashed
167 * but not yet fully deleted (In recycle bin).
169 public function getTrashedCounts(): array
173 /** @var Entity $instance */
174 foreach ((new EntityProvider)->all() as $key => $instance) {
175 $counts[$key] = $instance->newQuery()->onlyTrashed()->count();
182 * Destroy all items that have pending deletions.
185 public function empty(): int
187 $deletions = Deletion::all();
189 foreach ($deletions as $deletion) {
190 $deleteCount += $this->destroyFromDeletion($deletion);
196 * Destroy an element from the given deletion model.
199 public function destroyFromDeletion(Deletion $deletion): int
201 // We directly load the deletable element here just to ensure it still
202 // exists in the event it has already been destroyed during this request.
203 $entity = $deletion->deletable()->first();
206 $count = $this->destroyEntity($deletion->deletable);
213 * Restore the content within the given deletion.
216 public function restoreFromDeletion(Deletion $deletion): int
218 $shouldRestore = true;
220 $parent = $deletion->deletable->getParent();
222 if ($parent && $parent->trashed()) {
223 $shouldRestore = false;
226 if ($shouldRestore) {
227 $restoreCount = $this->restoreEntity($deletion->deletable);
231 return $restoreCount;
235 * Automatically clear old content from the recycle bin
236 * depending on the configured lifetime.
237 * Returns the total number of deleted elements.
240 public function autoClearOld(): int
242 $lifetime = intval(config('app.recycle_bin_lifetime'));
247 $clearBeforeDate = Carbon::now()->addSeconds(10)->subDays($lifetime);
250 $deletionsToRemove = Deletion::query()->where('created_at', '<', $clearBeforeDate)->get();
251 foreach ($deletionsToRemove as $deletion) {
252 $deleteCount += $this->destroyFromDeletion($deletion);
259 * Restore an entity so it is essentially un-deleted.
260 * Deletions on restored child elements will be removed during this restoration.
262 protected function restoreEntity(Entity $entity): int
267 $restoreAction = function ($entity) use (&$count) {
268 if ($entity->deletions_count > 0) {
269 $entity->deletions()->delete();
276 if ($entity->isA('chapter') || $entity->isA('book')) {
277 $entity->pages()->withTrashed()->withCount('deletions')->get()->each($restoreAction);
280 if ($entity->isA('book')) {
281 $entity->chapters()->withTrashed()->withCount('deletions')->get()->each($restoreAction);
288 * Destroy the given entity.
290 protected function destroyEntity(Entity $entity): int
292 if ($entity->isA('page')) {
293 return $this->destroyPage($entity);
295 if ($entity->isA('chapter')) {
296 return $this->destroyChapter($entity);
298 if ($entity->isA('book')) {
299 return $this->destroyBook($entity);
301 if ($entity->isA('shelf')) {
302 return $this->destroyShelf($entity);
307 * Update entity relations to remove or update outstanding connections.
309 protected function destroyCommonRelations(Entity $entity)
311 Activity::removeEntity($entity);
312 $entity->views()->delete();
313 $entity->permissions()->delete();
314 $entity->tags()->delete();
315 $entity->comments()->delete();
316 $entity->jointPermissions()->delete();
317 $entity->searchTerms()->delete();
318 $entity->deletions()->delete();
320 if ($entity instanceof HasCoverImage && $entity->cover) {
321 $imageService = app()->make(ImageService::class);
322 $imageService->destroy($entity->cover);