Skip to content

Commit 4c72620

Browse files
authored
Merge pull request #4262 from BookStackApp/command_cleanup
Command cleanup & alignment
2 parents 3b31ac7 + 431aeef commit 4c72620

22 files changed

+267
-292
lines changed

app/Activity/Models/View.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,4 @@ public static function incrementFor(Viewable $viewable): int
5454

5555
return $view->views;
5656
}
57-
58-
/**
59-
* Clear all views from the system.
60-
*/
61-
public static function clearAll()
62-
{
63-
static::query()->truncate();
64-
}
6557
}

app/Console/Commands/CleanupImages.php renamed to app/Console/Commands/CleanupImagesCommand.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Illuminate\Console\Command;
77
use Symfony\Component\Console\Output\OutputInterface;
88

9-
class CleanupImages extends Command
9+
class CleanupImagesCommand extends Command
1010
{
1111
/**
1212
* The name and signature of the console command.
@@ -25,60 +25,49 @@ class CleanupImages extends Command
2525
*/
2626
protected $description = 'Cleanup images and drawings';
2727

28-
protected $imageService;
29-
30-
/**
31-
* Create a new command instance.
32-
*
33-
* @param \BookStack\Uploads\ImageService $imageService
34-
*/
35-
public function __construct(ImageService $imageService)
36-
{
37-
$this->imageService = $imageService;
38-
parent::__construct();
39-
}
40-
4128
/**
4229
* Execute the console command.
43-
*
44-
* @return mixed
4530
*/
46-
public function handle()
31+
public function handle(ImageService $imageService): int
4732
{
48-
$checkRevisions = $this->option('all') ? false : true;
49-
$dryRun = $this->option('force') ? false : true;
33+
$checkRevisions = !$this->option('all');
34+
$dryRun = !$this->option('force');
5035

5136
if (!$dryRun) {
52-
$proceed = $this->confirm("This operation is destructive and is not guaranteed to be fully accurate.\nEnsure you have a backup of your images.\nAre you sure you want to proceed?");
37+
$this->warn("This operation is destructive and is not guaranteed to be fully accurate.\nEnsure you have a backup of your images.\n");
38+
$proceed = $this->confirm("Are you sure you want to proceed?");
5339
if (!$proceed) {
54-
return;
40+
return 0;
5541
}
5642
}
5743

58-
$deleted = $this->imageService->deleteUnusedImages($checkRevisions, $dryRun);
44+
$deleted = $imageService->deleteUnusedImages($checkRevisions, $dryRun);
5945
$deleteCount = count($deleted);
6046

6147
if ($dryRun) {
62-
$this->comment('Dry run, No images have been deleted');
48+
$this->comment('Dry run, no images have been deleted');
6349
$this->comment($deleteCount . ' images found that would have been deleted');
6450
$this->showDeletedImages($deleted);
6551
$this->comment('Run with -f or --force to perform deletions');
6652

67-
return;
53+
return 0;
6854
}
6955

7056
$this->showDeletedImages($deleted);
7157
$this->comment($deleteCount . ' images deleted');
58+
return 0;
7259
}
7360

74-
protected function showDeletedImages($paths)
61+
protected function showDeletedImages($paths): void
7562
{
7663
if ($this->getOutput()->getVerbosity() <= OutputInterface::VERBOSITY_NORMAL) {
7764
return;
7865
}
66+
7967
if (count($paths) > 0) {
8068
$this->line('Images to delete:');
8169
}
70+
8271
foreach ($paths as $path) {
8372
$this->line($path);
8473
}

app/Console/Commands/ClearActivity.php renamed to app/Console/Commands/ClearActivityCommand.php

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use BookStack\Activity\Models\Activity;
66
use Illuminate\Console\Command;
77

8-
class ClearActivity extends Command
8+
class ClearActivityCommand extends Command
99
{
1010
/**
1111
* The name and signature of the console command.
@@ -21,27 +21,13 @@ class ClearActivity extends Command
2121
*/
2222
protected $description = 'Clear user activity from the system';
2323

24-
protected $activity;
25-
26-
/**
27-
* Create a new command instance.
28-
*
29-
* @param Activity $activity
30-
*/
31-
public function __construct(Activity $activity)
32-
{
33-
$this->activity = $activity;
34-
parent::__construct();
35-
}
36-
3724
/**
3825
* Execute the console command.
39-
*
40-
* @return mixed
4126
*/
42-
public function handle()
27+
public function handle(): int
4328
{
44-
$this->activity->newQuery()->truncate();
29+
Activity::query()->truncate();
4530
$this->comment('System activity cleared');
31+
return 0;
4632
}
4733
}

app/Console/Commands/ClearRevisions.php renamed to app/Console/Commands/ClearRevisionsCommand.php

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use BookStack\Entities\Models\PageRevision;
66
use Illuminate\Console\Command;
77

8-
class ClearRevisions extends Command
8+
class ClearRevisionsCommand extends Command
99
{
1010
/**
1111
* The name and signature of the console command.
@@ -23,28 +23,14 @@ class ClearRevisions extends Command
2323
*/
2424
protected $description = 'Clear page revisions';
2525

26-
protected $pageRevision;
27-
28-
/**
29-
* Create a new command instance.
30-
*
31-
* @param PageRevision $pageRevision
32-
*/
33-
public function __construct(PageRevision $pageRevision)
34-
{
35-
$this->pageRevision = $pageRevision;
36-
parent::__construct();
37-
}
38-
3926
/**
4027
* Execute the console command.
41-
*
42-
* @return mixed
4328
*/
44-
public function handle()
29+
public function handle(): int
4530
{
4631
$deleteTypes = $this->option('all') ? ['version', 'update_draft'] : ['version'];
47-
$this->pageRevision->newQuery()->whereIn('type', $deleteTypes)->delete();
32+
PageRevision::query()->whereIn('type', $deleteTypes)->delete();
4833
$this->comment('Revisions deleted');
34+
return 0;
4935
}
5036
}

app/Console/Commands/ClearViews.php renamed to app/Console/Commands/ClearViewsCommand.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use BookStack\Activity\Models\View;
66
use Illuminate\Console\Command;
77

8-
class ClearViews extends Command
8+
class ClearViewsCommand extends Command
99
{
1010
/**
1111
* The name and signature of the console command.
@@ -21,22 +21,13 @@ class ClearViews extends Command
2121
*/
2222
protected $description = 'Clear all view-counts for all entities';
2323

24-
/**
25-
* Create a new command instance.
26-
*/
27-
public function __construct()
28-
{
29-
parent::__construct();
30-
}
31-
3224
/**
3325
* Execute the console command.
34-
*
35-
* @return mixed
3626
*/
37-
public function handle()
27+
public function handle(): int
3828
{
39-
View::clearAll();
29+
View::query()->truncate();
4030
$this->comment('Views cleared');
31+
return 0;
4132
}
4233
}

app/Console/Commands/CopyShelfPermissions.php renamed to app/Console/Commands/CopyShelfPermissionsCommand.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use BookStack\Entities\Tools\PermissionsUpdater;
77
use Illuminate\Console\Command;
88

9-
class CopyShelfPermissions extends Command
9+
class CopyShelfPermissionsCommand extends Command
1010
{
1111
/**
1212
* The name and signature of the console command.
@@ -25,25 +25,10 @@ class CopyShelfPermissions extends Command
2525
*/
2626
protected $description = 'Copy shelf permissions to all child books';
2727

28-
protected PermissionsUpdater $permissionsUpdater;
29-
30-
/**
31-
* Create a new command instance.
32-
*
33-
* @return void
34-
*/
35-
public function __construct(PermissionsUpdater $permissionsUpdater)
36-
{
37-
$this->permissionsUpdater = $permissionsUpdater;
38-
parent::__construct();
39-
}
40-
4128
/**
4229
* Execute the console command.
43-
*
44-
* @return mixed
4530
*/
46-
public function handle()
31+
public function handle(PermissionsUpdater $permissionsUpdater): int
4732
{
4833
$shelfSlug = $this->option('slug');
4934
$cascadeAll = $this->option('all');
@@ -52,7 +37,7 @@ public function handle()
5237
if (!$cascadeAll && !$shelfSlug) {
5338
$this->error('Either a --slug or --all option must be provided.');
5439

55-
return;
40+
return 1;
5641
}
5742

5843
if ($cascadeAll) {
@@ -63,7 +48,7 @@ public function handle()
6348
);
6449

6550
if (!$continue && !$this->hasOption('no-interaction')) {
66-
return;
51+
return 0;
6752
}
6853

6954
$shelves = Bookshelf::query()->get(['id']);
@@ -77,10 +62,11 @@ public function handle()
7762
}
7863

7964
foreach ($shelves as $shelf) {
80-
$this->permissionsUpdater->updateBookPermissionsFromShelf($shelf, false);
65+
$permissionsUpdater->updateBookPermissionsFromShelf($shelf, false);
8166
$this->info('Copied permissions for shelf [' . $shelf->id . ']');
8267
}
8368

8469
$this->info('Permissions copied for ' . $shelves->count() . ' shelves.');
70+
return 0;
8571
}
8672
}

app/Console/Commands/CreateAdmin.php renamed to app/Console/Commands/CreateAdminCommand.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22

33
namespace BookStack\Console\Commands;
44

5-
use BookStack\Exceptions\NotFoundException;
65
use BookStack\Users\Models\Role;
76
use BookStack\Users\UserRepo;
87
use Illuminate\Console\Command;
98
use Illuminate\Support\Facades\Validator;
109
use Illuminate\Support\Str;
1110
use Illuminate\Validation\Rules\Password;
1211
use Illuminate\Validation\Rules\Unique;
13-
use Symfony\Component\Console\Command\Command as SymfonyCommand;
1412

15-
class CreateAdmin extends Command
13+
class CreateAdminCommand extends Command
1614
{
1715
/**
1816
* The name and signature of the console command.
@@ -32,25 +30,10 @@ class CreateAdmin extends Command
3230
*/
3331
protected $description = 'Add a new admin user to the system';
3432

35-
protected $userRepo;
36-
37-
/**
38-
* Create a new command instance.
39-
*/
40-
public function __construct(UserRepo $userRepo)
41-
{
42-
$this->userRepo = $userRepo;
43-
parent::__construct();
44-
}
45-
4633
/**
4734
* Execute the console command.
48-
*
49-
* @throws NotFoundException
50-
*
51-
* @return mixed
5235
*/
53-
public function handle()
36+
public function handle(UserRepo $userRepo): int
5437
{
5538
$details = $this->snakeCaseOptions();
5639

@@ -82,17 +65,17 @@ public function handle()
8265
$this->error($error);
8366
}
8467

85-
return SymfonyCommand::FAILURE;
68+
return 1;
8669
}
8770

88-
$user = $this->userRepo->createWithoutActivity($validator->validated());
71+
$user = $userRepo->createWithoutActivity($validator->validated());
8972
$user->attachRole(Role::getSystemRole('admin'));
9073
$user->email_confirmed = true;
9174
$user->save();
9275

9376
$this->info("Admin account with email \"{$user->email}\" successfully created!");
9477

95-
return SymfonyCommand::SUCCESS;
78+
return 0;
9679
}
9780

9881
protected function snakeCaseOptions(): array

app/Console/Commands/DeleteUsers.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)