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\NullOutput
;
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</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[] = [$label, $status, $help];
92 // testing if connection to the database can be etablished
93 $label = '<comment>Database connection</comment>';
94 $status = '<info>OK!</info>';
98 $this->getContainer()->get('doctrine')->getManager()->getConnection()->connect();
99 } catch (\Exception
$e) {
100 if (false === strpos($e->getMessage(), 'Unknown database')) {
102 $status = '<error>ERROR!</error>';
103 $help = 'Can\'t connect to the database: '.$e->getMessage();
107 $rows[] = [$label, $status, $help];
109 foreach ($this->functionExists
as $functionRequired) {
110 $label = '<comment>'.$functionRequired.'</comment>';
111 $status = '<info>OK!</info>';
114 if (!function_exists($functionRequired)) {
116 $status = '<error>ERROR!</error>';
117 $help = 'You need the '.$functionRequired.' function activated';
120 $rows[] = [$label, $status, $help];
123 $table = new Table($this->defaultOutput
);
125 ->setHeaders(['Checked', 'Status', 'Recommendation'])
130 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
133 $this->defaultOutput
->writeln('<info>Success! Your system can run Wallabag properly.</info>');
135 $this->defaultOutput
->writeln('');
140 protected function setupDatabase()
142 $this->defaultOutput
->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
144 // user want to reset everything? Don't care about what is already here
145 if (true === $this->defaultInput
->getOption('reset')) {
146 $this->defaultOutput
->writeln('Droping database, creating database and schema, clearing the cache');
149 ->runCommand('doctrine:database:drop', ['--force' => true])
150 ->runCommand('doctrine:database:create')
151 ->runCommand('doctrine:schema:create')
152 ->runCommand('cache:clear')
155 $this->defaultOutput
->writeln('');
160 if (!$this->isDatabasePresent()) {
161 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
164 ->runCommand('doctrine:database:create')
165 ->runCommand('doctrine:schema:create')
166 ->runCommand('cache:clear')
169 $this->defaultOutput
->writeln('');
174 $questionHelper = $this->getHelper('question');
175 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
177 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
178 $this->defaultOutput
->writeln('Droping database, creating database and schema');
181 ->runCommand('doctrine:database:drop', ['--force' => true])
182 ->runCommand('doctrine:database:create')
183 ->runCommand('doctrine:schema:create')
185 } elseif ($this->isSchemaPresent()) {
186 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
187 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
188 $this->defaultOutput
->writeln('Droping schema and creating schema');
191 ->runCommand('doctrine:schema:drop', ['--force' => true])
192 ->runCommand('doctrine:schema:create')
196 $this->defaultOutput
->writeln('Creating schema');
199 ->runCommand('doctrine:schema:create')
203 $this->defaultOutput
->writeln('Clearing the cache');
204 $this->runCommand('cache:clear');
206 $this->defaultOutput
->writeln('');
211 protected function setupAdmin()
213 $this->defaultOutput
->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
215 $questionHelper = $this->getHelperSet()->get('question');
216 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true);
218 if (!$questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
222 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
224 $userManager = $this->getContainer()->get('fos_user.user_manager');
225 $user = $userManager->createUser();
227 $question = new Question('Username (default: wallabag) :', 'wallabag');
228 $user->setUsername($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
230 $question = new Question('Password (default: wallabag) :', 'wallabag');
231 $user->setPlainPassword($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
233 $question = new Question('Email:', '');
234 $user->setEmail($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
236 $user->setEnabled(true);
237 $user->addRole('ROLE_SUPER_ADMIN');
241 // dispatch a created event so the associated config will be created
242 $event = new UserEvent($user);
243 $this->getContainer()->get('event_dispatcher')->dispatch(FOSUserEvents
::USER_CREATED
, $event);
245 $this->defaultOutput
->writeln('');
250 protected function setupConfig()
252 $this->defaultOutput
->writeln('<info><comment>Step 4 of 4.</comment> Config setup.</info>');
253 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
255 // cleanup before insert new stuff
256 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
260 'name' => 'share_public',
262 'section' => 'entry',
267 'section' => 'entry',
270 'name' => 'share_diaspora',
272 'section' => 'entry',
275 'name' => 'diaspora_url',
276 'value' => 'http://diasporapod.com',
277 'section' => 'entry',
280 'name' => 'share_shaarli',
282 'section' => 'entry',
285 'name' => 'shaarli_url',
286 'value' => 'http://myshaarli.com',
287 'section' => 'entry',
290 'name' => 'share_mail',
292 'section' => 'entry',
295 'name' => 'share_twitter',
297 'section' => 'entry',
300 'name' => 'export_epub',
302 'section' => 'export',
305 'name' => 'export_mobi',
307 'section' => 'export',
310 'name' => 'export_pdf',
312 'section' => 'export',
315 'name' => 'export_csv',
317 'section' => 'export',
320 'name' => 'export_json',
322 'section' => 'export',
325 'name' => 'export_txt',
327 'section' => 'export',
330 'name' => 'export_xml',
332 'section' => 'export',
335 'name' => 'import_with_redis',
337 'section' => 'import',
340 'name' => 'import_with_rabbitmq',
342 'section' => 'import',
345 'name' => 'show_printlink',
347 'section' => 'entry',
350 'name' => 'wallabag_support_url',
351 'value' => 'https://www.wallabag.org/pages/support.html',
355 'name' => 'wallabag_url',
356 'value' => 'http://v2.wallabag.org',
360 'name' => 'piwik_enabled',
362 'section' => 'analytics',
365 'name' => 'piwik_host',
366 'value' => 'http://v2.wallabag.org',
367 'section' => 'analytics',
370 'name' => 'piwik_site_id',
372 'section' => 'analytics',
375 'name' => 'demo_mode_enabled',
380 'name' => 'demo_mode_username',
381 'value' => 'wallabag',
386 foreach ($settings as $setting) {
387 $newSetting = new Setting();
388 $newSetting->setName($setting['name']);
389 $newSetting->setValue($setting['value']);
390 $newSetting->setSection($setting['section']);
391 $em->persist($newSetting);
396 $this->defaultOutput
->writeln('');
404 * @param string $command
405 * @param array $parameters Parameters to this command (usually 'force' => true)
407 protected function runCommand($command, $parameters = [])
409 $parameters = array_merge(
410 ['command' => $command],
413 '--no-debug' => true,
414 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
418 if ($this->defaultInput
->getOption('no-interaction')) {
419 $parameters = array_merge($parameters, ['--no-interaction' => true]);
422 $this->getApplication()->setAutoExit(false);
423 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
425 if (0 !== $exitCode) {
426 $this->getApplication()->setAutoExit(true);
428 $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode);
429 $this->defaultOutput
->writeln("<error>$errorMessage</error>");
430 $exception = new \
Exception($errorMessage, $exitCode);
435 // PDO does not always close the connection after Doctrine commands.
436 // See https://github.com/symfony/symfony/issues/11750.
437 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
443 * Check if the database already exists.
447 private function isDatabasePresent()
449 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
450 $databaseName = $connection->getDatabase();
453 $schemaManager = $connection->getSchemaManager();
454 } catch (\Exception
$exception) {
456 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
461 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
468 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
469 if ('sqlite' === $schemaManager->getDatabasePlatform()->getName()) {
470 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
472 if (isset($params['path']) && file_exists($params['path'])) {
480 return in_array($databaseName, $schemaManager->listDatabases());
481 } catch (\Doctrine\DBAL\Exception\DriverException
$e) {
482 // it means we weren't able to get database list, assume the database doesn't exist
489 * Check if the schema is already created.
490 * If we found at least oen table, it means the schema exists.
494 private function isSchemaPresent()
496 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
498 return count($schemaManager->listTableNames()) > 0 ? true : false;