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