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 = [
39 protected function configure()
42 ->setName('wallabag:install')
43 ->setDescription('Wallabag installer.')
47 InputOption
::VALUE_NONE
,
48 'Reset current database'
53 protected function execute(InputInterface
$input, OutputInterface
$output)
55 $this->defaultInput
= $input;
56 $this->defaultOutput
= $output;
58 $output->writeln('<info>Installing Wallabag...</info>');
68 $output->writeln('<info>Wallabag has been successfully installed.</info>');
69 $output->writeln('<comment>Just execute `php bin/console server:run --env=prod` for using wallabag: http://localhost:8000</comment>');
72 protected function checkRequirements()
74 $this->defaultOutput
->writeln('<info><comment>Step 1 of 4.</comment> Checking system requirements.</info>');
78 $label = '<comment>PDO Drivers</comment>';
79 if (extension_loaded('pdo_sqlite') || extension_loaded('pdo_mysql') || extension_loaded('pdo_pgsql')) {
80 $status = '<info>OK!</info>';
84 $status = '<error>ERROR!</error>';
85 $help = 'Needs one of sqlite, mysql or pgsql PDO drivers';
88 $rows[] = array($label, $status, $help);
90 foreach ($this->functionExists
as $functionRequired) {
91 $label = '<comment>'.$functionRequired.'</comment>';
93 if (function_exists($functionRequired)) {
94 $status = '<info>OK!</info>';
98 $status = '<error>ERROR!</error>';
99 $help = 'You need the '.$requirement.' function activated';
102 $rows[] = array($label, $status, $help);
105 $table = new Table($this->defaultOutput
);
107 ->setHeaders(array('Checked', 'Status', 'Recommendation'))
112 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
115 $this->defaultOutput
->writeln('<info>Success! Your system can run Wallabag properly.</info>');
117 $this->defaultOutput
->writeln('');
122 protected function setupDatabase()
124 $this->defaultOutput
->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
126 // user want to reset everything? Don't care about what is already here
127 if (true === $this->defaultInput
->getOption('reset')) {
128 $this->defaultOutput
->writeln('Droping database, creating database and schema, clearing the cache');
131 ->runCommand('doctrine:database:drop', array('--force' => true))
132 ->runCommand('doctrine:database:create')
133 ->runCommand('doctrine:schema:create')
134 ->runCommand('cache:clear')
137 $this->defaultOutput
->writeln('');
142 if (!$this->isDatabasePresent()) {
143 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
146 ->runCommand('doctrine:database:create')
147 ->runCommand('doctrine:schema:create')
148 ->runCommand('cache:clear')
151 $this->defaultOutput
->writeln('');
156 $questionHelper = $this->getHelper('question');
157 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
159 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
160 $this->defaultOutput
->writeln('Droping database, creating database and schema');
163 ->runCommand('doctrine:database:drop', array('--force' => true))
164 ->runCommand('doctrine:database:create')
165 ->runCommand('doctrine:schema:create')
167 } elseif ($this->isSchemaPresent()) {
168 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
169 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
170 $this->defaultOutput
->writeln('Droping schema and creating schema');
173 ->runCommand('doctrine:schema:drop', array('--force' => true))
174 ->runCommand('doctrine:schema:create')
178 $this->defaultOutput
->writeln('Creating schema');
181 ->runCommand('doctrine:schema:create')
185 $this->defaultOutput
->writeln('Clearing the cache');
186 $this->runCommand('cache:clear');
188 $this->defaultOutput
->writeln('');
193 protected function setupAdmin()
195 $this->defaultOutput
->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
197 $questionHelper = $this->getHelperSet()->get('question');
198 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (y/N)', true);
200 if (!$questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
204 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
206 $userManager = $this->getContainer()->get('fos_user.user_manager');
207 $user = $userManager->createUser();
209 $question = new Question('Username (default: wallabag) :', 'wallabag');
210 $user->setUsername($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
212 $question = new Question('Password (default: wallabag) :', 'wallabag');
213 $user->setPlainPassword($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
215 $question = new Question('Email:', '');
216 $user->setEmail($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
218 $user->setEnabled(true);
219 $user->addRole('ROLE_SUPER_ADMIN');
223 $config = new Config($user);
224 $config->setTheme($this->getContainer()->getParameter('wallabag_core.theme'));
225 $config->setItemsPerPage($this->getContainer()->getParameter('wallabag_core.items_on_page'));
226 $config->setRssLimit($this->getContainer()->getParameter('wallabag_core.rss_limit'));
227 $config->setLanguage($this->getContainer()->getParameter('wallabag_core.language'));
229 $em->persist($config);
231 // cleanup before insert new stuff
232 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
236 'name' => 'download_pictures',
238 'section' => 'entry',
243 'section' => 'entry',
246 'name' => 'share_diaspora',
248 'section' => 'entry',
251 'name' => 'diaspora_url',
252 'value' => 'http://diasporapod.com',
253 'section' => 'entry',
256 'name' => 'share_shaarli',
258 'section' => 'entry',
261 'name' => 'shaarli_url',
262 'value' => 'http://myshaarli.com',
263 'section' => 'entry',
266 'name' => 'share_mail',
268 'section' => 'entry',
271 'name' => 'share_twitter',
273 'section' => 'entry',
276 'name' => 'export_epub',
278 'section' => 'export',
281 'name' => 'export_mobi',
283 'section' => 'export',
286 'name' => 'export_pdf',
288 'section' => 'export',
291 'name' => 'export_csv',
293 'section' => 'export',
296 'name' => 'export_json',
298 'section' => 'export',
301 'name' => 'export_txt',
303 'section' => 'export',
306 'name' => 'export_xml',
308 'section' => 'export',
311 'name' => 'pocket_consumer_key',
313 'section' => 'import',
316 'name' => 'show_printlink',
318 'section' => 'entry',
321 'name' => 'wallabag_support_url',
322 'value' => 'https://www.wallabag.org/pages/support.html',
326 'name' => 'wallabag_url',
327 'value' => 'http://v2.wallabag.org',
331 'name' => 'piwik_enabled',
333 'section' => 'analytics',
336 'name' => 'piwik_host',
337 'value' => 'http://v2.wallabag.org',
338 'section' => 'analytics',
341 'name' => 'piwik_site_id',
343 'section' => 'analytics',
346 'name' => 'demo_mode_enabled',
351 'name' => 'demo_mode_username',
352 'value' => 'wallabag',
357 foreach ($settings as $setting) {
358 $newSetting = new Setting();
359 $newSetting->setName($setting['name']);
360 $newSetting->setValue($setting['value']);
361 $newSetting->setSection($setting['section']);
362 $em->persist($newSetting);
367 $this->defaultOutput
->writeln('');
372 protected function setupAsset()
374 $this->defaultOutput
->writeln('<info><comment>Step 4 of 4.</comment> Installing assets.</info>');
377 ->runCommand('assets:install')
378 ->runCommand('assetic:dump')
381 $this->defaultOutput
->writeln('');
389 * @param string $command
390 * @param array $parameters Parameters to this command (usually 'force' => true)
392 protected function runCommand($command, $parameters = array())
394 $parameters = array_merge(
395 array('command' => $command),
398 '--no-debug' => true,
399 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
403 if ($this->defaultInput
->getOption('no-interaction')) {
404 $parameters = array_merge($parameters, array('--no-interaction' => true));
407 $this->getApplication()->setAutoExit(false);
408 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
410 if (0 !== $exitCode) {
411 $this->getApplication()->setAutoExit(true);
413 $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode);
414 $this->defaultOutput
->writeln("<error>$errorMessage</error>");
415 $exception = new \
Exception($errorMessage, $exitCode);
420 // PDO does not always close the connection after Doctrine commands.
421 // See https://github.com/symfony/symfony/issues/11750.
422 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
428 * Check if the database already exists.
432 private function isDatabasePresent()
434 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
435 $databaseName = $connection->getDatabase();
438 $schemaManager = $connection->getSchemaManager();
439 } catch (\Exception
$exception) {
441 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
446 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
453 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
454 if ('sqlite' == $schemaManager->getDatabasePlatform()->getName()) {
455 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
457 if (isset($params['path']) && file_exists($params['path'])) {
464 return in_array($databaseName, $schemaManager->listDatabases());
468 * Check if the schema is already created.
469 * If we found at least oen table, it means the schema exists.
473 private function isSchemaPresent()
475 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
477 return count($schemaManager->listTableNames()) > 0 ? true : false;