ClearVersionsCommand.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of Packagist.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. * Nils Adermann <naderman@naderman.de>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Packagist\WebBundle\Command;
  12. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. /**
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. */
  19. class ClearVersionsCommand extends ContainerAwareCommand
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. protected function configure()
  25. {
  26. $this
  27. ->setName('packagist:clear:versions')
  28. ->setDefinition(array(
  29. new InputOption('force', null, InputOption::VALUE_NONE, 'Force execution, by default it runs in dry-run mode'),
  30. new InputOption('filter', null, InputOption::VALUE_NONE, 'Filter (regex) against "<version name> <version number>"'),
  31. ))
  32. ->setDescription('Clears all versions from the databases')
  33. ->setHelp(<<<EOF
  34. EOF
  35. )
  36. ;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. protected function execute(InputInterface $input, OutputInterface $output)
  42. {
  43. $force = $input->getOption('force');
  44. $filter = $input->getOption('filter');
  45. $doctrine = $this->getContainer()->get('doctrine');
  46. $versionRepo = $doctrine->getRepository('PackagistWebBundle:Version');
  47. $packages = $doctrine->getManager()->getConnection()->fetchAll('SELECT id FROM package ORDER BY id ASC');
  48. $ids = array();
  49. foreach ($packages as $package) {
  50. $ids[] = $package['id'];
  51. }
  52. $packageNames = array();
  53. while ($ids) {
  54. $qb = $versionRepo->createQueryBuilder('v');
  55. $qb
  56. ->join('v.package', 'p')
  57. ->where($qb->expr()->in('p.id', array_splice($ids, 0, 50)));
  58. $versions = $qb->getQuery()->iterate();
  59. foreach ($versions as $version) {
  60. $version = $version[0];
  61. $name = $version->getName().' '.$version->getVersion();
  62. if (!$filter || preg_match('{'.$filter.'}i', $name)) {
  63. $output->writeln('Clearing '.$name);
  64. if ($force) {
  65. $packageNames[] = $version->getName();
  66. $versionRepo->remove($version);
  67. }
  68. }
  69. }
  70. $doctrine->getManager()->flush();
  71. $doctrine->getManager()->clear();
  72. unset($versions);
  73. }
  74. if ($force) {
  75. // mark packages as recently crawled so that they get updated
  76. $packageRepo = $doctrine->getRepository('PackagistWebBundle:Package');
  77. foreach ($packageNames as $name) {
  78. $package = $packageRepo->findOneByName($name);
  79. $package->setCrawledAt(new \DateTime);
  80. }
  81. $doctrine->getManager()->flush();
  82. }
  83. }
  84. }