2011-04-12 04:27:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FOQ\ElasticaBundle\Command;
|
|
|
|
|
2011-06-21 19:23:25 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
2011-04-12 04:27:47 +02:00
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Console\Output\Output;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate the search index
|
|
|
|
*/
|
2011-06-21 19:23:25 +02:00
|
|
|
class PopulateCommand extends ContainerAwareCommand
|
2011-04-12 04:27:47 +02:00
|
|
|
{
|
|
|
|
/**
|
2012-03-12 17:02:25 +01:00
|
|
|
* @var FOQ\ElasticaBundle\IndexManager
|
|
|
|
*/
|
|
|
|
private $indexManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FOQ\ElasticaBundle\Provider\ProviderRegistry
|
|
|
|
*/
|
|
|
|
private $providerRegistry;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FOQ\ElasticaBundle\Resetter
|
|
|
|
*/
|
|
|
|
private $resetter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Symfony\Component\Console\Command\Command::configure()
|
2011-04-12 04:27:47 +02:00
|
|
|
*/
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('foq:elastica:populate')
|
2012-03-12 17:02:25 +01:00
|
|
|
->addOption('index', null, InputOption::VALUE_OPTIONAL, 'The index to repopulate')
|
|
|
|
->addOption('type', null, InputOption::VALUE_OPTIONAL, 'The type to repopulate')
|
2012-05-07 18:10:26 +02:00
|
|
|
->addOption('no-reset', null, InputOption::VALUE_NONE, 'If set, the indexes will not been resetted before populating.')
|
2012-03-12 17:02:25 +01:00
|
|
|
->setDescription('Populates search indexes from providers')
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Symfony\Component\Console\Command\Command::initialize()
|
|
|
|
*/
|
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
|
|
|
$this->indexManager = $this->getContainer()->get('foq_elastica.index_manager');
|
|
|
|
$this->providerRegistry = $this->getContainer()->get('foq_elastica.provider_registry');
|
|
|
|
$this->resetter = $this->getContainer()->get('foq_elastica.resetter');
|
2011-04-12 04:27:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-12 17:02:25 +01:00
|
|
|
* @see Symfony\Component\Console\Command\Command::execute()
|
2011-04-12 04:27:47 +02:00
|
|
|
*/
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2012-06-13 17:52:34 +02:00
|
|
|
$index = $input->getOption('index');
|
|
|
|
$type = $input->getOption('type');
|
|
|
|
$reset = $input->getOption('no-reset') ? false : true;
|
2012-03-12 17:02:25 +01:00
|
|
|
|
|
|
|
if (null === $index && null !== $type) {
|
|
|
|
throw new \InvalidArgumentException('Cannot specify type option without an index.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $index) {
|
|
|
|
if (null !== $type) {
|
2012-06-13 17:52:34 +02:00
|
|
|
$this->populateIndexType($output, $index, $type, $reset);
|
2012-03-12 17:02:25 +01:00
|
|
|
} else {
|
2012-06-13 17:52:34 +02:00
|
|
|
$this->populateIndex($output, $index, $reset);
|
2012-03-12 17:02:25 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$indexes = array_keys($this->indexManager->getAllIndexes());
|
|
|
|
|
|
|
|
foreach ($indexes as $index) {
|
2012-06-14 13:36:48 +02:00
|
|
|
$this->populateIndex($output, $index, $reset);
|
2012-03-12 17:02:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recreates an index, populates its types, and refreshes the index.
|
|
|
|
*
|
|
|
|
* @param OutputInterface $output
|
|
|
|
* @param string $index
|
2012-06-13 17:52:34 +02:00
|
|
|
* @param boolean $reset
|
2012-03-12 17:02:25 +01:00
|
|
|
*/
|
2012-06-13 17:52:34 +02:00
|
|
|
private function populateIndex(OutputInterface $output, $index, $reset)
|
2012-03-12 17:02:25 +01:00
|
|
|
{
|
2012-06-13 17:52:34 +02:00
|
|
|
if ($reset) {
|
2012-05-07 18:10:26 +02:00
|
|
|
$output->writeln(sprintf('Resetting: %s', $index));
|
|
|
|
$this->resetter->resetIndex($index);
|
2012-06-13 18:08:08 +02:00
|
|
|
}
|
2012-03-12 17:02:25 +01:00
|
|
|
|
|
|
|
$providers = $this->providerRegistry->getIndexProviders($index);
|
|
|
|
|
|
|
|
foreach ($providers as $type => $provider) {
|
|
|
|
$loggerClosure = function($message) use ($output, $index, $type) {
|
|
|
|
$output->writeln(sprintf('Populating: %s/%s, %s', $index, $type, $message));
|
|
|
|
};
|
|
|
|
|
|
|
|
$provider->populate($loggerClosure);
|
|
|
|
}
|
|
|
|
|
|
|
|
$output->writeln(sprintf('Refreshing: %s', $index));
|
|
|
|
$this->indexManager->getIndex($index)->refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes/remaps an index type, populates it, and refreshes the index.
|
|
|
|
*
|
|
|
|
* @param OutputInterface $output
|
|
|
|
* @param string $index
|
|
|
|
* @param string $type
|
2012-06-13 17:52:34 +02:00
|
|
|
* @param boolean $reset
|
2012-03-12 17:02:25 +01:00
|
|
|
*/
|
2012-06-13 17:52:34 +02:00
|
|
|
private function populateIndexType(OutputInterface $output, $index, $type, $reset)
|
2012-03-12 17:02:25 +01:00
|
|
|
{
|
2012-06-13 17:52:34 +02:00
|
|
|
if ($reset) {
|
2012-05-07 18:10:26 +02:00
|
|
|
$output->writeln(sprintf('Resetting: %s/%s', $index, $type));
|
|
|
|
$this->resetter->resetIndexType($index, $type);
|
|
|
|
}
|
2011-04-12 04:27:47 +02:00
|
|
|
|
2012-03-12 17:02:25 +01:00
|
|
|
$loggerClosure = function($message) use ($output, $index, $type) {
|
|
|
|
$output->writeln(sprintf('Populating: %s/%s, %s', $index, $type, $message));
|
|
|
|
};
|
2011-04-12 04:27:47 +02:00
|
|
|
|
2012-03-12 17:02:25 +01:00
|
|
|
$provider = $this->providerRegistry->getProvider($index, $type);
|
|
|
|
$provider->populate($loggerClosure);
|
2011-09-12 18:28:59 +02:00
|
|
|
|
2012-03-12 17:02:25 +01:00
|
|
|
$output->writeln(sprintf('Refreshing: %s', $index));
|
|
|
|
$this->indexManager->getIndex($index)->refresh();
|
2011-04-12 04:27:47 +02:00
|
|
|
}
|
|
|
|
}
|