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;
29 protected function configure()
32 ->setName('wallabag:install')
33 ->setDescription('Wallabag installer.')
37 InputOption
::VALUE_NONE
,
38 'Reset current database'
43 protected function execute(InputInterface
$input, OutputInterface
$output)
45 $this->defaultInput
= $input;
46 $this->defaultOutput
= $output;
48 $output->writeln('<info>Installing Wallabag...</info>');
58 $output->writeln('<info>Wallabag has been successfully installed.</info>');
59 $output->writeln('<comment>Just execute `php bin/console server:run --env=prod` for using wallabag: http://localhost:8000</comment>');
62 protected function checkRequirements()
64 $this->defaultOutput
->writeln('<info><comment>Step 1 of 4.</comment> Checking system requirements.</info>');
68 $label = '<comment>PCRE</comment>';
69 if (extension_loaded('pcre')) {
70 $status = '<info>OK!</info>';
74 $status = '<error>ERROR!</error>';
75 $help = 'You should enabled PCRE extension';
77 $rows[] = array($label, $status, $help);
79 $label = '<comment>DOM</comment>';
80 if (extension_loaded('DOM')) {
81 $status = '<info>OK!</info>';
85 $status = '<error>ERROR!</error>';
86 $help = 'You should enabled DOM extension';
88 $rows[] = array($label, $status, $help);
90 $table = new Table($this->defaultOutput
);
92 ->setHeaders(array('Checked', 'Status', 'Recommendation'))
97 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
100 $this->defaultOutput
->writeln('<info>Success! Your system can run Wallabag properly.</info>');
102 $this->defaultOutput
->writeln('');
107 protected function setupDatabase()
109 $this->defaultOutput
->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
111 // user want to reset everything? Don't care about what is already here
112 if (true === $this->defaultInput
->getOption('reset')) {
113 $this->defaultOutput
->writeln('Droping database, creating database and schema, clearing the cache');
116 ->runCommand('doctrine:database:drop', array('--force' => true))
117 ->runCommand('doctrine:database:create')
118 ->runCommand('doctrine:schema:create')
119 ->runCommand('cache:clear')
122 $this->defaultOutput
->writeln('');
127 if (!$this->isDatabasePresent()) {
128 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
131 ->runCommand('doctrine:database:create')
132 ->runCommand('doctrine:schema:create')
133 ->runCommand('cache:clear')
136 $this->defaultOutput
->writeln('');
141 $questionHelper = $this->getHelper('question');
142 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
144 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
145 $this->defaultOutput
->writeln('Droping database, creating database and schema');
148 ->runCommand('doctrine:database:drop', array('--force' => true))
149 ->runCommand('doctrine:database:create')
150 ->runCommand('doctrine:schema:create')
152 } elseif ($this->isSchemaPresent()) {
153 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
154 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
155 $this->defaultOutput
->writeln('Droping schema and creating schema');
158 ->runCommand('doctrine:schema:drop', array('--force' => true))
159 ->runCommand('doctrine:schema:create')
163 $this->defaultOutput
->writeln('Creating schema');
166 ->runCommand('doctrine:schema:create')
170 $this->defaultOutput
->writeln('Clearing the cache');
171 $this->runCommand('cache:clear');
173 $this->defaultOutput
->writeln('');
178 protected function setupAdmin()
180 $this->defaultOutput
->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
182 $questionHelper = $this->getHelperSet()->get('question');
183 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (y/N)', true);
185 if (!$questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
189 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
191 $userManager = $this->getContainer()->get('fos_user.user_manager');
192 $user = $userManager->createUser();
194 $question = new Question('Username (default: wallabag) :', 'wallabag');
195 $user->setUsername($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
197 $question = new Question('Password (default: wallabag) :', 'wallabag');
198 $user->setPlainPassword($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
200 $question = new Question('Email:', '');
201 $user->setEmail($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
203 $user->setEnabled(true);
204 $user->addRole('ROLE_SUPER_ADMIN');
208 $config = new Config($user);
209 $config->setTheme($this->getContainer()->getParameter('wallabag_core.theme'));
210 $config->setItemsPerPage($this->getContainer()->getParameter('wallabag_core.items_on_page'));
211 $config->setRssLimit($this->getContainer()->getParameter('wallabag_core.rss_limit'));
212 $config->setLanguage($this->getContainer()->getParameter('wallabag_core.language'));
214 $em->persist($config);
216 // cleanup before insert new stuff
217 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
221 'name' => 'download_pictures',
223 'section' => 'entry',
228 'section' => 'entry',
231 'name' => 'share_diaspora',
233 'section' => 'entry',
236 'name' => 'diaspora_url',
237 'value' => 'http://diasporapod.com',
238 'section' => 'entry',
241 'name' => 'share_shaarli',
243 'section' => 'entry',
246 'name' => 'shaarli_url',
247 'value' => 'http://myshaarli.com',
248 'section' => 'entry',
251 'name' => 'share_mail',
253 'section' => 'entry',
256 'name' => 'share_twitter',
258 'section' => 'entry',
261 'name' => 'export_epub',
263 'section' => 'export',
266 'name' => 'export_mobi',
268 'section' => 'export',
271 'name' => 'export_pdf',
273 'section' => 'export',
276 'name' => 'export_csv',
278 'section' => 'export',
281 'name' => 'export_json',
283 'section' => 'export',
286 'name' => 'export_txt',
288 'section' => 'export',
291 'name' => 'export_xml',
293 'section' => 'export',
296 'name' => 'pocket_consumer_key',
298 'section' => 'import',
301 'name' => 'show_printlink',
303 'section' => 'entry',
306 'name' => 'wallabag_support_url',
307 'value' => 'https://www.wallabag.org/pages/support.html',
311 'name' => 'wallabag_url',
312 'value' => 'http://v2.wallabag.org',
316 'name' => 'piwik_enabled',
318 'section' => 'analytics',
321 'name' => 'piwik_host',
322 'value' => 'http://v2.wallabag.org',
323 'section' => 'analytics',
326 'name' => 'piwik_site_id',
328 'section' => 'analytics',
331 'name' => 'demo_mode_enabled',
336 'name' => 'demo_mode_username',
337 'value' => 'wallabag',
342 foreach ($settings as $setting) {
343 $newSetting = new Setting();
344 $newSetting->setName($setting['name']);
345 $newSetting->setValue($setting['value']);
346 $newSetting->setSection($setting['section']);
347 $em->persist($newSetting);
352 $this->defaultOutput
->writeln('');
357 protected function setupAsset()
359 $this->defaultOutput
->writeln('<info><comment>Step 4 of 4.</comment> Installing assets.</info>');
362 ->runCommand('assets:install')
363 ->runCommand('assetic:dump')
366 $this->defaultOutput
->writeln('');
374 * @param string $command
375 * @param array $parameters Parameters to this command (usually 'force' => true)
377 protected function runCommand($command, $parameters = array())
379 $parameters = array_merge(
380 array('command' => $command),
383 '--no-debug' => true,
384 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
388 if ($this->defaultInput
->getOption('no-interaction')) {
389 $parameters = array_merge($parameters, array('--no-interaction' => true));
392 $this->getApplication()->setAutoExit(false);
393 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
395 if (0 !== $exitCode) {
396 $this->getApplication()->setAutoExit(true);
398 $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode);
399 $this->defaultOutput
->writeln("<error>$errorMessage</error>");
400 $exception = new \
Exception($errorMessage, $exitCode);
405 // PDO does not always close the connection after Doctrine commands.
406 // See https://github.com/symfony/symfony/issues/11750.
407 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
413 * Check if the database already exists.
417 private function isDatabasePresent()
419 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
420 $databaseName = $connection->getDatabase();
423 $schemaManager = $connection->getSchemaManager();
424 } catch (\Exception
$exception) {
426 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
431 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
438 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
439 if ('sqlite' == $schemaManager->getDatabasePlatform()->getName()) {
440 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
442 if (isset($params['path']) && file_exists($params['path'])) {
449 return in_array($databaseName, $schemaManager->listDatabases());
453 * Check if the schema is already created.
454 * If we found at least oen table, it means the schema exists.
458 private function isSchemaPresent()
460 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
462 return count($schemaManager->listTableNames()) > 0 ? true : false;