3 namespace BookStack\Http\Controllers\Api;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\BookChild;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Deletion;
9 use BookStack\Entities\Repos\DeletionRepo;
11 use Illuminate\Database\Eloquent\Builder;
13 class RecycleBinApiController extends ApiController
15 public function __construct()
17 $this->middleware(function ($request, $next) {
18 $this->checkPermission('settings-manage');
19 $this->checkPermission('restrictions-manage-all');
21 return $next($request);
26 * Get a top-level listing of the items in the recycle bin.
27 * The "deletable" property will reflect the main item deleted.
28 * For books and chapters, counts of child pages/chapters will
29 * be loaded within this "deletable" data.
30 * For chapters & pages, the parent item will be loaded within this "deletable" data.
31 * Requires permission to manage both system settings and permissions.
33 public function list()
35 return $this->apiListingResponse(Deletion::query()->with('deletable'), [
42 ], [Closure::fromCallable([$this, 'listFormatter'])]);
46 * Restore a single deletion from the recycle bin.
47 * Requires permission to manage both system settings and permissions.
49 public function restore(DeletionRepo $deletionRepo, string $deletionId)
51 $restoreCount = $deletionRepo->restore(intval($deletionId));
53 return response()->json(['restore_count' => $restoreCount]);
57 * Remove a single deletion from the recycle bin.
58 * Use this endpoint carefully as it will entirely remove the underlying deleted items from the system.
59 * Requires permission to manage both system settings and permissions.
61 public function destroy(DeletionRepo $deletionRepo, string $deletionId)
63 $deleteCount = $deletionRepo->destroy(intval($deletionId));
65 return response()->json(['delete_count' => $deleteCount]);
69 * Load some related details for the deletion listing.
71 protected function listFormatter(Deletion $deletion)
73 $deletable = $deletion->deletable;
74 $withTrashedQuery = fn (Builder $query) => $query->withTrashed();
76 if ($deletable instanceof BookChild) {
77 $parent = $deletable->getParent();
78 $parent->setAttribute('type', $parent->getType());
79 $deletable->setRelation('parent', $parent);
82 if ($deletable instanceof Book || $deletable instanceof Chapter) {
83 $countsToLoad = ['pages' => $withTrashedQuery];
84 if ($deletable instanceof Book) {
85 $countsToLoad['chapters'] = $withTrashedQuery;
87 $deletable->loadCount($countsToLoad);