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 Driver</comment>';
77 $status = '<info>OK!</info>';
80 if (!extension_loaded($this->getContainer()->getParameter('database_driver'))) {
82 $status = '<error>ERROR!</error>';
83 $help = 'Database driver "'.$this->getContainer()->getParameter('database_driver').'" is not installed.';
87 $rows[] = [$label, $status, $help];
89 foreach ($this->functionExists
as $functionRequired) {
90 $label = '<comment>'.$functionRequired.'</comment>';
91 $status = '<info>OK!</info>';
94 if (!function_exists($functionRequired)) {
96 $status = '<error>ERROR!</error>';
97 $help = 'You need the '.$functionRequired.' function activated';
100 $rows[] = [$label, $status, $help];
103 $table = new Table($this->defaultOutput
);
105 ->setHeaders(['Checked', 'Status', 'Recommendation'])
110 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
113 $this->defaultOutput
->writeln('<info>Success! Your system can run Wallabag properly.</info>');
115 $this->defaultOutput
->writeln('');
120 protected function setupDatabase()
122 $this->defaultOutput
->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
124 // user want to reset everything? Don't care about what is already here
125 if (true === $this->defaultInput
->getOption('reset')) {
126 $this->defaultOutput
->writeln('Droping database, creating database and schema, clearing the cache');
129 ->runCommand('doctrine:database:drop', ['--force' => true])
130 ->runCommand('doctrine:database:create')
131 ->runCommand('doctrine:schema:create')
132 ->runCommand('cache:clear')
135 $this->defaultOutput
->writeln('');
140 if (!$this->isDatabasePresent()) {
141 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
144 ->runCommand('doctrine:database:create')
145 ->runCommand('doctrine:schema:create')
146 ->runCommand('cache:clear')
149 $this->defaultOutput
->writeln('');
154 $questionHelper = $this->getHelper('question');
155 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
157 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
158 $this->defaultOutput
->writeln('Droping database, creating database and schema');
161 ->runCommand('doctrine:database:drop', ['--force' => true])
162 ->runCommand('doctrine:database:create')
163 ->runCommand('doctrine:schema:create')
165 } elseif ($this->isSchemaPresent()) {
166 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
167 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
168 $this->defaultOutput
->writeln('Droping schema and creating schema');
171 ->runCommand('doctrine:schema:drop', ['--force' => true])
172 ->runCommand('doctrine:schema:create')
176 $this->defaultOutput
->writeln('Creating schema');
179 ->runCommand('doctrine:schema:create')
183 $this->defaultOutput
->writeln('Clearing the cache');
184 $this->runCommand('cache:clear');
186 $this->defaultOutput
->writeln('');
191 protected function setupAdmin()
193 $this->defaultOutput
->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
195 $questionHelper = $this->getHelperSet()->get('question');
196 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true);
198 if (!$questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
202 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
204 $userManager = $this->getContainer()->get('fos_user.user_manager');
205 $user = $userManager->createUser();
207 $question = new Question('Username (default: wallabag) :', 'wallabag');
208 $user->setUsername($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
210 $question = new Question('Password (default: wallabag) :', 'wallabag');
211 $user->setPlainPassword($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
213 $question = new Question('Email:', '');
214 $user->setEmail($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
216 $user->setEnabled(true);
217 $user->addRole('ROLE_SUPER_ADMIN');
221 $config = new Config($user);
222 $config->setTheme($this->getContainer()->getParameter('wallabag_core.theme'));
223 $config->setItemsPerPage($this->getContainer()->getParameter('wallabag_core.items_on_page'));
224 $config->setRssLimit($this->getContainer()->getParameter('wallabag_core.rss_limit'));
225 $config->setReadingSpeed($this->getContainer()->getParameter('wallabag_core.reading_speed'));
226 $config->setLanguage($this->getContainer()->getParameter('wallabag_core.language'));
228 $em->persist($config);
230 $this->defaultOutput
->writeln('');
235 protected function setupConfig()
237 $this->defaultOutput
->writeln('<info><comment>Step 4 of 4.</comment> Config setup.</info>');
238 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
240 // cleanup before insert new stuff
241 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
245 'name' => 'share_public',
247 'section' => 'entry',
252 'section' => 'entry',
255 'name' => 'share_diaspora',
257 'section' => 'entry',
260 'name' => 'diaspora_url',
261 'value' => 'http://diasporapod.com',
262 'section' => 'entry',
265 'name' => 'share_shaarli',
267 'section' => 'entry',
270 'name' => 'shaarli_url',
271 'value' => 'http://myshaarli.com',
272 'section' => 'entry',
275 'name' => 'share_mail',
277 'section' => 'entry',
280 'name' => 'share_twitter',
282 'section' => 'entry',
285 'name' => 'export_epub',
287 'section' => 'export',
290 'name' => 'export_mobi',
292 'section' => 'export',
295 'name' => 'export_pdf',
297 'section' => 'export',
300 'name' => 'export_csv',
302 'section' => 'export',
305 'name' => 'export_json',
307 'section' => 'export',
310 'name' => 'export_txt',
312 'section' => 'export',
315 'name' => 'export_xml',
317 'section' => 'export',
320 'name' => 'pocket_consumer_key',
322 'section' => 'import',
325 'name' => 'show_printlink',
327 'section' => 'entry',
330 'name' => 'wallabag_support_url',
331 'value' => 'https://www.wallabag.org/pages/support.html',
335 'name' => 'wallabag_url',
336 'value' => 'http://v2.wallabag.org',
340 'name' => 'piwik_enabled',
342 'section' => 'analytics',
345 'name' => 'piwik_host',
346 'value' => 'http://v2.wallabag.org',
347 'section' => 'analytics',
350 'name' => 'piwik_site_id',
352 'section' => 'analytics',
355 'name' => 'demo_mode_enabled',
360 'name' => 'demo_mode_username',
361 'value' => 'wallabag',
366 foreach ($settings as $setting) {
367 $newSetting = new Setting();
368 $newSetting->setName($setting['name']);
369 $newSetting->setValue($setting['value']);
370 $newSetting->setSection($setting['section']);
371 $em->persist($newSetting);
376 $this->defaultOutput
->writeln('');
384 * @param string $command
385 * @param array $parameters Parameters to this command (usually 'force' => true)
387 protected function runCommand($command, $parameters = [])
389 $parameters = array_merge(
390 ['command' => $command],
393 '--no-debug' => true,
394 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
398 if ($this->defaultInput
->getOption('no-interaction')) {
399 $parameters = array_merge($parameters, ['--no-interaction' => true]);
402 $this->getApplication()->setAutoExit(false);
403 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
405 if (0 !== $exitCode) {
406 $this->getApplication()->setAutoExit(true);
408 $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode);
409 $this->defaultOutput
->writeln("<error>$errorMessage</error>");
410 $exception = new \
Exception($errorMessage, $exitCode);
415 // PDO does not always close the connection after Doctrine commands.
416 // See https://github.com/symfony/symfony/issues/11750.
417 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
423 * Check if the database already exists.
427 private function isDatabasePresent()
429 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
430 $databaseName = $connection->getDatabase();
433 $schemaManager = $connection->getSchemaManager();
434 } catch (\Exception
$exception) {
436 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
441 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
448 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
449 if ('sqlite' == $schemaManager->getDatabasePlatform()->getName()) {
450 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
452 if (isset($params['path']) && file_exists($params['path'])) {
460 return in_array($databaseName, $schemaManager->listDatabases());
461 } catch (\Doctrine\DBAL\Exception\DriverException
$e) {
462 // it means we weren't able to get database list, assume the database doesn't exist
469 * Check if the schema is already created.
470 * If we found at least oen table, it means the schema exists.
474 private function isSchemaPresent()
476 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
478 return count($schemaManager->listTableNames()) > 0 ? true : false;