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` 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 // @TODO: find a better way to check requirements
69 $label = '<comment>PCRE</comment>';
70 if (extension_loaded('pcre')) {
71 $status = '<info>OK!</info>';
75 $status = '<error>ERROR!</error>';
76 $help = 'You should enabled PCRE extension';
78 $rows[] = array($label, $status, $help);
80 $label = '<comment>DOM</comment>';
81 if (extension_loaded('DOM')) {
82 $status = '<info>OK!</info>';
86 $status = '<error>ERROR!</error>';
87 $help = 'You should enabled DOM extension';
89 $rows[] = array($label, $status, $help);
91 $table = new Table($this->defaultOutput
);
93 ->setHeaders(array('Checked', 'Status', 'Recommendation'))
98 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
101 $this->defaultOutput
->writeln('<info>Success! Your system can run Wallabag properly.</info>');
103 $this->defaultOutput
->writeln('');
108 protected function setupDatabase()
110 $this->defaultOutput
->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
112 // user want to reset everything? Don't care about what is already here
113 if (true === $this->defaultInput
->getOption('reset')) {
114 $this->defaultOutput
->writeln('Droping database, creating database and schema, clearing the cache');
117 ->runCommand('doctrine:database:drop', array('--force' => true))
118 ->runCommand('doctrine:database:create')
119 ->runCommand('doctrine:schema:create')
120 ->runCommand('cache:clear')
123 $this->defaultOutput
->writeln('');
128 if (!$this->isDatabasePresent()) {
129 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
132 ->runCommand('doctrine:database:create')
133 ->runCommand('doctrine:schema:create')
134 ->runCommand('cache:clear')
137 $this->defaultOutput
->writeln('');
142 $questionHelper = $this->getHelper('question');
143 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
145 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
146 $this->defaultOutput
->writeln('Droping database, creating database and schema');
149 ->runCommand('doctrine:database:drop', array('--force' => true))
150 ->runCommand('doctrine:database:create')
151 ->runCommand('doctrine:schema:create')
153 } elseif ($this->isSchemaPresent()) {
154 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
155 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
156 $this->defaultOutput
->writeln('Droping schema and creating schema');
159 ->runCommand('doctrine:schema:drop', array('--force' => true))
160 ->runCommand('doctrine:schema:create')
164 $this->defaultOutput
->writeln('Creating schema');
167 ->runCommand('doctrine:schema:create')
171 $this->defaultOutput
->writeln('Clearing the cache');
172 $this->runCommand('cache:clear');
174 $this->defaultOutput
->writeln('');
179 protected function setupAdmin()
181 $this->defaultOutput
->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
183 $questionHelper = $this->getHelperSet()->get('question');
184 $question = new ConfirmationQuestion('Would you like to create a new user ? (y/N)', false);
186 if (!$questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
190 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
192 $userManager = $this->getContainer()->get('fos_user.user_manager');
193 $user = $userManager->createUser();
195 $question = new Question('Username (default: wallabag) :', 'wallabag');
196 $user->setUsername($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
198 $question = new Question('Password (default: wallabag) :', 'wallabag');
199 $user->setPlainPassword($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
201 $question = new Question('Email:', '');
202 $user->setEmail($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
204 $user->setEnabled(true);
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' => 'pocket_consumer_key',
278 'section' => 'import',
281 'name' => 'show_printlink',
283 'section' => 'entry',
286 'name' => 'wallabag_support_url',
287 'value' => 'https://www.wallabag.org/pages/support.html',
291 'name' => 'wallabag_url',
292 'value' => 'http://v2.wallabag.org',
297 foreach ($settings as $setting) {
298 $newSetting = new Setting();
299 $newSetting->setName($setting['name']);
300 $newSetting->setValue($setting['value']);
301 $newSetting->setSection($setting['section']);
302 $em->persist($newSetting);
307 $this->defaultOutput
->writeln('');
312 protected function setupAsset()
314 $this->defaultOutput
->writeln('<info><comment>Step 4 of 4.</comment> Installing assets.</info>');
317 ->runCommand('assets:install')
318 ->runCommand('assetic:dump')
321 $this->defaultOutput
->writeln('');
329 * @param string $command
330 * @param array $parameters Parameters to this command (usually 'force' => true)
332 protected function runCommand($command, $parameters = array())
334 $parameters = array_merge(
335 array('command' => $command),
338 '--no-debug' => true,
339 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
343 if ($this->defaultInput
->getOption('no-interaction')) {
344 $parameters = array_merge($parameters, array('--no-interaction' => true));
347 $this->getApplication()->setAutoExit(false);
348 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
350 if (0 !== $exitCode) {
351 $this->getApplication()->setAutoExit(true);
353 $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode);
354 $this->defaultOutput
->writeln("<error>$errorMessage</error>");
355 $exception = new \
Exception($errorMessage, $exitCode);
360 // PDO does not always close the connection after Doctrine commands.
361 // See https://github.com/symfony/symfony/issues/11750.
362 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
368 * Check if the database already exists.
372 private function isDatabasePresent()
374 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
375 $databaseName = $connection->getDatabase();
378 $schemaManager = $connection->getSchemaManager();
379 } catch (\Exception
$exception) {
381 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
386 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
393 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
394 if ('sqlite' == $schemaManager->getDatabasePlatform()->getName()) {
395 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
397 if (isset($params['path']) && file_exists($params['path'])) {
404 return in_array($databaseName, $schemaManager->listDatabases());
408 * Check if the schema is already created.
409 * If we found at least oen table, it means the schema exists.
413 private function isSchemaPresent()
415 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
417 return count($schemaManager->listTableNames()) > 0 ? true : false;