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>');
66 $output->writeln('<info>Wallabag has been successfully installed.</info>');
67 $output->writeln('<comment>Just execute `php bin/console server:run --env=prod` for using wallabag: http://localhost:8000</comment>');
70 protected function checkRequirements()
72 $this->defaultOutput
->writeln('<info><comment>Step 1 of 4.</comment> Checking system requirements.</info>');
76 // testing if database driver exists
78 $label = '<comment>PDO Driver</comment>';
79 $status = '<info>OK!</info>';
82 if (!extension_loaded($this->getContainer()->getParameter('database_driver'))) {
84 $status = '<error>ERROR!</error>';
85 $help = 'Database driver "'.$this->getContainer()->getParameter('database_driver').'" is not installed.';
88 $rows[] = [$label, $status, $help];
90 // testing if connection to the database can be etablished
91 $label = '<comment>Database connection</comment>';
92 $status = '<info>OK!</info>';
96 $this->getContainer()->get('doctrine')->getManager()->getConnection()->connect();
97 } catch (\Exception
$e) {
98 if (false === strpos($e->getMessage(), 'Unknown database')) {
100 $status = '<error>ERROR!</error>';
101 $help = 'Can\'t connect to the database: '.$e->getMessage();
105 $rows[] = [$label, $status, $help];
107 foreach ($this->functionExists
as $functionRequired) {
108 $label = '<comment>'.$functionRequired.'</comment>';
109 $status = '<info>OK!</info>';
112 if (!function_exists($functionRequired)) {
114 $status = '<error>ERROR!</error>';
115 $help = 'You need the '.$functionRequired.' function activated';
118 $rows[] = [$label, $status, $help];
121 $table = new Table($this->defaultOutput
);
123 ->setHeaders(['Checked', 'Status', 'Recommendation'])
128 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
131 $this->defaultOutput
->writeln('<info>Success! Your system can run Wallabag properly.</info>');
133 $this->defaultOutput
->writeln('');
138 protected function setupDatabase()
140 $this->defaultOutput
->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
142 // user want to reset everything? Don't care about what is already here
143 if (true === $this->defaultInput
->getOption('reset')) {
144 $this->defaultOutput
->writeln('Droping database, creating database and schema, clearing the cache');
147 ->runCommand('doctrine:database:drop', ['--force' => true])
148 ->runCommand('doctrine:database:create')
149 ->runCommand('doctrine:schema:create')
150 ->runCommand('cache:clear')
153 $this->defaultOutput
->writeln('');
158 if (!$this->isDatabasePresent()) {
159 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
162 ->runCommand('doctrine:database:create')
163 ->runCommand('doctrine:schema:create')
164 ->runCommand('cache:clear')
167 $this->defaultOutput
->writeln('');
172 $questionHelper = $this->getHelper('question');
173 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
175 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
176 $this->defaultOutput
->writeln('Droping database, creating database and schema');
179 ->runCommand('doctrine:database:drop', ['--force' => true])
180 ->runCommand('doctrine:database:create')
181 ->runCommand('doctrine:schema:create')
183 } elseif ($this->isSchemaPresent()) {
184 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
185 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
186 $this->defaultOutput
->writeln('Droping schema and creating schema');
189 ->runCommand('doctrine:schema:drop', ['--force' => true])
190 ->runCommand('doctrine:schema:create')
194 $this->defaultOutput
->writeln('Creating schema');
197 ->runCommand('doctrine:schema:create')
201 $this->defaultOutput
->writeln('Clearing the cache');
202 $this->runCommand('cache:clear');
204 $this->defaultOutput
->writeln('');
209 protected function setupAdmin()
211 $this->defaultOutput
->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
213 $questionHelper = $this->getHelperSet()->get('question');
214 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true);
216 if (!$questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
220 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
222 $userManager = $this->getContainer()->get('fos_user.user_manager');
223 $user = $userManager->createUser();
225 $question = new Question('Username (default: wallabag) :', 'wallabag');
226 $user->setUsername($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
228 $question = new Question('Password (default: wallabag) :', 'wallabag');
229 $user->setPlainPassword($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
231 $question = new Question('Email:', '');
232 $user->setEmail($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
234 $user->setEnabled(true);
235 $user->addRole('ROLE_SUPER_ADMIN');
239 $config = new Config($user);
240 $config->setTheme($this->getContainer()->getParameter('wallabag_core.theme'));
241 $config->setItemsPerPage($this->getContainer()->getParameter('wallabag_core.items_on_page'));
242 $config->setRssLimit($this->getContainer()->getParameter('wallabag_core.rss_limit'));
243 $config->setReadingSpeed($this->getContainer()->getParameter('wallabag_core.reading_speed'));
244 $config->setLanguage($this->getContainer()->getParameter('wallabag_core.language'));
246 $em->persist($config);
248 $this->defaultOutput
->writeln('');
253 protected function setupConfig()
255 $this->defaultOutput
->writeln('<info><comment>Step 4 of 4.</comment> Config setup.</info>');
256 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
258 // cleanup before insert new stuff
259 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
263 'name' => 'share_public',
265 'section' => 'entry',
270 'section' => 'entry',
273 'name' => 'share_diaspora',
275 'section' => 'entry',
278 'name' => 'diaspora_url',
279 'value' => 'http://diasporapod.com',
280 'section' => 'entry',
283 'name' => 'share_shaarli',
285 'section' => 'entry',
288 'name' => 'shaarli_url',
289 'value' => 'http://myshaarli.com',
290 'section' => 'entry',
293 'name' => 'share_mail',
295 'section' => 'entry',
298 'name' => 'share_twitter',
300 'section' => 'entry',
303 'name' => 'export_epub',
305 'section' => 'export',
308 'name' => 'export_mobi',
310 'section' => 'export',
313 'name' => 'export_pdf',
315 'section' => 'export',
318 'name' => 'export_csv',
320 'section' => 'export',
323 'name' => 'export_json',
325 'section' => 'export',
328 'name' => 'export_txt',
330 'section' => 'export',
333 'name' => 'export_xml',
335 'section' => 'export',
338 'name' => 'import_with_redis',
340 'section' => 'import',
343 'name' => 'import_with_rabbitmq',
345 'section' => 'import',
348 'name' => 'show_printlink',
350 'section' => 'entry',
353 'name' => 'wallabag_support_url',
354 'value' => 'https://www.wallabag.org/pages/support.html',
358 'name' => 'wallabag_url',
359 'value' => 'http://v2.wallabag.org',
363 'name' => 'piwik_enabled',
365 'section' => 'analytics',
368 'name' => 'piwik_host',
369 'value' => 'http://v2.wallabag.org',
370 'section' => 'analytics',
373 'name' => 'piwik_site_id',
375 'section' => 'analytics',
378 'name' => 'demo_mode_enabled',
383 'name' => 'demo_mode_username',
384 'value' => 'wallabag',
389 foreach ($settings as $setting) {
390 $newSetting = new Setting();
391 $newSetting->setName($setting['name']);
392 $newSetting->setValue($setting['value']);
393 $newSetting->setSection($setting['section']);
394 $em->persist($newSetting);
399 $this->defaultOutput
->writeln('');
407 * @param string $command
408 * @param array $parameters Parameters to this command (usually 'force' => true)
410 protected function runCommand($command, $parameters = [])
412 $parameters = array_merge(
413 ['command' => $command],
416 '--no-debug' => true,
417 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
421 if ($this->defaultInput
->getOption('no-interaction')) {
422 $parameters = array_merge($parameters, ['--no-interaction' => true]);
425 $this->getApplication()->setAutoExit(false);
426 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
428 if (0 !== $exitCode) {
429 $this->getApplication()->setAutoExit(true);
431 $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode);
432 $this->defaultOutput
->writeln("<error>$errorMessage</error>");
433 $exception = new \
Exception($errorMessage, $exitCode);
438 // PDO does not always close the connection after Doctrine commands.
439 // See https://github.com/symfony/symfony/issues/11750.
440 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
446 * Check if the database already exists.
450 private function isDatabasePresent()
452 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
453 $databaseName = $connection->getDatabase();
456 $schemaManager = $connection->getSchemaManager();
457 } catch (\Exception
$exception) {
459 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
464 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
471 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
472 if ('sqlite' === $schemaManager->getDatabasePlatform()->getName()) {
473 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
475 if (isset($params['path']) && file_exists($params['path'])) {
483 return in_array($databaseName, $schemaManager->listDatabases());
484 } catch (\Doctrine\DBAL\Exception\DriverException
$e) {
485 // it means we weren't able to get database list, assume the database doesn't exist
492 * Check if the schema is already created.
493 * If we found at least oen table, it means the schema exists.
497 private function isSchemaPresent()
499 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
501 return count($schemaManager->listTableNames()) > 0 ? true : false;