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>');
69 $output->writeln('<info>wallabag has been successfully installed.</info>');
70 $output->writeln('<comment>Just execute `php bin/console server:run --env=prod` for using wallabag: http://localhost:8000</comment>');
73 protected function checkRequirements()
75 $this->defaultOutput
->writeln('<info><comment>Step 1 of 5.</comment> Checking system requirements.</info>');
76 $doctrineManager = $this->getContainer()->get('doctrine')->getManager();
80 // testing if database driver exists
82 $label = '<comment>PDO Driver (%s)</comment>';
83 $status = '<info>OK!</info>';
86 if (!extension_loaded($this->getContainer()->getParameter('database_driver'))) {
88 $status = '<error>ERROR!</error>';
89 $help = 'Database driver "'.$this->getContainer()->getParameter('database_driver').'" is not installed.';
92 $rows[] = [sprintf($label, $this->getContainer()->getParameter('database_driver')), $status, $help];
94 // testing if connection to the database can be etablished
95 $label = '<comment>Database connection</comment>';
96 $status = '<info>OK!</info>';
100 $conn = $this->getContainer()->get('doctrine')->getManager()->getConnection();
102 } catch (\Exception
$e) {
103 if (false === strpos($e->getMessage(), 'Unknown database')
104 && false === strpos($e->getMessage(), 'database "'.$this->getContainer()->getParameter('database_name').'" does not exist')) {
106 $status = '<error>ERROR!</error>';
107 $help = 'Can\'t connect to the database: '.$e->getMessage();
111 $rows[] = [$label, $status, $help];
113 // check MySQL & PostgreSQL version
114 $label = '<comment>Database version</comment>';
115 $status = '<info>OK!</info>';
118 // now check if MySQL isn't too old to handle utf8mb4
119 if ($conn->isConnected() && 'mysql' === $conn->getDatabasePlatform()->getName()) {
120 $version = $conn->query('select version()')->fetchColumn();
121 $minimalVersion = '5.5.4';
123 if (false === version_compare($version, $minimalVersion, '>')) {
125 $status = '<error>ERROR!</error>';
126 $help = 'Your MySQL version ('.$version.') is too old, consider upgrading ('.$minimalVersion.'+).';
130 // testing if PostgreSQL > 9.1
131 if ($conn->isConnected() && 'postgresql' === $conn->getDatabasePlatform()->getName()) {
132 // return version should be like "PostgreSQL 9.5.4 on x86_64-apple-darwin15.6.0, compiled by Apple LLVM version 8.0.0 (clang-800.0.38), 64-bit"
133 $version = $doctrineManager->getConnection()->query('SELECT version();')->fetchColumn();
135 preg_match('/PostgreSQL ([0-9\.]+)/i', $version, $matches);
137 if (isset($matches[1]) & version_compare($matches[1], '9.2.0', '<')) {
139 $status = '<error>ERROR!</error>';
140 $help = 'PostgreSQL should be greater than 9.1 (actual version: '.$matches[1].')';
144 $rows[] = [$label, $status, $help];
146 foreach ($this->functionExists
as $functionRequired) {
147 $label = '<comment>'.$functionRequired.'</comment>';
148 $status = '<info>OK!</info>';
151 if (!function_exists($functionRequired)) {
153 $status = '<error>ERROR!</error>';
154 $help = 'You need the '.$functionRequired.' function activated';
157 $rows[] = [$label, $status, $help];
160 $table = new Table($this->defaultOutput
);
162 ->setHeaders(['Checked', 'Status', 'Recommendation'])
167 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
170 $this->defaultOutput
->writeln('<info>Success! Your system can run wallabag properly.</info>');
172 $this->defaultOutput
->writeln('');
177 protected function setupDatabase()
179 $this->defaultOutput
->writeln('<info><comment>Step 2 of 5.</comment> Setting up database.</info>');
181 // user want to reset everything? Don't care about what is already here
182 if (true === $this->defaultInput
->getOption('reset')) {
183 $this->defaultOutput
->writeln('Dropping database, creating database and schema, clearing the cache');
186 ->runCommand('doctrine:database:drop', ['--force' => true])
187 ->runCommand('doctrine:database:create')
188 ->runCommand('doctrine:schema:create')
189 ->runCommand('cache:clear')
192 $this->defaultOutput
->writeln('');
197 if (!$this->isDatabasePresent()) {
198 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
201 ->runCommand('doctrine:database:create')
202 ->runCommand('doctrine:schema:create')
203 ->runCommand('cache:clear')
206 $this->defaultOutput
->writeln('');
211 $questionHelper = $this->getHelper('question');
212 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
214 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
215 $this->defaultOutput
->writeln('Dropping database, creating database and schema');
218 ->runCommand('doctrine:database:drop', ['--force' => true])
219 ->runCommand('doctrine:database:create')
220 ->runCommand('doctrine:schema:create')
222 } elseif ($this->isSchemaPresent()) {
223 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
224 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
225 $this->defaultOutput
->writeln('Dropping schema and creating schema');
228 ->runCommand('doctrine:schema:drop', ['--force' => true])
229 ->runCommand('doctrine:schema:create')
233 $this->defaultOutput
->writeln('Creating schema');
236 ->runCommand('doctrine:schema:create')
240 $this->defaultOutput
->writeln('Clearing the cache');
241 $this->runCommand('cache:clear');
243 $this->defaultOutput
->writeln('');
248 protected function setupAdmin()
250 $this->defaultOutput
->writeln('<info><comment>Step 3 of 5.</comment> Administration setup.</info>');
252 $questionHelper = $this->getHelperSet()->get('question');
253 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true);
255 if (!$questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
259 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
261 $userManager = $this->getContainer()->get('fos_user.user_manager');
262 $user = $userManager->createUser();
264 $question = new Question('Username (default: wallabag) :', 'wallabag');
265 $user->setUsername($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
267 $question = new Question('Password (default: wallabag) :', 'wallabag');
268 $user->setPlainPassword($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
270 $question = new Question('Email:', '');
271 $user->setEmail($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
273 $user->setEnabled(true);
274 $user->addRole('ROLE_SUPER_ADMIN');
278 // dispatch a created event so the associated config will be created
279 $event = new UserEvent($user);
280 $this->getContainer()->get('event_dispatcher')->dispatch(FOSUserEvents
::USER_CREATED
, $event);
282 $this->defaultOutput
->writeln('');
287 protected function setupConfig()
289 $this->defaultOutput
->writeln('<info><comment>Step 4 of 5.</comment> Config setup.</info>');
290 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
292 // cleanup before insert new stuff
293 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
297 'name' => 'share_public',
299 'section' => 'entry',
304 'section' => 'entry',
307 'name' => 'share_diaspora',
309 'section' => 'entry',
312 'name' => 'diaspora_url',
313 'value' => 'http://diasporapod.com',
314 'section' => 'entry',
317 'name' => 'share_unmark',
319 'section' => 'entry',
322 'name' => 'unmark_url',
323 'value' => 'https://unmark.it',
324 'section' => 'entry',
327 'name' => 'share_shaarli',
329 'section' => 'entry',
332 'name' => 'shaarli_url',
333 'value' => 'http://myshaarli.com',
334 'section' => 'entry',
337 'name' => 'share_scuttle',
339 'section' => 'entry',
342 'name' => 'scuttle_url',
343 'value' => 'http://scuttle.org',
344 'section' => 'entry',
347 'name' => 'share_mail',
349 'section' => 'entry',
352 'name' => 'share_twitter',
354 'section' => 'entry',
357 'name' => 'export_epub',
359 'section' => 'export',
362 'name' => 'export_mobi',
364 'section' => 'export',
367 'name' => 'export_pdf',
369 'section' => 'export',
372 'name' => 'export_csv',
374 'section' => 'export',
377 'name' => 'export_json',
379 'section' => 'export',
382 'name' => 'export_txt',
384 'section' => 'export',
387 'name' => 'export_xml',
389 'section' => 'export',
392 'name' => 'import_with_redis',
394 'section' => 'import',
397 'name' => 'import_with_rabbitmq',
399 'section' => 'import',
402 'name' => 'show_printlink',
404 'section' => 'entry',
407 'name' => 'wallabag_support_url',
408 'value' => 'https://www.wallabag.org/pages/support.html',
412 'name' => 'wallabag_url',
417 'name' => 'piwik_enabled',
419 'section' => 'analytics',
422 'name' => 'piwik_host',
423 'value' => 'v2.wallabag.org',
424 'section' => 'analytics',
427 'name' => 'piwik_site_id',
429 'section' => 'analytics',
432 'name' => 'demo_mode_enabled',
437 'name' => 'demo_mode_username',
438 'value' => 'wallabag',
442 'name' => 'download_images_enabled',
447 'name' => 'restricted_access',
449 'section' => 'entry',
453 foreach ($settings as $setting) {
454 $newSetting = new Setting();
455 $newSetting->setName($setting['name']);
456 $newSetting->setValue($setting['value']);
457 $newSetting->setSection($setting['section']);
458 $em->persist($newSetting);
463 $this->defaultOutput
->writeln('');
468 protected function runMigrations()
470 $this->defaultOutput
->writeln('<info><comment>Step 5 of 5.</comment> Run migrations.</info>');
473 ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true]);
479 * @param string $command
480 * @param array $parameters Parameters to this command (usually 'force' => true)
482 protected function runCommand($command, $parameters = [])
484 $parameters = array_merge(
485 ['command' => $command],
488 '--no-debug' => true,
489 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
493 if ($this->defaultInput
->getOption('no-interaction')) {
494 $parameters = array_merge($parameters, ['--no-interaction' => true]);
497 $this->getApplication()->setAutoExit(false);
499 $output = new BufferedOutput();
500 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), $output);
502 // PDO does not always close the connection after Doctrine commands.
503 // See https://github.com/symfony/symfony/issues/11750.
504 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
506 if (0 !== $exitCode) {
507 $this->getApplication()->setAutoExit(true);
509 throw new \
RuntimeException(
510 'The command "'.$command."\" generates some errors: \n\n"
518 * Check if the database already exists.
522 private function isDatabasePresent()
524 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
525 $databaseName = $connection->getDatabase();
528 $schemaManager = $connection->getSchemaManager();
529 } catch (\Exception
$exception) {
531 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
536 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
543 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
544 if ('sqlite' === $schemaManager->getDatabasePlatform()->getName()) {
545 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
547 if (isset($params['path']) && file_exists($params['path'])) {
555 return in_array($databaseName, $schemaManager->listDatabases());
556 } catch (\Doctrine\DBAL\Exception\DriverException
$e) {
557 // it means we weren't able to get database list, assume the database doesn't exist
564 * Check if the schema is already created.
565 * If we found at least oen table, it means the schema exists.
569 private function isSchemaPresent()
571 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
573 return count($schemaManager->listTableNames()) > 0 ? true : false;