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