示例#1
0
 /**
  * Rebuild lead lists
  *
  * @param LeadList        $entity
  * @param int             $limit
  * @param bool            $maxLeads
  * @param OutputInterface $output
  *
  * @return int
  */
 public function rebuildListLeads(LeadList $entity, $limit = 1000, $maxLeads = false, OutputInterface $output = null)
 {
     defined('MAUTIC_REBUILDING_LEAD_LISTS') or define('MAUTIC_REBUILDING_LEAD_LISTS', 1);
     $id = $entity->getId();
     $list = array('id' => $id, 'filters' => $entity->getFilters());
     $dtHelper = $this->factory->getDate();
     $batchLimiters = array('dateTime' => $dtHelper->toUtcString());
     $localDateTime = $dtHelper->getLocalDateTime();
     // Get a count of leads to add
     $newLeadsCount = $this->getLeadsByList($list, true, array('countOnly' => true, 'newOnly' => true, 'batchLimiters' => $batchLimiters));
     // Ensure the same list is used each batch
     $batchLimiters['maxId'] = (int) $newLeadsCount[$id]['maxId'];
     // Number of total leads to process
     $leadCount = (int) $newLeadsCount[$id]['count'];
     if ($output) {
         $output->writeln($this->translator->trans('mautic.lead.list.rebuild.to_be_added', array('%leads%' => $leadCount, '%batch%' => $limit)));
     }
     // Handle by batches
     $start = $lastRoundPercentage = $leadsProcessed = 0;
     // Try to save some memory
     gc_enable();
     if ($leadCount) {
         $maxCount = $maxLeads ? $maxLeads : $leadCount;
         if ($output) {
             $progress = new ProgressBar($output, $maxCount);
             $progress->start();
         }
         // Add leads
         while ($start < $leadCount) {
             // Keep CPU down for large lists; sleep per $limit batch
             $this->batchSleep();
             $newLeadList = $this->getLeadsByList($list, true, array('newOnly' => true, 'limit' => $limit, 'batchLimiters' => $batchLimiters));
             if (empty($newLeadList[$id])) {
                 // Somehow ran out of leads so break out
                 break;
             }
             foreach ($newLeadList[$id] as $l) {
                 $this->addLead($l, $entity, false, true, -1, $localDateTime);
                 unset($l);
                 $leadsProcessed++;
                 if ($output && $leadsProcessed < $maxCount) {
                     $progress->setProgress($leadsProcessed);
                 }
                 if ($maxLeads && $leadsProcessed >= $maxLeads) {
                     break;
                 }
             }
             $start += $limit;
             // Dispatch batch event
             if ($this->dispatcher->hasListeners(LeadEvents::LEAD_LIST_BATCH_CHANGE)) {
                 $event = new ListChangeEvent($newLeadList[$id], $entity, true);
                 $this->dispatcher->dispatch(LeadEvents::LEAD_LIST_BATCH_CHANGE, $event);
                 unset($event);
             }
             unset($newLeadList);
             // Free some memory
             gc_collect_cycles();
             if ($maxLeads && $leadsProcessed >= $maxLeads) {
                 if ($output) {
                     $progress->finish();
                     $output->writeln('');
                 }
                 return $leadsProcessed;
             }
         }
         if ($output) {
             $progress->finish();
             $output->writeln('');
         }
     }
     // Get a count of leads to be removed
     $removeLeadCount = $this->getLeadsByList($list, true, array('countOnly' => true, 'nonMembersOnly' => true, 'batchLimiters' => $batchLimiters));
     // Restart batching
     $start = $lastRoundPercentage = 0;
     $leadCount = $removeLeadCount[$id]['count'];
     if ($output) {
         $output->writeln($this->translator->trans('mautic.lead.list.rebuild.to_be_removed', array('%leads%' => $leadCount, '%batch%' => $limit)));
     }
     if ($leadCount) {
         $maxCount = $maxLeads ? $maxLeads : $leadCount;
         if ($output) {
             $progress = new ProgressBar($output, $maxCount);
             $progress->start();
         }
         // Remove leads
         while ($start < $leadCount) {
             // Keep CPU down for large lists; sleep per $limit batch
             $this->batchSleep();
             $removeLeadList = $this->getLeadsByList($list, true, array('limit' => $limit, 'nonMembersOnly' => true, 'batchLimiters' => $batchLimiters));
             if (empty($removeLeadList[$id])) {
                 // Somehow ran out of leads so break out
                 break;
             }
             foreach ($removeLeadList[$id] as $l) {
                 $this->removeLead($l, $entity, false, true, true);
                 $leadsProcessed++;
                 if ($output && $leadsProcessed < $maxCount) {
                     $progress->setProgress($leadsProcessed);
                 }
                 if ($maxLeads && $leadsProcessed >= $maxLeads) {
                     break;
                 }
             }
             // Dispatch batch event
             if ($this->dispatcher->hasListeners(LeadEvents::LEAD_LIST_BATCH_CHANGE)) {
                 $event = new ListChangeEvent($removeLeadList[$id], $entity, false);
                 $this->dispatcher->dispatch(LeadEvents::LEAD_LIST_BATCH_CHANGE, $event);
                 unset($event);
             }
             $start += $limit;
             unset($removeLeadList);
             // Free some memory
             gc_collect_cycles();
             if ($maxLeads && $leadsProcessed >= $maxLeads) {
                 if ($output) {
                     $progress->finish();
                     $output->writeln('');
                 }
                 return $leadsProcessed;
             }
         }
         if ($output) {
             $progress->finish();
             $output->writeln('');
         }
     }
     return $leadsProcessed;
 }
 /**
  * {@inheritDoc}
  */
 public function getFilters()
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'getFilters', array());
     return parent::getFilters();
 }