]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Command/InstallCommand.php
Merge pull request #1786 from wallabag/v2-restrict-user-infos
[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 ->setupAsset()
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 Drivers</comment>';
77 if (extension_loaded('pdo_sqlite') || extension_loaded('pdo_mysql') || extension_loaded('pdo_pgsql')) {
78 $status = '<info>OK!</info>';
79 $help = '';
80 } else {
81 $fulfilled = false;
82 $status = '<error>ERROR!</error>';
83 $help = 'Needs one of sqlite, mysql or pgsql PDO drivers';
84 }
85
86 $rows[] = array($label, $status, $help);
87
88 foreach ($this->functionExists as $functionRequired) {
89 $label = '<comment>'.$functionRequired.'</comment>';
90
91 if (function_exists($functionRequired)) {
92 $status = '<info>OK!</info>';
93 $help = '';
94 } else {
95 $fulfilled = false;
96 $status = '<error>ERROR!</error>';
97 $help = 'You need the '.$functionRequired.' function activated';
98 }
99
100 $rows[] = array($label, $status, $help);
101 }
102
103 $table = new Table($this->defaultOutput);
104 $table
105 ->setHeaders(array('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', array('--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', array('--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', array('--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->setLanguage($this->getContainer()->getParameter('wallabag_core.language'));
226
227 $em->persist($config);
228
229 // cleanup before insert new stuff
230 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
231
232 $settings = [
233 [
234 'name' => 'download_pictures',
235 'value' => '1',
236 'section' => 'entry',
237 ],
238 [
239 'name' => 'carrot',
240 'value' => '1',
241 'section' => 'entry',
242 ],
243 [
244 'name' => 'share_diaspora',
245 'value' => '1',
246 'section' => 'entry',
247 ],
248 [
249 'name' => 'diaspora_url',
250 'value' => 'http://diasporapod.com',
251 'section' => 'entry',
252 ],
253 [
254 'name' => 'share_shaarli',
255 'value' => '1',
256 'section' => 'entry',
257 ],
258 [
259 'name' => 'shaarli_url',
260 'value' => 'http://myshaarli.com',
261 'section' => 'entry',
262 ],
263 [
264 'name' => 'share_mail',
265 'value' => '1',
266 'section' => 'entry',
267 ],
268 [
269 'name' => 'share_twitter',
270 'value' => '1',
271 'section' => 'entry',
272 ],
273 [
274 'name' => 'export_epub',
275 'value' => '1',
276 'section' => 'export',
277 ],
278 [
279 'name' => 'export_mobi',
280 'value' => '1',
281 'section' => 'export',
282 ],
283 [
284 'name' => 'export_pdf',
285 'value' => '1',
286 'section' => 'export',
287 ],
288 [
289 'name' => 'export_csv',
290 'value' => '1',
291 'section' => 'export',
292 ],
293 [
294 'name' => 'export_json',
295 'value' => '1',
296 'section' => 'export',
297 ],
298 [
299 'name' => 'export_txt',
300 'value' => '1',
301 'section' => 'export',
302 ],
303 [
304 'name' => 'export_xml',
305 'value' => '1',
306 'section' => 'export',
307 ],
308 [
309 'name' => 'pocket_consumer_key',
310 'value' => null,
311 'section' => 'import',
312 ],
313 [
314 'name' => 'show_printlink',
315 'value' => '1',
316 'section' => 'entry',
317 ],
318 [
319 'name' => 'wallabag_support_url',
320 'value' => 'https://www.wallabag.org/pages/support.html',
321 'section' => 'misc',
322 ],
323 [
324 'name' => 'wallabag_url',
325 'value' => 'http://v2.wallabag.org',
326 'section' => 'misc',
327 ],
328 [
329 'name' => 'piwik_enabled',
330 'value' => '0',
331 'section' => 'analytics',
332 ],
333 [
334 'name' => 'piwik_host',
335 'value' => 'http://v2.wallabag.org',
336 'section' => 'analytics',
337 ],
338 [
339 'name' => 'piwik_site_id',
340 'value' => '1',
341 'section' => 'analytics',
342 ],
343 [
344 'name' => 'demo_mode_enabled',
345 'value' => '0',
346 'section' => 'misc',
347 ],
348 [
349 'name' => 'demo_mode_username',
350 'value' => 'wallabag',
351 'section' => 'misc',
352 ],
353 ];
354
355 foreach ($settings as $setting) {
356 $newSetting = new Setting();
357 $newSetting->setName($setting['name']);
358 $newSetting->setValue($setting['value']);
359 $newSetting->setSection($setting['section']);
360 $em->persist($newSetting);
361 }
362
363 $em->flush();
364
365 $this->defaultOutput->writeln('');
366
367 return $this;
368 }
369
370 protected function setupAsset()
371 {
372 $this->defaultOutput->writeln('<info><comment>Step 4 of 4.</comment> Installing assets.</info>');
373
374 $this
375 ->runCommand('assets:install')
376 ->runCommand('assetic:dump')
377 ;
378
379 $this->defaultOutput->writeln('');
380
381 return $this;
382 }
383
384 /**
385 * Run a command.
386 *
387 * @param string $command
388 * @param array $parameters Parameters to this command (usually 'force' => true)
389 */
390 protected function runCommand($command, $parameters = array())
391 {
392 $parameters = array_merge(
393 array('command' => $command),
394 $parameters,
395 array(
396 '--no-debug' => true,
397 '--env' => $this->defaultInput->getOption('env') ?: 'dev',
398 )
399 );
400
401 if ($this->defaultInput->getOption('no-interaction')) {
402 $parameters = array_merge($parameters, array('--no-interaction' => true));
403 }
404
405 $this->getApplication()->setAutoExit(false);
406 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
407
408 if (0 !== $exitCode) {
409 $this->getApplication()->setAutoExit(true);
410
411 $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode);
412 $this->defaultOutput->writeln("<error>$errorMessage</error>");
413 $exception = new \Exception($errorMessage, $exitCode);
414
415 throw $exception;
416 }
417
418 // PDO does not always close the connection after Doctrine commands.
419 // See https://github.com/symfony/symfony/issues/11750.
420 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
421
422 return $this;
423 }
424
425 /**
426 * Check if the database already exists.
427 *
428 * @return bool
429 */
430 private function isDatabasePresent()
431 {
432 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
433 $databaseName = $connection->getDatabase();
434
435 try {
436 $schemaManager = $connection->getSchemaManager();
437 } catch (\Exception $exception) {
438 // mysql & sqlite
439 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
440 return false;
441 }
442
443 // pgsql
444 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
445 return false;
446 }
447
448 throw $exception;
449 }
450
451 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
452 if ('sqlite' == $schemaManager->getDatabasePlatform()->getName()) {
453 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
454
455 if (isset($params['path']) && file_exists($params['path'])) {
456 return true;
457 }
458
459 return false;
460 }
461
462 return in_array($databaseName, $schemaManager->listDatabases());
463 }
464
465 /**
466 * Check if the schema is already created.
467 * If we found at least oen table, it means the schema exists.
468 *
469 * @return bool
470 */
471 private function isSchemaPresent()
472 {
473 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
474
475 return count($schemaManager->listTableNames()) > 0 ? true : false;
476 }
477 }