]>
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() | |
63 | ->setupAsset() | |
2e45e7be J |
64 | ; |
65 | ||
66 | $output->writeln('<info>Wallabag has been successfully installed.</info>'); | |
0c6845a9 | 67 | $output->writeln('<comment>Just execute `php bin/console server:run --env=prod` for using wallabag: http://localhost:8000</comment>'); |
2e45e7be J |
68 | } |
69 | ||
0bf99bb1 | 70 | protected function checkRequirements() |
2e45e7be | 71 | { |
0bf99bb1 | 72 | $this->defaultOutput->writeln('<info><comment>Step 1 of 4.</comment> Checking system requirements.</info>'); |
2e45e7be J |
73 | |
74 | $fulfilled = true; | |
75 | ||
db847ca0 TC |
76 | $label = '<comment>PDO Drivers</comment>'; |
77 | if (extension_loaded('pdo_sqlite') || extension_loaded('pdo_mysql') || extension_loaded('pdo_pgsql')) { | |
78 | $status = '<info>OK!</info>'; | |
79 | $help = ''; | |
80 | } else { | |
81 | $fulfilled = false; | |
82 | $status = '<error>ERROR!</error>'; | |
83 | $help = 'Needs one of sqlite, mysql or pgsql PDO drivers'; | |
84 | } | |
85 | ||
4d0ec0e7 JB |
86 | $rows = []; |
87 | $rows[] = [$label, $status, $help]; | |
db847ca0 TC |
88 | |
89 | foreach ($this->functionExists as $functionRequired) { | |
90 | $label = '<comment>'.$functionRequired.'</comment>'; | |
91 | ||
92 | if (function_exists($functionRequired)) { | |
fc6020b2 NL |
93 | $status = '<info>OK!</info>'; |
94 | $help = ''; | |
95 | } else { | |
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 | { |
0bf99bb1 | 123 | $this->defaultOutput->writeln('<info><comment>Step 2 of 4.</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 | { |
0bf99bb1 J |
194 | $this->defaultOutput->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>'); |
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 | |
d6ba77e8 JB |
231 | // cleanup before insert new stuff |
232 | $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute(); | |
233 | ||
234 | $settings = [ | |
235 | [ | |
236 | 'name' => 'download_pictures', | |
237 | 'value' => '1', | |
238 | 'section' => 'entry', | |
239 | ], | |
240 | [ | |
241 | 'name' => 'carrot', | |
242 | 'value' => '1', | |
243 | 'section' => 'entry', | |
244 | ], | |
245 | [ | |
246 | 'name' => 'share_diaspora', | |
247 | 'value' => '1', | |
248 | 'section' => 'entry', | |
249 | ], | |
250 | [ | |
251 | 'name' => 'diaspora_url', | |
252 | 'value' => 'http://diasporapod.com', | |
253 | 'section' => 'entry', | |
254 | ], | |
255 | [ | |
256 | 'name' => 'share_shaarli', | |
257 | 'value' => '1', | |
258 | 'section' => 'entry', | |
259 | ], | |
260 | [ | |
261 | 'name' => 'shaarli_url', | |
262 | 'value' => 'http://myshaarli.com', | |
263 | 'section' => 'entry', | |
264 | ], | |
265 | [ | |
266 | 'name' => 'share_mail', | |
267 | 'value' => '1', | |
268 | 'section' => 'entry', | |
269 | ], | |
270 | [ | |
271 | 'name' => 'share_twitter', | |
272 | 'value' => '1', | |
273 | 'section' => 'entry', | |
274 | ], | |
275 | [ | |
276 | 'name' => 'export_epub', | |
277 | 'value' => '1', | |
278 | 'section' => 'export', | |
279 | ], | |
280 | [ | |
281 | 'name' => 'export_mobi', | |
282 | 'value' => '1', | |
283 | 'section' => 'export', | |
284 | ], | |
285 | [ | |
286 | 'name' => 'export_pdf', | |
287 | 'value' => '1', | |
288 | 'section' => 'export', | |
289 | ], | |
a74a6ca2 JB |
290 | [ |
291 | 'name' => 'export_csv', | |
292 | 'value' => '1', | |
293 | 'section' => 'export', | |
294 | ], | |
295 | [ | |
296 | 'name' => 'export_json', | |
297 | 'value' => '1', | |
298 | 'section' => 'export', | |
299 | ], | |
300 | [ | |
301 | 'name' => 'export_txt', | |
302 | 'value' => '1', | |
303 | 'section' => 'export', | |
304 | ], | |
305 | [ | |
306 | 'name' => 'export_xml', | |
307 | 'value' => '1', | |
308 | 'section' => 'export', | |
309 | ], | |
d6ba77e8 JB |
310 | [ |
311 | 'name' => 'pocket_consumer_key', | |
1c7d6664 | 312 | 'value' => null, |
d6ba77e8 JB |
313 | 'section' => 'import', |
314 | ], | |
315 | [ | |
316 | 'name' => 'show_printlink', | |
317 | 'value' => '1', | |
318 | 'section' => 'entry', | |
319 | ], | |
320 | [ | |
321 | 'name' => 'wallabag_support_url', | |
322 | 'value' => 'https://www.wallabag.org/pages/support.html', | |
323 | 'section' => 'misc', | |
324 | ], | |
325 | [ | |
326 | 'name' => 'wallabag_url', | |
327 | 'value' => 'http://v2.wallabag.org', | |
328 | 'section' => 'misc', | |
329 | ], | |
07643dde NL |
330 | [ |
331 | 'name' => 'piwik_enabled', | |
332 | 'value' => '0', | |
333 | 'section' => 'analytics', | |
334 | ], | |
335 | [ | |
336 | 'name' => 'piwik_host', | |
337 | 'value' => 'http://v2.wallabag.org', | |
338 | 'section' => 'analytics', | |
339 | ], | |
340 | [ | |
341 | 'name' => 'piwik_site_id', | |
342 | 'value' => '1', | |
343 | 'section' => 'analytics', | |
344 | ], | |
a4f42c59 JB |
345 | [ |
346 | 'name' => 'demo_mode_enabled', | |
347 | 'value' => '0', | |
348 | 'section' => 'misc', | |
349 | ], | |
350 | [ | |
351 | 'name' => 'demo_mode_username', | |
352 | 'value' => 'wallabag', | |
353 | 'section' => 'misc', | |
354 | ], | |
d6ba77e8 JB |
355 | ]; |
356 | ||
357 | foreach ($settings as $setting) { | |
358 | $newSetting = new Setting(); | |
359 | $newSetting->setName($setting['name']); | |
360 | $newSetting->setValue($setting['value']); | |
361 | $newSetting->setSection($setting['section']); | |
362 | $em->persist($newSetting); | |
363 | } | |
364 | ||
0bf99bb1 J |
365 | $em->flush(); |
366 | ||
367 | $this->defaultOutput->writeln(''); | |
368 | ||
369 | return $this; | |
2e45e7be J |
370 | } |
371 | ||
0bf99bb1 | 372 | protected function setupAsset() |
2e45e7be | 373 | { |
0bf99bb1 J |
374 | $this->defaultOutput->writeln('<info><comment>Step 4 of 4.</comment> Installing assets.</info>'); |
375 | ||
2e45e7be | 376 | $this |
0bf99bb1 J |
377 | ->runCommand('assets:install') |
378 | ->runCommand('assetic:dump') | |
2e45e7be J |
379 | ; |
380 | ||
0bf99bb1 J |
381 | $this->defaultOutput->writeln(''); |
382 | ||
383 | return $this; | |
384 | } | |
385 | ||
386 | /** | |
4346a860 | 387 | * Run a command. |
0bf99bb1 J |
388 | * |
389 | * @param string $command | |
390 | * @param array $parameters Parameters to this command (usually 'force' => true) | |
391 | */ | |
4d0ec0e7 | 392 | protected function runCommand($command, $parameters = []) |
0bf99bb1 J |
393 | { |
394 | $parameters = array_merge( | |
4d0ec0e7 | 395 | ['command' => $command], |
0bf99bb1 | 396 | $parameters, |
4d0ec0e7 | 397 | [ |
0bf99bb1 J |
398 | '--no-debug' => true, |
399 | '--env' => $this->defaultInput->getOption('env') ?: 'dev', | |
4d0ec0e7 | 400 | ] |
0bf99bb1 J |
401 | ); |
402 | ||
403 | if ($this->defaultInput->getOption('no-interaction')) { | |
4d0ec0e7 | 404 | $parameters = array_merge($parameters, ['--no-interaction' => true]); |
0bf99bb1 J |
405 | } |
406 | ||
407 | $this->getApplication()->setAutoExit(false); | |
408 | $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput()); | |
409 | ||
410 | if (0 !== $exitCode) { | |
411 | $this->getApplication()->setAutoExit(true); | |
412 | ||
413 | $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode); | |
414 | $this->defaultOutput->writeln("<error>$errorMessage</error>"); | |
415 | $exception = new \Exception($errorMessage, $exitCode); | |
416 | ||
417 | throw $exception; | |
418 | } | |
419 | ||
420 | // PDO does not always close the connection after Doctrine commands. | |
421 | // See https://github.com/symfony/symfony/issues/11750. | |
422 | $this->getContainer()->get('doctrine')->getManager()->getConnection()->close(); | |
423 | ||
2e45e7be J |
424 | return $this; |
425 | } | |
0bf99bb1 J |
426 | |
427 | /** | |
4346a860 | 428 | * Check if the database already exists. |
0bf99bb1 | 429 | * |
4346a860 | 430 | * @return bool |
0bf99bb1 J |
431 | */ |
432 | private function isDatabasePresent() | |
433 | { | |
af43bd37 JB |
434 | $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection(); |
435 | $databaseName = $connection->getDatabase(); | |
0bf99bb1 J |
436 | |
437 | try { | |
af43bd37 | 438 | $schemaManager = $connection->getSchemaManager(); |
0bf99bb1 | 439 | } catch (\Exception $exception) { |
54a2241e | 440 | // mysql & sqlite |
0bf99bb1 J |
441 | if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) { |
442 | return false; | |
443 | } | |
444 | ||
54a2241e JB |
445 | // pgsql |
446 | if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) { | |
447 | return false; | |
448 | } | |
449 | ||
0bf99bb1 J |
450 | throw $exception; |
451 | } | |
452 | ||
732c2ad8 J |
453 | // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite |
454 | if ('sqlite' == $schemaManager->getDatabasePlatform()->getName()) { | |
455 | $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams(); | |
456 | ||
457 | if (isset($params['path']) && file_exists($params['path'])) { | |
458 | return true; | |
459 | } | |
460 | ||
461 | return false; | |
462 | } | |
463 | ||
0bf99bb1 J |
464 | return in_array($databaseName, $schemaManager->listDatabases()); |
465 | } | |
466 | ||
467 | /** | |
164bd801 | 468 | * Check if the schema is already created. |
4346a860 | 469 | * If we found at least oen table, it means the schema exists. |
0bf99bb1 | 470 | * |
4346a860 | 471 | * @return bool |
0bf99bb1 J |
472 | */ |
473 | private function isSchemaPresent() | |
474 | { | |
475 | $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager(); | |
476 | ||
164bd801 | 477 | return count($schemaManager->listTableNames()) > 0 ? true : false; |
0bf99bb1 | 478 | } |
2e45e7be | 479 | } |