2011-04-12 04:27:47 +02:00
< ? php
2013-03-27 18:44:06 +01:00
namespace FOS\ElasticaBundle\Command ;
2011-04-12 04:27:47 +02:00
2015-03-11 04:11:18 +01:00
use FOS\ElasticaBundle\Event\IndexPopulateEvent ;
use FOS\ElasticaBundle\Event\TypePopulateEvent ;
2011-06-21 19:23:25 +02:00
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand ;
2013-10-08 12:00:04 +02:00
use Symfony\Component\Console\Helper\DialogHelper ;
2011-04-12 04:27:47 +02:00
use Symfony\Component\Console\Input\InputOption ;
use Symfony\Component\Console\Input\InputInterface ;
use Symfony\Component\Console\Output\OutputInterface ;
2013-03-27 19:58:34 +01:00
use FOS\ElasticaBundle\IndexManager ;
use FOS\ElasticaBundle\Provider\ProviderRegistry ;
use FOS\ElasticaBundle\Resetter ;
2015-01-07 18:42:15 +01:00
use Symfony\Component\Console\Helper\ProgressBar ;
2011-04-12 04:27:47 +02:00
/**
2015-03-12 11:20:00 +01:00
* Populate the search index .
2011-04-12 04:27:47 +02:00
*/
2011-06-21 19:23:25 +02:00
class PopulateCommand extends ContainerAwareCommand
2011-04-12 04:27:47 +02:00
{
2015-03-11 04:11:18 +01:00
/**
* @ var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
private $dispatcher ;
2011-04-12 04:27:47 +02:00
/**
2013-03-02 12:23:44 +01:00
* @ var IndexManager
2012-03-12 17:02:25 +01:00
*/
private $indexManager ;
2015-03-11 05:11:42 +01:00
/**
* @ var ProgressClosureBuilder
*/
private $progressClosureBuilder ;
2012-03-12 17:02:25 +01:00
/**
2013-03-02 12:23:44 +01:00
* @ var ProviderRegistry
2012-03-12 17:02:25 +01:00
*/
private $providerRegistry ;
/**
2013-03-02 12:23:44 +01:00
* @ var Resetter
2012-03-12 17:02:25 +01:00
*/
private $resetter ;
/**
* @ see Symfony\Component\Console\Command\Command :: configure ()
2011-04-12 04:27:47 +02:00
*/
protected function configure ()
{
$this
2013-03-27 18:44:06 +01:00
-> setName ( 'fos: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' )
2013-02-22 20:03:40 +01:00
-> addOption ( 'no-reset' , null , InputOption :: VALUE_NONE , 'Do not reset index before populating' )
2013-10-08 12:00:04 +02:00
-> addOption ( 'offset' , null , InputOption :: VALUE_REQUIRED , 'Start indexing at offset' , 0 )
-> addOption ( 'sleep' , null , InputOption :: VALUE_REQUIRED , 'Sleep time between persisting iterations (microseconds)' , 0 )
-> addOption ( 'batch-size' , null , InputOption :: VALUE_REQUIRED , 'Index packet size (overrides provider config option)' )
2014-02-19 11:01:54 +01:00
-> addOption ( 'ignore-errors' , null , InputOption :: VALUE_NONE , 'Do not stop on errors' )
2015-03-11 05:09:48 +01:00
-> addOption ( 'no-overwrite-format' , null , InputOption :: VALUE_NONE , 'Prevent this command from overwriting ProgressBar\'s formats' )
2012-03-12 17:02:25 +01:00
-> setDescription ( 'Populates search indexes from providers' )
;
}
/**
2015-03-11 05:09:48 +01:00
* { @ inheritDoc }
2012-03-12 17:02:25 +01:00
*/
protected function initialize ( InputInterface $input , OutputInterface $output )
{
2015-03-11 04:11:18 +01:00
$this -> dispatcher = $this -> getContainer () -> get ( 'event_dispatcher' );
2013-03-27 18:44:06 +01:00
$this -> indexManager = $this -> getContainer () -> get ( 'fos_elastica.index_manager' );
$this -> providerRegistry = $this -> getContainer () -> get ( 'fos_elastica.provider_registry' );
$this -> resetter = $this -> getContainer () -> get ( 'fos_elastica.resetter' );
2015-03-11 05:11:42 +01:00
$this -> progressClosureBuilder = new ProgressClosureBuilder ();
2015-03-11 05:09:48 +01:00
2015-03-23 11:39:13 +01:00
if ( ! $input -> getOption ( 'no-overwrite-format' ) && class_exists ( 'Symfony\\Component\\Console\\Helper\\ProgressBar' )) {
2015-03-11 05:09:48 +01:00
ProgressBar :: setFormatDefinition ( 'normal' , " %current%/%max% [%bar%] %percent:3s%% \n %message% " );
ProgressBar :: setFormatDefinition ( 'verbose' , " %current%/%max% [%bar%] %percent:3s%% %elapsed:6s% \n %message% " );
ProgressBar :: setFormatDefinition ( 'very_verbose' , " %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% \n %message% " );
ProgressBar :: setFormatDefinition ( 'debug' , " %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s% \n %message% " );
}
2011-04-12 04:27:47 +02:00
}
/**
2015-03-11 05:11:13 +01:00
* { @ inheritDoc }
2011-04-12 04:27:47 +02:00
*/
protected function execute ( InputInterface $input , OutputInterface $output )
{
2015-03-11 05:11:13 +01:00
$index = $input -> getOption ( 'index' );
$type = $input -> getOption ( 'type' );
$reset = ! $input -> getOption ( 'no-reset' );
2015-03-14 09:54:23 +01:00
$options = array (
'ignore_errors' => $input -> getOption ( 'ignore-errors' ),
'offset' => $input -> getOption ( 'offset' ),
'sleep' => $input -> getOption ( 'sleep' )
);
2015-03-27 01:45:03 +01:00
if ( $input -> getOption ( 'batch-size' )) {
$options [ 'batch_size' ] = ( int ) $input -> getOption ( 'batch-size' );
}
2014-02-19 11:01:54 +01:00
2014-01-24 14:20:10 +01:00
if ( $input -> isInteractive () && $reset && $input -> getOption ( 'offset' )) {
2013-10-08 12:00:04 +02:00
/** @var DialogHelper $dialog */
$dialog = $this -> getHelperSet () -> get ( 'dialog' );
if ( ! $dialog -> askConfirmation ( $output , '<question>You chose to reset the index and start indexing with an offset. Do you really want to do that?</question>' , true )) {
return ;
}
}
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 ) {
2013-10-08 12:00:04 +02:00
$this -> populateIndexType ( $output , $index , $type , $reset , $options );
2012-03-12 17:02:25 +01:00
} else {
2013-10-08 12:00:04 +02:00
$this -> populateIndex ( $output , $index , $reset , $options );
2012-03-12 17:02:25 +01:00
}
} else {
$indexes = array_keys ( $this -> indexManager -> getAllIndexes ());
foreach ( $indexes as $index ) {
2013-10-08 12:00:04 +02:00
$this -> populateIndex ( $output , $index , $reset , $options );
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
2013-10-08 12:00:04 +02:00
* @ param array $options
2012-03-12 17:02:25 +01:00
*/
2013-10-08 12:00:04 +02:00
private function populateIndex ( OutputInterface $output , $index , $reset , $options )
2012-03-12 17:02:25 +01:00
{
2015-03-11 04:11:18 +01:00
$event = new IndexPopulateEvent ( $index , $reset , $options );
$this -> dispatcher -> dispatch ( IndexPopulateEvent :: PRE_INDEX_POPULATE , $event );
if ( $event -> isReset ()) {
2012-09-19 12:48:05 +02:00
$output -> writeln ( sprintf ( '<info>Resetting</info> <comment>%s</comment>' , $index ));
2014-06-27 07:08:08 +02:00
$this -> resetter -> resetIndex ( $index , true );
2012-06-13 18:08:08 +02:00
}
2012-03-12 17:02:25 +01:00
2015-03-11 12:17:14 +01:00
$types = array_keys ( $this -> providerRegistry -> getIndexProviders ( $index ));
foreach ( $types as $type ) {
2015-03-11 04:30:10 +01:00
$this -> populateIndexType ( $output , $index , $type , false , $event -> getOptions ());
2012-03-12 17:02:25 +01:00
}
2015-03-11 04:11:18 +01:00
$this -> dispatcher -> dispatch ( IndexPopulateEvent :: POST_INDEX_POPULATE , $event );
2015-01-22 01:18:51 +01:00
$this -> refreshIndex ( $output , $index );
2012-03-12 17:02:25 +01:00
}
/**
* 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
2013-10-08 12:00:04 +02:00
* @ param array $options
2012-03-12 17:02:25 +01:00
*/
2013-10-08 12:00:04 +02:00
private function populateIndexType ( OutputInterface $output , $index , $type , $reset , $options )
2012-03-12 17:02:25 +01:00
{
2015-03-11 04:30:10 +01:00
$event = new TypePopulateEvent ( $index , $type , $reset , $options );
$this -> dispatcher -> dispatch ( TypePopulateEvent :: PRE_TYPE_POPULATE , $event );
if ( $event -> isReset ()) {
2013-06-18 09:52:44 +02:00
$output -> writeln ( sprintf ( '<info>Resetting</info> <comment>%s/%s</comment>' , $index , $type ));
2012-05-07 18:10:26 +02:00
$this -> resetter -> resetIndexType ( $index , $type );
}
2011-04-12 04:27:47 +02:00
2012-03-12 17:02:25 +01:00
$provider = $this -> providerRegistry -> getProvider ( $index , $type );
2015-03-12 07:41:26 +01:00
$loggerClosure = $this -> progressClosureBuilder -> build ( $output , 'Populating' , $index , $type );
2015-03-11 04:30:10 +01:00
$provider -> populate ( $loggerClosure , $event -> getOptions ());
$this -> dispatcher -> dispatch ( TypePopulateEvent :: POST_TYPE_POPULATE , $event );
2015-01-22 01:18:51 +01:00
$this -> refreshIndex ( $output , $index , false );
}
/**
* Refreshes an index .
*
* @ param OutputInterface $output
2015-03-12 11:45:24 +01:00
* @ param string $index
* @ param bool $postPopulate
2015-01-22 01:18:51 +01:00
*/
private function refreshIndex ( OutputInterface $output , $index , $postPopulate = true )
{
if ( $postPopulate ) {
$this -> resetter -> postPopulate ( $index );
}
2011-09-12 18:28:59 +02:00
2013-06-18 09:52:44 +02:00
$output -> writeln ( sprintf ( '<info>Refreshing</info> <comment>%s</comment>' , $index ));
2012-03-12 17:02:25 +01:00
$this -> indexManager -> getIndex ( $index ) -> refresh ();
2011-04-12 04:27:47 +02:00
}
}