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