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</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[] = [$label, $status, $help];
93 // testing if connection to the database can be etablished
94 $label = '<comment>Database connection</comment>';
95 $status = '<info>OK!</info>';
99 $doctrineManager->getConnection()->connect();
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 // testing if PostgreSQL > 9.1
112 $label = '<comment>SGBD version</comment>';
113 $status = '<info>OK!</info>';
116 if ('postgresql' === $doctrineManager->getConnection()->getSchemaManager()->getDatabasePlatform()->getName()) {
117 // 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"
118 $version = $doctrineManager->getConnection()->query('SELECT version();')->fetchColumn();
120 preg_match('/PostgreSQL ([0-9\.]+)/i', $version, $matches);
122 if (isset($matches[1]) & version_compare($matches[1], '9.2.0', '<')) {
124 $status = '<error>ERROR!</error>';
125 $help = 'PostgreSQL should be greater than 9.1 (actual version: '.$matches[1].')';
129 $rows[] = [$label, $status, $help];
131 foreach ($this->functionExists
as $functionRequired) {
132 $label = '<comment>'.$functionRequired.'</comment>';
133 $status = '<info>OK!</info>';
136 if (!function_exists($functionRequired)) {
138 $status = '<error>ERROR!</error>';
139 $help = 'You need the '.$functionRequired.' function activated';
142 $rows[] = [$label, $status, $help];
145 $table = new Table($this->defaultOutput
);
147 ->setHeaders(['Checked', 'Status', 'Recommendation'])
152 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
155 $this->defaultOutput
->writeln('<info>Success! Your system can run Wallabag properly.</info>');
157 $this->defaultOutput
->writeln('');
162 protected function setupDatabase()
164 $this->defaultOutput
->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
166 // user want to reset everything? Don't care about what is already here
167 if (true === $this->defaultInput
->getOption('reset')) {
168 $this->defaultOutput
->writeln('Droping database, creating database and schema, clearing the cache');
171 ->runCommand('doctrine:database:drop', ['--force' => true])
172 ->runCommand('doctrine:database:create')
173 ->runCommand('doctrine:schema:create')
174 ->runCommand('cache:clear')
177 $this->defaultOutput
->writeln('');
182 if (!$this->isDatabasePresent()) {
183 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
186 ->runCommand('doctrine:database:create')
187 ->runCommand('doctrine:schema:create')
188 ->runCommand('cache:clear')
191 $this->defaultOutput
->writeln('');
196 $questionHelper = $this->getHelper('question');
197 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
199 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
200 $this->defaultOutput
->writeln('Droping database, creating database and schema');
203 ->runCommand('doctrine:database:drop', ['--force' => true])
204 ->runCommand('doctrine:database:create')
205 ->runCommand('doctrine:schema:create')
207 } elseif ($this->isSchemaPresent()) {
208 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
209 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
210 $this->defaultOutput
->writeln('Droping schema and creating schema');
213 ->runCommand('doctrine:schema:drop', ['--force' => true])
214 ->runCommand('doctrine:schema:create')
218 $this->defaultOutput
->writeln('Creating schema');
221 ->runCommand('doctrine:schema:create')
225 $this->defaultOutput
->writeln('Clearing the cache');
226 $this->runCommand('cache:clear');
228 $this->defaultOutput
->writeln('');
233 protected function setupAdmin()
235 $this->defaultOutput
->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
237 $questionHelper = $this->getHelperSet()->get('question');
238 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true);
240 if (!$questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
244 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
246 $userManager = $this->getContainer()->get('fos_user.user_manager');
247 $user = $userManager->createUser();
249 $question = new Question('Username (default: wallabag) :', 'wallabag');
250 $user->setUsername($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
252 $question = new Question('Password (default: wallabag) :', 'wallabag');
253 $user->setPlainPassword($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
255 $question = new Question('Email:', '');
256 $user->setEmail($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
258 $user->setEnabled(true);
259 $user->addRole('ROLE_SUPER_ADMIN');
263 // dispatch a created event so the associated config will be created
264 $event = new UserEvent($user);
265 $this->getContainer()->get('event_dispatcher')->dispatch(FOSUserEvents
::USER_CREATED
, $event);
267 $this->defaultOutput
->writeln('');
272 protected function setupConfig()
274 $this->defaultOutput
->writeln('<info><comment>Step 4 of 4.</comment> Config setup.</info>');
275 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
277 // cleanup before insert new stuff
278 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
282 'name' => 'share_public',
284 'section' => 'entry',
289 'section' => 'entry',
292 'name' => 'share_diaspora',
294 'section' => 'entry',
297 'name' => 'diaspora_url',
298 'value' => 'http://diasporapod.com',
299 'section' => 'entry',
302 'name' => 'share_shaarli',
304 'section' => 'entry',
307 'name' => 'shaarli_url',
308 'value' => 'http://myshaarli.com',
309 'section' => 'entry',
312 'name' => 'share_mail',
314 'section' => 'entry',
317 'name' => 'share_twitter',
319 'section' => 'entry',
322 'name' => 'export_epub',
324 'section' => 'export',
327 'name' => 'export_mobi',
329 'section' => 'export',
332 'name' => 'export_pdf',
334 'section' => 'export',
337 'name' => 'export_csv',
339 'section' => 'export',
342 'name' => 'export_json',
344 'section' => 'export',
347 'name' => 'export_txt',
349 'section' => 'export',
352 'name' => 'export_xml',
354 'section' => 'export',
357 'name' => 'import_with_redis',
359 'section' => 'import',
362 'name' => 'import_with_rabbitmq',
364 'section' => 'import',
367 'name' => 'show_printlink',
369 'section' => 'entry',
372 'name' => 'wallabag_support_url',
373 'value' => 'https://www.wallabag.org/pages/support.html',
377 'name' => 'wallabag_url',
378 'value' => 'http://v2.wallabag.org',
382 'name' => 'piwik_enabled',
384 'section' => 'analytics',
387 'name' => 'piwik_host',
388 'value' => 'v2.wallabag.org',
389 'section' => 'analytics',
392 'name' => 'piwik_site_id',
394 'section' => 'analytics',
397 'name' => 'demo_mode_enabled',
402 'name' => 'demo_mode_username',
403 'value' => 'wallabag',
408 foreach ($settings as $setting) {
409 $newSetting = new Setting();
410 $newSetting->setName($setting['name']);
411 $newSetting->setValue($setting['value']);
412 $newSetting->setSection($setting['section']);
413 $em->persist($newSetting);
418 $this->defaultOutput
->writeln('');
426 * @param string $command
427 * @param array $parameters Parameters to this command (usually 'force' => true)
429 protected function runCommand($command, $parameters = [])
431 $parameters = array_merge(
432 ['command' => $command],
435 '--no-debug' => true,
436 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
440 if ($this->defaultInput
->getOption('no-interaction')) {
441 $parameters = array_merge($parameters, ['--no-interaction' => true]);
444 $this->getApplication()->setAutoExit(false);
446 $output = new BufferedOutput();
447 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), $output);
449 if (0 !== $exitCode) {
450 $this->getApplication()->setAutoExit(true);
452 $this->defaultOutput
->writeln('');
453 $this->defaultOutput
->writeln('<error>The command "'.$command.'" generates some errors: </error>');
454 $this->defaultOutput
->writeln($output->fetch());
459 // PDO does not always close the connection after Doctrine commands.
460 // See https://github.com/symfony/symfony/issues/11750.
461 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
467 * Check if the database already exists.
471 private function isDatabasePresent()
473 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
474 $databaseName = $connection->getDatabase();
477 $schemaManager = $connection->getSchemaManager();
478 } catch (\Exception
$exception) {
480 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
485 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
492 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
493 if ('sqlite' === $schemaManager->getDatabasePlatform()->getName()) {
494 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
496 if (isset($params['path']) && file_exists($params['path'])) {
504 return in_array($databaseName, $schemaManager->listDatabases());
505 } catch (\Doctrine\DBAL\Exception\DriverException
$e) {
506 // it means we weren't able to get database list, assume the database doesn't exist
513 * Check if the schema is already created.
514 * If we found at least oen table, it means the schema exists.
518 private function isSchemaPresent()
520 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
522 return count($schemaManager->listTableNames()) > 0 ? true : false;