aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Command
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Command')
-rw-r--r--src/Wallabag/CoreBundle/Command/CleanDuplicatesCommand.php119
-rw-r--r--src/Wallabag/CoreBundle/Command/InstallCommand.php189
2 files changed, 143 insertions, 165 deletions
diff --git a/src/Wallabag/CoreBundle/Command/CleanDuplicatesCommand.php b/src/Wallabag/CoreBundle/Command/CleanDuplicatesCommand.php
new file mode 100644
index 00000000..65f35d8e
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Command/CleanDuplicatesCommand.php
@@ -0,0 +1,119 @@
1<?php
2
3namespace Wallabag\CoreBundle\Command;
4
5use Doctrine\ORM\NoResultException;
6use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7use Symfony\Component\Console\Input\InputArgument;
8use Symfony\Component\Console\Input\InputInterface;
9use Symfony\Component\Console\Output\OutputInterface;
10use Wallabag\CoreBundle\Entity\Entry;
11use Wallabag\UserBundle\Entity\User;
12
13class CleanDuplicatesCommand extends ContainerAwareCommand
14{
15 /** @var OutputInterface */
16 protected $output;
17
18 protected $duplicates = 0;
19
20 protected function configure()
21 {
22 $this
23 ->setName('wallabag:clean-duplicates')
24 ->setDescription('Cleans the database for duplicates')
25 ->setHelp('This command helps you to clean your articles list in case of duplicates')
26 ->addArgument(
27 'username',
28 InputArgument::OPTIONAL,
29 'User to clean'
30 );
31 }
32
33 protected function execute(InputInterface $input, OutputInterface $output)
34 {
35 $this->output = $output;
36
37 $username = $input->getArgument('username');
38
39 if ($username) {
40 try {
41 $user = $this->getUser($username);
42 $this->cleanDuplicates($user);
43 } catch (NoResultException $e) {
44 $output->writeln(sprintf('<error>User "%s" not found.</error>', $username));
45
46 return 1;
47 }
48 } else {
49 $users = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findAll();
50
51 $output->writeln(sprintf('Cleaning through %d user accounts', count($users)));
52
53 foreach ($users as $user) {
54 $output->writeln(sprintf('Processing user %s', $user->getUsername()));
55 $this->cleanDuplicates($user);
56 }
57 $output->writeln(sprintf('Finished cleaning. %d duplicates found in total', $this->duplicates));
58 }
59
60 return 0;
61 }
62
63 /**
64 * @param User $user
65 */
66 private function cleanDuplicates(User $user)
67 {
68 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
69 $repo = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
70
71 $entries = $repo->getAllEntriesIdAndUrl($user->getId());
72
73 $duplicatesCount = 0;
74 $urls = [];
75 foreach ($entries as $entry) {
76 $url = $this->similarUrl($entry['url']);
77
78 /* @var $entry Entry */
79 if (in_array($url, $urls)) {
80 ++$duplicatesCount;
81
82 $em->remove($repo->find($entry['id']));
83 $em->flush(); // Flushing at the end of the loop would require the instance not being online
84 } else {
85 $urls[] = $entry['url'];
86 }
87 }
88
89 $this->duplicates += $duplicatesCount;
90
91 $this->output->writeln(sprintf('Cleaned %d duplicates for user %s', $duplicatesCount, $user->getUserName()));
92 }
93
94 private function similarUrl($url)
95 {
96 if (in_array(substr($url, -1), ['/', '#'])) { // get rid of "/" and "#" and the end of urls
97 return substr($url, 0, strlen($url));
98 }
99
100 return $url;
101 }
102
103 /**
104 * Fetches a user from its username.
105 *
106 * @param string $username
107 *
108 * @return \Wallabag\UserBundle\Entity\User
109 */
110 private function getUser($username)
111 {
112 return $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($username);
113 }
114
115 private function getDoctrine()
116 {
117 return $this->getContainer()->get('doctrine');
118 }
119}
diff --git a/src/Wallabag/CoreBundle/Command/InstallCommand.php b/src/Wallabag/CoreBundle/Command/InstallCommand.php
index f0738b91..0f119377 100644
--- a/src/Wallabag/CoreBundle/Command/InstallCommand.php
+++ b/src/Wallabag/CoreBundle/Command/InstallCommand.php
@@ -63,6 +63,7 @@ class InstallCommand extends ContainerAwareCommand
63 ->setupDatabase() 63 ->setupDatabase()
64 ->setupAdmin() 64 ->setupAdmin()
65 ->setupConfig() 65 ->setupConfig()
66 ->runMigrations()
66 ; 67 ;
67 68
68 $output->writeln('<info>wallabag has been successfully installed.</info>'); 69 $output->writeln('<info>wallabag has been successfully installed.</info>');
@@ -71,7 +72,7 @@ class InstallCommand extends ContainerAwareCommand
71 72
72 protected function checkRequirements() 73 protected function checkRequirements()
73 { 74 {
74 $this->defaultOutput->writeln('<info><comment>Step 1 of 4.</comment> Checking system requirements.</info>'); 75 $this->defaultOutput->writeln('<info><comment>Step 1 of 5.</comment> Checking system requirements.</info>');
75 $doctrineManager = $this->getContainer()->get('doctrine')->getManager(); 76 $doctrineManager = $this->getContainer()->get('doctrine')->getManager();
76 77
77 $rows = []; 78 $rows = [];
@@ -175,11 +176,11 @@ class InstallCommand extends ContainerAwareCommand
175 176
176 protected function setupDatabase() 177 protected function setupDatabase()
177 { 178 {
178 $this->defaultOutput->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>'); 179 $this->defaultOutput->writeln('<info><comment>Step 2 of 5.</comment> Setting up database.</info>');
179 180
180 // user want to reset everything? Don't care about what is already here 181 // user want to reset everything? Don't care about what is already here
181 if (true === $this->defaultInput->getOption('reset')) { 182 if (true === $this->defaultInput->getOption('reset')) {
182 $this->defaultOutput->writeln('Droping database, creating database and schema, clearing the cache'); 183 $this->defaultOutput->writeln('Dropping database, creating database and schema, clearing the cache');
183 184
184 $this 185 $this
185 ->runCommand('doctrine:database:drop', ['--force' => true]) 186 ->runCommand('doctrine:database:drop', ['--force' => true])
@@ -211,7 +212,7 @@ class InstallCommand extends ContainerAwareCommand
211 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false); 212 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
212 213
213 if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) { 214 if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) {
214 $this->defaultOutput->writeln('Droping database, creating database and schema'); 215 $this->defaultOutput->writeln('Dropping database, creating database and schema');
215 216
216 $this 217 $this
217 ->runCommand('doctrine:database:drop', ['--force' => true]) 218 ->runCommand('doctrine:database:drop', ['--force' => true])
@@ -221,7 +222,7 @@ class InstallCommand extends ContainerAwareCommand
221 } elseif ($this->isSchemaPresent()) { 222 } elseif ($this->isSchemaPresent()) {
222 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false); 223 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
223 if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) { 224 if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) {
224 $this->defaultOutput->writeln('Droping schema and creating schema'); 225 $this->defaultOutput->writeln('Dropping schema and creating schema');
225 226
226 $this 227 $this
227 ->runCommand('doctrine:schema:drop', ['--force' => true]) 228 ->runCommand('doctrine:schema:drop', ['--force' => true])
@@ -246,7 +247,7 @@ class InstallCommand extends ContainerAwareCommand
246 247
247 protected function setupAdmin() 248 protected function setupAdmin()
248 { 249 {
249 $this->defaultOutput->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>'); 250 $this->defaultOutput->writeln('<info><comment>Step 3 of 5.</comment> Administration setup.</info>');
250 251
251 $questionHelper = $this->getHelperSet()->get('question'); 252 $questionHelper = $this->getHelperSet()->get('question');
252 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true); 253 $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true);
@@ -285,161 +286,13 @@ class InstallCommand extends ContainerAwareCommand
285 286
286 protected function setupConfig() 287 protected function setupConfig()
287 { 288 {
288 $this->defaultOutput->writeln('<info><comment>Step 4 of 4.</comment> Config setup.</info>'); 289 $this->defaultOutput->writeln('<info><comment>Step 4 of 5.</comment> Config setup.</info>');
289 $em = $this->getContainer()->get('doctrine.orm.entity_manager'); 290 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
290 291
291 // cleanup before insert new stuff 292 // cleanup before insert new stuff
292 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute(); 293 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
293 294
294 $settings = [ 295 foreach ($this->getContainer()->getParameter('wallabag_core.default_internal_settings') as $setting) {
295 [
296 'name' => 'share_public',
297 'value' => '1',
298 'section' => 'entry',
299 ],
300 [
301 'name' => 'carrot',
302 'value' => '1',
303 'section' => 'entry',
304 ],
305 [
306 'name' => 'share_diaspora',
307 'value' => '1',
308 'section' => 'entry',
309 ],
310 [
311 'name' => 'diaspora_url',
312 'value' => 'http://diasporapod.com',
313 'section' => 'entry',
314 ],
315 [
316 'name' => 'share_unmark',
317 'value' => '1',
318 'section' => 'entry',
319 ],
320 [
321 'name' => 'unmark_url',
322 'value' => 'https://unmark.it',
323 'section' => 'entry',
324 ],
325 [
326 'name' => 'share_shaarli',
327 'value' => '1',
328 'section' => 'entry',
329 ],
330 [
331 'name' => 'shaarli_url',
332 'value' => 'http://myshaarli.com',
333 'section' => 'entry',
334 ],
335 [
336 'name' => 'share_mail',
337 'value' => '1',
338 'section' => 'entry',
339 ],
340 [
341 'name' => 'share_twitter',
342 'value' => '1',
343 'section' => 'entry',
344 ],
345 [
346 'name' => 'export_epub',
347 'value' => '1',
348 'section' => 'export',
349 ],
350 [
351 'name' => 'export_mobi',
352 'value' => '1',
353 'section' => 'export',
354 ],
355 [
356 'name' => 'export_pdf',
357 'value' => '1',
358 'section' => 'export',
359 ],
360 [
361 'name' => 'export_csv',
362 'value' => '1',
363 'section' => 'export',
364 ],
365 [
366 'name' => 'export_json',
367 'value' => '1',
368 'section' => 'export',
369 ],
370 [
371 'name' => 'export_txt',
372 'value' => '1',
373 'section' => 'export',
374 ],
375 [
376 'name' => 'export_xml',
377 'value' => '1',
378 'section' => 'export',
379 ],
380 [
381 'name' => 'import_with_redis',
382 'value' => '0',
383 'section' => 'import',
384 ],
385 [
386 'name' => 'import_with_rabbitmq',
387 'value' => '0',
388 'section' => 'import',
389 ],
390 [
391 'name' => 'show_printlink',
392 'value' => '1',
393 'section' => 'entry',
394 ],
395 [
396 'name' => 'wallabag_support_url',
397 'value' => 'https://www.wallabag.org/pages/support.html',
398 'section' => 'misc',
399 ],
400 [
401 'name' => 'wallabag_url',
402 'value' => '',
403 'section' => 'misc',
404 ],
405 [
406 'name' => 'piwik_enabled',
407 'value' => '0',
408 'section' => 'analytics',
409 ],
410 [
411 'name' => 'piwik_host',
412 'value' => 'v2.wallabag.org',
413 'section' => 'analytics',
414 ],
415 [
416 'name' => 'piwik_site_id',
417 'value' => '1',
418 'section' => 'analytics',
419 ],
420 [
421 'name' => 'demo_mode_enabled',
422 'value' => '0',
423 'section' => 'misc',
424 ],
425 [
426 'name' => 'demo_mode_username',
427 'value' => 'wallabag',
428 'section' => 'misc',
429 ],
430 [
431 'name' => 'download_images_enabled',
432 'value' => '0',
433 'section' => 'misc',
434 ],
435 [
436 'name' => 'restricted_access',
437 'value' => '0',
438 'section' => 'entry',
439 ],
440 ];
441
442 foreach ($settings as $setting) {
443 $newSetting = new Setting(); 296 $newSetting = new Setting();
444 $newSetting->setName($setting['name']); 297 $newSetting->setName($setting['name']);
445 $newSetting->setValue($setting['value']); 298 $newSetting->setValue($setting['value']);
@@ -454,6 +307,14 @@ class InstallCommand extends ContainerAwareCommand
454 return $this; 307 return $this;
455 } 308 }
456 309
310 protected function runMigrations()
311 {
312 $this->defaultOutput->writeln('<info><comment>Step 5 of 5.</comment> Run migrations.</info>');
313
314 $this
315 ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true]);
316 }
317
457 /** 318 /**
458 * Run a command. 319 * Run a command.
459 * 320 *
@@ -480,20 +341,18 @@ class InstallCommand extends ContainerAwareCommand
480 $output = new BufferedOutput(); 341 $output = new BufferedOutput();
481 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), $output); 342 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), $output);
482 343
344 // PDO does not always close the connection after Doctrine commands.
345 // See https://github.com/symfony/symfony/issues/11750.
346 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
347
483 if (0 !== $exitCode) { 348 if (0 !== $exitCode) {
484 $this->getApplication()->setAutoExit(true); 349 $this->getApplication()->setAutoExit(true);
485 350
486 $this->defaultOutput->writeln(''); 351 throw new \RuntimeException(
487 $this->defaultOutput->writeln('<error>The command "'.$command.'" generates some errors: </error>'); 352 'The command "'.$command."\" generates some errors: \n\n"
488 $this->defaultOutput->writeln($output->fetch()); 353 .$output->fetch());
489
490 die();
491 } 354 }
492 355
493 // PDO does not always close the connection after Doctrine commands.
494 // See https://github.com/symfony/symfony/issues/11750.
495 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
496
497 return $this; 356 return $this;
498 } 357 }
499 358