3 namespace Wallabag\CoreBundle\Command
;
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
;
19 class InstallCommand
extends ContainerAwareCommand
24 protected $defaultInput;
27 * @var OutputInterface
29 protected $defaultOutput;
34 protected $functionExists = [
39 protected function configure()
42 ->setName('wallabag:install')
43 ->setDescription('wallabag installer.')
47 InputOption
::VALUE_NONE
,
48 'Reset current database'
53 protected function execute(InputInterface
$input, OutputInterface
$output)
55 $this->defaultInput
= $input;
56 $this->defaultOutput
= $output;
58 $output->writeln('<info>Installing wallabag...</info>');
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>');
72 protected function checkRequirements()
74 $this->defaultOutput
->writeln('<info><comment>Step 1 of 4.</comment> Checking system requirements.</info>');
78 // testing if database driver exists
80 $label = '<comment>PDO Driver (%s)</comment>';
81 $status = '<info>OK!</info>';
84 if (!extension_loaded($this->getContainer()->getParameter('database_driver'))) {
86 $status = '<error>ERROR!</error>';
87 $help = 'Database driver "'.$this->getContainer()->getParameter('database_driver').'" is not installed.';
90 $rows[] = [sprintf($label, $this->getContainer()->getParameter('database_driver')), $status, $help];
92 // testing if connection to the database can be etablished
93 $label = '<comment>Database connection</comment>';
94 $status = '<info>OK!</info>';
98 $conn = $this->getContainer()->get('doctrine')->getManager()->getConnection();
100 } catch (\Exception
$e) {
101 if (false === strpos($e->getMessage(), 'Unknown database')
102 && false === strpos($e->getMessage(), 'database "'.$this->getContainer()->getParameter('database_name').'" does not exist')) {
104 $status = '<error>ERROR!</error>';
105 $help = 'Can\'t connect to the database: '.$e->getMessage();
109 $rows[] = [$label, $status, $help];
111 // now check if MySQL isn't too old to handle utf8mb4
112 if ($conn->isConnected() && $conn->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\MySqlPlatform
) {
113 $version = $conn->query('select version()')->fetchColumn();
114 $minimalVersion = '5.5.4';
116 if (false === version_compare($version, $minimalVersion, '>')) {
119 '<comment>Database version</comment>',
120 '<error>ERROR!</error>',
121 'Your MySQL version ('.$version.') is too old, consider upgrading ('.$minimalVersion.'+).',
126 foreach ($this->functionExists
as $functionRequired) {
127 $label = '<comment>'.$functionRequired.'</comment>';
128 $status = '<info>OK!</info>';
131 if (!function_exists($functionRequired)) {
133 $status = '<error>ERROR!</error>';
134 $help = 'You need the '.$functionRequired.' function activated';
137 $rows[] = [$label, $status, $help];
140 $table = new Table($this->defaultOutput
);
142 ->setHeaders(['Checked', 'Status', 'Recommendation'])
147 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
150 $this->defaultOutput
->writeln('<info>Success! Your system can run wallabag properly.</info>');
152 $this->defaultOutput
->writeln('');
157 protected function setupDatabase()
159 $this->defaultOutput
->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
161 // user want to reset everything? Don't care about what is already here
162 if (true === $this->defaultInput
->getOption('reset')) {
163 $this->defaultOutput
->writeln('Droping database, creating database and schema, clearing the cache');
166 ->runCommand('doctrine:database:drop', ['--force' => true])
167 ->runCommand('doctrine:database:create')
168 ->runCommand('doctrine:schema:create')
169 ->runCommand('cache:clear')
172 $this->defaultOutput
->writeln('');
177 if (!$this->isDatabasePresent()) {
178 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
181 ->runCommand('doctrine:database:create')
182 ->runCommand('doctrine:schema:create')
183 ->runCommand('cache:clear')
186 $this->defaultOutput
->writeln('');
191 $questionHelper = $this->getHelper('question');
192 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
194 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
195 $this->defaultOutput
->writeln('Droping database, creating database and schema');
198 ->runCommand('doctrine:database:drop', ['--force' => true])
199 ->runCommand('doctrine:database:create')
200 ->runCommand('doctrine:schema:create')
202 } elseif ($this->isSchemaPresent()) {
203 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
204 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
205 $this->defaultOutput
->writeln('Droping schema and creating schema');
208 ->runCommand('doctrine:schema:drop', ['--force' => true])
209 ->runCommand('doctrine:schema:create')
213 $this->defaultOutput
->writeln('Creating schema');
216 ->runCommand('doctrine:schema:create')
220 $this->defaultOutput
->writeln('Clearing the cache');
221 $this->runCommand('cache:clear');
223 $this->defaultOutput
->writeln('');
228 protected function setupAdmin()
230 $this->defaultOutput
->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
232 $questionHelper = $this->getHelperSet()->get('question');
233 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true);
235 if (!$questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
239 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
241 $userManager = $this->getContainer()->get('fos_user.user_manager');
242 $user = $userManager->createUser();
244 $question = new Question('Username (default: wallabag) :', 'wallabag');
245 $user->setUsername($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
247 $question = new Question('Password (default: wallabag) :', 'wallabag');
248 $user->setPlainPassword($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
250 $question = new Question('Email:', '');
251 $user->setEmail($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
253 $user->setEnabled(true);
254 $user->addRole('ROLE_SUPER_ADMIN');
258 // dispatch a created event so the associated config will be created
259 $event = new UserEvent($user);
260 $this->getContainer()->get('event_dispatcher')->dispatch(FOSUserEvents
::USER_CREATED
, $event);
262 $this->defaultOutput
->writeln('');
267 protected function setupConfig()
269 $this->defaultOutput
->writeln('<info><comment>Step 4 of 4.</comment> Config setup.</info>');
270 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
272 // cleanup before insert new stuff
273 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
277 'name' => 'share_public',
279 'section' => 'entry',
284 'section' => 'entry',
287 'name' => 'share_diaspora',
289 'section' => 'entry',
292 'name' => 'diaspora_url',
293 'value' => 'http://diasporapod.com',
294 'section' => 'entry',
297 'name' => 'share_unmark',
299 'section' => 'entry',
302 'name' => 'unmark_url',
303 'value' => 'https://unmark.it',
304 'section' => 'entry',
307 'name' => 'share_shaarli',
309 'section' => 'entry',
312 'name' => 'shaarli_url',
313 'value' => 'http://myshaarli.com',
314 'section' => 'entry',
317 'name' => 'share_mail',
319 'section' => 'entry',
322 'name' => 'share_twitter',
324 'section' => 'entry',
327 'name' => 'export_epub',
329 'section' => 'export',
332 'name' => 'export_mobi',
334 'section' => 'export',
337 'name' => 'export_pdf',
339 'section' => 'export',
342 'name' => 'export_csv',
344 'section' => 'export',
347 'name' => 'export_json',
349 'section' => 'export',
352 'name' => 'export_txt',
354 'section' => 'export',
357 'name' => 'export_xml',
359 'section' => 'export',
362 'name' => 'import_with_redis',
364 'section' => 'import',
367 'name' => 'import_with_rabbitmq',
369 'section' => 'import',
372 'name' => 'show_printlink',
374 'section' => 'entry',
377 'name' => 'wallabag_support_url',
378 'value' => 'https://www.wallabag.org/pages/support.html',
382 'name' => 'wallabag_url',
387 'name' => 'piwik_enabled',
389 'section' => 'analytics',
392 'name' => 'piwik_host',
393 'value' => 'v2.wallabag.org',
394 'section' => 'analytics',
397 'name' => 'piwik_site_id',
399 'section' => 'analytics',
402 'name' => 'demo_mode_enabled',
407 'name' => 'demo_mode_username',
408 'value' => 'wallabag',
412 'name' => 'download_images_enabled',
418 foreach ($settings as $setting) {
419 $newSetting = new Setting();
420 $newSetting->setName($setting['name']);
421 $newSetting->setValue($setting['value']);
422 $newSetting->setSection($setting['section']);
423 $em->persist($newSetting);
428 $this->defaultOutput
->writeln('');
436 * @param string $command
437 * @param array $parameters Parameters to this command (usually 'force' => true)
439 protected function runCommand($command, $parameters = [])
441 $parameters = array_merge(
442 ['command' => $command],
445 '--no-debug' => true,
446 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
450 if ($this->defaultInput
->getOption('no-interaction')) {
451 $parameters = array_merge($parameters, ['--no-interaction' => true]);
454 $this->getApplication()->setAutoExit(false);
456 $output = new BufferedOutput();
457 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), $output);
459 if (0 !== $exitCode) {
460 $this->getApplication()->setAutoExit(true);
462 $this->defaultOutput
->writeln('');
463 $this->defaultOutput
->writeln('<error>The command "'.$command.'" generates some errors: </error>');
464 $this->defaultOutput
->writeln($output->fetch());
469 // PDO does not always close the connection after Doctrine commands.
470 // See https://github.com/symfony/symfony/issues/11750.
471 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
477 * Check if the database already exists.
481 private function isDatabasePresent()
483 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
484 $databaseName = $connection->getDatabase();
487 $schemaManager = $connection->getSchemaManager();
488 } catch (\Exception
$exception) {
490 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
495 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
502 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
503 if ('sqlite' === $schemaManager->getDatabasePlatform()->getName()) {
504 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
506 if (isset($params['path']) && file_exists($params['path'])) {
514 return in_array($databaseName, $schemaManager->listDatabases());
515 } catch (\Doctrine\DBAL\Exception\DriverException
$e) {
516 // it means we weren't able to get database list, assume the database doesn't exist
523 * Check if the schema is already created.
524 * If we found at least oen table, it means the schema exists.
528 private function isSchemaPresent()
530 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
532 return count($schemaManager->listTableNames()) > 0 ? true : false;