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