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