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) {
99 if (false === strpos($e->getMessage(), "Unknown database")) {
101 $status = '<error>ERROR!</error>';
102 $help = 'Can\'t connect to the database: '.$e->getMessage();
106 $rows[] = [$label, $status, $help];
108 foreach ($this->functionExists
as $functionRequired) {
109 $label = '<comment>'.$functionRequired.'</comment>';
110 $status = '<info>OK!</info>';
113 if (!function_exists($functionRequired)) {
115 $status = '<error>ERROR!</error>';
116 $help = 'You need the '.$functionRequired.' function activated';
119 $rows[] = [$label, $status, $help];
122 $table = new Table($this->defaultOutput
);
124 ->setHeaders(['Checked', 'Status', 'Recommendation'])
129 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
132 $this->defaultOutput
->writeln('<info>Success! Your system can run Wallabag properly.</info>');
134 $this->defaultOutput
->writeln('');
139 protected function setupDatabase()
141 $this->defaultOutput
->writeln('<info><comment>Step 2 of 5.</comment> Setting up database.</info>');
143 // user want to reset everything? Don't care about what is already here
144 if (true === $this->defaultInput
->getOption('reset')) {
145 $this->defaultOutput
->writeln('Droping database, creating database and schema, clearing the cache');
148 ->runCommand('doctrine:database:drop', ['--force' => true])
149 ->runCommand('doctrine:database:create')
150 ->runCommand('doctrine:schema:create')
151 ->runCommand('cache:clear')
154 $this->defaultOutput
->writeln('');
159 if (!$this->isDatabasePresent()) {
160 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
163 ->runCommand('doctrine:database:create')
164 ->runCommand('doctrine:schema:create')
165 ->runCommand('cache:clear')
168 $this->defaultOutput
->writeln('');
173 $questionHelper = $this->getHelper('question');
174 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
176 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
177 $this->defaultOutput
->writeln('Droping database, creating database and schema');
180 ->runCommand('doctrine:database:drop', ['--force' => true])
181 ->runCommand('doctrine:database:create')
182 ->runCommand('doctrine:schema:create')
184 } elseif ($this->isSchemaPresent()) {
185 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
186 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
187 $this->defaultOutput
->writeln('Droping schema and creating schema');
190 ->runCommand('doctrine:schema:drop', ['--force' => true])
191 ->runCommand('doctrine:schema:create')
195 $this->defaultOutput
->writeln('Creating schema');
198 ->runCommand('doctrine:schema:create')
202 $this->defaultOutput
->writeln('Clearing the cache');
203 $this->runCommand('cache:clear');
205 $this->defaultOutput
->writeln('');
210 protected function setupAdmin()
212 $this->defaultOutput
->writeln('<info><comment>Step 3 of 5.</comment> Administration setup.</info>');
214 $questionHelper = $this->getHelperSet()->get('question');
215 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true);
217 if (!$questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
221 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
223 $userManager = $this->getContainer()->get('fos_user.user_manager');
224 $user = $userManager->createUser();
226 $question = new Question('Username (default: wallabag) :', 'wallabag');
227 $user->setUsername($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
229 $question = new Question('Password (default: wallabag) :', 'wallabag');
230 $user->setPlainPassword($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
232 $question = new Question('Email:', '');
233 $user->setEmail($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
235 $user->setEnabled(true);
236 $user->addRole('ROLE_SUPER_ADMIN');
240 $config = new Config($user);
241 $config->setTheme($this->getContainer()->getParameter('wallabag_core.theme'));
242 $config->setItemsPerPage($this->getContainer()->getParameter('wallabag_core.items_on_page'));
243 $config->setRssLimit($this->getContainer()->getParameter('wallabag_core.rss_limit'));
244 $config->setReadingSpeed($this->getContainer()->getParameter('wallabag_core.reading_speed'));
245 $config->setLanguage($this->getContainer()->getParameter('wallabag_core.language'));
247 $em->persist($config);
249 $this->defaultOutput
->writeln('');
254 protected function setupConfig()
256 $this->defaultOutput
->writeln('<info><comment>Step 4 of 5.</comment> Config setup.</info>');
257 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
259 // cleanup before insert new stuff
260 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
266 'section' => 'entry',
269 'name' => 'share_diaspora',
271 'section' => 'entry',
274 'name' => 'diaspora_url',
275 'value' => 'http://diasporapod.com',
276 'section' => 'entry',
279 'name' => 'share_shaarli',
281 'section' => 'entry',
284 'name' => 'shaarli_url',
285 'value' => 'http://myshaarli.com',
286 'section' => 'entry',
289 'name' => 'share_mail',
291 'section' => 'entry',
294 'name' => 'share_twitter',
296 'section' => 'entry',
299 'name' => 'export_epub',
301 'section' => 'export',
304 'name' => 'export_mobi',
306 'section' => 'export',
309 'name' => 'export_pdf',
311 'section' => 'export',
314 'name' => 'export_csv',
316 'section' => 'export',
319 'name' => 'export_json',
321 'section' => 'export',
324 'name' => 'export_txt',
326 'section' => 'export',
329 'name' => 'export_xml',
331 'section' => 'export',
334 'name' => 'pocket_consumer_key',
336 'section' => 'import',
339 'name' => 'show_printlink',
341 'section' => 'entry',
344 'name' => 'wallabag_support_url',
345 'value' => 'https://www.wallabag.org/pages/support.html',
349 'name' => 'wallabag_url',
350 'value' => 'http://v2.wallabag.org',
354 'name' => 'piwik_enabled',
356 'section' => 'analytics',
359 'name' => 'piwik_host',
360 'value' => 'http://v2.wallabag.org',
361 'section' => 'analytics',
364 'name' => 'piwik_site_id',
366 'section' => 'analytics',
369 'name' => 'demo_mode_enabled',
374 'name' => 'demo_mode_username',
375 'value' => 'wallabag',
380 foreach ($settings as $setting) {
381 $newSetting = new Setting();
382 $newSetting->setName($setting['name']);
383 $newSetting->setValue($setting['value']);
384 $newSetting->setSection($setting['section']);
385 $em->persist($newSetting);
390 $this->defaultOutput
->writeln('');
395 protected function setupAsset()
397 $this->defaultOutput
->writeln('<info><comment>Step 5 of 5.</comment> Installing assets.</info>');
400 ->runCommand('assets:install')
401 ->runCommand('assetic:dump')
404 $this->defaultOutput
->writeln('');
412 * @param string $command
413 * @param array $parameters Parameters to this command (usually 'force' => true)
415 protected function runCommand($command, $parameters = [])
417 $parameters = array_merge(
418 ['command' => $command],
421 '--no-debug' => true,
422 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
426 if ($this->defaultInput
->getOption('no-interaction')) {
427 $parameters = array_merge($parameters, ['--no-interaction' => true]);
430 $this->getApplication()->setAutoExit(false);
431 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
433 if (0 !== $exitCode) {
434 $this->getApplication()->setAutoExit(true);
436 $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode);
437 $this->defaultOutput
->writeln("<error>$errorMessage</error>");
438 $exception = new \
Exception($errorMessage, $exitCode);
443 // PDO does not always close the connection after Doctrine commands.
444 // See https://github.com/symfony/symfony/issues/11750.
445 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
451 * Check if the database already exists.
455 private function isDatabasePresent()
457 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
458 $databaseName = $connection->getDatabase();
461 $schemaManager = $connection->getSchemaManager();
462 } catch (\Exception
$exception) {
464 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
469 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
476 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
477 if ('sqlite' === $schemaManager->getDatabasePlatform()->getName()) {
478 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
480 if (isset($params['path']) && file_exists($params['path'])) {
488 return in_array($databaseName, $schemaManager->listDatabases());
489 } catch (\Doctrine\DBAL\Exception\DriverException
$e) {
490 // it means we weren't able to get database list, assume the database doesn't exist
497 * Check if the schema is already created.
498 * If we found at least oen table, it means the schema exists.
502 private function isSchemaPresent()
504 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
506 return count($schemaManager->listTableNames()) > 0 ? true : false;