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