3 namespace Wallabag\CoreBundle\Command
;
5 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand
;
6 use Symfony\Component\Console\Helper\Table
;
7 use Symfony\Component\Console\Input\ArrayInput
;
8 use Symfony\Component\Console\Input\InputInterface
;
9 use Symfony\Component\Console\Input\InputOption
;
10 use Symfony\Component\Console\Output\NullOutput
;
11 use Symfony\Component\Console\Output\OutputInterface
;
12 use Symfony\Component\Console\Question\ConfirmationQuestion
;
13 use Symfony\Component\Console\Question\Question
;
14 use Wallabag\CoreBundle\Entity\Config
;
15 use Craue\ConfigBundle\Entity\Setting
;
17 class InstallCommand
extends ContainerAwareCommand
22 protected $defaultInput;
25 * @var OutputInterface
27 protected $defaultOutput;
32 protected $functionExists = [
37 protected function configure()
40 ->setName('wallabag:install')
41 ->setDescription('Wallabag installer.')
45 InputOption
::VALUE_NONE
,
46 'Reset current database'
51 protected function execute(InputInterface
$input, OutputInterface
$output)
53 $this->defaultInput
= $input;
54 $this->defaultOutput
= $output;
56 $output->writeln('<info>Installing Wallabag...</info>');
67 $output->writeln('<info>Wallabag has been successfully installed.</info>');
68 $output->writeln('<comment>Just execute `php bin/console server:run --env=prod` for using wallabag: http://localhost:8000</comment>');
71 protected function checkRequirements()
73 $this->defaultOutput
->writeln('<info><comment>Step 1 of 5.</comment> Checking system requirements.</info>');
77 // testing if database driver exists
79 $label = '<comment>PDO Driver</comment>';
80 $status = '<info>OK!</info>';
83 if (!extension_loaded($this->getContainer()->getParameter('database_driver'))) {
85 $status = '<error>ERROR!</error>';
86 $help = 'Database driver "'.$this->getContainer()->getParameter('database_driver').'" is not installed.';
89 $rows[] = [$label, $status, $help];
91 // testing if connection to the database can be etablished
92 $label = '<comment>Database connection</comment>';
93 $status = '<info>OK!</info>';
97 $this->getContainer()->get('doctrine')->getManager()->getConnection()->connect();
98 } catch (\Exception
$e) {
100 $status = '<error>ERROR!</error>';
101 $help = 'Can\'t connect to the database: '.$e->getMessage();
104 $rows[] = [$label, $status, $help];
106 foreach ($this->functionExists
as $functionRequired) {
107 $label = '<comment>'.$functionRequired.'</comment>';
108 $status = '<info>OK!</info>';
111 if (!function_exists($functionRequired)) {
113 $status = '<error>ERROR!</error>';
114 $help = 'You need the '.$functionRequired.' function activated';
117 $rows[] = [$label, $status, $help];
120 $table = new Table($this->defaultOutput
);
122 ->setHeaders(['Checked', 'Status', 'Recommendation'])
127 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
130 $this->defaultOutput
->writeln('<info>Success! Your system can run Wallabag properly.</info>');
132 $this->defaultOutput
->writeln('');
137 protected function setupDatabase()
139 $this->defaultOutput
->writeln('<info><comment>Step 2 of 5.</comment> Setting up database.</info>');
141 // user want to reset everything? Don't care about what is already here
142 if (true === $this->defaultInput
->getOption('reset')) {
143 $this->defaultOutput
->writeln('Droping database, creating database and schema, clearing the cache');
146 ->runCommand('doctrine:database:drop', ['--force' => true])
147 ->runCommand('doctrine:database:create')
148 ->runCommand('doctrine:schema:create')
149 ->runCommand('cache:clear')
152 $this->defaultOutput
->writeln('');
157 if (!$this->isDatabasePresent()) {
158 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
161 ->runCommand('doctrine:database:create')
162 ->runCommand('doctrine:schema:create')
163 ->runCommand('cache:clear')
166 $this->defaultOutput
->writeln('');
171 $questionHelper = $this->getHelper('question');
172 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
174 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
175 $this->defaultOutput
->writeln('Droping database, creating database and schema');
178 ->runCommand('doctrine:database:drop', ['--force' => true])
179 ->runCommand('doctrine:database:create')
180 ->runCommand('doctrine:schema:create')
182 } elseif ($this->isSchemaPresent()) {
183 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
184 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
185 $this->defaultOutput
->writeln('Droping schema and creating schema');
188 ->runCommand('doctrine:schema:drop', ['--force' => true])
189 ->runCommand('doctrine:schema:create')
193 $this->defaultOutput
->writeln('Creating schema');
196 ->runCommand('doctrine:schema:create')
200 $this->defaultOutput
->writeln('Clearing the cache');
201 $this->runCommand('cache:clear');
203 $this->defaultOutput
->writeln('');
208 protected function setupAdmin()
210 $this->defaultOutput
->writeln('<info><comment>Step 3 of 5.</comment> Administration setup.</info>');
212 $questionHelper = $this->getHelperSet()->get('question');
213 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true);
215 if (!$questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
219 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
221 $userManager = $this->getContainer()->get('fos_user.user_manager');
222 $user = $userManager->createUser();
224 $question = new Question('Username (default: wallabag) :', 'wallabag');
225 $user->setUsername($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
227 $question = new Question('Password (default: wallabag) :', 'wallabag');
228 $user->setPlainPassword($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
230 $question = new Question('Email:', '');
231 $user->setEmail($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
233 $user->setEnabled(true);
234 $user->addRole('ROLE_SUPER_ADMIN');
238 $config = new Config($user);
239 $config->setTheme($this->getContainer()->getParameter('wallabag_core.theme'));
240 $config->setItemsPerPage($this->getContainer()->getParameter('wallabag_core.items_on_page'));
241 $config->setRssLimit($this->getContainer()->getParameter('wallabag_core.rss_limit'));
242 $config->setReadingSpeed($this->getContainer()->getParameter('wallabag_core.reading_speed'));
243 $config->setLanguage($this->getContainer()->getParameter('wallabag_core.language'));
245 $em->persist($config);
247 $this->defaultOutput
->writeln('');
252 protected function setupConfig()
254 $this->defaultOutput
->writeln('<info><comment>Step 4 of 5.</comment> Config setup.</info>');
255 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
257 // cleanup before insert new stuff
258 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
264 'section' => 'entry',
267 'name' => 'share_diaspora',
269 'section' => 'entry',
272 'name' => 'diaspora_url',
273 'value' => 'http://diasporapod.com',
274 'section' => 'entry',
277 'name' => 'share_shaarli',
279 'section' => 'entry',
282 'name' => 'shaarli_url',
283 'value' => 'http://myshaarli.com',
284 'section' => 'entry',
287 'name' => 'share_mail',
289 'section' => 'entry',
292 'name' => 'share_twitter',
294 'section' => 'entry',
297 'name' => 'export_epub',
299 'section' => 'export',
302 'name' => 'export_mobi',
304 'section' => 'export',
307 'name' => 'export_pdf',
309 'section' => 'export',
312 'name' => 'export_csv',
314 'section' => 'export',
317 'name' => 'export_json',
319 'section' => 'export',
322 'name' => 'export_txt',
324 'section' => 'export',
327 'name' => 'export_xml',
329 'section' => 'export',
332 'name' => 'pocket_consumer_key',
334 'section' => 'import',
337 'name' => 'show_printlink',
339 'section' => 'entry',
342 'name' => 'wallabag_support_url',
343 'value' => 'https://www.wallabag.org/pages/support.html',
347 'name' => 'wallabag_url',
348 'value' => 'http://v2.wallabag.org',
352 'name' => 'piwik_enabled',
354 'section' => 'analytics',
357 'name' => 'piwik_host',
358 'value' => 'http://v2.wallabag.org',
359 'section' => 'analytics',
362 'name' => 'piwik_site_id',
364 'section' => 'analytics',
367 'name' => 'demo_mode_enabled',
372 'name' => 'demo_mode_username',
373 'value' => 'wallabag',
378 foreach ($settings as $setting) {
379 $newSetting = new Setting();
380 $newSetting->setName($setting['name']);
381 $newSetting->setValue($setting['value']);
382 $newSetting->setSection($setting['section']);
383 $em->persist($newSetting);
388 $this->defaultOutput
->writeln('');
393 protected function setupAsset()
395 $this->defaultOutput
->writeln('<info><comment>Step 5 of 5.</comment> Installing assets.</info>');
398 ->runCommand('assets:install')
399 ->runCommand('assetic:dump')
402 $this->defaultOutput
->writeln('');
410 * @param string $command
411 * @param array $parameters Parameters to this command (usually 'force' => true)
413 protected function runCommand($command, $parameters = [])
415 $parameters = array_merge(
416 ['command' => $command],
419 '--no-debug' => true,
420 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
424 if ($this->defaultInput
->getOption('no-interaction')) {
425 $parameters = array_merge($parameters, ['--no-interaction' => true]);
428 $this->getApplication()->setAutoExit(false);
429 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
431 if (0 !== $exitCode) {
432 $this->getApplication()->setAutoExit(true);
434 $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode);
435 $this->defaultOutput
->writeln("<error>$errorMessage</error>");
436 $exception = new \
Exception($errorMessage, $exitCode);
441 // PDO does not always close the connection after Doctrine commands.
442 // See https://github.com/symfony/symfony/issues/11750.
443 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
449 * Check if the database already exists.
453 private function isDatabasePresent()
455 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
456 $databaseName = $connection->getDatabase();
459 $schemaManager = $connection->getSchemaManager();
460 } catch (\Exception
$exception) {
462 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
467 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
474 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
475 if ('sqlite' == $schemaManager->getDatabasePlatform()->getName()) {
476 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
478 if (isset($params['path']) && file_exists($params['path'])) {
486 return in_array($databaseName, $schemaManager->listDatabases());
487 } catch (\Doctrine\DBAL\Exception\DriverException
$e) {
488 // it means we weren't able to get database list, assume the database doesn't exist
495 * Check if the schema is already created.
496 * If we found at least oen table, it means the schema exists.
500 private function isSchemaPresent()
502 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
504 return count($schemaManager->listTableNames()) > 0 ? true : false;