]> BookStack Code Mirror - bookstack/blob - app/Settings/MaintenanceController.php
Merge pull request #5917 from BookStackApp/copy_references
[bookstack] / app / Settings / MaintenanceController.php
1 <?php
2
3 namespace BookStack\Settings;
4
5 use BookStack\Activity\ActivityType;
6 use BookStack\App\AppVersion;
7 use BookStack\Entities\Tools\TrashCan;
8 use BookStack\Http\Controller;
9 use BookStack\Permissions\Permission;
10 use BookStack\References\ReferenceStore;
11 use BookStack\Uploads\ImageService;
12 use Illuminate\Http\Request;
13
14 class MaintenanceController extends Controller
15 {
16     /**
17      * Show the page for application maintenance.
18      */
19     public function index(TrashCan $trashCan)
20     {
21         $this->checkPermission(Permission::SettingsManage);
22         $this->setPageTitle(trans('settings.maint'));
23
24         // Recycle bin details
25         $recycleStats = $trashCan->getTrashedCounts();
26
27         return view('settings.maintenance', [
28             'version'      => AppVersion::get(),
29             'recycleStats' => $recycleStats,
30         ]);
31     }
32
33     /**
34      * Action to clean-up images in the system.
35      */
36     public function cleanupImages(Request $request, ImageService $imageService)
37     {
38         $this->checkPermission(Permission::SettingsManage);
39         $this->logActivity(ActivityType::MAINTENANCE_ACTION_RUN, 'cleanup-images');
40
41         $checkRevisions = !($request->get('ignore_revisions', 'false') === 'true');
42         $dryRun = !($request->has('confirm'));
43
44         $imagesToDelete = $imageService->deleteUnusedImages($checkRevisions, $dryRun);
45         $deleteCount = count($imagesToDelete);
46         if ($deleteCount === 0) {
47             $this->showWarningNotification(trans('settings.maint_image_cleanup_nothing_found'));
48
49             return redirect('/settings/maintenance')->withInput();
50         }
51
52         if ($dryRun) {
53             session()->flash('cleanup-images-warning', trans('settings.maint_image_cleanup_warning', ['count' => $deleteCount]));
54         } else {
55             $this->showSuccessNotification(trans('settings.maint_image_cleanup_success', ['count' => $deleteCount]));
56         }
57
58         return redirect('/settings/maintenance#image-cleanup')->withInput();
59     }
60
61     /**
62      * Action to send a test e-mail to the current user.
63      */
64     public function sendTestEmail()
65     {
66         $this->checkPermission(Permission::SettingsManage);
67         $this->logActivity(ActivityType::MAINTENANCE_ACTION_RUN, 'send-test-email');
68
69         try {
70             user()->notifyNow(new TestEmailNotification());
71             $this->showSuccessNotification(trans('settings.maint_send_test_email_success', ['address' => user()->email]));
72         } catch (\Exception $exception) {
73             $errorMessage = trans('errors.maintenance_test_email_failure') . "\n" . $exception->getMessage();
74             $this->showErrorNotification($errorMessage);
75         }
76
77         return redirect('/settings/maintenance#image-cleanup');
78     }
79
80     /**
81      * Action to regenerate the reference index in the system.
82      */
83     public function regenerateReferences(ReferenceStore $referenceStore)
84     {
85         $this->checkPermission(Permission::SettingsManage);
86         $this->logActivity(ActivityType::MAINTENANCE_ACTION_RUN, 'regenerate-references');
87
88         try {
89             $referenceStore->updateForAll();
90             $this->showSuccessNotification(trans('settings.maint_regen_references_success'));
91         } catch (\Exception $exception) {
92             $this->showErrorNotification($exception->getMessage());
93         }
94
95         return redirect('/settings/maintenance#regenerate-references');
96     }
97 }