use BookStack\Uploads\AttachmentService;
use BookStack\Uploads\ImageService;
use Exception;
+use Illuminate\Support\Carbon;
class TrashCan
{
* Remove a bookshelf from the system.
* @throws Exception
*/
- public function destroyShelf(Bookshelf $shelf): int
+ protected function destroyShelf(Bookshelf $shelf): int
{
$this->destroyCommonRelations($shelf);
$shelf->forceDelete();
* Destroys any child chapters and pages.
* @throws Exception
*/
- public function destroyBook(Book $book): int
+ protected function destroyBook(Book $book): int
{
$count = 0;
$pages = $book->pages()->withTrashed()->get();
* Destroys all pages within.
* @throws Exception
*/
- public function destroyChapter(Chapter $chapter): int
+ protected function destroyChapter(Chapter $chapter): int
{
$count = 0;
$pages = $chapter->pages()->withTrashed()->get();
* Remove a page from the system.
* @throws Exception
*/
- public function destroyPage(Page $page): int
+ protected function destroyPage(Page $page): int
{
$this->destroyCommonRelations($page);
* Destroy all items that have pending deletions.
* @throws Exception
*/
- public function destroyFromAllDeletions(): int
+ public function empty(): int
{
$deletions = Deletion::all();
$deleteCount = 0;
return $restoreCount;
}
+ /**
+ * Automatically clear old content from the recycle bin
+ * depending on the configured lifetime.
+ * Returns the total number of deleted elements.
+ * @throws Exception
+ */
+ public function autoClearOld(): int
+ {
+ $lifetime = intval(config('app.recycle_bin_lifetime'));
+ if ($lifetime < 0) {
+ return 0;
+ }
+
+ $clearBeforeDate = Carbon::now()->addSeconds(10)->subDays($lifetime);
+ $deleteCount = 0;
+
+ $deletionsToRemove = Deletion::query()->where('created_at', '<', $clearBeforeDate)->get();
+ foreach ($deletionsToRemove as $deletion) {
+ $deleteCount += $this->destroyFromDeletion($deletion);
+ }
+
+ return $deleteCount;
+ }
+
/**
* Restore an entity so it is essentially un-deleted.
* Deletions on restored child elements will be removed during this restoration.
$count = 1;
$entity->restore();
- if ($entity->isA('chapter') || $entity->isA('book')) {
- foreach ($entity->pages()->withTrashed()->withCount('deletions')->get() as $page) {
- if ($page->deletions_count > 0) {
- $page->deletions()->delete();
- }
-
- $page->restore();
- $count++;
+ $restoreAction = function ($entity) use (&$count) {
+ if ($entity->deletions_count > 0) {
+ $entity->deletions()->delete();
}
+
+ $entity->restore();
+ $count++;
+ };
+
+ if ($entity->isA('chapter') || $entity->isA('book')) {
+ $entity->pages()->withTrashed()->withCount('deletions')->get()->each($restoreAction);
}
if ($entity->isA('book')) {
- foreach ($entity->chapters()->withTrashed()->withCount('deletions')->get() as $chapter) {
- if ($chapter->deletions_count === 0) {
- $chapter->deletions()->delete();
- }
-
- $chapter->restore();
- $count++;
- }
+ $entity->chapters()->withTrashed()->withCount('deletions')->get()->each($restoreAction);
}
return $count;