]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Command/InstallCommand.php
Merge pull request #1652 from wallabag/v2-superadmin
[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 protected function configure()
30 {
31 $this
32 ->setName('wallabag:install')
33 ->setDescription('Wallabag installer.')
34 ->addOption(
35 'reset',
36 null,
37 InputOption::VALUE_NONE,
38 'Reset current database'
39 )
40 ;
41 }
42
43 protected function execute(InputInterface $input, OutputInterface $output)
44 {
45 $this->defaultInput = $input;
46 $this->defaultOutput = $output;
47
48 $output->writeln('<info>Installing Wallabag...</info>');
49 $output->writeln('');
50
51 $this
52 ->checkRequirements()
53 ->setupDatabase()
54 ->setupAdmin()
55 ->setupAsset()
56 ;
57
58 $output->writeln('<info>Wallabag has been successfully installed.</info>');
59 $output->writeln('<comment>Just execute `php bin/console server:run` for using wallabag: http://localhost:8000</comment>');
60 }
61
62 protected function checkRequirements()
63 {
64 $this->defaultOutput->writeln('<info><comment>Step 1 of 4.</comment> Checking system requirements.</info>');
65
66 $fulfilled = true;
67
68 $label = '<comment>PCRE</comment>';
69 if (extension_loaded('pcre')) {
70 $status = '<info>OK!</info>';
71 $help = '';
72 } else {
73 $fulfilled = false;
74 $status = '<error>ERROR!</error>';
75 $help = 'You should enabled PCRE extension';
76 }
77 $rows[] = array($label, $status, $help);
78
79 $label = '<comment>DOM</comment>';
80 if (extension_loaded('DOM')) {
81 $status = '<info>OK!</info>';
82 $help = '';
83 } else {
84 $fulfilled = false;
85 $status = '<error>ERROR!</error>';
86 $help = 'You should enabled DOM extension';
87 }
88 $rows[] = array($label, $status, $help);
89
90 $table = new Table($this->defaultOutput);
91 $table
92 ->setHeaders(array('Checked', 'Status', 'Recommendation'))
93 ->setRows($rows)
94 ->render();
95
96 if (!$fulfilled) {
97 throw new \RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
98 }
99
100 $this->defaultOutput->writeln('<info>Success! Your system can run Wallabag properly.</info>');
101
102 $this->defaultOutput->writeln('');
103
104 return $this;
105 }
106
107 protected function setupDatabase()
108 {
109 $this->defaultOutput->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
110
111 // user want to reset everything? Don't care about what is already here
112 if (true === $this->defaultInput->getOption('reset')) {
113 $this->defaultOutput->writeln('Droping database, creating database and schema, clearing the cache');
114
115 $this
116 ->runCommand('doctrine:database:drop', array('--force' => true))
117 ->runCommand('doctrine:database:create')
118 ->runCommand('doctrine:schema:create')
119 ->runCommand('cache:clear')
120 ;
121
122 $this->defaultOutput->writeln('');
123
124 return $this;
125 }
126
127 if (!$this->isDatabasePresent()) {
128 $this->defaultOutput->writeln('Creating database and schema, clearing the cache');
129
130 $this
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 $questionHelper = $this->getHelper('question');
142 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
143
144 if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) {
145 $this->defaultOutput->writeln('Droping database, creating database and schema');
146
147 $this
148 ->runCommand('doctrine:database:drop', array('--force' => true))
149 ->runCommand('doctrine:database:create')
150 ->runCommand('doctrine:schema:create')
151 ;
152 } elseif ($this->isSchemaPresent()) {
153 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
154 if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) {
155 $this->defaultOutput->writeln('Droping schema and creating schema');
156
157 $this
158 ->runCommand('doctrine:schema:drop', array('--force' => true))
159 ->runCommand('doctrine:schema:create')
160 ;
161 }
162 } else {
163 $this->defaultOutput->writeln('Creating schema');
164
165 $this
166 ->runCommand('doctrine:schema:create')
167 ;
168 }
169
170 $this->defaultOutput->writeln('Clearing the cache');
171 $this->runCommand('cache:clear');
172
173 $this->defaultOutput->writeln('');
174
175 return $this;
176 }
177
178 protected function setupAdmin()
179 {
180 $this->defaultOutput->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
181
182 $questionHelper = $this->getHelperSet()->get('question');
183 $question = new ConfirmationQuestion('Would you like to create a new user ? (y/N)', false);
184
185 if (!$questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) {
186 return $this;
187 }
188
189 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
190
191 $userManager = $this->getContainer()->get('fos_user.user_manager');
192 $user = $userManager->createUser();
193
194 $question = new Question('Username (default: wallabag) :', 'wallabag');
195 $user->setUsername($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question));
196
197 $question = new Question('Password (default: wallabag) :', 'wallabag');
198 $user->setPlainPassword($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question));
199
200 $question = new Question('Email:', '');
201 $user->setEmail($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question));
202
203 $user->setEnabled(true);
204 $user->addRole('ROLE_SUPER_ADMIN');
205
206 $em->persist($user);
207
208 $config = new Config($user);
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'));
213
214 $em->persist($config);
215
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' => 'export_csv',
277 'value' => '1',
278 'section' => 'export',
279 ],
280 [
281 'name' => 'export_json',
282 'value' => '1',
283 'section' => 'export',
284 ],
285 [
286 'name' => 'export_txt',
287 'value' => '1',
288 'section' => 'export',
289 ],
290 [
291 'name' => 'export_xml',
292 'value' => '1',
293 'section' => 'export',
294 ],
295 [
296 'name' => 'pocket_consumer_key',
297 'value' => null,
298 'section' => 'import',
299 ],
300 [
301 'name' => 'show_printlink',
302 'value' => '1',
303 'section' => 'entry',
304 ],
305 [
306 'name' => 'wallabag_support_url',
307 'value' => 'https://www.wallabag.org/pages/support.html',
308 'section' => 'misc',
309 ],
310 [
311 'name' => 'wallabag_url',
312 'value' => 'http://v2.wallabag.org',
313 'section' => 'misc',
314 ],
315 ];
316
317 foreach ($settings as $setting) {
318 $newSetting = new Setting();
319 $newSetting->setName($setting['name']);
320 $newSetting->setValue($setting['value']);
321 $newSetting->setSection($setting['section']);
322 $em->persist($newSetting);
323 }
324
325 $em->flush();
326
327 $this->defaultOutput->writeln('');
328
329 return $this;
330 }
331
332 protected function setupAsset()
333 {
334 $this->defaultOutput->writeln('<info><comment>Step 4 of 4.</comment> Installing assets.</info>');
335
336 $this
337 ->runCommand('assets:install')
338 ->runCommand('assetic:dump')
339 ;
340
341 $this->defaultOutput->writeln('');
342
343 return $this;
344 }
345
346 /**
347 * Run a command.
348 *
349 * @param string $command
350 * @param array $parameters Parameters to this command (usually 'force' => true)
351 */
352 protected function runCommand($command, $parameters = array())
353 {
354 $parameters = array_merge(
355 array('command' => $command),
356 $parameters,
357 array(
358 '--no-debug' => true,
359 '--env' => $this->defaultInput->getOption('env') ?: 'dev',
360 )
361 );
362
363 if ($this->defaultInput->getOption('no-interaction')) {
364 $parameters = array_merge($parameters, array('--no-interaction' => true));
365 }
366
367 $this->getApplication()->setAutoExit(false);
368 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
369
370 if (0 !== $exitCode) {
371 $this->getApplication()->setAutoExit(true);
372
373 $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode);
374 $this->defaultOutput->writeln("<error>$errorMessage</error>");
375 $exception = new \Exception($errorMessage, $exitCode);
376
377 throw $exception;
378 }
379
380 // PDO does not always close the connection after Doctrine commands.
381 // See https://github.com/symfony/symfony/issues/11750.
382 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
383
384 return $this;
385 }
386
387 /**
388 * Check if the database already exists.
389 *
390 * @return bool
391 */
392 private function isDatabasePresent()
393 {
394 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
395 $databaseName = $connection->getDatabase();
396
397 try {
398 $schemaManager = $connection->getSchemaManager();
399 } catch (\Exception $exception) {
400 // mysql & sqlite
401 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
402 return false;
403 }
404
405 // pgsql
406 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
407 return false;
408 }
409
410 throw $exception;
411 }
412
413 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
414 if ('sqlite' == $schemaManager->getDatabasePlatform()->getName()) {
415 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
416
417 if (isset($params['path']) && file_exists($params['path'])) {
418 return true;
419 }
420
421 return false;
422 }
423
424 return in_array($databaseName, $schemaManager->listDatabases());
425 }
426
427 /**
428 * Check if the schema is already created.
429 * If we found at least oen table, it means the schema exists.
430 *
431 * @return bool
432 */
433 private function isSchemaPresent()
434 {
435 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
436
437 return count($schemaManager->listTableNames()) > 0 ? true : false;
438 }
439 }