]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Command/InstallCommand.php
Replace RabbitMQ injection with CraueConfiguration
[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()
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 {
5ecdfcd0 72 $this->defaultOutput->writeln('<info><comment>Step 1 of 4.</comment> Checking system requirements.</info>');
2e45e7be
J
73
74 $fulfilled = true;
75
c61b68e8 76 $label = '<comment>PDO Driver</comment>';
0e49487b
JB
77 $status = '<info>OK!</info>';
78 $help = '';
c61b68e8
JB
79
80 if (!extension_loaded($this->getContainer()->getParameter('database_driver'))) {
db847ca0
TC
81 $fulfilled = false;
82 $status = '<error>ERROR!</error>';
c61b68e8 83 $help = 'Database driver "'.$this->getContainer()->getParameter('database_driver').'" is not installed.';
db847ca0
TC
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>';
0e49487b
JB
91 $status = '<info>OK!</info>';
92 $help = '';
db847ca0 93
0e49487b 94 if (!function_exists($functionRequired)) {
fc6020b2
NL
95 $fulfilled = false;
96 $status = '<error>ERROR!</error>';
f2fcb65b 97 $help = 'You need the '.$functionRequired.' function activated';
fc6020b2 98 }
db847ca0 99
4d0ec0e7 100 $rows[] = [$label, $status, $help];
2e45e7be 101 }
0bf99bb1 102
78507d28
JB
103 $table = new Table($this->defaultOutput);
104 $table
4d0ec0e7 105 ->setHeaders(['Checked', 'Status', 'Recommendation'])
0bf99bb1 106 ->setRows($rows)
78507d28 107 ->render();
2e45e7be
J
108
109 if (!$fulfilled) {
0bf99bb1 110 throw new \RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
2e45e7be
J
111 }
112
8a493541
JB
113 $this->defaultOutput->writeln('<info>Success! Your system can run Wallabag properly.</info>');
114
0bf99bb1 115 $this->defaultOutput->writeln('');
2e45e7be
J
116
117 return $this;
118 }
119
0bf99bb1 120 protected function setupDatabase()
2e45e7be 121 {
5ecdfcd0 122 $this->defaultOutput->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
2e45e7be 123
0bf99bb1
J
124 // user want to reset everything? Don't care about what is already here
125 if (true === $this->defaultInput->getOption('reset')) {
d5027625 126 $this->defaultOutput->writeln('Droping database, creating database and schema, clearing the cache');
2e45e7be 127
0bf99bb1 128 $this
4d0ec0e7 129 ->runCommand('doctrine:database:drop', ['--force' => true])
0bf99bb1
J
130 ->runCommand('doctrine:database:create')
131 ->runCommand('doctrine:schema:create')
d5027625 132 ->runCommand('cache:clear')
0bf99bb1 133 ;
2e45e7be 134
d5027625
JB
135 $this->defaultOutput->writeln('');
136
0bf99bb1
J
137 return $this;
138 }
2e45e7be 139
0bf99bb1
J
140 if (!$this->isDatabasePresent()) {
141 $this->defaultOutput->writeln('Creating database and schema, clearing the cache');
2e45e7be 142
0bf99bb1
J
143 $this
144 ->runCommand('doctrine:database:create')
145 ->runCommand('doctrine:schema:create')
146 ->runCommand('cache:clear')
147 ;
2e45e7be 148
d5027625
JB
149 $this->defaultOutput->writeln('');
150
0bf99bb1
J
151 return $this;
152 }
2e45e7be 153
78507d28
JB
154 $questionHelper = $this->getHelper('question');
155 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
2e45e7be 156
78507d28 157 if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) {
0bf99bb1 158 $this->defaultOutput->writeln('Droping database, creating database and schema');
2e45e7be 159
0bf99bb1 160 $this
4d0ec0e7 161 ->runCommand('doctrine:database:drop', ['--force' => true])
0bf99bb1
J
162 ->runCommand('doctrine:database:create')
163 ->runCommand('doctrine:schema:create')
164 ;
165 } elseif ($this->isSchemaPresent()) {
78507d28
JB
166 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
167 if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) {
0bf99bb1 168 $this->defaultOutput->writeln('Droping schema and creating schema');
2e45e7be 169
0bf99bb1 170 $this
4d0ec0e7 171 ->runCommand('doctrine:schema:drop', ['--force' => true])
0bf99bb1
J
172 ->runCommand('doctrine:schema:create')
173 ;
174 }
2e45e7be 175 } else {
0bf99bb1
J
176 $this->defaultOutput->writeln('Creating schema');
177
178 $this
179 ->runCommand('doctrine:schema:create')
180 ;
2e45e7be
J
181 }
182
0bf99bb1
J
183 $this->defaultOutput->writeln('Clearing the cache');
184 $this->runCommand('cache:clear');
185
0bf99bb1
J
186 $this->defaultOutput->writeln('');
187
188 return $this;
2e45e7be
J
189 }
190
0bf99bb1 191 protected function setupAdmin()
2e45e7be 192 {
5ecdfcd0 193 $this->defaultOutput->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
0bf99bb1 194
78507d28 195 $questionHelper = $this->getHelperSet()->get('question');
3c39f5ac 196 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true);
0bf99bb1 197
78507d28 198 if (!$questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) {
0bf99bb1
J
199 return $this;
200 }
201
2e45e7be
J
202 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
203
ec3ce598
NL
204 $userManager = $this->getContainer()->get('fos_user.user_manager');
205 $user = $userManager->createUser();
78507d28
JB
206
207 $question = new Question('Username (default: wallabag) :', 'wallabag');
208 $user->setUsername($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question));
209
210 $question = new Question('Password (default: wallabag) :', 'wallabag');
211 $user->setPlainPassword($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question));
212
213 $question = new Question('Email:', '');
214 $user->setEmail($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question));
215
a1691859 216 $user->setEnabled(true);
8f06a8c4 217 $user->addRole('ROLE_SUPER_ADMIN');
2e45e7be
J
218
219 $em->persist($user);
220
0bd2cb1e 221 $config = new Config($user);
bc789687
JB
222 $config->setTheme($this->getContainer()->getParameter('wallabag_core.theme'));
223 $config->setItemsPerPage($this->getContainer()->getParameter('wallabag_core.items_on_page'));
224 $config->setRssLimit($this->getContainer()->getParameter('wallabag_core.rss_limit'));
bca54859 225 $config->setReadingSpeed($this->getContainer()->getParameter('wallabag_core.reading_speed'));
bc789687 226 $config->setLanguage($this->getContainer()->getParameter('wallabag_core.language'));
2e45e7be 227
4d85d7e9 228 $em->persist($config);
0bf99bb1 229
637dc4bb
JB
230 $this->defaultOutput->writeln('');
231
232 return $this;
233 }
234
235 protected function setupConfig()
236 {
5ecdfcd0 237 $this->defaultOutput->writeln('<info><comment>Step 4 of 4.</comment> Config setup.</info>');
637dc4bb
JB
238 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
239
d6ba77e8
JB
240 // cleanup before insert new stuff
241 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
242
243 $settings = [
d6ba77e8 244 [
d0545b6b
NL
245 'name' => 'share_public',
246 'value' => '1',
247 'section' => 'entry',
248 ],
249 [
d6ba77e8
JB
250 'name' => 'carrot',
251 'value' => '1',
252 'section' => 'entry',
253 ],
254 [
255 'name' => 'share_diaspora',
256 'value' => '1',
257 'section' => 'entry',
258 ],
259 [
260 'name' => 'diaspora_url',
261 'value' => 'http://diasporapod.com',
262 'section' => 'entry',
263 ],
264 [
265 'name' => 'share_shaarli',
266 'value' => '1',
267 'section' => 'entry',
268 ],
269 [
270 'name' => 'shaarli_url',
271 'value' => 'http://myshaarli.com',
272 'section' => 'entry',
273 ],
274 [
275 'name' => 'share_mail',
276 'value' => '1',
277 'section' => 'entry',
278 ],
279 [
280 'name' => 'share_twitter',
281 'value' => '1',
282 'section' => 'entry',
283 ],
284 [
285 'name' => 'export_epub',
286 'value' => '1',
287 'section' => 'export',
288 ],
289 [
290 'name' => 'export_mobi',
291 'value' => '1',
292 'section' => 'export',
293 ],
294 [
295 'name' => 'export_pdf',
296 'value' => '1',
297 'section' => 'export',
298 ],
a74a6ca2
JB
299 [
300 'name' => 'export_csv',
301 'value' => '1',
302 'section' => 'export',
303 ],
304 [
305 'name' => 'export_json',
306 'value' => '1',
307 'section' => 'export',
308 ],
309 [
310 'name' => 'export_txt',
311 'value' => '1',
312 'section' => 'export',
313 ],
314 [
315 'name' => 'export_xml',
316 'value' => '1',
317 'section' => 'export',
318 ],
d6ba77e8
JB
319 [
320 'name' => 'pocket_consumer_key',
1c7d6664 321 'value' => null,
d6ba77e8
JB
322 'section' => 'import',
323 ],
40d2a294
NL
324 [
325 'name' => 'rabbitmq',
326 'value' => '0',
327 'section' => 'import',
328 ],
d6ba77e8
JB
329 [
330 'name' => 'show_printlink',
331 'value' => '1',
332 'section' => 'entry',
333 ],
334 [
335 'name' => 'wallabag_support_url',
336 'value' => 'https://www.wallabag.org/pages/support.html',
337 'section' => 'misc',
338 ],
339 [
340 'name' => 'wallabag_url',
341 'value' => 'http://v2.wallabag.org',
342 'section' => 'misc',
343 ],
07643dde
NL
344 [
345 'name' => 'piwik_enabled',
346 'value' => '0',
347 'section' => 'analytics',
348 ],
349 [
350 'name' => 'piwik_host',
351 'value' => 'http://v2.wallabag.org',
352 'section' => 'analytics',
353 ],
354 [
355 'name' => 'piwik_site_id',
356 'value' => '1',
357 'section' => 'analytics',
358 ],
a4f42c59
JB
359 [
360 'name' => 'demo_mode_enabled',
361 'value' => '0',
362 'section' => 'misc',
363 ],
364 [
365 'name' => 'demo_mode_username',
366 'value' => 'wallabag',
367 'section' => 'misc',
368 ],
d6ba77e8
JB
369 ];
370
371 foreach ($settings as $setting) {
372 $newSetting = new Setting();
373 $newSetting->setName($setting['name']);
374 $newSetting->setValue($setting['value']);
375 $newSetting->setSection($setting['section']);
376 $em->persist($newSetting);
377 }
378
0bf99bb1
J
379 $em->flush();
380
381 $this->defaultOutput->writeln('');
382
383 return $this;
2e45e7be
J
384 }
385
0bf99bb1 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
69c21157
JB
464 try {
465 return in_array($databaseName, $schemaManager->listDatabases());
443cff98 466 } catch (\Doctrine\DBAL\Exception\DriverException $e) {
69c21157
JB
467 // it means we weren't able to get database list, assume the database doesn't exist
468
469 return false;
470 }
0bf99bb1
J
471 }
472
473 /**
164bd801 474 * Check if the schema is already created.
4346a860 475 * If we found at least oen table, it means the schema exists.
0bf99bb1 476 *
4346a860 477 * @return bool
0bf99bb1
J
478 */
479 private function isSchemaPresent()
480 {
481 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
482
164bd801 483 return count($schemaManager->listTableNames()) > 0 ? true : false;
0bf99bb1 484 }
2e45e7be 485}