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 $label = '<comment>PDO Drivers</comment>';
77 $status = '<info>OK!</info>';
79 if (!(extension_loaded('pdo_sqlite') || extension_loaded('pdo_mysql') || extension_loaded('pdo_pgsql'))) {
81 $status = '<error>ERROR!</error>';
82 $help = 'Needs one of sqlite, mysql or pgsql PDO drivers';
86 $rows[] = [$label, $status, $help];
88 foreach ($this->functionExists
as $functionRequired) {
89 $label = '<comment>'.$functionRequired.'</comment>';
90 $status = '<info>OK!</info>';
93 if (!function_exists($functionRequired)) {
95 $status = '<error>ERROR!</error>';
96 $help = 'You need the '.$functionRequired.' function activated';
99 $rows[] = [$label, $status, $help];
102 $table = new Table($this->defaultOutput
);
104 ->setHeaders(['Checked', 'Status', 'Recommendation'])
109 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
112 $this->defaultOutput
->writeln('<info>Success! Your system can run Wallabag properly.</info>');
114 $this->defaultOutput
->writeln('');
119 protected function setupDatabase()
121 $this->defaultOutput
->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
123 // user want to reset everything? Don't care about what is already here
124 if (true === $this->defaultInput
->getOption('reset')) {
125 $this->defaultOutput
->writeln('Droping database, creating database and schema, clearing the cache');
128 ->runCommand('doctrine:database:drop', ['--force' => true])
129 ->runCommand('doctrine:database:create')
130 ->runCommand('doctrine:schema:create')
131 ->runCommand('cache:clear')
134 $this->defaultOutput
->writeln('');
139 if (!$this->isDatabasePresent()) {
140 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
143 ->runCommand('doctrine:database:create')
144 ->runCommand('doctrine:schema:create')
145 ->runCommand('cache:clear')
148 $this->defaultOutput
->writeln('');
153 $questionHelper = $this->getHelper('question');
154 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
156 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
157 $this->defaultOutput
->writeln('Droping database, creating database and schema');
160 ->runCommand('doctrine:database:drop', ['--force' => true])
161 ->runCommand('doctrine:database:create')
162 ->runCommand('doctrine:schema:create')
164 } elseif ($this->isSchemaPresent()) {
165 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
166 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
167 $this->defaultOutput
->writeln('Droping schema and creating schema');
170 ->runCommand('doctrine:schema:drop', ['--force' => true])
171 ->runCommand('doctrine:schema:create')
175 $this->defaultOutput
->writeln('Creating schema');
178 ->runCommand('doctrine:schema:create')
182 $this->defaultOutput
->writeln('Clearing the cache');
183 $this->runCommand('cache:clear');
185 $this->defaultOutput
->writeln('');
190 protected function setupAdmin()
192 $this->defaultOutput
->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
194 $questionHelper = $this->getHelperSet()->get('question');
195 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true);
197 if (!$questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
201 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
203 $userManager = $this->getContainer()->get('fos_user.user_manager');
204 $user = $userManager->createUser();
206 $question = new Question('Username (default: wallabag) :', 'wallabag');
207 $user->setUsername($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
209 $question = new Question('Password (default: wallabag) :', 'wallabag');
210 $user->setPlainPassword($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
212 $question = new Question('Email:', '');
213 $user->setEmail($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
215 $user->setEnabled(true);
216 $user->addRole('ROLE_SUPER_ADMIN');
220 $config = new Config($user);
221 $config->setTheme($this->getContainer()->getParameter('wallabag_core.theme'));
222 $config->setItemsPerPage($this->getContainer()->getParameter('wallabag_core.items_on_page'));
223 $config->setRssLimit($this->getContainer()->getParameter('wallabag_core.rss_limit'));
224 $config->setReadingSpeed($this->getContainer()->getParameter('wallabag_core.reading_speed'));
225 $config->setLanguage($this->getContainer()->getParameter('wallabag_core.language'));
227 $em->persist($config);
229 // cleanup before insert new stuff
230 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
234 'name' => 'download_pictures',
236 'section' => 'entry',
241 'section' => 'entry',
244 'name' => 'share_diaspora',
246 'section' => 'entry',
249 'name' => 'diaspora_url',
250 'value' => 'http://diasporapod.com',
251 'section' => 'entry',
254 'name' => 'share_shaarli',
256 'section' => 'entry',
259 'name' => 'shaarli_url',
260 'value' => 'http://myshaarli.com',
261 'section' => 'entry',
264 'name' => 'share_mail',
266 'section' => 'entry',
269 'name' => 'share_twitter',
271 'section' => 'entry',
274 'name' => 'export_epub',
276 'section' => 'export',
279 'name' => 'export_mobi',
281 'section' => 'export',
284 'name' => 'export_pdf',
286 'section' => 'export',
289 'name' => 'export_csv',
291 'section' => 'export',
294 'name' => 'export_json',
296 'section' => 'export',
299 'name' => 'export_txt',
301 'section' => 'export',
304 'name' => 'export_xml',
306 'section' => 'export',
309 'name' => 'pocket_consumer_key',
311 'section' => 'import',
314 'name' => 'show_printlink',
316 'section' => 'entry',
319 'name' => 'wallabag_support_url',
320 'value' => 'https://www.wallabag.org/pages/support.html',
324 'name' => 'wallabag_url',
325 'value' => 'http://v2.wallabag.org',
329 'name' => 'piwik_enabled',
331 'section' => 'analytics',
334 'name' => 'piwik_host',
335 'value' => 'http://v2.wallabag.org',
336 'section' => 'analytics',
339 'name' => 'piwik_site_id',
341 'section' => 'analytics',
344 'name' => 'demo_mode_enabled',
349 'name' => 'demo_mode_username',
350 'value' => 'wallabag',
355 foreach ($settings as $setting) {
356 $newSetting = new Setting();
357 $newSetting->setName($setting['name']);
358 $newSetting->setValue($setting['value']);
359 $newSetting->setSection($setting['section']);
360 $em->persist($newSetting);
365 $this->defaultOutput
->writeln('');
370 protected function setupAsset()
372 $this->defaultOutput
->writeln('<info><comment>Step 4 of 4.</comment> Installing assets.</info>');
375 ->runCommand('assets:install')
376 ->runCommand('assetic:dump')
379 $this->defaultOutput
->writeln('');
387 * @param string $command
388 * @param array $parameters Parameters to this command (usually 'force' => true)
390 protected function runCommand($command, $parameters = [])
392 $parameters = array_merge(
393 ['command' => $command],
396 '--no-debug' => true,
397 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
401 if ($this->defaultInput
->getOption('no-interaction')) {
402 $parameters = array_merge($parameters, ['--no-interaction' => true]);
405 $this->getApplication()->setAutoExit(false);
406 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
408 if (0 !== $exitCode) {
409 $this->getApplication()->setAutoExit(true);
411 $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode);
412 $this->defaultOutput
->writeln("<error>$errorMessage</error>");
413 $exception = new \
Exception($errorMessage, $exitCode);
418 // PDO does not always close the connection after Doctrine commands.
419 // See https://github.com/symfony/symfony/issues/11750.
420 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
426 * Check if the database already exists.
430 private function isDatabasePresent()
432 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
433 $databaseName = $connection->getDatabase();
436 $schemaManager = $connection->getSchemaManager();
437 } catch (\Exception
$exception) {
439 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
444 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
451 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
452 if ('sqlite' == $schemaManager->getDatabasePlatform()->getName()) {
453 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
455 if (isset($params['path']) && file_exists($params['path'])) {
462 return in_array($databaseName, $schemaManager->listDatabases());
466 * Check if the schema is already created.
467 * If we found at least oen table, it means the schema exists.
471 private function isSchemaPresent()
473 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
475 return count($schemaManager->listTableNames()) > 0 ? true : false;