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 $requirements = [
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 foreach ($this->requirements
as $requirement) {
77 $label = '<comment>'.strtoupper($requirement).'</comment>';
78 if (extension_loaded($requirement)) {
79 $status = '<info>OK!</info>';
83 $status = '<error>ERROR!</error>';
84 $help = 'You should enabled '.$requirement.' extension';
86 $rows[] = array($label, $status, $help);
89 $table = new Table($this->defaultOutput
);
91 ->setHeaders(array('Checked', 'Status', 'Recommendation'))
96 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
99 $this->defaultOutput
->writeln('<info>Success! Your system can run Wallabag properly.</info>');
101 $this->defaultOutput
->writeln('');
106 protected function setupDatabase()
108 $this->defaultOutput
->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
110 // user want to reset everything? Don't care about what is already here
111 if (true === $this->defaultInput
->getOption('reset')) {
112 $this->defaultOutput
->writeln('Droping database, creating database and schema, clearing the cache');
115 ->runCommand('doctrine:database:drop', array('--force' => true))
116 ->runCommand('doctrine:database:create')
117 ->runCommand('doctrine:schema:create')
118 ->runCommand('cache:clear')
121 $this->defaultOutput
->writeln('');
126 if (!$this->isDatabasePresent()) {
127 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
130 ->runCommand('doctrine:database:create')
131 ->runCommand('doctrine:schema:create')
132 ->runCommand('cache:clear')
135 $this->defaultOutput
->writeln('');
140 $questionHelper = $this->getHelper('question');
141 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
143 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
144 $this->defaultOutput
->writeln('Droping database, creating database and schema');
147 ->runCommand('doctrine:database:drop', array('--force' => true))
148 ->runCommand('doctrine:database:create')
149 ->runCommand('doctrine:schema:create')
151 } elseif ($this->isSchemaPresent()) {
152 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
153 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
154 $this->defaultOutput
->writeln('Droping schema and creating schema');
157 ->runCommand('doctrine:schema:drop', array('--force' => true))
158 ->runCommand('doctrine:schema:create')
162 $this->defaultOutput
->writeln('Creating schema');
165 ->runCommand('doctrine:schema:create')
169 $this->defaultOutput
->writeln('Clearing the cache');
170 $this->runCommand('cache:clear');
172 $this->defaultOutput
->writeln('');
177 protected function setupAdmin()
179 $this->defaultOutput
->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
181 $questionHelper = $this->getHelperSet()->get('question');
182 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (y/N)', true);
184 if (!$questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
188 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
190 $userManager = $this->getContainer()->get('fos_user.user_manager');
191 $user = $userManager->createUser();
193 $question = new Question('Username (default: wallabag) :', 'wallabag');
194 $user->setUsername($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
196 $question = new Question('Password (default: wallabag) :', 'wallabag');
197 $user->setPlainPassword($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
199 $question = new Question('Email:', '');
200 $user->setEmail($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
202 $user->setEnabled(true);
203 $user->addRole('ROLE_SUPER_ADMIN');
207 $config = new Config($user);
208 $config->setTheme($this->getContainer()->getParameter('wallabag_core.theme'));
209 $config->setItemsPerPage($this->getContainer()->getParameter('wallabag_core.items_on_page'));
210 $config->setRssLimit($this->getContainer()->getParameter('wallabag_core.rss_limit'));
211 $config->setLanguage($this->getContainer()->getParameter('wallabag_core.language'));
213 $em->persist($config);
215 // cleanup before insert new stuff
216 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
220 'name' => 'download_pictures',
222 'section' => 'entry',
227 'section' => 'entry',
230 'name' => 'share_diaspora',
232 'section' => 'entry',
235 'name' => 'diaspora_url',
236 'value' => 'http://diasporapod.com',
237 'section' => 'entry',
240 'name' => 'share_shaarli',
242 'section' => 'entry',
245 'name' => 'shaarli_url',
246 'value' => 'http://myshaarli.com',
247 'section' => 'entry',
250 'name' => 'share_mail',
252 'section' => 'entry',
255 'name' => 'share_twitter',
257 'section' => 'entry',
260 'name' => 'export_epub',
262 'section' => 'export',
265 'name' => 'export_mobi',
267 'section' => 'export',
270 'name' => 'export_pdf',
272 'section' => 'export',
275 'name' => 'export_csv',
277 'section' => 'export',
280 'name' => 'export_json',
282 'section' => 'export',
285 'name' => 'export_txt',
287 'section' => 'export',
290 'name' => 'export_xml',
292 'section' => 'export',
295 'name' => 'pocket_consumer_key',
297 'section' => 'import',
300 'name' => 'show_printlink',
302 'section' => 'entry',
305 'name' => 'wallabag_support_url',
306 'value' => 'https://www.wallabag.org/pages/support.html',
310 'name' => 'wallabag_url',
311 'value' => 'http://v2.wallabag.org',
315 'name' => 'piwik_enabled',
317 'section' => 'analytics',
320 'name' => 'piwik_host',
321 'value' => 'http://v2.wallabag.org',
322 'section' => 'analytics',
325 'name' => 'piwik_site_id',
327 'section' => 'analytics',
330 'name' => 'demo_mode_enabled',
335 'name' => 'demo_mode_username',
336 'value' => 'wallabag',
341 foreach ($settings as $setting) {
342 $newSetting = new Setting();
343 $newSetting->setName($setting['name']);
344 $newSetting->setValue($setting['value']);
345 $newSetting->setSection($setting['section']);
346 $em->persist($newSetting);
351 $this->defaultOutput
->writeln('');
356 protected function setupAsset()
358 $this->defaultOutput
->writeln('<info><comment>Step 4 of 4.</comment> Installing assets.</info>');
361 ->runCommand('assets:install')
362 ->runCommand('assetic:dump')
365 $this->defaultOutput
->writeln('');
373 * @param string $command
374 * @param array $parameters Parameters to this command (usually 'force' => true)
376 protected function runCommand($command, $parameters = array())
378 $parameters = array_merge(
379 array('command' => $command),
382 '--no-debug' => true,
383 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
387 if ($this->defaultInput
->getOption('no-interaction')) {
388 $parameters = array_merge($parameters, array('--no-interaction' => true));
391 $this->getApplication()->setAutoExit(false);
392 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
394 if (0 !== $exitCode) {
395 $this->getApplication()->setAutoExit(true);
397 $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode);
398 $this->defaultOutput
->writeln("<error>$errorMessage</error>");
399 $exception = new \
Exception($errorMessage, $exitCode);
404 // PDO does not always close the connection after Doctrine commands.
405 // See https://github.com/symfony/symfony/issues/11750.
406 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
412 * Check if the database already exists.
416 private function isDatabasePresent()
418 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
419 $databaseName = $connection->getDatabase();
422 $schemaManager = $connection->getSchemaManager();
423 } catch (\Exception
$exception) {
425 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
430 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
437 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
438 if ('sqlite' == $schemaManager->getDatabasePlatform()->getName()) {
439 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
441 if (isset($params['path']) && file_exists($params['path'])) {
448 return in_array($databaseName, $schemaManager->listDatabases());
452 * Check if the schema is already created.
453 * If we found at least oen table, it means the schema exists.
457 private function isSchemaPresent()
459 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
461 return count($schemaManager->listTableNames()) > 0 ? true : false;