3 namespace Wallabag\CoreBundle\Command
;
5 use Craue\ConfigBundle\Entity\Setting
;
6 use FOS\UserBundle\Event\UserEvent
;
7 use FOS\UserBundle\FOSUserEvents
;
8 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand
;
9 use Symfony\Component\Console\Input\ArrayInput
;
10 use Symfony\Component\Console\Input\InputInterface
;
11 use Symfony\Component\Console\Input\InputOption
;
12 use Symfony\Component\Console\Output\BufferedOutput
;
13 use Symfony\Component\Console\Output\OutputInterface
;
14 use Symfony\Component\Console\Question\Question
;
15 use Symfony\Component\Console\Style\SymfonyStyle
;
17 class InstallCommand
extends ContainerAwareCommand
22 protected $defaultInput;
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;
55 $this->io
= new SymfonyStyle($input, $output);
57 $this->io
->title('Wallabag installer');
66 $this->io
->success('Wallabag has been successfully installed.');
67 $this->io
->success('You can now configure your web server, see https://doc.wallabag.org');
70 protected function checkRequirements()
72 $this->io
->section('Step 1 of 4: Checking system requirements.');
74 $doctrineManager = $this->getContainer()->get('doctrine')->getManager();
78 // testing if database driver exists
80 $label = '<comment>PDO Driver (%s)</comment>';
81 $status = '<info>OK!</info>';
84 if (!extension_loaded($this->getContainer()->getParameter('database_driver'))) {
86 $status = '<error>ERROR!</error>';
87 $help = 'Database driver "' . $this->getContainer()->getParameter('database_driver') . '" is not installed.';
90 $rows[] = [sprintf($label, $this->getContainer()->getParameter('database_driver')), $status, $help];
92 // testing if connection to the database can be etablished
93 $label = '<comment>Database connection</comment>';
94 $status = '<info>OK!</info>';
98 $conn = $this->getContainer()->get('doctrine')->getManager()->getConnection();
100 } catch (\Exception
$e) {
101 if (false === strpos($e->getMessage(), 'Unknown database')
102 && false === strpos($e->getMessage(), 'database "' . $this->getContainer()->getParameter('database_name') . '" does not exist')) {
104 $status = '<error>ERROR!</error>';
105 $help = 'Can\'t connect to the database: ' . $e->getMessage();
109 $rows[] = [$label, $status, $help];
111 // check MySQL & PostgreSQL version
112 $label = '<comment>Database version</comment>';
113 $status = '<info>OK!</info>';
116 // now check if MySQL isn't too old to handle utf8mb4
117 if ($conn->isConnected() && 'mysql' === $conn->getDatabasePlatform()->getName()) {
118 $version = $conn->query('select version()')->fetchColumn();
119 $minimalVersion = '5.5.4';
121 if (false === version_compare($version, $minimalVersion, '>')) {
123 $status = '<error>ERROR!</error>';
124 $help = 'Your MySQL version (' . $version . ') is too old, consider upgrading (' . $minimalVersion . '+).';
128 // testing if PostgreSQL > 9.1
129 if ($conn->isConnected() && 'postgresql' === $conn->getDatabasePlatform()->getName()) {
130 // 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"
131 $version = $doctrineManager->getConnection()->query('SELECT version();')->fetchColumn();
133 preg_match('/PostgreSQL ([0-9\.]+)/i', $version, $matches);
135 if (isset($matches[1]) & version_compare($matches[1], '9.2.0', '<')) {
137 $status = '<error>ERROR!</error>';
138 $help = 'PostgreSQL should be greater than 9.1 (actual version: ' . $matches[1] . ')';
142 $rows[] = [$label, $status, $help];
144 foreach ($this->functionExists
as $functionRequired) {
145 $label = '<comment>' . $functionRequired . '</comment>';
146 $status = '<info>OK!</info>';
149 if (!function_exists($functionRequired)) {
151 $status = '<error>ERROR!</error>';
152 $help = 'You need the ' . $functionRequired . ' function activated';
155 $rows[] = [$label, $status, $help];
158 $this->io
->table(['Checked', 'Status', 'Recommendation'], $rows);
161 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
164 $this->io
->success('Success! Your system can run wallabag properly.');
169 protected function setupDatabase()
171 $this->io
->section('Step 2 of 4: Setting up database.');
173 // user want to reset everything? Don't care about what is already here
174 if (true === $this->defaultInput
->getOption('reset')) {
175 $this->io
->text('Dropping database, creating database and schema, clearing the cache');
178 ->runCommand('doctrine:database:drop', ['--force' => true])
179 ->runCommand('doctrine:database:create')
180 ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true])
181 ->runCommand('cache:clear')
184 $this->io
->newLine();
189 if (!$this->isDatabasePresent()) {
190 $this->io
->text('Creating database and schema, clearing the cache');
193 ->runCommand('doctrine:database:create')
194 ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true])
195 ->runCommand('cache:clear')
198 $this->io
->newLine();
203 if ($this->io
->confirm('It appears that your database already exists. Would you like to reset it?', false)) {
204 $this->io
->text('Dropping database, creating database and schema...');
207 ->runCommand('doctrine:database:drop', ['--force' => true])
208 ->runCommand('doctrine:database:create')
209 ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true])
211 } elseif ($this->isSchemaPresent()) {
212 if ($this->io
->confirm('Seems like your database contains schema. Do you want to reset it?', false)) {
213 $this->io
->text('Dropping schema and creating schema...');
216 ->runCommand('doctrine:schema:drop', ['--force' => true])
217 ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true])
221 $this->io
->text('Creating schema...');
224 ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true])
228 $this->io
->text('Clearing the cache...');
229 $this->runCommand('cache:clear');
231 $this->io
->newLine();
232 $this->io
->text('<info>Database successfully setup.</info>');
237 protected function setupAdmin()
239 $this->io
->section('Step 3 of 4: Administration setup.');
241 if (!$this->io
->confirm('Would you like to create a new admin user (recommended)?', true)) {
245 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
247 $userManager = $this->getContainer()->get('fos_user.user_manager');
248 $user = $userManager->createUser();
250 $user->setUsername($this->io
->ask('Username', 'wallabag'));
252 $question = new Question('Password', 'wallabag');
253 $question->setHidden(true);
254 $user->setPlainPassword($this->io
->askQuestion($question));
256 $user->setEmail($this->io
->ask('Email', ''));
258 $user->setEnabled(true);
259 $user->addRole('ROLE_SUPER_ADMIN');
263 // dispatch a created event so the associated config will be created
264 $event = new UserEvent($user);
265 $this->getContainer()->get('event_dispatcher')->dispatch(FOSUserEvents
::USER_CREATED
, $event);
267 $this->io
->text('<info>Administration successfully setup.</info>');
272 protected function setupConfig()
274 $this->io
->section('Step 4 of 4: Config setup.');
275 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
277 // cleanup before insert new stuff
278 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
280 foreach ($this->getContainer()->getParameter('wallabag_core.default_internal_settings') as $setting) {
281 $newSetting = new Setting();
282 $newSetting->setName($setting['name']);
283 $newSetting->setValue($setting['value']);
284 $newSetting->setSection($setting['section']);
285 $em->persist($newSetting);
290 $this->io
->text('<info>Config successfully setup.</info>');
298 * @param string $command
299 * @param array $parameters Parameters to this command (usually 'force' => true)
301 protected function runCommand($command, $parameters = [])
303 $parameters = array_merge(
304 ['command' => $command],
307 '--no-debug' => true,
308 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
312 if ($this->defaultInput
->getOption('no-interaction')) {
313 $parameters = array_merge($parameters, ['--no-interaction' => true]);
316 $this->getApplication()->setAutoExit(false);
318 $output = new BufferedOutput();
319 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), $output);
321 // PDO does not always close the connection after Doctrine commands.
322 // See https://github.com/symfony/symfony/issues/11750.
323 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
325 if (0 !== $exitCode) {
326 $this->getApplication()->setAutoExit(true);
328 throw new \
RuntimeException(
329 'The command "' . $command . "\" generates some errors: \n\n"
337 * Check if the database already exists.
341 private function isDatabasePresent()
343 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
344 $databaseName = $connection->getDatabase();
347 $schemaManager = $connection->getSchemaManager();
348 } catch (\Exception
$exception) {
350 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
355 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
362 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
363 if ('sqlite' === $schemaManager->getDatabasePlatform()->getName()) {
364 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
366 if (isset($params['path']) && file_exists($params['path'])) {
374 return in_array($databaseName, $schemaManager->listDatabases(), true);
375 } catch (\Doctrine\DBAL\Exception\DriverException
$e) {
376 // it means we weren't able to get database list, assume the database doesn't exist
383 * Check if the schema is already created.
384 * If we found at least oen table, it means the schema exists.
388 private function isSchemaPresent()
390 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
392 return count($schemaManager->listTableNames()) > 0 ? true : false;