]>
Commit | Line | Data |
---|---|---|
2e45e7be J |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Command; | |
4 | ||
ca17abce JB |
5 | use FOS\UserBundle\Event\UserEvent; |
6 | use FOS\UserBundle\FOSUserEvents; | |
2e45e7be | 7 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
619cc453 JB |
8 | use Symfony\Component\Console\Helper\Table; |
9 | use Symfony\Component\Console\Input\ArrayInput; | |
2e45e7be | 10 | use Symfony\Component\Console\Input\InputInterface; |
0bf99bb1 | 11 | use Symfony\Component\Console\Input\InputOption; |
39a19bdf | 12 | use Symfony\Component\Console\Output\BufferedOutput; |
619cc453 | 13 | use Symfony\Component\Console\Output\OutputInterface; |
78507d28 | 14 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
619cc453 | 15 | use Symfony\Component\Console\Question\Question; |
4d85d7e9 | 16 | use Wallabag\CoreBundle\Entity\Config; |
d6ba77e8 | 17 | use Craue\ConfigBundle\Entity\Setting; |
2e45e7be J |
18 | |
19 | class InstallCommand extends ContainerAwareCommand | |
20 | { | |
0bf99bb1 J |
21 | /** |
22 | * @var InputInterface | |
23 | */ | |
24 | protected $defaultInput; | |
25 | ||
26 | /** | |
27 | * @var OutputInterface | |
28 | */ | |
29 | protected $defaultOutput; | |
30 | ||
fc6020b2 NL |
31 | /** |
32 | * @var array | |
33 | */ | |
db847ca0 | 34 | protected $functionExists = [ |
db847ca0 TC |
35 | 'curl_exec', |
36 | 'curl_multi_init', | |
fc6020b2 NL |
37 | ]; |
38 | ||
2e45e7be J |
39 | protected function configure() |
40 | { | |
41 | $this | |
42 | ->setName('wallabag:install') | |
43 | ->setDescription('Wallabag installer.') | |
0bf99bb1 J |
44 | ->addOption( |
45 | 'reset', | |
46 | null, | |
47 | InputOption::VALUE_NONE, | |
48 | 'Reset current database' | |
49 | ) | |
2e45e7be J |
50 | ; |
51 | } | |
52 | ||
53 | protected function execute(InputInterface $input, OutputInterface $output) | |
54 | { | |
0bf99bb1 J |
55 | $this->defaultInput = $input; |
56 | $this->defaultOutput = $output; | |
57 | ||
58 | $output->writeln('<info>Installing Wallabag...</info>'); | |
2e45e7be J |
59 | $output->writeln(''); |
60 | ||
61 | $this | |
0bf99bb1 J |
62 | ->checkRequirements() |
63 | ->setupDatabase() | |
64 | ->setupAdmin() | |
637dc4bb | 65 | ->setupConfig() |
2e45e7be J |
66 | ; |
67 | ||
68 | $output->writeln('<info>Wallabag has been successfully installed.</info>'); | |
0c6845a9 | 69 | $output->writeln('<comment>Just execute `php bin/console server:run --env=prod` for using wallabag: http://localhost:8000</comment>'); |
2e45e7be J |
70 | } |
71 | ||
0bf99bb1 | 72 | protected function checkRequirements() |
2e45e7be | 73 | { |
5ecdfcd0 | 74 | $this->defaultOutput->writeln('<info><comment>Step 1 of 4.</comment> Checking system requirements.</info>'); |
2e45e7be | 75 | |
001a7bad | 76 | $rows = []; |
2e45e7be | 77 | |
001a7bad JB |
78 | // testing if database driver exists |
79 | $fulfilled = true; | |
a730cae3 | 80 | $label = '<comment>PDO Driver (%s)</comment>'; |
0e49487b JB |
81 | $status = '<info>OK!</info>'; |
82 | $help = ''; | |
c61b68e8 JB |
83 | |
84 | if (!extension_loaded($this->getContainer()->getParameter('database_driver'))) { | |
db847ca0 TC |
85 | $fulfilled = false; |
86 | $status = '<error>ERROR!</error>'; | |
c61b68e8 | 87 | $help = 'Database driver "'.$this->getContainer()->getParameter('database_driver').'" is not installed.'; |
db847ca0 TC |
88 | } |
89 | ||
a730cae3 | 90 | $rows[] = [sprintf($label, $this->getContainer()->getParameter('database_driver')), $status, $help]; |
001a7bad JB |
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 | try { | |
fc79f1ff JB |
98 | $conn = $this->getContainer()->get('doctrine')->getManager()->getConnection(); |
99 | $conn->connect(); | |
001a7bad | 100 | } catch (\Exception $e) { |
39a19bdf JB |
101 | if (false === strpos($e->getMessage(), 'Unknown database') |
102 | && false === strpos($e->getMessage(), 'database "'.$this->getContainer()->getParameter('database_name').'" does not exist')) { | |
f62c3faf JB |
103 | $fulfilled = false; |
104 | $status = '<error>ERROR!</error>'; | |
105 | $help = 'Can\'t connect to the database: '.$e->getMessage(); | |
106 | } | |
001a7bad JB |
107 | } |
108 | ||
4d0ec0e7 | 109 | $rows[] = [$label, $status, $help]; |
db847ca0 | 110 | |
fc79f1ff JB |
111 | // now check if MySQL isn't too old to handle utf8mb4 |
112 | if ($conn->isConnected() && $conn->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\MySqlPlatform) { | |
113 | $version = $conn->query('select version()')->fetchColumn(); | |
114 | $minimalVersion = '5.5.4'; | |
115 | ||
116 | if (false === version_compare($version, $minimalVersion, '>')) { | |
117 | $fulfilled = false; | |
118 | $rows[] = [ | |
119 | '<comment>Database version</comment>', | |
120 | '<error>ERROR!</error>', | |
121 | 'Your MySQL version ('.$version.') is too old, consider upgrading ('.$minimalVersion.'+).' | |
122 | ]; | |
123 | } | |
124 | } | |
125 | ||
db847ca0 TC |
126 | foreach ($this->functionExists as $functionRequired) { |
127 | $label = '<comment>'.$functionRequired.'</comment>'; | |
0e49487b JB |
128 | $status = '<info>OK!</info>'; |
129 | $help = ''; | |
db847ca0 | 130 | |
0e49487b | 131 | if (!function_exists($functionRequired)) { |
fc6020b2 NL |
132 | $fulfilled = false; |
133 | $status = '<error>ERROR!</error>'; | |
f2fcb65b | 134 | $help = 'You need the '.$functionRequired.' function activated'; |
fc6020b2 | 135 | } |
db847ca0 | 136 | |
4d0ec0e7 | 137 | $rows[] = [$label, $status, $help]; |
2e45e7be | 138 | } |
0bf99bb1 | 139 | |
78507d28 JB |
140 | $table = new Table($this->defaultOutput); |
141 | $table | |
4d0ec0e7 | 142 | ->setHeaders(['Checked', 'Status', 'Recommendation']) |
0bf99bb1 | 143 | ->setRows($rows) |
78507d28 | 144 | ->render(); |
2e45e7be J |
145 | |
146 | if (!$fulfilled) { | |
0bf99bb1 | 147 | throw new \RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.'); |
2e45e7be J |
148 | } |
149 | ||
8a493541 JB |
150 | $this->defaultOutput->writeln('<info>Success! Your system can run Wallabag properly.</info>'); |
151 | ||
0bf99bb1 | 152 | $this->defaultOutput->writeln(''); |
2e45e7be J |
153 | |
154 | return $this; | |
155 | } | |
156 | ||
0bf99bb1 | 157 | protected function setupDatabase() |
2e45e7be | 158 | { |
5ecdfcd0 | 159 | $this->defaultOutput->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>'); |
2e45e7be | 160 | |
0bf99bb1 J |
161 | // user want to reset everything? Don't care about what is already here |
162 | if (true === $this->defaultInput->getOption('reset')) { | |
d5027625 | 163 | $this->defaultOutput->writeln('Droping database, creating database and schema, clearing the cache'); |
2e45e7be | 164 | |
0bf99bb1 | 165 | $this |
4d0ec0e7 | 166 | ->runCommand('doctrine:database:drop', ['--force' => true]) |
0bf99bb1 J |
167 | ->runCommand('doctrine:database:create') |
168 | ->runCommand('doctrine:schema:create') | |
d5027625 | 169 | ->runCommand('cache:clear') |
0bf99bb1 | 170 | ; |
2e45e7be | 171 | |
d5027625 JB |
172 | $this->defaultOutput->writeln(''); |
173 | ||
0bf99bb1 J |
174 | return $this; |
175 | } | |
2e45e7be | 176 | |
0bf99bb1 J |
177 | if (!$this->isDatabasePresent()) { |
178 | $this->defaultOutput->writeln('Creating database and schema, clearing the cache'); | |
2e45e7be | 179 | |
0bf99bb1 J |
180 | $this |
181 | ->runCommand('doctrine:database:create') | |
182 | ->runCommand('doctrine:schema:create') | |
183 | ->runCommand('cache:clear') | |
184 | ; | |
2e45e7be | 185 | |
d5027625 JB |
186 | $this->defaultOutput->writeln(''); |
187 | ||
0bf99bb1 J |
188 | return $this; |
189 | } | |
2e45e7be | 190 | |
78507d28 JB |
191 | $questionHelper = $this->getHelper('question'); |
192 | $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false); | |
2e45e7be | 193 | |
78507d28 | 194 | if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) { |
0bf99bb1 | 195 | $this->defaultOutput->writeln('Droping database, creating database and schema'); |
2e45e7be | 196 | |
0bf99bb1 | 197 | $this |
4d0ec0e7 | 198 | ->runCommand('doctrine:database:drop', ['--force' => true]) |
0bf99bb1 J |
199 | ->runCommand('doctrine:database:create') |
200 | ->runCommand('doctrine:schema:create') | |
201 | ; | |
202 | } elseif ($this->isSchemaPresent()) { | |
78507d28 JB |
203 | $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false); |
204 | if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) { | |
0bf99bb1 | 205 | $this->defaultOutput->writeln('Droping schema and creating schema'); |
2e45e7be | 206 | |
0bf99bb1 | 207 | $this |
4d0ec0e7 | 208 | ->runCommand('doctrine:schema:drop', ['--force' => true]) |
0bf99bb1 J |
209 | ->runCommand('doctrine:schema:create') |
210 | ; | |
211 | } | |
2e45e7be | 212 | } else { |
0bf99bb1 J |
213 | $this->defaultOutput->writeln('Creating schema'); |
214 | ||
215 | $this | |
216 | ->runCommand('doctrine:schema:create') | |
217 | ; | |
2e45e7be J |
218 | } |
219 | ||
0bf99bb1 J |
220 | $this->defaultOutput->writeln('Clearing the cache'); |
221 | $this->runCommand('cache:clear'); | |
222 | ||
0bf99bb1 J |
223 | $this->defaultOutput->writeln(''); |
224 | ||
225 | return $this; | |
2e45e7be J |
226 | } |
227 | ||
0bf99bb1 | 228 | protected function setupAdmin() |
2e45e7be | 229 | { |
5ecdfcd0 | 230 | $this->defaultOutput->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>'); |
0bf99bb1 | 231 | |
78507d28 | 232 | $questionHelper = $this->getHelperSet()->get('question'); |
3c39f5ac | 233 | $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true); |
0bf99bb1 | 234 | |
78507d28 | 235 | if (!$questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) { |
0bf99bb1 J |
236 | return $this; |
237 | } | |
238 | ||
2e45e7be J |
239 | $em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
240 | ||
ec3ce598 NL |
241 | $userManager = $this->getContainer()->get('fos_user.user_manager'); |
242 | $user = $userManager->createUser(); | |
78507d28 JB |
243 | |
244 | $question = new Question('Username (default: wallabag) :', 'wallabag'); | |
245 | $user->setUsername($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)); | |
246 | ||
247 | $question = new Question('Password (default: wallabag) :', 'wallabag'); | |
248 | $user->setPlainPassword($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)); | |
249 | ||
250 | $question = new Question('Email:', ''); | |
251 | $user->setEmail($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)); | |
252 | ||
a1691859 | 253 | $user->setEnabled(true); |
8f06a8c4 | 254 | $user->addRole('ROLE_SUPER_ADMIN'); |
2e45e7be J |
255 | |
256 | $em->persist($user); | |
257 | ||
ca17abce JB |
258 | // dispatch a created event so the associated config will be created |
259 | $event = new UserEvent($user); | |
260 | $this->getContainer()->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); | |
0bf99bb1 | 261 | |
637dc4bb JB |
262 | $this->defaultOutput->writeln(''); |
263 | ||
264 | return $this; | |
265 | } | |
266 | ||
267 | protected function setupConfig() | |
268 | { | |
5ecdfcd0 | 269 | $this->defaultOutput->writeln('<info><comment>Step 4 of 4.</comment> Config setup.</info>'); |
637dc4bb JB |
270 | $em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
271 | ||
d6ba77e8 JB |
272 | // cleanup before insert new stuff |
273 | $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute(); | |
274 | ||
275 | $settings = [ | |
d6ba77e8 | 276 | [ |
d0545b6b NL |
277 | 'name' => 'share_public', |
278 | 'value' => '1', | |
279 | 'section' => 'entry', | |
280 | ], | |
281 | [ | |
d6ba77e8 JB |
282 | 'name' => 'carrot', |
283 | 'value' => '1', | |
284 | 'section' => 'entry', | |
285 | ], | |
286 | [ | |
287 | 'name' => 'share_diaspora', | |
288 | 'value' => '1', | |
289 | 'section' => 'entry', | |
290 | ], | |
291 | [ | |
292 | 'name' => 'diaspora_url', | |
293 | 'value' => 'http://diasporapod.com', | |
294 | 'section' => 'entry', | |
295 | ], | |
296 | [ | |
297 | 'name' => 'share_shaarli', | |
298 | 'value' => '1', | |
299 | 'section' => 'entry', | |
300 | ], | |
301 | [ | |
302 | 'name' => 'shaarli_url', | |
303 | 'value' => 'http://myshaarli.com', | |
304 | 'section' => 'entry', | |
305 | ], | |
306 | [ | |
307 | 'name' => 'share_mail', | |
308 | 'value' => '1', | |
309 | 'section' => 'entry', | |
310 | ], | |
311 | [ | |
312 | 'name' => 'share_twitter', | |
313 | 'value' => '1', | |
314 | 'section' => 'entry', | |
315 | ], | |
316 | [ | |
317 | 'name' => 'export_epub', | |
318 | 'value' => '1', | |
319 | 'section' => 'export', | |
320 | ], | |
321 | [ | |
322 | 'name' => 'export_mobi', | |
323 | 'value' => '1', | |
324 | 'section' => 'export', | |
325 | ], | |
326 | [ | |
327 | 'name' => 'export_pdf', | |
328 | 'value' => '1', | |
329 | 'section' => 'export', | |
330 | ], | |
a74a6ca2 JB |
331 | [ |
332 | 'name' => 'export_csv', | |
333 | 'value' => '1', | |
334 | 'section' => 'export', | |
335 | ], | |
336 | [ | |
337 | 'name' => 'export_json', | |
338 | 'value' => '1', | |
339 | 'section' => 'export', | |
340 | ], | |
341 | [ | |
342 | 'name' => 'export_txt', | |
343 | 'value' => '1', | |
344 | 'section' => 'export', | |
345 | ], | |
346 | [ | |
347 | 'name' => 'export_xml', | |
348 | 'value' => '1', | |
349 | 'section' => 'export', | |
350 | ], | |
40d2a294 | 351 | [ |
b3437d58 JB |
352 | 'name' => 'import_with_redis', |
353 | 'value' => '0', | |
354 | 'section' => 'import', | |
355 | ], | |
356 | [ | |
357 | 'name' => 'import_with_rabbitmq', | |
40d2a294 NL |
358 | 'value' => '0', |
359 | 'section' => 'import', | |
360 | ], | |
d6ba77e8 JB |
361 | [ |
362 | 'name' => 'show_printlink', | |
363 | 'value' => '1', | |
364 | 'section' => 'entry', | |
365 | ], | |
366 | [ | |
367 | 'name' => 'wallabag_support_url', | |
368 | 'value' => 'https://www.wallabag.org/pages/support.html', | |
369 | 'section' => 'misc', | |
370 | ], | |
371 | [ | |
372 | 'name' => 'wallabag_url', | |
373 | 'value' => 'http://v2.wallabag.org', | |
374 | 'section' => 'misc', | |
375 | ], | |
07643dde NL |
376 | [ |
377 | 'name' => 'piwik_enabled', | |
378 | 'value' => '0', | |
379 | 'section' => 'analytics', | |
380 | ], | |
381 | [ | |
382 | 'name' => 'piwik_host', | |
383 | 'value' => 'http://v2.wallabag.org', | |
384 | 'section' => 'analytics', | |
385 | ], | |
386 | [ | |
387 | 'name' => 'piwik_site_id', | |
388 | 'value' => '1', | |
389 | 'section' => 'analytics', | |
390 | ], | |
a4f42c59 JB |
391 | [ |
392 | 'name' => 'demo_mode_enabled', | |
393 | 'value' => '0', | |
394 | 'section' => 'misc', | |
395 | ], | |
396 | [ | |
397 | 'name' => 'demo_mode_username', | |
398 | 'value' => 'wallabag', | |
399 | 'section' => 'misc', | |
400 | ], | |
d6ba77e8 JB |
401 | ]; |
402 | ||
403 | foreach ($settings as $setting) { | |
404 | $newSetting = new Setting(); | |
405 | $newSetting->setName($setting['name']); | |
406 | $newSetting->setValue($setting['value']); | |
407 | $newSetting->setSection($setting['section']); | |
408 | $em->persist($newSetting); | |
409 | } | |
410 | ||
0bf99bb1 J |
411 | $em->flush(); |
412 | ||
413 | $this->defaultOutput->writeln(''); | |
414 | ||
415 | return $this; | |
2e45e7be J |
416 | } |
417 | ||
0bf99bb1 | 418 | /** |
4346a860 | 419 | * Run a command. |
0bf99bb1 J |
420 | * |
421 | * @param string $command | |
422 | * @param array $parameters Parameters to this command (usually 'force' => true) | |
423 | */ | |
4d0ec0e7 | 424 | protected function runCommand($command, $parameters = []) |
0bf99bb1 J |
425 | { |
426 | $parameters = array_merge( | |
4d0ec0e7 | 427 | ['command' => $command], |
0bf99bb1 | 428 | $parameters, |
4d0ec0e7 | 429 | [ |
0bf99bb1 J |
430 | '--no-debug' => true, |
431 | '--env' => $this->defaultInput->getOption('env') ?: 'dev', | |
4d0ec0e7 | 432 | ] |
0bf99bb1 J |
433 | ); |
434 | ||
435 | if ($this->defaultInput->getOption('no-interaction')) { | |
4d0ec0e7 | 436 | $parameters = array_merge($parameters, ['--no-interaction' => true]); |
0bf99bb1 J |
437 | } |
438 | ||
439 | $this->getApplication()->setAutoExit(false); | |
39a19bdf JB |
440 | |
441 | $output = new BufferedOutput(); | |
442 | $exitCode = $this->getApplication()->run(new ArrayInput($parameters), $output); | |
0bf99bb1 J |
443 | |
444 | if (0 !== $exitCode) { | |
445 | $this->getApplication()->setAutoExit(true); | |
446 | ||
39a19bdf JB |
447 | $this->defaultOutput->writeln(''); |
448 | $this->defaultOutput->writeln('<error>The command "'.$command.'" generates some errors: </error>'); | |
449 | $this->defaultOutput->writeln($output->fetch()); | |
0bf99bb1 | 450 | |
39a19bdf | 451 | die(); |
0bf99bb1 J |
452 | } |
453 | ||
454 | // PDO does not always close the connection after Doctrine commands. | |
455 | // See https://github.com/symfony/symfony/issues/11750. | |
456 | $this->getContainer()->get('doctrine')->getManager()->getConnection()->close(); | |
457 | ||
2e45e7be J |
458 | return $this; |
459 | } | |
0bf99bb1 J |
460 | |
461 | /** | |
4346a860 | 462 | * Check if the database already exists. |
0bf99bb1 | 463 | * |
4346a860 | 464 | * @return bool |
0bf99bb1 J |
465 | */ |
466 | private function isDatabasePresent() | |
467 | { | |
af43bd37 JB |
468 | $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection(); |
469 | $databaseName = $connection->getDatabase(); | |
0bf99bb1 J |
470 | |
471 | try { | |
af43bd37 | 472 | $schemaManager = $connection->getSchemaManager(); |
0bf99bb1 | 473 | } catch (\Exception $exception) { |
54a2241e | 474 | // mysql & sqlite |
0bf99bb1 J |
475 | if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) { |
476 | return false; | |
477 | } | |
478 | ||
54a2241e JB |
479 | // pgsql |
480 | if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) { | |
481 | return false; | |
482 | } | |
483 | ||
0bf99bb1 J |
484 | throw $exception; |
485 | } | |
486 | ||
732c2ad8 | 487 | // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite |
f62c3faf | 488 | if ('sqlite' === $schemaManager->getDatabasePlatform()->getName()) { |
732c2ad8 J |
489 | $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams(); |
490 | ||
491 | if (isset($params['path']) && file_exists($params['path'])) { | |
492 | return true; | |
493 | } | |
494 | ||
495 | return false; | |
496 | } | |
497 | ||
69c21157 JB |
498 | try { |
499 | return in_array($databaseName, $schemaManager->listDatabases()); | |
443cff98 | 500 | } catch (\Doctrine\DBAL\Exception\DriverException $e) { |
69c21157 JB |
501 | // it means we weren't able to get database list, assume the database doesn't exist |
502 | ||
503 | return false; | |
504 | } | |
0bf99bb1 J |
505 | } |
506 | ||
507 | /** | |
164bd801 | 508 | * Check if the schema is already created. |
4346a860 | 509 | * If we found at least oen table, it means the schema exists. |
0bf99bb1 | 510 | * |
4346a860 | 511 | * @return bool |
0bf99bb1 J |
512 | */ |
513 | private function isSchemaPresent() | |
514 | { | |
515 | $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager(); | |
516 | ||
164bd801 | 517 | return count($schemaManager->listTableNames()) > 0 ? true : false; |
0bf99bb1 | 518 | } |
2e45e7be | 519 | } |