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