]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Command; | |
4 | ||
5 | use FOS\UserBundle\Event\UserEvent; | |
6 | use FOS\UserBundle\FOSUserEvents; | |
7 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
8 | use Symfony\Component\Console\Helper\Table; | |
9 | use Symfony\Component\Console\Input\ArrayInput; | |
10 | use Symfony\Component\Console\Input\InputInterface; | |
11 | use Symfony\Component\Console\Input\InputOption; | |
12 | use Symfony\Component\Console\Output\BufferedOutput; | |
13 | use Symfony\Component\Console\Output\OutputInterface; | |
14 | use Symfony\Component\Console\Question\ConfirmationQuestion; | |
15 | use Symfony\Component\Console\Question\Question; | |
16 | use Wallabag\CoreBundle\Entity\Config; | |
17 | use Craue\ConfigBundle\Entity\Setting; | |
18 | ||
19 | class InstallCommand extends ContainerAwareCommand | |
20 | { | |
21 | /** | |
22 | * @var InputInterface | |
23 | */ | |
24 | protected $defaultInput; | |
25 | ||
26 | /** | |
27 | * @var OutputInterface | |
28 | */ | |
29 | protected $defaultOutput; | |
30 | ||
31 | /** | |
32 | * @var array | |
33 | */ | |
34 | protected $functionExists = [ | |
35 | 'curl_exec', | |
36 | 'curl_multi_init', | |
37 | ]; | |
38 | ||
39 | protected function configure() | |
40 | { | |
41 | $this | |
42 | ->setName('wallabag:install') | |
43 | ->setDescription('Wallabag installer.') | |
44 | ->addOption( | |
45 | 'reset', | |
46 | null, | |
47 | InputOption::VALUE_NONE, | |
48 | 'Reset current database' | |
49 | ) | |
50 | ; | |
51 | } | |
52 | ||
53 | protected function execute(InputInterface $input, OutputInterface $output) | |
54 | { | |
55 | $this->defaultInput = $input; | |
56 | $this->defaultOutput = $output; | |
57 | ||
58 | $output->writeln('<info>Installing Wallabag...</info>'); | |
59 | $output->writeln(''); | |
60 | ||
61 | $this | |
62 | ->checkRequirements() | |
63 | ->setupDatabase() | |
64 | ->setupAdmin() | |
65 | ->setupConfig() | |
66 | ; | |
67 | ||
68 | $output->writeln('<info>Wallabag has been successfully installed.</info>'); | |
69 | $output->writeln('<comment>Just execute `php bin/console server:run --env=prod` for using wallabag: http://localhost:8000</comment>'); | |
70 | } | |
71 | ||
72 | protected function checkRequirements() | |
73 | { | |
74 | $this->defaultOutput->writeln('<info><comment>Step 1 of 4.</comment> Checking system requirements.</info>'); | |
75 | ||
76 | $rows = []; | |
77 | ||
78 | // testing if database driver exists | |
79 | $fulfilled = true; | |
80 | $label = '<comment>PDO Driver (%s)</comment>'; | |
81 | $status = '<info>OK!</info>'; | |
82 | $help = ''; | |
83 | ||
84 | if (!extension_loaded($this->getContainer()->getParameter('database_driver'))) { | |
85 | $fulfilled = false; | |
86 | $status = '<error>ERROR!</error>'; | |
87 | $help = 'Database driver "'.$this->getContainer()->getParameter('database_driver').'" is not installed.'; | |
88 | } | |
89 | ||
90 | $rows[] = [sprintf($label, $this->getContainer()->getParameter('database_driver')), $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) { | |
100 | if (false === strpos($e->getMessage(), 'Unknown database') | |
101 | && false === strpos($e->getMessage(), 'database "'.$this->getContainer()->getParameter('database_name').'" does not exist')) { | |
102 | $fulfilled = false; | |
103 | $status = '<error>ERROR!</error>'; | |
104 | $help = 'Can\'t connect to the database: '.$e->getMessage(); | |
105 | } | |
106 | } | |
107 | ||
108 | $rows[] = [$label, $status, $help]; | |
109 | ||
110 | foreach ($this->functionExists as $functionRequired) { | |
111 | $label = '<comment>'.$functionRequired.'</comment>'; | |
112 | $status = '<info>OK!</info>'; | |
113 | $help = ''; | |
114 | ||
115 | if (!function_exists($functionRequired)) { | |
116 | $fulfilled = false; | |
117 | $status = '<error>ERROR!</error>'; | |
118 | $help = 'You need the '.$functionRequired.' function activated'; | |
119 | } | |
120 | ||
121 | $rows[] = [$label, $status, $help]; | |
122 | } | |
123 | ||
124 | $table = new Table($this->defaultOutput); | |
125 | $table | |
126 | ->setHeaders(['Checked', 'Status', 'Recommendation']) | |
127 | ->setRows($rows) | |
128 | ->render(); | |
129 | ||
130 | if (!$fulfilled) { | |
131 | throw new \RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.'); | |
132 | } | |
133 | ||
134 | $this->defaultOutput->writeln('<info>Success! Your system can run Wallabag properly.</info>'); | |
135 | ||
136 | $this->defaultOutput->writeln(''); | |
137 | ||
138 | return $this; | |
139 | } | |
140 | ||
141 | protected function setupDatabase() | |
142 | { | |
143 | $this->defaultOutput->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>'); | |
144 | ||
145 | // user want to reset everything? Don't care about what is already here | |
146 | if (true === $this->defaultInput->getOption('reset')) { | |
147 | $this->defaultOutput->writeln('Droping database, creating database and schema, clearing the cache'); | |
148 | ||
149 | $this | |
150 | ->runCommand('doctrine:database:drop', ['--force' => true]) | |
151 | ->runCommand('doctrine:database:create') | |
152 | ->runCommand('doctrine:schema:create') | |
153 | ->runCommand('cache:clear') | |
154 | ; | |
155 | ||
156 | $this->defaultOutput->writeln(''); | |
157 | ||
158 | return $this; | |
159 | } | |
160 | ||
161 | if (!$this->isDatabasePresent()) { | |
162 | $this->defaultOutput->writeln('Creating database and schema, clearing the cache'); | |
163 | ||
164 | $this | |
165 | ->runCommand('doctrine:database:create') | |
166 | ->runCommand('doctrine:schema:create') | |
167 | ->runCommand('cache:clear') | |
168 | ; | |
169 | ||
170 | $this->defaultOutput->writeln(''); | |
171 | ||
172 | return $this; | |
173 | } | |
174 | ||
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); | |
177 | ||
178 | if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) { | |
179 | $this->defaultOutput->writeln('Droping database, creating database and schema'); | |
180 | ||
181 | $this | |
182 | ->runCommand('doctrine:database:drop', ['--force' => true]) | |
183 | ->runCommand('doctrine:database:create') | |
184 | ->runCommand('doctrine:schema:create') | |
185 | ; | |
186 | } elseif ($this->isSchemaPresent()) { | |
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)) { | |
189 | $this->defaultOutput->writeln('Droping schema and creating schema'); | |
190 | ||
191 | $this | |
192 | ->runCommand('doctrine:schema:drop', ['--force' => true]) | |
193 | ->runCommand('doctrine:schema:create') | |
194 | ; | |
195 | } | |
196 | } else { | |
197 | $this->defaultOutput->writeln('Creating schema'); | |
198 | ||
199 | $this | |
200 | ->runCommand('doctrine:schema:create') | |
201 | ; | |
202 | } | |
203 | ||
204 | $this->defaultOutput->writeln('Clearing the cache'); | |
205 | $this->runCommand('cache:clear'); | |
206 | ||
207 | $this->defaultOutput->writeln(''); | |
208 | ||
209 | return $this; | |
210 | } | |
211 | ||
212 | protected function setupAdmin() | |
213 | { | |
214 | $this->defaultOutput->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>'); | |
215 | ||
216 | $questionHelper = $this->getHelperSet()->get('question'); | |
217 | $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true); | |
218 | ||
219 | if (!$questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) { | |
220 | return $this; | |
221 | } | |
222 | ||
223 | $em = $this->getContainer()->get('doctrine.orm.entity_manager'); | |
224 | ||
225 | $userManager = $this->getContainer()->get('fos_user.user_manager'); | |
226 | $user = $userManager->createUser(); | |
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 | ||
237 | $user->setEnabled(true); | |
238 | $user->addRole('ROLE_SUPER_ADMIN'); | |
239 | ||
240 | $em->persist($user); | |
241 | ||
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); | |
245 | ||
246 | $this->defaultOutput->writeln(''); | |
247 | ||
248 | return $this; | |
249 | } | |
250 | ||
251 | protected function setupConfig() | |
252 | { | |
253 | $this->defaultOutput->writeln('<info><comment>Step 4 of 4.</comment> Config setup.</info>'); | |
254 | $em = $this->getContainer()->get('doctrine.orm.entity_manager'); | |
255 | ||
256 | // cleanup before insert new stuff | |
257 | $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute(); | |
258 | ||
259 | $settings = [ | |
260 | [ | |
261 | 'name' => 'share_public', | |
262 | 'value' => '1', | |
263 | 'section' => 'entry', | |
264 | ], | |
265 | [ | |
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 | ], | |
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 | ], | |
335 | [ | |
336 | 'name' => 'import_with_redis', | |
337 | 'value' => '0', | |
338 | 'section' => 'import', | |
339 | ], | |
340 | [ | |
341 | 'name' => 'import_with_rabbitmq', | |
342 | 'value' => '0', | |
343 | 'section' => 'import', | |
344 | ], | |
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 | ], | |
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 | ], | |
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 | ], | |
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 | ||
395 | $em->flush(); | |
396 | ||
397 | $this->defaultOutput->writeln(''); | |
398 | ||
399 | return $this; | |
400 | } | |
401 | ||
402 | /** | |
403 | * Run a command. | |
404 | * | |
405 | * @param string $command | |
406 | * @param array $parameters Parameters to this command (usually 'force' => true) | |
407 | */ | |
408 | protected function runCommand($command, $parameters = []) | |
409 | { | |
410 | $parameters = array_merge( | |
411 | ['command' => $command], | |
412 | $parameters, | |
413 | [ | |
414 | '--no-debug' => true, | |
415 | '--env' => $this->defaultInput->getOption('env') ?: 'dev', | |
416 | ] | |
417 | ); | |
418 | ||
419 | if ($this->defaultInput->getOption('no-interaction')) { | |
420 | $parameters = array_merge($parameters, ['--no-interaction' => true]); | |
421 | } | |
422 | ||
423 | $this->getApplication()->setAutoExit(false); | |
424 | ||
425 | $output = new BufferedOutput(); | |
426 | $exitCode = $this->getApplication()->run(new ArrayInput($parameters), $output); | |
427 | ||
428 | if (0 !== $exitCode) { | |
429 | $this->getApplication()->setAutoExit(true); | |
430 | ||
431 | $this->defaultOutput->writeln(''); | |
432 | $this->defaultOutput->writeln('<error>The command "'.$command.'" generates some errors: </error>'); | |
433 | $this->defaultOutput->writeln($output->fetch()); | |
434 | ||
435 | die(); | |
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 | ||
442 | return $this; | |
443 | } | |
444 | ||
445 | /** | |
446 | * Check if the database already exists. | |
447 | * | |
448 | * @return bool | |
449 | */ | |
450 | private function isDatabasePresent() | |
451 | { | |
452 | $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection(); | |
453 | $databaseName = $connection->getDatabase(); | |
454 | ||
455 | try { | |
456 | $schemaManager = $connection->getSchemaManager(); | |
457 | } catch (\Exception $exception) { | |
458 | // mysql & sqlite | |
459 | if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) { | |
460 | return false; | |
461 | } | |
462 | ||
463 | // pgsql | |
464 | if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) { | |
465 | return false; | |
466 | } | |
467 | ||
468 | throw $exception; | |
469 | } | |
470 | ||
471 | // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite | |
472 | if ('sqlite' === $schemaManager->getDatabasePlatform()->getName()) { | |
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 | ||
482 | try { | |
483 | return in_array($databaseName, $schemaManager->listDatabases()); | |
484 | } catch (\Doctrine\DBAL\Exception\DriverException $e) { | |
485 | // it means we weren't able to get database list, assume the database doesn't exist | |
486 | ||
487 | return false; | |
488 | } | |
489 | } | |
490 | ||
491 | /** | |
492 | * Check if the schema is already created. | |
493 | * If we found at least oen table, it means the schema exists. | |
494 | * | |
495 | * @return bool | |
496 | */ | |
497 | private function isSchemaPresent() | |
498 | { | |
499 | $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager(); | |
500 | ||
501 | return count($schemaManager->listTableNames()) > 0 ? true : false; | |
502 | } | |
503 | } |