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