UpdatePackagesCommand.php 6.4 KB

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