]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Command; | |
4 | ||
5 | use FOS\UserBundle\Event\UserEvent; | |
6 | use FOS\UserBundle\FOSUserEvents; | |
7 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
8 | use Symfony\Component\Console\Input\ArrayInput; | |
9 | use Symfony\Component\Console\Input\InputInterface; | |
10 | use Symfony\Component\Console\Input\InputOption; | |
11 | use Symfony\Component\Console\Output\BufferedOutput; | |
12 | use Symfony\Component\Console\Output\OutputInterface; | |
13 | use Symfony\Component\Console\Question\Question; | |
14 | use Symfony\Component\Console\Style\SymfonyStyle; | |
15 | use Wallabag\CoreBundle\Entity\InternalSetting; | |
16 | ||
17 | class InstallCommand extends ContainerAwareCommand | |
18 | { | |
19 | /** | |
20 | * @var InputInterface | |
21 | */ | |
22 | protected $defaultInput; | |
23 | ||
24 | /** | |
25 | * @var SymfonyStyle | |
26 | */ | |
27 | protected $io; | |
28 | ||
29 | /** | |
30 | * @var array | |
31 | */ | |
32 | protected $functionExists = [ | |
33 | 'curl_exec', | |
34 | 'curl_multi_init', | |
35 | ]; | |
36 | ||
37 | protected function configure() | |
38 | { | |
39 | $this | |
40 | ->setName('wallabag:install') | |
41 | ->setDescription('wallabag installer.') | |
42 | ->addOption( | |
43 | 'reset', | |
44 | null, | |
45 | InputOption::VALUE_NONE, | |
46 | 'Reset current database' | |
47 | ) | |
48 | ; | |
49 | } | |
50 | ||
51 | protected function execute(InputInterface $input, OutputInterface $output) | |
52 | { | |
53 | $this->defaultInput = $input; | |
54 | ||
55 | $this->io = new SymfonyStyle($input, $output); | |
56 | ||
57 | $this->io->title('wallabag installer'); | |
58 | ||
59 | $this | |
60 | ->checkRequirements() | |
61 | ->setupDatabase() | |
62 | ->setupAdmin() | |
63 | ->setupConfig() | |
64 | ; | |
65 | ||
66 | $this->io->success('wallabag has been successfully installed.'); | |
67 | $this->io->success('You can now configure your web server, see https://doc.wallabag.org'); | |
68 | } | |
69 | ||
70 | protected function checkRequirements() | |
71 | { | |
72 | $this->io->section('Step 1 of 4: Checking system requirements.'); | |
73 | ||
74 | $doctrineManager = $this->getContainer()->get('doctrine')->getManager(); | |
75 | ||
76 | $rows = []; | |
77 | ||
78 | // testing if database driver exists | |
79 | $fulfilled = true; | |
80 | $label = '<comment>PDO Driver (%s)</comment>'; | |
81 | $status = '<info>OK!</info>'; | |
82 | $help = ''; | |
83 | ||
84 | if (!\extension_loaded($this->getContainer()->getParameter('database_driver'))) { | |
85 | $fulfilled = false; | |
86 | $status = '<error>ERROR!</error>'; | |
87 | $help = 'Database driver "' . $this->getContainer()->getParameter('database_driver') . '" is not installed.'; | |
88 | } | |
89 | ||
90 | $rows[] = [sprintf($label, $this->getContainer()->getParameter('database_driver')), $status, $help]; | |
91 | ||
92 | // testing if connection to the database can be etablished | |
93 | $label = '<comment>Database connection</comment>'; | |
94 | $status = '<info>OK!</info>'; | |
95 | $help = ''; | |
96 | ||
97 | $conn = $this->getContainer()->get('doctrine')->getManager()->getConnection(); | |
98 | ||
99 | try { | |
100 | $conn->connect(); | |
101 | } catch (\Exception $e) { | |
102 | if (false === strpos($e->getMessage(), 'Unknown database') | |
103 | && false === strpos($e->getMessage(), 'database "' . $this->getContainer()->getParameter('database_name') . '" does not exist')) { | |
104 | $fulfilled = false; | |
105 | $status = '<error>ERROR!</error>'; | |
106 | $help = 'Can\'t connect to the database: ' . $e->getMessage(); | |
107 | } | |
108 | } | |
109 | ||
110 | $rows[] = [$label, $status, $help]; | |
111 | ||
112 | // check MySQL & PostgreSQL version | |
113 | $label = '<comment>Database version</comment>'; | |
114 | $status = '<info>OK!</info>'; | |
115 | $help = ''; | |
116 | ||
117 | // now check if MySQL isn't too old to handle utf8mb4 | |
118 | if ($conn->isConnected() && 'mysql' === $conn->getDatabasePlatform()->getName()) { | |
119 | $version = $conn->query('select version()')->fetchColumn(); | |
120 | $minimalVersion = '5.5.4'; | |
121 | ||
122 | if (false === version_compare($version, $minimalVersion, '>')) { | |
123 | $fulfilled = false; | |
124 | $status = '<error>ERROR!</error>'; | |
125 | $help = 'Your MySQL version (' . $version . ') is too old, consider upgrading (' . $minimalVersion . '+).'; | |
126 | } | |
127 | } | |
128 | ||
129 | // testing if PostgreSQL > 9.1 | |
130 | if ($conn->isConnected() && 'postgresql' === $conn->getDatabasePlatform()->getName()) { | |
131 | // return version should be like "PostgreSQL 9.5.4 on x86_64-apple-darwin15.6.0, compiled by Apple LLVM version 8.0.0 (clang-800.0.38), 64-bit" | |
132 | $version = $doctrineManager->getConnection()->query('SELECT version();')->fetchColumn(); | |
133 | ||
134 | preg_match('/PostgreSQL ([0-9\.]+)/i', $version, $matches); | |
135 | ||
136 | if (isset($matches[1]) & version_compare($matches[1], '9.2.0', '<')) { | |
137 | $fulfilled = false; | |
138 | $status = '<error>ERROR!</error>'; | |
139 | $help = 'PostgreSQL should be greater than 9.1 (actual version: ' . $matches[1] . ')'; | |
140 | } | |
141 | } | |
142 | ||
143 | $rows[] = [$label, $status, $help]; | |
144 | ||
145 | foreach ($this->functionExists as $functionRequired) { | |
146 | $label = '<comment>' . $functionRequired . '</comment>'; | |
147 | $status = '<info>OK!</info>'; | |
148 | $help = ''; | |
149 | ||
150 | if (!\function_exists($functionRequired)) { | |
151 | $fulfilled = false; | |
152 | $status = '<error>ERROR!</error>'; | |
153 | $help = 'You need the ' . $functionRequired . ' function activated'; | |
154 | } | |
155 | ||
156 | $rows[] = [$label, $status, $help]; | |
157 | } | |
158 | ||
159 | $this->io->table(['Checked', 'Status', 'Recommendation'], $rows); | |
160 | ||
161 | if (!$fulfilled) { | |
162 | throw new \RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.'); | |
163 | } | |
164 | ||
165 | $this->io->success('Success! Your system can run wallabag properly.'); | |
166 | ||
167 | return $this; | |
168 | } | |
169 | ||
170 | protected function setupDatabase() | |
171 | { | |
172 | $this->io->section('Step 2 of 4: Setting up database.'); | |
173 | ||
174 | // user want to reset everything? Don't care about what is already here | |
175 | if (true === $this->defaultInput->getOption('reset')) { | |
176 | $this->io->text('Dropping database, creating database and schema, clearing the cache'); | |
177 | ||
178 | $this | |
179 | ->runCommand('doctrine:database:drop', ['--force' => true]) | |
180 | ->runCommand('doctrine:database:create') | |
181 | ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true]) | |
182 | ->runCommand('cache:clear') | |
183 | ; | |
184 | ||
185 | $this->io->newLine(); | |
186 | ||
187 | return $this; | |
188 | } | |
189 | ||
190 | if (!$this->isDatabasePresent()) { | |
191 | $this->io->text('Creating database and schema, clearing the cache'); | |
192 | ||
193 | $this | |
194 | ->runCommand('doctrine:database:create') | |
195 | ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true]) | |
196 | ->runCommand('cache:clear') | |
197 | ; | |
198 | ||
199 | $this->io->newLine(); | |
200 | ||
201 | return $this; | |
202 | } | |
203 | ||
204 | if ($this->io->confirm('It appears that your database already exists. Would you like to reset it?', false)) { | |
205 | $this->io->text('Dropping database, creating database and schema...'); | |
206 | ||
207 | $this | |
208 | ->runCommand('doctrine:database:drop', ['--force' => true]) | |
209 | ->runCommand('doctrine:database:create') | |
210 | ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true]) | |
211 | ; | |
212 | } elseif ($this->isSchemaPresent()) { | |
213 | if ($this->io->confirm('Seems like your database contains schema. Do you want to reset it?', false)) { | |
214 | $this->io->text('Dropping schema and creating schema...'); | |
215 | ||
216 | $this | |
217 | ->runCommand('doctrine:schema:drop', ['--force' => true]) | |
218 | ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true]) | |
219 | ; | |
220 | } | |
221 | } else { | |
222 | $this->io->text('Creating schema...'); | |
223 | ||
224 | $this | |
225 | ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true]) | |
226 | ; | |
227 | } | |
228 | ||
229 | $this->io->text('Clearing the cache...'); | |
230 | $this->runCommand('cache:clear'); | |
231 | ||
232 | $this->io->newLine(); | |
233 | $this->io->text('<info>Database successfully setup.</info>'); | |
234 | ||
235 | return $this; | |
236 | } | |
237 | ||
238 | protected function setupAdmin() | |
239 | { | |
240 | $this->io->section('Step 3 of 4: Administration setup.'); | |
241 | ||
242 | if (!$this->io->confirm('Would you like to create a new admin user (recommended)?', true)) { | |
243 | return $this; | |
244 | } | |
245 | ||
246 | $em = $this->getContainer()->get('doctrine.orm.entity_manager'); | |
247 | ||
248 | $userManager = $this->getContainer()->get('fos_user.user_manager'); | |
249 | $user = $userManager->createUser(); | |
250 | ||
251 | $user->setUsername($this->io->ask('Username', 'wallabag')); | |
252 | ||
253 | $question = new Question('Password', 'wallabag'); | |
254 | $question->setHidden(true); | |
255 | $user->setPlainPassword($this->io->askQuestion($question)); | |
256 | ||
257 | $user->setEmail($this->io->ask('Email', 'wallabag@wallabag.io')); | |
258 | ||
259 | $user->setEnabled(true); | |
260 | $user->addRole('ROLE_SUPER_ADMIN'); | |
261 | ||
262 | $em->persist($user); | |
263 | ||
264 | // dispatch a created event so the associated config will be created | |
265 | $event = new UserEvent($user); | |
266 | $this->getContainer()->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); | |
267 | ||
268 | $this->io->text('<info>Administration successfully setup.</info>'); | |
269 | ||
270 | return $this; | |
271 | } | |
272 | ||
273 | protected function setupConfig() | |
274 | { | |
275 | $this->io->section('Step 4 of 4: Config setup.'); | |
276 | $em = $this->getContainer()->get('doctrine.orm.entity_manager'); | |
277 | ||
278 | // cleanup before insert new stuff | |
279 | $em->createQuery('DELETE FROM WallabagCoreBundle:InternalSetting')->execute(); | |
280 | ||
281 | foreach ($this->getContainer()->getParameter('wallabag_core.default_internal_settings') as $setting) { | |
282 | $newSetting = new InternalSetting(); | |
283 | $newSetting->setName($setting['name']); | |
284 | $newSetting->setValue($setting['value']); | |
285 | $newSetting->setSection($setting['section']); | |
286 | $em->persist($newSetting); | |
287 | } | |
288 | ||
289 | $em->flush(); | |
290 | ||
291 | $this->io->text('<info>Config successfully setup.</info>'); | |
292 | ||
293 | return $this; | |
294 | } | |
295 | ||
296 | /** | |
297 | * Run a command. | |
298 | * | |
299 | * @param string $command | |
300 | * @param array $parameters Parameters to this command (usually 'force' => true) | |
301 | */ | |
302 | protected function runCommand($command, $parameters = []) | |
303 | { | |
304 | $parameters = array_merge( | |
305 | ['command' => $command], | |
306 | $parameters, | |
307 | [ | |
308 | '--no-debug' => true, | |
309 | '--env' => $this->defaultInput->getOption('env') ?: 'dev', | |
310 | ] | |
311 | ); | |
312 | ||
313 | if ($this->defaultInput->getOption('no-interaction')) { | |
314 | $parameters = array_merge($parameters, ['--no-interaction' => true]); | |
315 | } | |
316 | ||
317 | $this->getApplication()->setAutoExit(false); | |
318 | ||
319 | $output = new BufferedOutput(); | |
320 | $exitCode = $this->getApplication()->run(new ArrayInput($parameters), $output); | |
321 | ||
322 | // PDO does not always close the connection after Doctrine commands. | |
323 | // See https://github.com/symfony/symfony/issues/11750. | |
324 | $this->getContainer()->get('doctrine')->getManager()->getConnection()->close(); | |
325 | ||
326 | if (0 !== $exitCode) { | |
327 | $this->getApplication()->setAutoExit(true); | |
328 | ||
329 | throw new \RuntimeException('The command "' . $command . "\" generates some errors: \n\n" . $output->fetch()); | |
330 | } | |
331 | ||
332 | return $this; | |
333 | } | |
334 | ||
335 | /** | |
336 | * Check if the database already exists. | |
337 | * | |
338 | * @return bool | |
339 | */ | |
340 | private function isDatabasePresent() | |
341 | { | |
342 | $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection(); | |
343 | $databaseName = $connection->getDatabase(); | |
344 | ||
345 | try { | |
346 | $schemaManager = $connection->getSchemaManager(); | |
347 | } catch (\Exception $exception) { | |
348 | // mysql & sqlite | |
349 | if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) { | |
350 | return false; | |
351 | } | |
352 | ||
353 | // pgsql | |
354 | if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) { | |
355 | return false; | |
356 | } | |
357 | ||
358 | throw $exception; | |
359 | } | |
360 | ||
361 | // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite | |
362 | if ('sqlite' === $schemaManager->getDatabasePlatform()->getName()) { | |
363 | $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams(); | |
364 | ||
365 | if (isset($params['path']) && file_exists($params['path'])) { | |
366 | return true; | |
367 | } | |
368 | ||
369 | return false; | |
370 | } | |
371 | ||
372 | try { | |
373 | return \in_array($databaseName, $schemaManager->listDatabases(), true); | |
374 | } catch (\Doctrine\DBAL\Exception\DriverException $e) { | |
375 | // it means we weren't able to get database list, assume the database doesn't exist | |
376 | ||
377 | return false; | |
378 | } | |
379 | } | |
380 | ||
381 | /** | |
382 | * Check if the schema is already created. | |
383 | * If we found at least oen table, it means the schema exists. | |
384 | * | |
385 | * @return bool | |
386 | */ | |
387 | private function isSchemaPresent() | |
388 | { | |
389 | $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager(); | |
390 | ||
391 | return \count($schemaManager->listTableNames()) > 0 ? true : false; | |
392 | } | |
393 | } |