UpdatePackagesCommand.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. use Symfony\Component\HttpKernel\KernelInterface;
  17. use Symfony\Component\Finder\Finder;
  18. use Packagist\WebBundle\Entity\Version;
  19. use Packagist\WebBundle\Entity\Tag;
  20. use Packagist\WebBundle\Entity\Author;
  21. use Packagist\WebBundle\Entity\Requirement;
  22. /**
  23. * @author Jordi Boggiano <j.boggiano@seld.be>
  24. */
  25. class UpdatePackagesCommand extends ContainerAwareCommand
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function configure()
  31. {
  32. $this
  33. ->setName('pkg:update')
  34. ->setDefinition(array(
  35. ))
  36. ->setDescription('Updates packages')
  37. ->setHelp(<<<EOF
  38. EOF
  39. )
  40. ;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. protected function execute(InputInterface $input, OutputInterface $output)
  46. {
  47. $doctrine = $this->getContainer()->get('doctrine');
  48. $logger = $this->getContainer()->get('logger');
  49. $provider = $this->getContainer()->get('repository_provider');
  50. $packages = $doctrine->getRepository('PackagistWebBundle:Package')->getStalePackages();
  51. foreach ($packages as $package) {
  52. $repository = $provider->getRepository($package->getRepository());
  53. if (!$repository) {
  54. $output->writeln('<error>Unsupported repository: '.$package->getRepository().'</error>');
  55. continue;
  56. }
  57. $output->writeln('Importing '.$repository->getUrl());
  58. try {
  59. foreach ($repository->getTags() as $tag => $identifier) {
  60. // TODO parse tag name (or fetch composer file?) w/ composer version parser, if no match, ignore the tag
  61. $this->fetchInformation($output, $doctrine, $package, $repository, $identifier);
  62. }
  63. foreach ($repository->getBranches() as $branch => $identifier) {
  64. // TODO parse branch name, matching a "$num.x.x" version scheme, + the master one
  65. // use for all "x.y.z-dev" versions, usable through "latest-dev"
  66. $this->fetchInformation($output, $doctrine, $package, $repository, $identifier);
  67. }
  68. $package->setUpdatedAt(new \DateTime);
  69. $package->setCrawledAt(new \DateTime);
  70. $doctrine->getEntityManager()->flush();
  71. } catch (\Exception $e) {
  72. $output->writeln('<error>Exception: '.$e->getMessage().', skipping package.</error>');
  73. continue;
  74. }
  75. }
  76. }
  77. protected function fetchInformation(OutputInterface $output, $doctrine, $package, $repository, $identifier)
  78. {
  79. $data = $repository->getComposerInformation($identifier);
  80. $em = $doctrine->getEntityManager();
  81. // check if we have that version yet
  82. foreach ($package->getVersions() as $version) {
  83. if ($version->getVersion() === $data['version']) {
  84. return;
  85. }
  86. }
  87. if ($data['name'] !== $package->getName()) {
  88. $output->writeln('<error>Package name seems to have changed for '.$repository->getUrl().'@'.$identifier.', skipping.</error>');
  89. return;
  90. }
  91. $version = new Version();
  92. $em->persist($version);
  93. foreach (array('name', 'description', 'homepage', 'license', 'version') as $field) {
  94. if (isset($data[$field])) {
  95. $version->{'set'.$field}($data[$field]);
  96. }
  97. }
  98. $version->setPackage($package);
  99. $version->setUpdatedAt(new \DateTime);
  100. $version->setReleasedAt(new \DateTime($data['time']));
  101. $version->setSource(array('type' => $repository->getType(), 'url' => $repository->getUrl()));
  102. $version->setDist($repository->getDist($identifier));
  103. if (isset($data['keywords'])) {
  104. foreach ($data['keywords'] as $keyword) {
  105. $version->addTags(Tag::getByName($em, $keyword, true));
  106. }
  107. }
  108. if (isset($data['authors'])) {
  109. foreach ($data['authors'] as $authorData) {
  110. $author = null;
  111. // skip authors with no information
  112. if (!isset($authorData['email']) && !isset($authorData['name'])) {
  113. continue;
  114. }
  115. if (isset($authorData['email'])) {
  116. $author = $doctrine->getRepository('PackagistWebBundle:Author')->findOneByEmail($authorData['email']);
  117. }
  118. if (!$author) {
  119. $author = new Author();
  120. $em->persist($author);
  121. }
  122. foreach (array('email', 'name', 'homepage') as $field) {
  123. if (isset($authorData[$field])) {
  124. $author->{'set'.$field}($authorData[$field]);
  125. }
  126. }
  127. $author->setUpdatedAt(new \DateTime);
  128. $version->addAuthors($author);
  129. $author->addVersions($version);
  130. }
  131. }
  132. if (isset($data['require'])) {
  133. foreach ($data['require'] as $requireName => $requireVersion) {
  134. $requirement = new Requirement();
  135. $em->persist($requirement);
  136. $requirement->setPackageName($requireName);
  137. $requirement->setPackageVersion($requireVersion);
  138. $version->addRequirements($requirement);
  139. $requirement->setVersion($version);
  140. }
  141. }
  142. }
  143. }