3 namespace Wallabag\CoreBundle\Command
;
5 use FOS\UserBundle\Event\UserEvent
;
6 use FOS\UserBundle\FOSUserEvents
;
7 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand
;
8 use Symfony\Component\Console\Input\ArrayInput
;
9 use Symfony\Component\Console\Input\InputInterface
;
10 use Symfony\Component\Console\Input\InputOption
;
11 use Symfony\Component\Console\Output\BufferedOutput
;
12 use Symfony\Component\Console\Output\OutputInterface
;
13 use Symfony\Component\Console\Question\Question
;
14 use Symfony\Component\Console\Style\SymfonyStyle
;
15 use Wallabag\CoreBundle\Entity\IgnoreOriginInstanceRule
;
16 use Wallabag\CoreBundle\Entity\InternalSetting
;
18 class InstallCommand
extends ContainerAwareCommand
23 protected $defaultInput;
33 protected $functionExists = [
38 protected function configure()
41 ->setName('wallabag:install')
42 ->setDescription('wallabag installer.')
46 InputOption
::VALUE_NONE
,
47 'Reset current database'
52 protected function execute(InputInterface
$input, OutputInterface
$output)
54 $this->defaultInput
= $input;
56 $this->io
= new SymfonyStyle($input, $output);
58 $this->io
->title('wallabag installer');
67 $this->io
->success('wallabag has been successfully installed.');
68 $this->io
->success('You can now configure your web server, see https://doc.wallabag.org');
71 protected function checkRequirements()
73 $this->io
->section('Step 1 of 4: Checking system requirements.');
75 $doctrineManager = $this->getContainer()->get('doctrine')->getManager();
79 // testing if database driver exists
81 $label = '<comment>PDO Driver (%s)</comment>';
82 $status = '<info>OK!</info>';
85 if (!\
extension_loaded($this->getContainer()->getParameter('database_driver'))) {
87 $status = '<error>ERROR!</error>';
88 $help = 'Database driver "' . $this->getContainer()->getParameter('database_driver') . '" is not installed.';
91 $rows[] = [sprintf($label, $this->getContainer()->getParameter('database_driver')), $status, $help];
93 // testing if connection to the database can be etablished
94 $label = '<comment>Database connection</comment>';
95 $status = '<info>OK!</info>';
98 $conn = $this->getContainer()->get('doctrine')->getManager()->getConnection();
102 } catch (\Exception
$e) {
103 if (false === strpos($e->getMessage(), 'Unknown database')
104 && false === strpos($e->getMessage(), 'database "' . $this->getContainer()->getParameter('database_name') . '" does not exist')) {
106 $status = '<error>ERROR!</error>';
107 $help = 'Can\'t connect to the database: ' . $e->getMessage();
111 $rows[] = [$label, $status, $help];
113 // check MySQL & PostgreSQL version
114 $label = '<comment>Database version</comment>';
115 $status = '<info>OK!</info>';
118 // now check if MySQL isn't too old to handle utf8mb4
119 if ($conn->isConnected() && 'mysql' === $conn->getDatabasePlatform()->getName()) {
120 $version = $conn->query('select version()')->fetchColumn();
121 $minimalVersion = '5.5.4';
123 if (false === version_compare($version, $minimalVersion, '>')) {
125 $status = '<error>ERROR!</error>';
126 $help = 'Your MySQL version (' . $version . ') is too old, consider upgrading (' . $minimalVersion . '+).';
130 // testing if PostgreSQL > 9.1
131 if ($conn->isConnected() && 'postgresql' === $conn->getDatabasePlatform()->getName()) {
132 // return version should be like "PostgreSQL 9.5.4 on x86_64-apple-darwin15.6.0, compiled by Apple LLVM version 8.0.0 (clang-800.0.38), 64-bit"
133 $version = $doctrineManager->getConnection()->query('SELECT version();')->fetchColumn();
135 preg_match('/PostgreSQL ([0-9\.]+)/i', $version, $matches);
137 if (isset($matches[1]) & version_compare($matches[1], '9.2.0', '<')) {
139 $status = '<error>ERROR!</error>';
140 $help = 'PostgreSQL should be greater than 9.1 (actual version: ' . $matches[1] . ')';
144 $rows[] = [$label, $status, $help];
146 foreach ($this->functionExists
as $functionRequired) {
147 $label = '<comment>' . $functionRequired . '</comment>';
148 $status = '<info>OK!</info>';
151 if (!\function_exists
($functionRequired)) {
153 $status = '<error>ERROR!</error>';
154 $help = 'You need the ' . $functionRequired . ' function activated';
157 $rows[] = [$label, $status, $help];
160 $this->io
->table(['Checked', 'Status', 'Recommendation'], $rows);
163 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
166 $this->io
->success('Success! Your system can run wallabag properly.');
171 protected function setupDatabase()
173 $this->io
->section('Step 2 of 4: Setting up database.');
175 // user want to reset everything? Don't care about what is already here
176 if (true === $this->defaultInput
->getOption('reset')) {
177 $this->io
->text('Dropping database, creating database and schema, clearing the cache');
180 ->runCommand('doctrine:database:drop', ['--force' => true])
181 ->runCommand('doctrine:database:create')
182 ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true])
183 ->runCommand('cache:clear')
186 $this->io
->newLine();
191 if (!$this->isDatabasePresent()) {
192 $this->io
->text('Creating database and schema, clearing the cache');
195 ->runCommand('doctrine:database:create')
196 ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true])
197 ->runCommand('cache:clear')
200 $this->io
->newLine();
205 if ($this->io
->confirm('It appears that your database already exists. Would you like to reset it?', false)) {
206 $this->io
->text('Dropping database, creating database and schema...');
209 ->runCommand('doctrine:database:drop', ['--force' => true])
210 ->runCommand('doctrine:database:create')
211 ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true])
213 } elseif ($this->isSchemaPresent()) {
214 if ($this->io
->confirm('Seems like your database contains schema. Do you want to reset it?', false)) {
215 $this->io
->text('Dropping schema and creating schema...');
218 ->runCommand('doctrine:schema:drop', ['--force' => true])
219 ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true])
223 $this->io
->text('Creating schema...');
226 ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true])
230 $this->io
->text('Clearing the cache...');
231 $this->runCommand('cache:clear');
233 $this->io
->newLine();
234 $this->io
->text('<info>Database successfully setup.</info>');
239 protected function setupAdmin()
241 $this->io
->section('Step 3 of 4: Administration setup.');
243 if (!$this->io
->confirm('Would you like to create a new admin user (recommended)?', true)) {
247 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
249 $userManager = $this->getContainer()->get('fos_user.user_manager');
250 $user = $userManager->createUser();
252 $user->setUsername($this->io
->ask('Username', 'wallabag'));
254 $question = new Question('Password', 'wallabag');
255 $question->setHidden(true);
256 $user->setPlainPassword($this->io
->askQuestion($question));
258 $user->setEmail($this->io
->ask('Email', 'wallabag@wallabag.io'));
260 $user->setEnabled(true);
261 $user->addRole('ROLE_SUPER_ADMIN');
265 // dispatch a created event so the associated config will be created
266 $event = new UserEvent($user);
267 $this->getContainer()->get('event_dispatcher')->dispatch(FOSUserEvents
::USER_CREATED
, $event);
269 $this->io
->text('<info>Administration successfully setup.</info>');
274 protected function setupConfig()
276 $this->io
->section('Step 4 of 4: Config setup.');
277 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
279 // cleanup before insert new stuff
280 $em->createQuery('DELETE FROM WallabagCoreBundle:InternalSetting')->execute();
281 $em->createQuery('DELETE FROM WallabagCoreBundle:IgnoreOriginInstanceRule')->execute();
283 foreach ($this->getContainer()->getParameter('wallabag_core.default_internal_settings') as $setting) {
284 $newSetting = new InternalSetting();
285 $newSetting->setName($setting['name']);
286 $newSetting->setValue($setting['value']);
287 $newSetting->setSection($setting['section']);
288 $em->persist($newSetting);
291 foreach ($this->getContainer()->getParameter('wallabag_core.default_ignore_origin_instance_rules') as $ignore_origin_instance_rule) {
292 $newIgnoreOriginInstanceRule = new IgnoreOriginInstanceRule();
293 $newIgnoreOriginInstanceRule->setRule($ignore_origin_instance_rule['rule']);
294 $em->persist($newIgnoreOriginInstanceRule);
299 $this->io
->text('<info>Config successfully setup.</info>');
307 * @param string $command
308 * @param array $parameters Parameters to this command (usually 'force' => true)
310 protected function runCommand($command, $parameters = [])
312 $parameters = array_merge(
313 ['command' => $command],
316 '--no-debug' => true,
317 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
321 if ($this->defaultInput
->getOption('no-interaction')) {
322 $parameters = array_merge($parameters, ['--no-interaction' => true]);
325 $this->getApplication()->setAutoExit(false);
327 $output = new BufferedOutput();
328 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), $output);
330 // PDO does not always close the connection after Doctrine commands.
331 // See https://github.com/symfony/symfony/issues/11750.
332 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
334 if (0 !== $exitCode) {
335 $this->getApplication()->setAutoExit(true);
337 throw new \
RuntimeException('The command "' . $command . "\" generates some errors: \n\n" . $output->fetch());
344 * Check if the database already exists.
348 private function isDatabasePresent()
350 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
351 $databaseName = $connection->getDatabase();
354 $schemaManager = $connection->getSchemaManager();
355 } catch (\Exception
$exception) {
357 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
362 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
369 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
370 if ('sqlite' === $schemaManager->getDatabasePlatform()->getName()) {
371 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
373 if (isset($params['path']) && file_exists($params['path'])) {
381 return \
in_array($databaseName, $schemaManager->listDatabases(), true);
382 } catch (\Doctrine\DBAL\Exception\DriverException
$e) {
383 // it means we weren't able to get database list, assume the database doesn't exist
390 * Check if the schema is already created.
391 * If we found at least oen table, it means the schema exists.
395 private function isSchemaPresent()
397 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
399 return \
count($schemaManager->listTableNames()) > 0 ? true : false;