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>');
75 $doctrineManager = $this->getContainer()->get('doctrine')->getManager();
79 // testing if database driver exists
81 $label = '<comment>PDO Driver (%s)</comment>';
82 $status = '<info>OK!</info>';
85 if (!extension_loaded($this->getContainer()->getParameter('database_driver'))) {
87 $status = '<error>ERROR!</error>';
88 $help = 'Database driver "'.$this->getContainer()->getParameter('database_driver').'" is not installed.';
91 $rows[] = [sprintf($label, $this->getContainer()->getParameter('database_driver')), $status, $help];
93 // testing if connection to the database can be etablished
94 $label = '<comment>Database connection</comment>';
95 $status = '<info>OK!</info>';
99 $conn = $this->getContainer()->get('doctrine')->getManager()->getConnection();
101 } catch (\Exception
$e) {
102 if (false === strpos($e->getMessage(), 'Unknown database')
103 && false === strpos($e->getMessage(), 'database "'.$this->getContainer()->getParameter('database_name').'" does not exist')) {
105 $status = '<error>ERROR!</error>';
106 $help = 'Can\'t connect to the database: '.$e->getMessage();
110 $rows[] = [$label, $status, $help];
112 // check MySQL & PostgreSQL version
113 $label = '<comment>Database version</comment>';
114 $status = '<info>OK!</info>';
117 // now check if MySQL isn't too old to handle utf8mb4
118 if ($conn->isConnected() && 'mysql' === $conn->getDatabasePlatform()->getName()) {
119 $version = $conn->query('select version()')->fetchColumn();
120 $minimalVersion = '5.5.4';
122 if (false === version_compare($version, $minimalVersion, '>')) {
124 $status = '<error>ERROR!</error>';
125 $help = 'Your MySQL version ('.$version.') is too old, consider upgrading ('.$minimalVersion.'+).';
129 // testing if PostgreSQL > 9.1
130 if ($conn->isConnected() && 'postgresql' === $conn->getDatabasePlatform()->getName()) {
131 // 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"
132 $version = $doctrineManager->getConnection()->query('SELECT version();')->fetchColumn();
134 preg_match('/PostgreSQL ([0-9\.]+)/i', $version, $matches);
136 if (isset($matches[1]) & version_compare($matches[1], '9.2.0', '<')) {
138 $status = '<error>ERROR!</error>';
139 $help = 'PostgreSQL should be greater than 9.1 (actual version: '.$matches[1].')';
143 $rows[] = [$label, $status, $help];
145 foreach ($this->functionExists
as $functionRequired) {
146 $label = '<comment>'.$functionRequired.'</comment>';
147 $status = '<info>OK!</info>';
150 if (!function_exists($functionRequired)) {
152 $status = '<error>ERROR!</error>';
153 $help = 'You need the '.$functionRequired.' function activated';
156 $rows[] = [$label, $status, $help];
159 $table = new Table($this->defaultOutput
);
161 ->setHeaders(['Checked', 'Status', 'Recommendation'])
166 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
169 $this->defaultOutput
->writeln('<info>Success! Your system can run wallabag properly.</info>');
171 $this->defaultOutput
->writeln('');
176 protected function setupDatabase()
178 $this->defaultOutput
->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
180 // user want to reset everything? Don't care about what is already here
181 if (true === $this->defaultInput
->getOption('reset')) {
182 $this->defaultOutput
->writeln('Droping database, creating database and schema, clearing the cache');
185 ->runCommand('doctrine:database:drop', ['--force' => true])
186 ->runCommand('doctrine:database:create')
187 ->runCommand('doctrine:schema:create')
188 ->runCommand('cache:clear')
191 $this->defaultOutput
->writeln('');
196 if (!$this->isDatabasePresent()) {
197 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
200 ->runCommand('doctrine:database:create')
201 ->runCommand('doctrine:schema:create')
202 ->runCommand('cache:clear')
205 $this->defaultOutput
->writeln('');
210 $questionHelper = $this->getHelper('question');
211 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
213 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
214 $this->defaultOutput
->writeln('Droping database, creating database and schema');
217 ->runCommand('doctrine:database:drop', ['--force' => true])
218 ->runCommand('doctrine:database:create')
219 ->runCommand('doctrine:schema:create')
221 } elseif ($this->isSchemaPresent()) {
222 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
223 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
224 $this->defaultOutput
->writeln('Droping schema and creating schema');
227 ->runCommand('doctrine:schema:drop', ['--force' => true])
228 ->runCommand('doctrine:schema:create')
232 $this->defaultOutput
->writeln('Creating schema');
235 ->runCommand('doctrine:schema:create')
239 $this->defaultOutput
->writeln('Clearing the cache');
240 $this->runCommand('cache:clear');
242 $this->defaultOutput
->writeln('');
247 protected function setupAdmin()
249 $this->defaultOutput
->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
251 $questionHelper = $this->getHelperSet()->get('question');
252 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true);
254 if (!$questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
258 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
260 $userManager = $this->getContainer()->get('fos_user.user_manager');
261 $user = $userManager->createUser();
263 $question = new Question('Username (default: wallabag) :', 'wallabag');
264 $user->setUsername($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
266 $question = new Question('Password (default: wallabag) :', 'wallabag');
267 $user->setPlainPassword($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
269 $question = new Question('Email:', '');
270 $user->setEmail($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
272 $user->setEnabled(true);
273 $user->addRole('ROLE_SUPER_ADMIN');
277 // dispatch a created event so the associated config will be created
278 $event = new UserEvent($user);
279 $this->getContainer()->get('event_dispatcher')->dispatch(FOSUserEvents
::USER_CREATED
, $event);
281 $this->defaultOutput
->writeln('');
286 protected function setupConfig()
288 $this->defaultOutput
->writeln('<info><comment>Step 4 of 4.</comment> Config setup.</info>');
289 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
291 // cleanup before insert new stuff
292 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
296 'name' => 'share_public',
298 'section' => 'entry',
303 'section' => 'entry',
306 'name' => 'share_diaspora',
308 'section' => 'entry',
311 'name' => 'diaspora_url',
312 'value' => 'http://diasporapod.com',
313 'section' => 'entry',
316 'name' => 'share_unmark',
318 'section' => 'entry',
321 'name' => 'unmark_url',
322 'value' => 'https://unmark.it',
323 'section' => 'entry',
326 'name' => 'share_shaarli',
328 'section' => 'entry',
331 'name' => 'shaarli_url',
332 'value' => 'http://myshaarli.com',
333 'section' => 'entry',
336 'name' => 'share_mail',
338 'section' => 'entry',
341 'name' => 'share_twitter',
343 'section' => 'entry',
346 'name' => 'export_epub',
348 'section' => 'export',
351 'name' => 'export_mobi',
353 'section' => 'export',
356 'name' => 'export_pdf',
358 'section' => 'export',
361 'name' => 'export_csv',
363 'section' => 'export',
366 'name' => 'export_json',
368 'section' => 'export',
371 'name' => 'export_txt',
373 'section' => 'export',
376 'name' => 'export_xml',
378 'section' => 'export',
381 'name' => 'import_with_redis',
383 'section' => 'import',
386 'name' => 'import_with_rabbitmq',
388 'section' => 'import',
391 'name' => 'show_printlink',
393 'section' => 'entry',
396 'name' => 'wallabag_support_url',
397 'value' => 'https://www.wallabag.org/pages/support.html',
401 'name' => 'wallabag_url',
406 'name' => 'piwik_enabled',
408 'section' => 'analytics',
411 'name' => 'piwik_host',
412 'value' => 'v2.wallabag.org',
413 'section' => 'analytics',
416 'name' => 'piwik_site_id',
418 'section' => 'analytics',
421 'name' => 'demo_mode_enabled',
426 'name' => 'demo_mode_username',
427 'value' => 'wallabag',
431 'name' => 'download_images_enabled',
436 'name' => 'restricted_access',
438 'section' => 'entry',
442 foreach ($settings as $setting) {
443 $newSetting = new Setting();
444 $newSetting->setName($setting['name']);
445 $newSetting->setValue($setting['value']);
446 $newSetting->setSection($setting['section']);
447 $em->persist($newSetting);
452 $this->defaultOutput
->writeln('');
460 * @param string $command
461 * @param array $parameters Parameters to this command (usually 'force' => true)
463 protected function runCommand($command, $parameters = [])
465 $parameters = array_merge(
466 ['command' => $command],
469 '--no-debug' => true,
470 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
474 if ($this->defaultInput
->getOption('no-interaction')) {
475 $parameters = array_merge($parameters, ['--no-interaction' => true]);
478 $this->getApplication()->setAutoExit(false);
480 $output = new BufferedOutput();
481 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), $output);
483 if (0 !== $exitCode) {
484 $this->getApplication()->setAutoExit(true);
486 $this->defaultOutput
->writeln('');
487 $this->defaultOutput
->writeln('<error>The command "'.$command.'" generates some errors: </error>');
488 $this->defaultOutput
->writeln($output->fetch());
493 // PDO does not always close the connection after Doctrine commands.
494 // See https://github.com/symfony/symfony/issues/11750.
495 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
501 * Check if the database already exists.
505 private function isDatabasePresent()
507 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
508 $databaseName = $connection->getDatabase();
511 $schemaManager = $connection->getSchemaManager();
512 } catch (\Exception
$exception) {
514 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
519 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
526 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
527 if ('sqlite' === $schemaManager->getDatabasePlatform()->getName()) {
528 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
530 if (isset($params['path']) && file_exists($params['path'])) {
538 return in_array($databaseName, $schemaManager->listDatabases());
539 } catch (\Doctrine\DBAL\Exception\DriverException
$e) {
540 // it means we weren't able to get database list, assume the database doesn't exist
547 * Check if the schema is already created.
548 * If we found at least oen table, it means the schema exists.
552 private function isSchemaPresent()
554 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
556 return count($schemaManager->listTableNames()) > 0 ? true : false;