]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/RefreshAvatarCommand.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / app / Console / Commands / RefreshAvatarCommand.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use BookStack\Users\Models\User;
6 use Exception;
7 use Illuminate\Console\Command;
8 use BookStack\Uploads\UserAvatars;
9
10 class RefreshAvatarCommand extends Command
11 {
12     use HandlesSingleUser;
13
14     /**
15      * The name and signature of the console command.
16      *
17      * @var string
18      */
19     protected $signature = 'bookstack:refresh-avatar
20                             {--id= : Numeric ID of the user to refresh avatar for}
21                             {--email= : Email address of the user to refresh avatar for}
22                             {--users-without-avatars : Refresh avatars for users that currently have no avatar}
23                             {--a|all : Refresh avatars for all users}
24                             {--f|force : Actually run the update, Defaults to a dry-run}';
25
26     /**
27      * The console command description.
28      *
29      * @var string
30      */
31     protected $description = 'Refresh avatar for the given user(s)';
32
33     public function handle(UserAvatars $userAvatar): int
34     {
35         if (!$userAvatar->avatarFetchEnabled()) {
36             $this->error("Avatar fetching is disabled on this instance.");
37             return self::FAILURE;
38         }
39
40         if ($this->option('users-without-avatars')) {
41             return $this->processUsers(User::query()->whereDoesntHave('avatar')->get()->all(), $userAvatar);
42         }
43
44         if ($this->option('all')) {
45             return $this->processUsers(User::query()->get()->all(), $userAvatar);
46         }
47
48         try {
49             $user = $this->fetchProvidedUser();
50             return $this->processUsers([$user], $userAvatar);
51         } catch (Exception $exception) {
52             $this->error($exception->getMessage());
53             return self::FAILURE;
54         }
55     }
56
57     /**
58      * @param User[] $users
59      */
60     private function processUsers(array $users, UserAvatars $userAvatar): int
61     {
62         $dryRun = !$this->option('force');
63         $this->info(count($users) . " user(s) found to update avatars for.");
64
65         if (count($users) === 0) {
66             return self::SUCCESS;
67         }
68
69         if (!$dryRun) {
70             $fetchHost = parse_url($userAvatar->getAvatarUrl(), PHP_URL_HOST);
71             $this->warn("This will destroy any existing avatar images these users have, and attempt to fetch new avatar images from {$fetchHost}.");
72             $proceed = !$this->input->isInteractive() || $this->confirm('Are you sure you want to proceed?');
73             if (!$proceed) {
74                 return self::SUCCESS;
75             }
76         }
77
78         $this->info("");
79
80         $exitCode = self::SUCCESS;
81         foreach ($users as $user) {
82             $linePrefix = "[ID: {$user->id}] $user->email -";
83
84             if ($dryRun) {
85                 $this->warn("{$linePrefix} Not updated");
86                 continue;
87             }
88
89             if ($this->fetchAvatar($userAvatar, $user)) {
90                 $this->info("{$linePrefix} Updated");
91             } else {
92                 $this->error("{$linePrefix} Not updated");
93                 $exitCode = self::FAILURE;
94             }
95         }
96
97         if ($dryRun) {
98             $this->comment("");
99             $this->comment("Dry run, no avatars were updated.");
100             $this->comment('Run with -f or --force to perform the update.');
101         }
102
103         return $exitCode;
104     }
105
106     private function fetchAvatar(UserAvatars $userAvatar, User $user): bool
107     {
108         $oldId = $user->avatar->id ?? 0;
109
110         $userAvatar->fetchAndAssignToUser($user);
111
112         $user->refresh();
113         $newId = $user->avatar->id ?? $oldId;
114         return $oldId !== $newId;
115     }
116 }