]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Command/InstallCommand.php
Fix permission to settings page
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Command / InstallCommand.php
CommitLineData
2e45e7be
J
1<?php
2
3namespace Wallabag\CoreBundle\Command;
4
5use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
619cc453
JB
6use Symfony\Component\Console\Helper\Table;
7use Symfony\Component\Console\Input\ArrayInput;
2e45e7be 8use Symfony\Component\Console\Input\InputInterface;
0bf99bb1 9use Symfony\Component\Console\Input\InputOption;
0bf99bb1 10use Symfony\Component\Console\Output\NullOutput;
619cc453 11use Symfony\Component\Console\Output\OutputInterface;
78507d28 12use Symfony\Component\Console\Question\ConfirmationQuestion;
619cc453 13use Symfony\Component\Console\Question\Question;
4d85d7e9 14use Wallabag\CoreBundle\Entity\Config;
d6ba77e8 15use Craue\ConfigBundle\Entity\Setting;
2e45e7be
J
16
17class InstallCommand extends ContainerAwareCommand
18{
0bf99bb1
J
19 /**
20 * @var InputInterface
21 */
22 protected $defaultInput;
23
24 /**
25 * @var OutputInterface
26 */
27 protected $defaultOutput;
28
2e45e7be
J
29 protected function configure()
30 {
31 $this
32 ->setName('wallabag:install')
33 ->setDescription('Wallabag installer.')
0bf99bb1
J
34 ->addOption(
35 'reset',
36 null,
37 InputOption::VALUE_NONE,
38 'Reset current database'
39 )
2e45e7be
J
40 ;
41 }
42
43 protected function execute(InputInterface $input, OutputInterface $output)
44 {
0bf99bb1
J
45 $this->defaultInput = $input;
46 $this->defaultOutput = $output;
47
48 $output->writeln('<info>Installing Wallabag...</info>');
2e45e7be
J
49 $output->writeln('');
50
51 $this
0bf99bb1
J
52 ->checkRequirements()
53 ->setupDatabase()
54 ->setupAdmin()
55 ->setupAsset()
2e45e7be
J
56 ;
57
58 $output->writeln('<info>Wallabag has been successfully installed.</info>');
c7f622d3 59 $output->writeln('<comment>Just execute `php bin/console server:run` for using wallabag: http://localhost:8000</comment>');
2e45e7be
J
60 }
61
0bf99bb1 62 protected function checkRequirements()
2e45e7be 63 {
0bf99bb1 64 $this->defaultOutput->writeln('<info><comment>Step 1 of 4.</comment> Checking system requirements.</info>');
2e45e7be
J
65
66 $fulfilled = true;
67
68 // @TODO: find a better way to check requirements
0bf99bb1 69 $label = '<comment>PCRE</comment>';
2e45e7be 70 if (extension_loaded('pcre')) {
0bf99bb1
J
71 $status = '<info>OK!</info>';
72 $help = '';
2e45e7be
J
73 } else {
74 $fulfilled = false;
0bf99bb1
J
75 $status = '<error>ERROR!</error>';
76 $help = 'You should enabled PCRE extension';
2e45e7be 77 }
0bf99bb1 78 $rows[] = array($label, $status, $help);
2e45e7be 79
0bf99bb1 80 $label = '<comment>DOM</comment>';
2e45e7be 81 if (extension_loaded('DOM')) {
0bf99bb1
J
82 $status = '<info>OK!</info>';
83 $help = '';
2e45e7be
J
84 } else {
85 $fulfilled = false;
0bf99bb1
J
86 $status = '<error>ERROR!</error>';
87 $help = 'You should enabled DOM extension';
2e45e7be 88 }
0bf99bb1
J
89 $rows[] = array($label, $status, $help);
90
78507d28
JB
91 $table = new Table($this->defaultOutput);
92 $table
0bf99bb1
J
93 ->setHeaders(array('Checked', 'Status', 'Recommendation'))
94 ->setRows($rows)
78507d28 95 ->render();
2e45e7be
J
96
97 if (!$fulfilled) {
0bf99bb1 98 throw new \RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
2e45e7be
J
99 }
100
8a493541
JB
101 $this->defaultOutput->writeln('<info>Success! Your system can run Wallabag properly.</info>');
102
0bf99bb1 103 $this->defaultOutput->writeln('');
2e45e7be
J
104
105 return $this;
106 }
107
0bf99bb1 108 protected function setupDatabase()
2e45e7be 109 {
0bf99bb1 110 $this->defaultOutput->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
2e45e7be 111
0bf99bb1
J
112 // user want to reset everything? Don't care about what is already here
113 if (true === $this->defaultInput->getOption('reset')) {
d5027625 114 $this->defaultOutput->writeln('Droping database, creating database and schema, clearing the cache');
2e45e7be 115
0bf99bb1
J
116 $this
117 ->runCommand('doctrine:database:drop', array('--force' => true))
118 ->runCommand('doctrine:database:create')
119 ->runCommand('doctrine:schema:create')
d5027625 120 ->runCommand('cache:clear')
0bf99bb1 121 ;
2e45e7be 122
d5027625
JB
123 $this->defaultOutput->writeln('');
124
0bf99bb1
J
125 return $this;
126 }
2e45e7be 127
0bf99bb1
J
128 if (!$this->isDatabasePresent()) {
129 $this->defaultOutput->writeln('Creating database and schema, clearing the cache');
2e45e7be 130
0bf99bb1
J
131 $this
132 ->runCommand('doctrine:database:create')
133 ->runCommand('doctrine:schema:create')
134 ->runCommand('cache:clear')
135 ;
2e45e7be 136
d5027625
JB
137 $this->defaultOutput->writeln('');
138
0bf99bb1
J
139 return $this;
140 }
2e45e7be 141
78507d28
JB
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);
2e45e7be 144
78507d28 145 if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) {
0bf99bb1 146 $this->defaultOutput->writeln('Droping database, creating database and schema');
2e45e7be 147
0bf99bb1
J
148 $this
149 ->runCommand('doctrine:database:drop', array('--force' => true))
150 ->runCommand('doctrine:database:create')
151 ->runCommand('doctrine:schema:create')
152 ;
153 } elseif ($this->isSchemaPresent()) {
78507d28
JB
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)) {
0bf99bb1 156 $this->defaultOutput->writeln('Droping schema and creating schema');
2e45e7be 157
0bf99bb1
J
158 $this
159 ->runCommand('doctrine:schema:drop', array('--force' => true))
160 ->runCommand('doctrine:schema:create')
161 ;
162 }
2e45e7be 163 } else {
0bf99bb1
J
164 $this->defaultOutput->writeln('Creating schema');
165
166 $this
167 ->runCommand('doctrine:schema:create')
168 ;
2e45e7be
J
169 }
170
0bf99bb1
J
171 $this->defaultOutput->writeln('Clearing the cache');
172 $this->runCommand('cache:clear');
173
0bf99bb1
J
174 $this->defaultOutput->writeln('');
175
176 return $this;
2e45e7be
J
177 }
178
0bf99bb1 179 protected function setupAdmin()
2e45e7be 180 {
0bf99bb1
J
181 $this->defaultOutput->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
182
78507d28
JB
183 $questionHelper = $this->getHelperSet()->get('question');
184 $question = new ConfirmationQuestion('Would you like to create a new user ? (y/N)', false);
0bf99bb1 185
78507d28 186 if (!$questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) {
0bf99bb1
J
187 return $this;
188 }
189
2e45e7be
J
190 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
191
ec3ce598
NL
192 $userManager = $this->getContainer()->get('fos_user.user_manager');
193 $user = $userManager->createUser();
78507d28
JB
194
195 $question = new Question('Username (default: wallabag) :', 'wallabag');
196 $user->setUsername($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question));
197
198 $question = new Question('Password (default: wallabag) :', 'wallabag');
199 $user->setPlainPassword($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question));
200
201 $question = new Question('Email:', '');
202 $user->setEmail($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question));
203
a1691859 204 $user->setEnabled(true);
2e45e7be
J
205
206 $em->persist($user);
207
0bd2cb1e 208 $config = new Config($user);
bc789687
JB
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'));
2e45e7be 213
4d85d7e9 214 $em->persist($config);
0bf99bb1 215
d6ba77e8
JB
216 // cleanup before insert new stuff
217 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
218
219 $settings = [
220 [
221 'name' => 'download_pictures',
222 'value' => '1',
223 'section' => 'entry',
224 ],
225 [
226 'name' => 'carrot',
227 'value' => '1',
228 'section' => 'entry',
229 ],
230 [
231 'name' => 'share_diaspora',
232 'value' => '1',
233 'section' => 'entry',
234 ],
235 [
236 'name' => 'diaspora_url',
237 'value' => 'http://diasporapod.com',
238 'section' => 'entry',
239 ],
240 [
241 'name' => 'share_shaarli',
242 'value' => '1',
243 'section' => 'entry',
244 ],
245 [
246 'name' => 'shaarli_url',
247 'value' => 'http://myshaarli.com',
248 'section' => 'entry',
249 ],
250 [
251 'name' => 'share_mail',
252 'value' => '1',
253 'section' => 'entry',
254 ],
255 [
256 'name' => 'share_twitter',
257 'value' => '1',
258 'section' => 'entry',
259 ],
260 [
261 'name' => 'export_epub',
262 'value' => '1',
263 'section' => 'export',
264 ],
265 [
266 'name' => 'export_mobi',
267 'value' => '1',
268 'section' => 'export',
269 ],
270 [
271 'name' => 'export_pdf',
272 'value' => '1',
273 'section' => 'export',
274 ],
275 [
276 'name' => 'pocket_consumer_key',
1c7d6664 277 'value' => null,
d6ba77e8
JB
278 'section' => 'import',
279 ],
280 [
281 'name' => 'show_printlink',
282 'value' => '1',
283 'section' => 'entry',
284 ],
285 [
286 'name' => 'wallabag_support_url',
287 'value' => 'https://www.wallabag.org/pages/support.html',
288 'section' => 'misc',
289 ],
290 [
291 'name' => 'wallabag_url',
292 'value' => 'http://v2.wallabag.org',
293 'section' => 'misc',
294 ],
295 ];
296
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);
303 }
304
0bf99bb1
J
305 $em->flush();
306
307 $this->defaultOutput->writeln('');
308
309 return $this;
2e45e7be
J
310 }
311
0bf99bb1 312 protected function setupAsset()
2e45e7be 313 {
0bf99bb1
J
314 $this->defaultOutput->writeln('<info><comment>Step 4 of 4.</comment> Installing assets.</info>');
315
2e45e7be 316 $this
0bf99bb1
J
317 ->runCommand('assets:install')
318 ->runCommand('assetic:dump')
2e45e7be
J
319 ;
320
0bf99bb1
J
321 $this->defaultOutput->writeln('');
322
323 return $this;
324 }
325
326 /**
4346a860 327 * Run a command.
0bf99bb1
J
328 *
329 * @param string $command
330 * @param array $parameters Parameters to this command (usually 'force' => true)
331 */
332 protected function runCommand($command, $parameters = array())
333 {
334 $parameters = array_merge(
335 array('command' => $command),
336 $parameters,
337 array(
338 '--no-debug' => true,
339 '--env' => $this->defaultInput->getOption('env') ?: 'dev',
340 )
341 );
342
343 if ($this->defaultInput->getOption('no-interaction')) {
344 $parameters = array_merge($parameters, array('--no-interaction' => true));
345 }
346
347 $this->getApplication()->setAutoExit(false);
348 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
349
350 if (0 !== $exitCode) {
351 $this->getApplication()->setAutoExit(true);
352
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);
356
357 throw $exception;
358 }
359
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();
363
2e45e7be
J
364 return $this;
365 }
0bf99bb1
J
366
367 /**
4346a860 368 * Check if the database already exists.
0bf99bb1 369 *
4346a860 370 * @return bool
0bf99bb1
J
371 */
372 private function isDatabasePresent()
373 {
af43bd37
JB
374 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
375 $databaseName = $connection->getDatabase();
0bf99bb1
J
376
377 try {
af43bd37 378 $schemaManager = $connection->getSchemaManager();
0bf99bb1 379 } catch (\Exception $exception) {
54a2241e 380 // mysql & sqlite
0bf99bb1
J
381 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
382 return false;
383 }
384
54a2241e
JB
385 // pgsql
386 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
387 return false;
388 }
389
0bf99bb1
J
390 throw $exception;
391 }
392
732c2ad8
J
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();
396
397 if (isset($params['path']) && file_exists($params['path'])) {
398 return true;
399 }
400
401 return false;
402 }
403
0bf99bb1
J
404 return in_array($databaseName, $schemaManager->listDatabases());
405 }
406
407 /**
164bd801 408 * Check if the schema is already created.
4346a860 409 * If we found at least oen table, it means the schema exists.
0bf99bb1 410 *
4346a860 411 * @return bool
0bf99bb1
J
412 */
413 private function isSchemaPresent()
414 {
415 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
416
164bd801 417 return count($schemaManager->listTableNames()) > 0 ? true : false;
0bf99bb1 418 }
2e45e7be 419}