IndexPackagesCommand.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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(array('id' => $doctrine->getRepository('PackagistWebBundle:Package')->findOneByName($package)->getId()));
  49. } elseif ($force) {
  50. $packages = $doctrine->getEntityManager()->getConnection()->fetchAll('SELECT id FROM package ORDER BY id ASC');
  51. } else {
  52. $packages = $doctrine->getRepository('PackagistWebBundle:Package')->getStalePackagesForIndexing();
  53. }
  54. $ids = array();
  55. foreach ($packages as $package) {
  56. $ids[] = $package['id'];
  57. }
  58. // clear index before a full-update
  59. if ($force && !$package) {
  60. if ($verbose) {
  61. $output->writeln('Deleting existing index');
  62. }
  63. $update = $solarium->createUpdate();
  64. $update->addDeleteQuery('*:*');
  65. $update->addCommit();
  66. $solarium->update($update);
  67. }
  68. // update package index
  69. while ($ids) {
  70. $packages = $doctrine->getRepository('PackagistWebBundle:Package')->getFullPackages(array_splice($ids, 0, 50));
  71. foreach ($packages as $package) {
  72. if ($verbose) {
  73. $output->writeln('Indexing '.$package->getName());
  74. }
  75. try {
  76. $update = $solarium->createUpdate();
  77. $document = $update->createDocument();
  78. $this->updateDocumentFromPackage($document, $package);
  79. $update->addDocument($document);
  80. $package->setIndexedAt(new \DateTime);
  81. } catch (\Exception $e) {
  82. $output->writeln('<error>Exception: '.$e->getMessage().', skipping package '.$package->getName().'.</error>');
  83. }
  84. }
  85. $update->addCommit();
  86. $solarium->update($update);
  87. $doctrine->getEntityManager()->flush();
  88. $doctrine->getEntityManager()->clear();
  89. unset($packages);
  90. }
  91. }
  92. private function updateDocumentFromPackage(\Solarium_Document_ReadWrite $document, Package $package)
  93. {
  94. $document->id = $package->getId();
  95. $document->name = $package->getName();
  96. $document->description = $package->getDescription();
  97. $tags = array();
  98. foreach ($package->getVersions() as $version) {
  99. foreach ($version->getTags() as $tag) {
  100. $tags[mb_strtolower($tag->getName(), 'UTF-8')] = true;
  101. }
  102. }
  103. $document->tags = array_keys($tags);
  104. }
  105. }