- Aligned usage of injecting through handler.
- Aligned handler return type.
- Aligned argument and arg desc format.
- Aligned lack of constructor.
return $view->views;
}
-
- /**
- * Clear all views from the system.
- */
- public static function clearAll()
- {
- static::query()->truncate();
- }
}
*/
protected $description = 'Cleanup images and drawings';
- protected $imageService;
-
- /**
- * Create a new command instance.
- *
- * @param \BookStack\Uploads\ImageService $imageService
- */
- public function __construct(ImageService $imageService)
- {
- $this->imageService = $imageService;
- parent::__construct();
- }
-
/**
* Execute the console command.
- *
- * @return mixed
*/
- public function handle()
+ public function handle(ImageService $imageService): int
{
- $checkRevisions = $this->option('all') ? false : true;
- $dryRun = $this->option('force') ? false : true;
+ $checkRevisions = !$this->option('all');
+ $dryRun = !$this->option('force');
if (!$dryRun) {
$this->warn("This operation is destructive and is not guaranteed to be fully accurate.\nEnsure you have a backup of your images.\n");
$proceed = $this->confirm("Are you sure you want to proceed?");
if (!$proceed) {
- return;
+ return 0;
}
}
- $deleted = $this->imageService->deleteUnusedImages($checkRevisions, $dryRun);
+ $deleted = $imageService->deleteUnusedImages($checkRevisions, $dryRun);
$deleteCount = count($deleted);
if ($dryRun) {
$this->showDeletedImages($deleted);
$this->comment('Run with -f or --force to perform deletions');
- return;
+ return 0;
}
$this->showDeletedImages($deleted);
$this->comment($deleteCount . ' images deleted');
+ return 0;
}
- protected function showDeletedImages($paths)
+ protected function showDeletedImages($paths): void
{
if ($this->getOutput()->getVerbosity() <= OutputInterface::VERBOSITY_NORMAL) {
return;
}
+
if (count($paths) > 0) {
$this->line('Images to delete:');
}
+
foreach ($paths as $path) {
$this->line($path);
}
*/
protected $description = 'Clear user activity from the system';
- protected $activity;
-
- /**
- * Create a new command instance.
- *
- * @param Activity $activity
- */
- public function __construct(Activity $activity)
- {
- $this->activity = $activity;
- parent::__construct();
- }
-
/**
* Execute the console command.
- *
- * @return mixed
*/
- public function handle()
+ public function handle(): int
{
- $this->activity->newQuery()->truncate();
+ Activity::query()->truncate();
$this->comment('System activity cleared');
+ return 0;
}
}
*/
protected $description = 'Clear page revisions';
- protected $pageRevision;
-
- /**
- * Create a new command instance.
- *
- * @param PageRevision $pageRevision
- */
- public function __construct(PageRevision $pageRevision)
- {
- $this->pageRevision = $pageRevision;
- parent::__construct();
- }
-
/**
* Execute the console command.
- *
- * @return mixed
*/
- public function handle()
+ public function handle(): int
{
$deleteTypes = $this->option('all') ? ['version', 'update_draft'] : ['version'];
- $this->pageRevision->newQuery()->whereIn('type', $deleteTypes)->delete();
+ PageRevision::query()->whereIn('type', $deleteTypes)->delete();
$this->comment('Revisions deleted');
+ return 0;
}
}
*/
protected $description = 'Clear all view-counts for all entities';
- /**
- * Create a new command instance.
- */
- public function __construct()
- {
- parent::__construct();
- }
-
/**
* Execute the console command.
- *
- * @return mixed
*/
- public function handle()
+ public function handle(): int
{
- View::clearAll();
+ View::query()->truncate();
$this->comment('Views cleared');
+ return 0;
}
}
*/
protected $description = 'Copy shelf permissions to all child books';
- protected PermissionsUpdater $permissionsUpdater;
-
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct(PermissionsUpdater $permissionsUpdater)
- {
- $this->permissionsUpdater = $permissionsUpdater;
- parent::__construct();
- }
-
/**
* Execute the console command.
- *
- * @return mixed
*/
- public function handle()
+ public function handle(PermissionsUpdater $permissionsUpdater): int
{
$shelfSlug = $this->option('slug');
$cascadeAll = $this->option('all');
if (!$cascadeAll && !$shelfSlug) {
$this->error('Either a --slug or --all option must be provided.');
- return;
+ return 1;
}
if ($cascadeAll) {
);
if (!$continue && !$this->hasOption('no-interaction')) {
- return;
+ return 0;
}
$shelves = Bookshelf::query()->get(['id']);
}
foreach ($shelves as $shelf) {
- $this->permissionsUpdater->updateBookPermissionsFromShelf($shelf, false);
+ $permissionsUpdater->updateBookPermissionsFromShelf($shelf, false);
$this->info('Copied permissions for shelf [' . $shelf->id . ']');
}
$this->info('Permissions copied for ' . $shelves->count() . ' shelves.');
+ return 0;
}
}
namespace BookStack\Console\Commands;
-use BookStack\Exceptions\NotFoundException;
use BookStack\Users\Models\Role;
use BookStack\Users\UserRepo;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Illuminate\Validation\Rules\Password;
use Illuminate\Validation\Rules\Unique;
-use Symfony\Component\Console\Command\Command as SymfonyCommand;
class CreateAdmin extends Command
{
*/
protected $description = 'Add a new admin user to the system';
- protected $userRepo;
-
- /**
- * Create a new command instance.
- */
- public function __construct(UserRepo $userRepo)
- {
- $this->userRepo = $userRepo;
- parent::__construct();
- }
-
/**
* Execute the console command.
- *
- * @throws NotFoundException
- *
- * @return mixed
*/
- public function handle()
+ public function handle(UserRepo $userRepo): int
{
$details = $this->snakeCaseOptions();
$this->error($error);
}
- return SymfonyCommand::FAILURE;
+ return 1;
}
- $user = $this->userRepo->createWithoutActivity($validator->validated());
+ $user = $userRepo->createWithoutActivity($validator->validated());
$user->attachRole(Role::getSystemRole('admin'));
$user->email_confirmed = true;
$user->save();
$this->info("Admin account with email \"{$user->email}\" successfully created!");
- return SymfonyCommand::SUCCESS;
+ return 0;
}
protected function snakeCaseOptions(): array
*/
protected $signature = 'bookstack:delete-users';
- protected $userRepo;
-
/**
* The console command description.
*
*/
protected $description = 'Delete users that are not "admin" or system users';
- public function __construct(UserRepo $userRepo)
+ /**
+ * Execute the console command.
+ */
+ public function handle(UserRepo $userRepo): int
{
- $this->userRepo = $userRepo;
- parent::__construct();
- }
+ $this->warn('This will delete all users from the system that are not "admin" or system users.');
+ $confirm = $this->confirm('Are you sure you want to continue?');
- public function handle()
- {
- $confirm = $this->ask('This will delete all users from the system that are not "admin" or system users. Are you sure you want to continue? (Type "yes" to continue)');
+ if (!$confirm) {
+ return 0;
+ }
+
+ $totalUsers = User::query()->count();
$numDeleted = 0;
- if (strtolower(trim($confirm)) === 'yes') {
- $totalUsers = User::query()->count();
- $users = User::query()->whereNull('system_name')->with('roles')->get();
- foreach ($users as $user) {
- if ($user->hasSystemRole('admin')) {
- // don't delete users with "admin" role
- continue;
- }
- $this->userRepo->destroy($user);
- $numDeleted++;
+ $users = User::query()->whereNull('system_name')->with('roles')->get();
+
+ foreach ($users as $user) {
+ if ($user->hasSystemRole('admin')) {
+ // don't delete users with "admin" role
+ continue;
}
- $this->info("Deleted $numDeleted of $totalUsers total users.");
- } else {
- $this->info('Exiting...');
+ $userRepo->destroy($user);
+ $numDeleted++;
}
+
+ $this->info("Deleted $numDeleted of $totalUsers total users.");
+ return 0;
}
}
*
* @var string
*/
- protected $signature = 'bookstack:regenerate-comment-content {--database= : The database connection to use.}';
+ protected $signature = 'bookstack:regenerate-comment-content
+ {--database= : The database connection to use}';
/**
* The console command description.
*/
protected $description = 'Regenerate the stored HTML of all comments';
- /**
- * @var CommentRepo
- */
- protected $commentRepo;
-
- /**
- * Create a new command instance.
- */
- public function __construct(CommentRepo $commentRepo)
- {
- $this->commentRepo = $commentRepo;
- parent::__construct();
- }
-
/**
* Execute the console command.
- *
- * @return mixed
*/
- public function handle()
+ public function handle(CommentRepo $commentRepo): int
{
$connection = DB::getDefaultConnection();
if ($this->option('database') !== null) {
DB::setDefaultConnection($this->option('database'));
}
- Comment::query()->chunk(100, function ($comments) {
+ Comment::query()->chunk(100, function ($comments) use ($commentRepo) {
foreach ($comments as $comment) {
- $comment->html = $this->commentRepo->commentToHtml($comment->text);
+ $comment->html = $commentRepo->commentToHtml($comment->text);
$comment->save();
}
});
*
* @var string
*/
- protected $signature = 'bookstack:regenerate-permissions {--database= : The database connection to use.}';
+ protected $signature = 'bookstack:regenerate-permissions
+ {--database= : The database connection to use}';
/**
* The console command description.
*/
protected $description = 'Regenerate all system permissions';
- protected JointPermissionBuilder $permissionBuilder;
-
- /**
- * Create a new command instance.
- */
- public function __construct(JointPermissionBuilder $permissionBuilder)
- {
- $this->permissionBuilder = $permissionBuilder;
- parent::__construct();
- }
-
/**
* Execute the console command.
- *
- * @return mixed
*/
- public function handle()
+ public function handle(JointPermissionBuilder $permissionBuilder): int
{
$connection = DB::getDefaultConnection();
DB::setDefaultConnection($this->option('database'));
}
- $this->permissionBuilder->rebuildForAll();
+ $permissionBuilder->rebuildForAll();
DB::setDefaultConnection($connection);
$this->comment('Permissions regenerated');
*
* @var string
*/
- protected $signature = 'bookstack:regenerate-references {--database= : The database connection to use.}';
+ protected $signature = 'bookstack:regenerate-references
+ {--database= : The database connection to use}';
/**
* The console command description.
*/
protected $description = 'Regenerate all the cross-item model reference index';
- protected ReferenceStore $references;
-
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct(ReferenceStore $references)
- {
- $this->references = $references;
- parent::__construct();
- }
-
/**
* Execute the console command.
- *
- * @return int
*/
- public function handle()
+ public function handle(ReferenceStore $references): int
{
$connection = DB::getDefaultConnection();
DB::setDefaultConnection($this->option('database'));
}
- $this->references->updateForAllPages();
+ $references->updateForAllPages();
DB::setDefaultConnection($connection);
*
* @var string
*/
- protected $signature = 'bookstack:regenerate-search {--database= : The database connection to use.}';
+ protected $signature = 'bookstack:regenerate-search
+ {--database= : The database connection to use}';
/**
* The console command description.
*/
protected $description = 'Re-index all content for searching';
- /**
- * @var SearchIndex
- */
- protected $searchIndex;
-
- /**
- * Create a new command instance.
- */
- public function __construct(SearchIndex $searchIndex)
- {
- parent::__construct();
- $this->searchIndex = $searchIndex;
- }
-
/**
* Execute the console command.
- *
- * @return mixed
*/
- public function handle()
+ public function handle(SearchIndex $searchIndex): int
{
$connection = DB::getDefaultConnection();
if ($this->option('database') !== null) {
DB::setDefaultConnection($this->option('database'));
}
- $this->searchIndex->indexAllEntities(function (Entity $model, int $processed, int $total): void {
+ $searchIndex->indexAllEntities(function (Entity $model, int $processed, int $total): void {
$this->info('Indexed ' . class_basename($model) . ' entries (' . $processed . '/' . $total . ')');
});
*/
protected $description = 'Reset & Clear any configured MFA methods for the given user';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
-
/**
* Execute the console command.
- *
- * @return mixed
*/
- public function handle()
+ public function handle(): int
{
$id = $this->option('id');
$email = $this->option('email');
$this->info("This will delete any configure multi-factor authentication methods for user: \n- ID: {$user->id}\n- Name: {$user->name}\n- Email: {$user->email}\n");
$this->info('If multi-factor authentication is required for this user they will be asked to reconfigure their methods on next login.');
$confirm = $this->confirm('Are you sure you want to proceed?');
- if ($confirm) {
- $user->mfaValues()->delete();
- $this->info('User MFA methods have been reset.');
-
- return 0;
+ if (!$confirm) {
+ return 1;
}
- return 1;
+ $user->mfaValues()->delete();
+ $this->info('User MFA methods have been reset.');
+
+ return 0;
}
}
/**
* Execute the console command.
- *
- * @return mixed
*/
- public function handle(Connection $db)
+ public function handle(Connection $db): int
{
$oldUrl = str_replace("'", '', $this->argument('oldUrl'));
$newUrl = str_replace("'", '', $this->argument('newUrl'));
*
* @var string
*/
- protected $signature = 'bookstack:db-utf8mb4 {--database= : The database connection to use.}';
+ protected $signature = 'bookstack:db-utf8mb4
+ {--database= : The database connection to use}';
/**
* The console command description.
*/
protected $description = 'Generate SQL commands to upgrade the database to UTF8mb4';
- /**
- * Create a new command instance.
- */
- public function __construct()
- {
- parent::__construct();
- }
/**
* Execute the console command.
- *
- * @return mixed
*/
- public function handle()
+ public function handle(): int
{
$connection = DB::getDefaultConnection();
if ($this->option('database') !== null) {
$key = 'Tables_in_' . $database;
foreach ($tables as $table) {
$tableName = $table->$key;
- $this->line('ALTER TABLE `' . $tableName . '` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;');
+ $this->line("ALTER TABLE `{$tableName}` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;");
}
DB::setDefaultConnection($connection);
+
+ return 0;
}
}
{
$this->artisan('bookstack:copy-shelf-permissions')
->expectsOutput('Either a --slug or --all option must be provided.')
- ->assertExitCode(0);
+ ->assertExitCode(1);
}
public function test_copy_shelf_permissions_command_using_slug()
$normalUserCount = $userCount - count($normalUsers);
$this->artisan('bookstack:delete-users')
- ->expectsQuestion('This will delete all users from the system that are not "admin" or system users. Are you sure you want to continue? (Type "yes" to continue)', 'yes')
+ ->expectsConfirmation('Are you sure you want to continue?', 'yes')
->expectsOutputToContain("Deleted $normalUserCount of $userCount total users.")
->assertExitCode(0);
$normalUsers = $this->getNormalUsers();
$this->artisan('bookstack:delete-users')
- ->expectsQuestion('This will delete all users from the system that are not "admin" or system users. Are you sure you want to continue? (Type "yes" to continue)', 'no')
+ ->expectsConfirmation('Are you sure you want to continue?', 'no')
->assertExitCode(0);
$this->assertDatabaseHas('users', ['id' => $normalUsers->first()->id]);