]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Command/InstallCommand.php
Fix issue #1991: correction of the height field to add articles
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Command / InstallCommand.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Command;
4
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;
16
17 class InstallCommand extends ContainerAwareCommand
18 {
19 /**
20 * @var InputInterface
21 */
22 protected $defaultInput;
23
24 /**
25 * @var OutputInterface
26 */
27 protected $defaultOutput;
28
29 /**
30 * @var array
31 */
32 protected $functionExists = [
33 'curl_exec',
34 'curl_multi_init',
35 ];
36
37 protected function configure()
38 {
39 $this
40 ->setName('wallabag:install')
41 ->setDescription('Wallabag installer.')
42 ->addOption(
43 'reset',
44 null,
45 InputOption::VALUE_NONE,
46 'Reset current database'
47 )
48 ;
49 }
50
51 protected function execute(InputInterface $input, OutputInterface $output)
52 {
53 $this->defaultInput = $input;
54 $this->defaultOutput = $output;
55
56 $output->writeln('<info>Installing Wallabag...</info>');
57 $output->writeln('');
58
59 $this
60 ->checkRequirements()
61 ->setupDatabase()
62 ->setupAdmin()
63 ->setupConfig()
64 ->setupAsset()
65 ;
66
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>');
69 }
70
71 protected function checkRequirements()
72 {
73 $this->defaultOutput->writeln('<info><comment>Step 1 of 5.</comment> Checking system requirements.</info>');
74
75 $fulfilled = true;
76
77 $label = '<comment>PDO Driver</comment>';
78 $status = '<info>OK!</info>';
79 $help = '';
80
81 if (!extension_loaded($this->getContainer()->getParameter('database_driver'))) {
82 $fulfilled = false;
83 $status = '<error>ERROR!</error>';
84 $help = 'Database driver "'.$this->getContainer()->getParameter('database_driver').'" is not installed.';
85 }
86
87 $rows = [];
88 $rows[] = [$label, $status, $help];
89
90 foreach ($this->functionExists as $functionRequired) {
91 $label = '<comment>'.$functionRequired.'</comment>';
92 $status = '<info>OK!</info>';
93 $help = '';
94
95 if (!function_exists($functionRequired)) {
96 $fulfilled = false;
97 $status = '<error>ERROR!</error>';
98 $help = 'You need the '.$functionRequired.' function activated';
99 }
100
101 $rows[] = [$label, $status, $help];
102 }
103
104 $table = new Table($this->defaultOutput);
105 $table
106 ->setHeaders(['Checked', 'Status', 'Recommendation'])
107 ->setRows($rows)
108 ->render();
109
110 if (!$fulfilled) {
111 throw new \RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
112 }
113
114 $this->defaultOutput->writeln('<info>Success! Your system can run Wallabag properly.</info>');
115
116 $this->defaultOutput->writeln('');
117
118 return $this;
119 }
120
121 protected function setupDatabase()
122 {
123 $this->defaultOutput->writeln('<info><comment>Step 2 of 5.</comment> Setting up database.</info>');
124
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');
128
129 $this
130 ->runCommand('doctrine:database:drop', ['--force' => true])
131 ->runCommand('doctrine:database:create')
132 ->runCommand('doctrine:schema:create')
133 ->runCommand('cache:clear')
134 ;
135
136 $this->defaultOutput->writeln('');
137
138 return $this;
139 }
140
141 if (!$this->isDatabasePresent()) {
142 $this->defaultOutput->writeln('Creating database and schema, clearing the cache');
143
144 $this
145 ->runCommand('doctrine:database:create')
146 ->runCommand('doctrine:schema:create')
147 ->runCommand('cache:clear')
148 ;
149
150 $this->defaultOutput->writeln('');
151
152 return $this;
153 }
154
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);
157
158 if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) {
159 $this->defaultOutput->writeln('Droping database, creating database and schema');
160
161 $this
162 ->runCommand('doctrine:database:drop', ['--force' => true])
163 ->runCommand('doctrine:database:create')
164 ->runCommand('doctrine:schema:create')
165 ;
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');
170
171 $this
172 ->runCommand('doctrine:schema:drop', ['--force' => true])
173 ->runCommand('doctrine:schema:create')
174 ;
175 }
176 } else {
177 $this->defaultOutput->writeln('Creating schema');
178
179 $this
180 ->runCommand('doctrine:schema:create')
181 ;
182 }
183
184 $this->defaultOutput->writeln('Clearing the cache');
185 $this->runCommand('cache:clear');
186
187 $this->defaultOutput->writeln('');
188
189 return $this;
190 }
191
192 protected function setupAdmin()
193 {
194 $this->defaultOutput->writeln('<info><comment>Step 3 of 5.</comment> Administration setup.</info>');
195
196 $questionHelper = $this->getHelperSet()->get('question');
197 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true);
198
199 if (!$questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) {
200 return $this;
201 }
202
203 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
204
205 $userManager = $this->getContainer()->get('fos_user.user_manager');
206 $user = $userManager->createUser();
207
208 $question = new Question('Username (default: wallabag) :', 'wallabag');
209 $user->setUsername($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question));
210
211 $question = new Question('Password (default: wallabag) :', 'wallabag');
212 $user->setPlainPassword($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question));
213
214 $question = new Question('Email:', '');
215 $user->setEmail($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question));
216
217 $user->setEnabled(true);
218 $user->addRole('ROLE_SUPER_ADMIN');
219
220 $em->persist($user);
221
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'));
228
229 $em->persist($config);
230
231 $this->defaultOutput->writeln('');
232
233 return $this;
234 }
235
236 protected function setupConfig()
237 {
238 $this->defaultOutput->writeln('<info><comment>Step 4 of 5.</comment> Config setup.</info>');
239 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
240
241 // cleanup before insert new stuff
242 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
243
244 $settings = [
245 [
246 'name' => 'carrot',
247 'value' => '1',
248 'section' => 'entry',
249 ],
250 [
251 'name' => 'share_diaspora',
252 'value' => '1',
253 'section' => 'entry',
254 ],
255 [
256 'name' => 'diaspora_url',
257 'value' => 'http://diasporapod.com',
258 'section' => 'entry',
259 ],
260 [
261 'name' => 'share_shaarli',
262 'value' => '1',
263 'section' => 'entry',
264 ],
265 [
266 'name' => 'shaarli_url',
267 'value' => 'http://myshaarli.com',
268 'section' => 'entry',
269 ],
270 [
271 'name' => 'share_mail',
272 'value' => '1',
273 'section' => 'entry',
274 ],
275 [
276 'name' => 'share_twitter',
277 'value' => '1',
278 'section' => 'entry',
279 ],
280 [
281 'name' => 'export_epub',
282 'value' => '1',
283 'section' => 'export',
284 ],
285 [
286 'name' => 'export_mobi',
287 'value' => '1',
288 'section' => 'export',
289 ],
290 [
291 'name' => 'export_pdf',
292 'value' => '1',
293 'section' => 'export',
294 ],
295 [
296 'name' => 'export_csv',
297 'value' => '1',
298 'section' => 'export',
299 ],
300 [
301 'name' => 'export_json',
302 'value' => '1',
303 'section' => 'export',
304 ],
305 [
306 'name' => 'export_txt',
307 'value' => '1',
308 'section' => 'export',
309 ],
310 [
311 'name' => 'export_xml',
312 'value' => '1',
313 'section' => 'export',
314 ],
315 [
316 'name' => 'pocket_consumer_key',
317 'value' => null,
318 'section' => 'import',
319 ],
320 [
321 'name' => 'show_printlink',
322 'value' => '1',
323 'section' => 'entry',
324 ],
325 [
326 'name' => 'wallabag_support_url',
327 'value' => 'https://www.wallabag.org/pages/support.html',
328 'section' => 'misc',
329 ],
330 [
331 'name' => 'wallabag_url',
332 'value' => 'http://v2.wallabag.org',
333 'section' => 'misc',
334 ],
335 [
336 'name' => 'piwik_enabled',
337 'value' => '0',
338 'section' => 'analytics',
339 ],
340 [
341 'name' => 'piwik_host',
342 'value' => 'http://v2.wallabag.org',
343 'section' => 'analytics',
344 ],
345 [
346 'name' => 'piwik_site_id',
347 'value' => '1',
348 'section' => 'analytics',
349 ],
350 [
351 'name' => 'demo_mode_enabled',
352 'value' => '0',
353 'section' => 'misc',
354 ],
355 [
356 'name' => 'demo_mode_username',
357 'value' => 'wallabag',
358 'section' => 'misc',
359 ],
360 ];
361
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);
368 }
369
370 $em->flush();
371
372 $this->defaultOutput->writeln('');
373
374 return $this;
375 }
376
377 protected function setupAsset()
378 {
379 $this->defaultOutput->writeln('<info><comment>Step 5 of 5.</comment> Installing assets.</info>');
380
381 $this
382 ->runCommand('assets:install')
383 ->runCommand('assetic:dump')
384 ;
385
386 $this->defaultOutput->writeln('');
387
388 return $this;
389 }
390
391 /**
392 * Run a command.
393 *
394 * @param string $command
395 * @param array $parameters Parameters to this command (usually 'force' => true)
396 */
397 protected function runCommand($command, $parameters = [])
398 {
399 $parameters = array_merge(
400 ['command' => $command],
401 $parameters,
402 [
403 '--no-debug' => true,
404 '--env' => $this->defaultInput->getOption('env') ?: 'dev',
405 ]
406 );
407
408 if ($this->defaultInput->getOption('no-interaction')) {
409 $parameters = array_merge($parameters, ['--no-interaction' => true]);
410 }
411
412 $this->getApplication()->setAutoExit(false);
413 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
414
415 if (0 !== $exitCode) {
416 $this->getApplication()->setAutoExit(true);
417
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);
421
422 throw $exception;
423 }
424
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();
428
429 return $this;
430 }
431
432 /**
433 * Check if the database already exists.
434 *
435 * @return bool
436 */
437 private function isDatabasePresent()
438 {
439 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
440 $databaseName = $connection->getDatabase();
441
442 try {
443 $schemaManager = $connection->getSchemaManager();
444 } catch (\Exception $exception) {
445 // mysql & sqlite
446 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
447 return false;
448 }
449
450 // pgsql
451 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
452 return false;
453 }
454
455 throw $exception;
456 }
457
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();
461
462 if (isset($params['path']) && file_exists($params['path'])) {
463 return true;
464 }
465
466 return false;
467 }
468
469 try {
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
473
474 return false;
475 }
476 }
477
478 /**
479 * Check if the schema is already created.
480 * If we found at least oen table, it means the schema exists.
481 *
482 * @return bool
483 */
484 private function isSchemaPresent()
485 {
486 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
487
488 return count($schemaManager->listTableNames()) > 0 ? true : false;
489 }
490 }