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>');
67 $output->writeln('<info>Wallabag has been successfully installed.</info>');
68 $output->writeln('<comment>Just execute `php bin/console server:run --env=prod` for using wallabag: http://localhost:8000</comment>');
71 protected function checkRequirements()
73 $this->defaultOutput
->writeln('<info><comment>Step 1 of 5.</comment> Checking system requirements.</info>');
77 $label = '<comment>PDO Driver</comment>';
78 $status = '<info>OK!</info>';
81 if (!extension_loaded($this->getContainer()->getParameter('database_driver'))) {
83 $status = '<error>ERROR!</error>';
84 $help = 'Database driver "'.$this->getContainer()->getParameter('database_driver').'" is not installed.';
88 $rows[] = [$label, $status, $help];
90 foreach ($this->functionExists
as $functionRequired) {
91 $label = '<comment>'.$functionRequired.'</comment>';
92 $status = '<info>OK!</info>';
95 if (!function_exists($functionRequired)) {
97 $status = '<error>ERROR!</error>';
98 $help = 'You need the '.$functionRequired.' function activated';
101 $rows[] = [$label, $status, $help];
104 $table = new Table($this->defaultOutput
);
106 ->setHeaders(['Checked', 'Status', 'Recommendation'])
111 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
114 $this->defaultOutput
->writeln('<info>Success! Your system can run Wallabag properly.</info>');
116 $this->defaultOutput
->writeln('');
121 protected function setupDatabase()
123 $this->defaultOutput
->writeln('<info><comment>Step 2 of 5.</comment> Setting up database.</info>');
125 // user want to reset everything? Don't care about what is already here
126 if (true === $this->defaultInput
->getOption('reset')) {
127 $this->defaultOutput
->writeln('Droping database, creating database and schema, clearing the cache');
130 ->runCommand('doctrine:database:drop', ['--force' => true])
131 ->runCommand('doctrine:database:create')
132 ->runCommand('doctrine:schema:create')
133 ->runCommand('cache:clear')
136 $this->defaultOutput
->writeln('');
141 if (!$this->isDatabasePresent()) {
142 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
145 ->runCommand('doctrine:database:create')
146 ->runCommand('doctrine:schema:create')
147 ->runCommand('cache:clear')
150 $this->defaultOutput
->writeln('');
155 $questionHelper = $this->getHelper('question');
156 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
158 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
159 $this->defaultOutput
->writeln('Droping database, creating database and schema');
162 ->runCommand('doctrine:database:drop', ['--force' => true])
163 ->runCommand('doctrine:database:create')
164 ->runCommand('doctrine:schema:create')
166 } elseif ($this->isSchemaPresent()) {
167 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
168 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
169 $this->defaultOutput
->writeln('Droping schema and creating schema');
172 ->runCommand('doctrine:schema:drop', ['--force' => true])
173 ->runCommand('doctrine:schema:create')
177 $this->defaultOutput
->writeln('Creating schema');
180 ->runCommand('doctrine:schema:create')
184 $this->defaultOutput
->writeln('Clearing the cache');
185 $this->runCommand('cache:clear');
187 $this->defaultOutput
->writeln('');
192 protected function setupAdmin()
194 $this->defaultOutput
->writeln('<info><comment>Step 3 of 5.</comment> Administration setup.</info>');
196 $questionHelper = $this->getHelperSet()->get('question');
197 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true);
199 if (!$questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
203 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
205 $userManager = $this->getContainer()->get('fos_user.user_manager');
206 $user = $userManager->createUser();
208 $question = new Question('Username (default: wallabag) :', 'wallabag');
209 $user->setUsername($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
211 $question = new Question('Password (default: wallabag) :', 'wallabag');
212 $user->setPlainPassword($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
214 $question = new Question('Email:', '');
215 $user->setEmail($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
217 $user->setEnabled(true);
218 $user->addRole('ROLE_SUPER_ADMIN');
222 $config = new Config($user);
223 $config->setTheme($this->getContainer()->getParameter('wallabag_core.theme'));
224 $config->setItemsPerPage($this->getContainer()->getParameter('wallabag_core.items_on_page'));
225 $config->setRssLimit($this->getContainer()->getParameter('wallabag_core.rss_limit'));
226 $config->setReadingSpeed($this->getContainer()->getParameter('wallabag_core.reading_speed'));
227 $config->setLanguage($this->getContainer()->getParameter('wallabag_core.language'));
229 $em->persist($config);
231 $this->defaultOutput
->writeln('');
236 protected function setupConfig()
238 $this->defaultOutput
->writeln('<info><comment>Step 4 of 5.</comment> Config setup.</info>');
239 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
241 // cleanup before insert new stuff
242 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
248 'section' => 'entry',
251 'name' => 'share_diaspora',
253 'section' => 'entry',
256 'name' => 'diaspora_url',
257 'value' => 'http://diasporapod.com',
258 'section' => 'entry',
261 'name' => 'share_shaarli',
263 'section' => 'entry',
266 'name' => 'shaarli_url',
267 'value' => 'http://myshaarli.com',
268 'section' => 'entry',
271 'name' => 'share_mail',
273 'section' => 'entry',
276 'name' => 'share_twitter',
278 'section' => 'entry',
281 'name' => 'export_epub',
283 'section' => 'export',
286 'name' => 'export_mobi',
288 'section' => 'export',
291 'name' => 'export_pdf',
293 'section' => 'export',
296 'name' => 'export_csv',
298 'section' => 'export',
301 'name' => 'export_json',
303 'section' => 'export',
306 'name' => 'export_txt',
308 'section' => 'export',
311 'name' => 'export_xml',
313 'section' => 'export',
316 'name' => 'pocket_consumer_key',
318 'section' => 'import',
321 'name' => 'show_printlink',
323 'section' => 'entry',
326 'name' => 'wallabag_support_url',
327 'value' => 'https://www.wallabag.org/pages/support.html',
331 'name' => 'wallabag_url',
332 'value' => 'http://v2.wallabag.org',
336 'name' => 'piwik_enabled',
338 'section' => 'analytics',
341 'name' => 'piwik_host',
342 'value' => 'http://v2.wallabag.org',
343 'section' => 'analytics',
346 'name' => 'piwik_site_id',
348 'section' => 'analytics',
351 'name' => 'demo_mode_enabled',
356 'name' => 'demo_mode_username',
357 'value' => 'wallabag',
362 foreach ($settings as $setting) {
363 $newSetting = new Setting();
364 $newSetting->setName($setting['name']);
365 $newSetting->setValue($setting['value']);
366 $newSetting->setSection($setting['section']);
367 $em->persist($newSetting);
372 $this->defaultOutput
->writeln('');
377 protected function setupAsset()
379 $this->defaultOutput
->writeln('<info><comment>Step 5 of 5.</comment> Installing assets.</info>');
382 ->runCommand('assets:install')
383 ->runCommand('assetic:dump')
386 $this->defaultOutput
->writeln('');
394 * @param string $command
395 * @param array $parameters Parameters to this command (usually 'force' => true)
397 protected function runCommand($command, $parameters = [])
399 $parameters = array_merge(
400 ['command' => $command],
403 '--no-debug' => true,
404 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
408 if ($this->defaultInput
->getOption('no-interaction')) {
409 $parameters = array_merge($parameters, ['--no-interaction' => true]);
412 $this->getApplication()->setAutoExit(false);
413 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
415 if (0 !== $exitCode) {
416 $this->getApplication()->setAutoExit(true);
418 $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode);
419 $this->defaultOutput
->writeln("<error>$errorMessage</error>");
420 $exception = new \
Exception($errorMessage, $exitCode);
425 // PDO does not always close the connection after Doctrine commands.
426 // See https://github.com/symfony/symfony/issues/11750.
427 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
433 * Check if the database already exists.
437 private function isDatabasePresent()
439 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
440 $databaseName = $connection->getDatabase();
443 $schemaManager = $connection->getSchemaManager();
444 } catch (\Exception
$exception) {
446 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
451 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
458 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
459 if ('sqlite' == $schemaManager->getDatabasePlatform()->getName()) {
460 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
462 if (isset($params['path']) && file_exists($params['path'])) {
470 return in_array($databaseName, $schemaManager->listDatabases());
471 } catch (\Doctrine\DBAL\Exception\DriverException
$e) {
472 // it means we weren't able to get database list, assume the database doesn't exist
479 * Check if the schema is already created.
480 * If we found at least oen table, it means the schema exists.
484 private function isSchemaPresent()
486 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
488 return count($schemaManager->listTableNames()) > 0 ? true : false;