]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Command/InstallCommand.php
manage assets through npm
[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 ;
65
66 $output->writeln('<info>Wallabag has been successfully installed.</info>');
67 $output->writeln('<comment>Just execute `php bin/console server:run --env=prod` for using wallabag: http://localhost:8000</comment>');
68 }
69
70 protected function checkRequirements()
71 {
72 $this->defaultOutput->writeln('<info><comment>Step 1 of 4.</comment> Checking system requirements.</info>');
73
74 $fulfilled = true;
75
76 $label = '<comment>PDO Driver</comment>';
77 $status = '<info>OK!</info>';
78 $help = '';
79
80 if (!extension_loaded($this->getContainer()->getParameter('database_driver'))) {
81 $fulfilled = false;
82 $status = '<error>ERROR!</error>';
83 $help = 'Database driver "'.$this->getContainer()->getParameter('database_driver').'" is not installed.';
84 }
85
86 $rows = [];
87 $rows[] = [$label, $status, $help];
88
89 foreach ($this->functionExists as $functionRequired) {
90 $label = '<comment>'.$functionRequired.'</comment>';
91 $status = '<info>OK!</info>';
92 $help = '';
93
94 if (!function_exists($functionRequired)) {
95 $fulfilled = false;
96 $status = '<error>ERROR!</error>';
97 $help = 'You need the '.$functionRequired.' function activated';
98 }
99
100 $rows[] = [$label, $status, $help];
101 }
102
103 $table = new Table($this->defaultOutput);
104 $table
105 ->setHeaders(['Checked', 'Status', 'Recommendation'])
106 ->setRows($rows)
107 ->render();
108
109 if (!$fulfilled) {
110 throw new \RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
111 }
112
113 $this->defaultOutput->writeln('<info>Success! Your system can run Wallabag properly.</info>');
114
115 $this->defaultOutput->writeln('');
116
117 return $this;
118 }
119
120 protected function setupDatabase()
121 {
122 $this->defaultOutput->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
123
124 // user want to reset everything? Don't care about what is already here
125 if (true === $this->defaultInput->getOption('reset')) {
126 $this->defaultOutput->writeln('Droping database, creating database and schema, clearing the cache');
127
128 $this
129 ->runCommand('doctrine:database:drop', ['--force' => true])
130 ->runCommand('doctrine:database:create')
131 ->runCommand('doctrine:schema:create')
132 ->runCommand('cache:clear')
133 ;
134
135 $this->defaultOutput->writeln('');
136
137 return $this;
138 }
139
140 if (!$this->isDatabasePresent()) {
141 $this->defaultOutput->writeln('Creating database and schema, clearing the cache');
142
143 $this
144 ->runCommand('doctrine:database:create')
145 ->runCommand('doctrine:schema:create')
146 ->runCommand('cache:clear')
147 ;
148
149 $this->defaultOutput->writeln('');
150
151 return $this;
152 }
153
154 $questionHelper = $this->getHelper('question');
155 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
156
157 if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) {
158 $this->defaultOutput->writeln('Droping database, creating database and schema');
159
160 $this
161 ->runCommand('doctrine:database:drop', ['--force' => true])
162 ->runCommand('doctrine:database:create')
163 ->runCommand('doctrine:schema:create')
164 ;
165 } elseif ($this->isSchemaPresent()) {
166 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
167 if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) {
168 $this->defaultOutput->writeln('Droping schema and creating schema');
169
170 $this
171 ->runCommand('doctrine:schema:drop', ['--force' => true])
172 ->runCommand('doctrine:schema:create')
173 ;
174 }
175 } else {
176 $this->defaultOutput->writeln('Creating schema');
177
178 $this
179 ->runCommand('doctrine:schema:create')
180 ;
181 }
182
183 $this->defaultOutput->writeln('Clearing the cache');
184 $this->runCommand('cache:clear');
185
186 $this->defaultOutput->writeln('');
187
188 return $this;
189 }
190
191 protected function setupAdmin()
192 {
193 $this->defaultOutput->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
194
195 $questionHelper = $this->getHelperSet()->get('question');
196 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true);
197
198 if (!$questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) {
199 return $this;
200 }
201
202 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
203
204 $userManager = $this->getContainer()->get('fos_user.user_manager');
205 $user = $userManager->createUser();
206
207 $question = new Question('Username (default: wallabag) :', 'wallabag');
208 $user->setUsername($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question));
209
210 $question = new Question('Password (default: wallabag) :', 'wallabag');
211 $user->setPlainPassword($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question));
212
213 $question = new Question('Email:', '');
214 $user->setEmail($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question));
215
216 $user->setEnabled(true);
217 $user->addRole('ROLE_SUPER_ADMIN');
218
219 $em->persist($user);
220
221 $config = new Config($user);
222 $config->setTheme($this->getContainer()->getParameter('wallabag_core.theme'));
223 $config->setItemsPerPage($this->getContainer()->getParameter('wallabag_core.items_on_page'));
224 $config->setRssLimit($this->getContainer()->getParameter('wallabag_core.rss_limit'));
225 $config->setReadingSpeed($this->getContainer()->getParameter('wallabag_core.reading_speed'));
226 $config->setLanguage($this->getContainer()->getParameter('wallabag_core.language'));
227
228 $em->persist($config);
229
230 $this->defaultOutput->writeln('');
231
232 return $this;
233 }
234
235 protected function setupConfig()
236 {
237 $this->defaultOutput->writeln('<info><comment>Step 4 of 4.</comment> Config setup.</info>');
238 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
239
240 // cleanup before insert new stuff
241 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
242
243 $settings = [
244 [
245 'name' => 'carrot',
246 'value' => '1',
247 'section' => 'entry',
248 ],
249 [
250 'name' => 'share_diaspora',
251 'value' => '1',
252 'section' => 'entry',
253 ],
254 [
255 'name' => 'diaspora_url',
256 'value' => 'http://diasporapod.com',
257 'section' => 'entry',
258 ],
259 [
260 'name' => 'share_shaarli',
261 'value' => '1',
262 'section' => 'entry',
263 ],
264 [
265 'name' => 'shaarli_url',
266 'value' => 'http://myshaarli.com',
267 'section' => 'entry',
268 ],
269 [
270 'name' => 'share_mail',
271 'value' => '1',
272 'section' => 'entry',
273 ],
274 [
275 'name' => 'share_twitter',
276 'value' => '1',
277 'section' => 'entry',
278 ],
279 [
280 'name' => 'export_epub',
281 'value' => '1',
282 'section' => 'export',
283 ],
284 [
285 'name' => 'export_mobi',
286 'value' => '1',
287 'section' => 'export',
288 ],
289 [
290 'name' => 'export_pdf',
291 'value' => '1',
292 'section' => 'export',
293 ],
294 [
295 'name' => 'export_csv',
296 'value' => '1',
297 'section' => 'export',
298 ],
299 [
300 'name' => 'export_json',
301 'value' => '1',
302 'section' => 'export',
303 ],
304 [
305 'name' => 'export_txt',
306 'value' => '1',
307 'section' => 'export',
308 ],
309 [
310 'name' => 'export_xml',
311 'value' => '1',
312 'section' => 'export',
313 ],
314 [
315 'name' => 'pocket_consumer_key',
316 'value' => null,
317 'section' => 'import',
318 ],
319 [
320 'name' => 'show_printlink',
321 'value' => '1',
322 'section' => 'entry',
323 ],
324 [
325 'name' => 'wallabag_support_url',
326 'value' => 'https://www.wallabag.org/pages/support.html',
327 'section' => 'misc',
328 ],
329 [
330 'name' => 'wallabag_url',
331 'value' => 'http://v2.wallabag.org',
332 'section' => 'misc',
333 ],
334 [
335 'name' => 'piwik_enabled',
336 'value' => '0',
337 'section' => 'analytics',
338 ],
339 [
340 'name' => 'piwik_host',
341 'value' => 'http://v2.wallabag.org',
342 'section' => 'analytics',
343 ],
344 [
345 'name' => 'piwik_site_id',
346 'value' => '1',
347 'section' => 'analytics',
348 ],
349 [
350 'name' => 'demo_mode_enabled',
351 'value' => '0',
352 'section' => 'misc',
353 ],
354 [
355 'name' => 'demo_mode_username',
356 'value' => 'wallabag',
357 'section' => 'misc',
358 ],
359 ];
360
361 foreach ($settings as $setting) {
362 $newSetting = new Setting();
363 $newSetting->setName($setting['name']);
364 $newSetting->setValue($setting['value']);
365 $newSetting->setSection($setting['section']);
366 $em->persist($newSetting);
367 }
368
369 $em->flush();
370
371 $this->defaultOutput->writeln('');
372
373 return $this;
374 }
375
376 /**
377 * Run a command.
378 *
379 * @param string $command
380 * @param array $parameters Parameters to this command (usually 'force' => true)
381 */
382 protected function runCommand($command, $parameters = [])
383 {
384 $parameters = array_merge(
385 ['command' => $command],
386 $parameters,
387 [
388 '--no-debug' => true,
389 '--env' => $this->defaultInput->getOption('env') ?: 'dev',
390 ]
391 );
392
393 if ($this->defaultInput->getOption('no-interaction')) {
394 $parameters = array_merge($parameters, ['--no-interaction' => true]);
395 }
396
397 $this->getApplication()->setAutoExit(false);
398 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
399
400 if (0 !== $exitCode) {
401 $this->getApplication()->setAutoExit(true);
402
403 $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode);
404 $this->defaultOutput->writeln("<error>$errorMessage</error>");
405 $exception = new \Exception($errorMessage, $exitCode);
406
407 throw $exception;
408 }
409
410 // PDO does not always close the connection after Doctrine commands.
411 // See https://github.com/symfony/symfony/issues/11750.
412 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
413
414 return $this;
415 }
416
417 /**
418 * Check if the database already exists.
419 *
420 * @return bool
421 */
422 private function isDatabasePresent()
423 {
424 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
425 $databaseName = $connection->getDatabase();
426
427 try {
428 $schemaManager = $connection->getSchemaManager();
429 } catch (\Exception $exception) {
430 // mysql & sqlite
431 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
432 return false;
433 }
434
435 // pgsql
436 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
437 return false;
438 }
439
440 throw $exception;
441 }
442
443 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
444 if ('sqlite' == $schemaManager->getDatabasePlatform()->getName()) {
445 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
446
447 if (isset($params['path']) && file_exists($params['path'])) {
448 return true;
449 }
450
451 return false;
452 }
453
454 try {
455 return in_array($databaseName, $schemaManager->listDatabases());
456 } catch (\Doctrine\DBAL\Exception\DriverException $e) {
457 // it means we weren't able to get database list, assume the database doesn't exist
458
459 return false;
460 }
461 }
462
463 /**
464 * Check if the schema is already created.
465 * If we found at least oen table, it means the schema exists.
466 *
467 * @return bool
468 */
469 private function isSchemaPresent()
470 {
471 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
472
473 return count($schemaManager->listTableNames()) > 0 ? true : false;
474 }
475 }