IndexPackagesCommand.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 Packagist\WebBundle\Entity\Package;
  13. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Input\InputArgument;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. /**
  19. * @author Igor Wiedler <igor@wiedler.ch>
  20. */
  21. class IndexPackagesCommand extends ContainerAwareCommand
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function configure()
  27. {
  28. $this
  29. ->setName('packagist:index')
  30. ->setDefinition(array(
  31. new InputOption('force', null, InputOption::VALUE_NONE, 'Force a re-indexing of all packages'),
  32. new InputArgument('package', InputArgument::OPTIONAL, 'Package name to index'),
  33. ))
  34. ->setDescription('Indexes packages in Solr')
  35. ;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. protected function execute(InputInterface $input, OutputInterface $output)
  41. {
  42. $verbose = $input->getOption('verbose');
  43. $force = $input->getOption('force');
  44. $package = $input->getArgument('package');
  45. $doctrine = $this->getContainer()->get('doctrine');
  46. $solarium = $this->getContainer()->get('solarium.client');
  47. if ($package) {
  48. $packages = array($doctrine->getRepository('PackagistWebBundle:Package')->findOneByName($package));
  49. } elseif ($force) {
  50. $packages = $doctrine->getRepository('PackagistWebBundle:Package')->findAll();
  51. } else {
  52. $packages = $doctrine->getRepository('PackagistWebBundle:Package')->getStalePackagesForIndexing();
  53. }
  54. // clear index before a full-update
  55. if ($force && !$package) {
  56. if ($verbose) {
  57. $output->writeln('Deleting existing index');
  58. }
  59. $update = $solarium->createUpdate();
  60. $update->addDeleteQuery('*:*');
  61. $update->addCommit();
  62. $solarium->update($update);
  63. }
  64. // update package index
  65. foreach ($packages as $package) {
  66. if ($verbose) {
  67. $output->writeln('Indexing '.$package->getName());
  68. }
  69. try {
  70. $update = $solarium->createUpdate();
  71. $document = $update->createDocument();
  72. $this->updateDocumentFromPackage($document, $package);
  73. $update->addDocument($document);
  74. $update->addCommit();
  75. $solarium->update($update);
  76. $package->setIndexedAt(new \DateTime);
  77. } catch (\Exception $e) {
  78. $output->writeln('<error>Exception: '.$e->getMessage().', skipping package '.$package->getName().'.</error>');
  79. }
  80. }
  81. $doctrine->getEntityManager()->flush();
  82. }
  83. private function updateDocumentFromPackage(\Solarium_Document_ReadWrite $document, Package $package)
  84. {
  85. $document->id = $package->getId();
  86. $document->name = $package->getName();
  87. $document->description = $package->getDescription();
  88. $tags = array();
  89. foreach ($package->getVersions() as $version) {
  90. foreach ($version->getTags() as $tag) {
  91. $tags[] = $tag->getName();
  92. }
  93. }
  94. $document->tags = array_unique($tags);
  95. }
  96. }