1 | <?php |
||
2 | /** |
||
3 | * @copyright Copyright (c) 2020, Matias De lellis <[email protected]> |
||
4 | * |
||
5 | * @author Matias De lellis <[email protected]> |
||
6 | * |
||
7 | * @license GNU AGPL version 3 or any later version |
||
8 | * |
||
9 | * This program is free software: you can redistribute it and/or modify |
||
10 | * it under the terms of the GNU Affero General Public License as |
||
11 | * published by the Free Software Foundation, either version 3 of the |
||
12 | * License, or (at your option) any later version. |
||
13 | * |
||
14 | * This program is distributed in the hope that it will be useful, |
||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
17 | * GNU Affero General Public License for more details. |
||
18 | * |
||
19 | * You should have received a copy of the GNU Affero General Public License |
||
20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
21 | * |
||
22 | */ |
||
23 | namespace OCA\FaceRecognition\Command; |
||
24 | |||
25 | use Symfony\Component\Console\Command\Command; |
||
26 | use Symfony\Component\Console\Helper\Table; |
||
27 | use Symfony\Component\Console\Input\InputOption; |
||
28 | use Symfony\Component\Console\Input\InputInterface; |
||
29 | use Symfony\Component\Console\Output\OutputInterface; |
||
30 | |||
31 | use OCP\App\IAppManager; |
||
32 | |||
33 | use OCP\IUser; |
||
34 | use OCP\IUserManager; |
||
35 | |||
36 | use OCA\FaceRecognition\Helper\PhotoAlbums; |
||
37 | use OCA\FaceRecognition\Db\PersonMapper; |
||
38 | |||
39 | use OCA\FaceRecognition\Service\SettingsService; |
||
40 | |||
41 | class SyncAlbumsCommand extends Command { |
||
42 | |||
43 | /** @var IUserManager */ |
||
44 | protected $userManager; |
||
45 | |||
46 | /** @var PersonMapper Person mapper*/ |
||
47 | private $personMapper; |
||
48 | |||
49 | /** @var IAppManager */ |
||
50 | private $appManager; |
||
51 | |||
52 | /** @var PhotoAlbums */ |
||
53 | protected $photoAlbums; |
||
54 | |||
55 | /** @var SettingsService */ |
||
56 | private $settingsService; |
||
57 | |||
58 | /** |
||
59 | * @param IUserManager $userManager |
||
60 | * @param PersonMapper $personMapper |
||
61 | * @param PhotoAlbums $photoAlbums |
||
62 | * @param SettingsService $settingsService |
||
63 | * @param IAppManager $appManager |
||
64 | */ |
||
65 | public function __construct(IUserManager $userManager, |
||
66 | PersonMapper $personMapper, |
||
67 | IAppManager $appManager, |
||
68 | PhotoAlbums $photoAlbums, |
||
69 | SettingsService $settingsService) |
||
70 | { |
||
71 | parent::__construct(); |
||
72 | |||
73 | $this->appManager = $appManager; |
||
74 | $this->personMapper = $personMapper; |
||
75 | $this->userManager = $userManager; |
||
76 | $this->photoAlbums = $photoAlbums; |
||
77 | $this->settingsService = $settingsService; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return void |
||
82 | */ |
||
83 | protected function configure() { |
||
84 | $this |
||
85 | ->setName('face:sync-albums') |
||
86 | ->setDescription('Synchronize the people found with the photo albums') |
||
87 | ->addOption( |
||
88 | 'user_id', |
||
89 | 'u', |
||
90 | InputOption::VALUE_REQUIRED, |
||
91 | 'Sync albums for a given user only. If not given, sync albums for all users.', |
||
92 | null |
||
93 | )->addOption( |
||
94 | 'list_person', |
||
95 | 'l', |
||
96 | InputOption::VALUE_NONE, |
||
97 | 'List all persons defined for the given user_id.', |
||
98 | null |
||
99 | )->addOption( |
||
100 | 'person_name', |
||
101 | 'p', |
||
102 | InputOption::VALUE_REQUIRED, |
||
103 | 'Sync albums for a given user and person name(s) (separate using comma). If not used, sync albums for all persons defined by the user.', |
||
104 | null |
||
105 | )->addOption( |
||
106 | 'mode', |
||
107 | 'm', |
||
108 | InputOption::VALUE_REQUIRED, |
||
109 | 'Album creation mode. Use "album-per-person" to create one album for each given person via person_name parameter. Use "album-combined" to create one album for all person names given via person_name parameter.', |
||
110 | 'album-per-person' |
||
111 | ); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param InputInterface $input |
||
116 | * @param OutputInterface $output |
||
117 | * @return int |
||
118 | */ |
||
119 | protected function execute(InputInterface $input, OutputInterface $output) { |
||
120 | if (!$this->appManager->isEnabledForUser('photos')) { |
||
121 | $output->writeln('The photos app is disabled.'); |
||
122 | return 1; |
||
123 | } |
||
124 | |||
125 | $users = array(); |
||
126 | $userId = $input->getOption('user_id'); |
||
127 | $person_name = $input->getOption('person_name'); |
||
128 | $mode = $input->getOption('mode'); |
||
129 | |||
130 | if (!is_null($userId)) { |
||
131 | if ($this->userManager->get($userId) === null) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
132 | $output->writeln("User with id <$userId> in unknown."); |
||
133 | return 1; |
||
134 | } |
||
135 | else { |
||
136 | $users[] = $userId; |
||
137 | } |
||
138 | } |
||
139 | else { |
||
140 | $this->userManager->callForAllUsers(function (IUser $iUser) use (&$users) { |
||
141 | $users[] = $iUser->getUID(); |
||
142 | }); |
||
143 | } |
||
144 | |||
145 | if ($input->getOption('list_person')) { |
||
146 | if (is_null($userId)) { |
||
147 | $output->writeln("List option requires option user_id!"); |
||
148 | return 1; |
||
149 | } else{ |
||
150 | $output->writeln("List of defined persons for the user <$userId> :"); |
||
151 | $modelId = $this->settingsService->getCurrentFaceModel(); |
||
152 | $distintNames = $this->personMapper->findDistinctNames($userId, $modelId); |
||
153 | foreach ($distintNames as $key=>$distintName) { |
||
154 | if ($key > 0 ){ |
||
155 | $output->write(", "); |
||
156 | } |
||
157 | $output->write($distintName->getName()); |
||
158 | } |
||
159 | $output->writeln(""); |
||
160 | $output->writeln("Done."); |
||
161 | } |
||
162 | return 0; |
||
163 | } |
||
164 | |||
165 | foreach ($users as $userId) { |
||
166 | if (!is_null($person_name)) { |
||
167 | if (is_null($userId)) { |
||
168 | $output->writeln("Person_name option requires option user_id!"); |
||
169 | return 1; |
||
170 | } |
||
171 | $output->writeln("Synchronizing albums for the user <$userId> and person_name <$person_name> using mode <$mode>... "); |
||
172 | if ($mode === "album-per-person") { |
||
173 | $personList = explode(",", $person_name); |
||
174 | foreach ($personList as $person) { |
||
175 | $this->photoAlbums->syncUserPersonNamesSelected($userId, $person, $output); |
||
176 | } |
||
177 | } |
||
178 | else if ($mode === "album-combined") { |
||
179 | $personList = explode(",", $person_name); |
||
180 | if (count($personList) < 2) { |
||
181 | $output->writeln("Note parameter mode <$mode> requires at least two persons separated using coma."); |
||
182 | return 1; |
||
183 | } |
||
184 | $this->photoAlbums->syncUserPersonNamesCombinedAlbum($userId, $personList, $output); |
||
185 | } |
||
186 | else { |
||
187 | $output->writeln("Error: invalid value for parameter mode <$mode>. "); |
||
188 | return 1; |
||
189 | } |
||
190 | $output->writeln("Done."); |
||
191 | } else { |
||
192 | $output->write("Synchronizing albums for the user <$userId>... "); |
||
193 | $this->photoAlbums->syncUser($userId); |
||
194 | $output->writeln("Done."); |
||
195 | } |
||
196 | } |
||
197 | |||
198 | return 0; |
||
199 | } |
||
200 | |||
201 | } |
||
202 |