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>');
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')
101 && false === strpos($e->getMessage(), 'database "'.$this->getContainer()->getParameter('database_name').'" does not exist')) {
103 $status = '<error>ERROR!</error>';
104 $help = 'Can\'t connect to the database: '.$e->getMessage();
108 $rows[] = [$label, $status, $help];
110 foreach ($this->functionExists
as $functionRequired) {
111 $label = '<comment>'.$functionRequired.'</comment>';
112 $status = '<info>OK!</info>';
115 if (!function_exists($functionRequired)) {
117 $status = '<error>ERROR!</error>';
118 $help = 'You need the '.$functionRequired.' function activated';
121 $rows[] = [$label, $status, $help];
124 $table = new Table($this->defaultOutput
);
126 ->setHeaders(['Checked', 'Status', 'Recommendation'])
131 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
134 $this->defaultOutput
->writeln('<info>Success! Your system can run Wallabag properly.</info>');
136 $this->defaultOutput
->writeln('');
141 protected function setupDatabase()
143 $this->defaultOutput
->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
145 // user want to reset everything? Don't care about what is already here
146 if (true === $this->defaultInput
->getOption('reset')) {
147 $this->defaultOutput
->writeln('Droping database, creating database and schema, clearing the cache');
150 ->runCommand('doctrine:database:drop', ['--force' => true])
151 ->runCommand('doctrine:database:create')
152 ->runCommand('doctrine:schema:create')
153 ->runCommand('cache:clear')
156 $this->defaultOutput
->writeln('');
161 if (!$this->isDatabasePresent()) {
162 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
165 ->runCommand('doctrine:database:create')
166 ->runCommand('doctrine:schema:create')
167 ->runCommand('cache:clear')
170 $this->defaultOutput
->writeln('');
175 $questionHelper = $this->getHelper('question');
176 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
178 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
179 $this->defaultOutput
->writeln('Droping database, creating database and schema');
182 ->runCommand('doctrine:database:drop', ['--force' => true])
183 ->runCommand('doctrine:database:create')
184 ->runCommand('doctrine:schema:create')
186 } elseif ($this->isSchemaPresent()) {
187 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
188 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
189 $this->defaultOutput
->writeln('Droping schema and creating schema');
192 ->runCommand('doctrine:schema:drop', ['--force' => true])
193 ->runCommand('doctrine:schema:create')
197 $this->defaultOutput
->writeln('Creating schema');
200 ->runCommand('doctrine:schema:create')
204 $this->defaultOutput
->writeln('Clearing the cache');
205 $this->runCommand('cache:clear');
207 $this->defaultOutput
->writeln('');
212 protected function setupAdmin()
214 $this->defaultOutput
->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
216 $questionHelper = $this->getHelperSet()->get('question');
217 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true);
219 if (!$questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
223 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
225 $userManager = $this->getContainer()->get('fos_user.user_manager');
226 $user = $userManager->createUser();
228 $question = new Question('Username (default: wallabag) :', 'wallabag');
229 $user->setUsername($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
231 $question = new Question('Password (default: wallabag) :', 'wallabag');
232 $user->setPlainPassword($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
234 $question = new Question('Email:', '');
235 $user->setEmail($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
237 $user->setEnabled(true);
238 $user->addRole('ROLE_SUPER_ADMIN');
242 // dispatch a created event so the associated config will be created
243 $event = new UserEvent($user);
244 $this->getContainer()->get('event_dispatcher')->dispatch(FOSUserEvents
::USER_CREATED
, $event);
246 $this->defaultOutput
->writeln('');
251 protected function setupConfig()
253 $this->defaultOutput
->writeln('<info><comment>Step 4 of 4.</comment> Config setup.</info>');
254 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
256 // cleanup before insert new stuff
257 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
261 'name' => 'share_public',
263 'section' => 'entry',
268 'section' => 'entry',
271 'name' => 'share_diaspora',
273 'section' => 'entry',
276 'name' => 'diaspora_url',
277 'value' => 'http://diasporapod.com',
278 'section' => 'entry',
281 'name' => 'share_shaarli',
283 'section' => 'entry',
286 'name' => 'shaarli_url',
287 'value' => 'http://myshaarli.com',
288 'section' => 'entry',
291 'name' => 'share_mail',
293 'section' => 'entry',
296 'name' => 'share_twitter',
298 'section' => 'entry',
301 'name' => 'export_epub',
303 'section' => 'export',
306 'name' => 'export_mobi',
308 'section' => 'export',
311 'name' => 'export_pdf',
313 'section' => 'export',
316 'name' => 'export_csv',
318 'section' => 'export',
321 'name' => 'export_json',
323 'section' => 'export',
326 'name' => 'export_txt',
328 'section' => 'export',
331 'name' => 'export_xml',
333 'section' => 'export',
336 'name' => 'import_with_redis',
338 'section' => 'import',
341 'name' => 'import_with_rabbitmq',
343 'section' => 'import',
346 'name' => 'show_printlink',
348 'section' => 'entry',
351 'name' => 'wallabag_support_url',
352 'value' => 'https://www.wallabag.org/pages/support.html',
356 'name' => 'wallabag_url',
357 'value' => 'http://v2.wallabag.org',
361 'name' => 'piwik_enabled',
363 'section' => 'analytics',
366 'name' => 'piwik_host',
367 'value' => 'v2.wallabag.org',
368 'section' => 'analytics',
371 'name' => 'piwik_site_id',
373 'section' => 'analytics',
376 'name' => 'demo_mode_enabled',
381 'name' => 'demo_mode_username',
382 'value' => 'wallabag',
387 foreach ($settings as $setting) {
388 $newSetting = new Setting();
389 $newSetting->setName($setting['name']);
390 $newSetting->setValue($setting['value']);
391 $newSetting->setSection($setting['section']);
392 $em->persist($newSetting);
397 $this->defaultOutput
->writeln('');
405 * @param string $command
406 * @param array $parameters Parameters to this command (usually 'force' => true)
408 protected function runCommand($command, $parameters = [])
410 $parameters = array_merge(
411 ['command' => $command],
414 '--no-debug' => true,
415 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
419 if ($this->defaultInput
->getOption('no-interaction')) {
420 $parameters = array_merge($parameters, ['--no-interaction' => true]);
423 $this->getApplication()->setAutoExit(false);
425 $output = new BufferedOutput();
426 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), $output);
428 if (0 !== $exitCode) {
429 $this->getApplication()->setAutoExit(true);
431 $this->defaultOutput
->writeln('');
432 $this->defaultOutput
->writeln('<error>The command "'.$command.'" generates some errors: </error>');
433 $this->defaultOutput
->writeln($output->fetch());
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;