/**
  * Process a single group.
  *
  * Without queuedjobs, it's necessary to shell this out to a background task as this is
  * very memory intensive.
  *
  * The sub-process will then invoke $processor->runGroup() in {@see Solr_Reindex::doReindex}
  *
  * @param LoggerInterface $logger
  * @param SolrIndex $indexInstance Index instance
  * @param array $state Variant state
  * @param string $class Class to index
  * @param int $groups Total groups
  * @param int $group Index of group to process
  * @param string $taskName Name of task script to run
  */
 protected function processGroup(LoggerInterface $logger, SolrIndex $indexInstance, $state, $class, $groups, $group, $taskName)
 {
     // Build state
     $statevar = json_encode($state);
     if (strpos(PHP_OS, "WIN") !== false) {
         $statevar = '"' . str_replace('"', '\\"', $statevar) . '"';
     } else {
         $statevar = "'" . $statevar . "'";
     }
     // Build script
     $indexName = $indexInstance->getIndexName();
     $scriptPath = sprintf("%s%sframework%scli-script.php", BASE_PATH, DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR);
     $scriptTask = "php {$scriptPath} dev/tasks/{$taskName}";
     $cmd = "{$scriptTask} index={$indexName} class={$class} group={$group} groups={$groups} variantstate={$statevar}";
     $cmd .= " verbose=1 2>&1";
     $logger->info("Running '{$cmd}'");
     // Execute script via shell
     $res = $logger ? passthru($cmd) : `{$cmd}`;
     if ($logger) {
         $logger->info(preg_replace('/\\r\\n|\\n/', '$0  ', $res));
     }
     // If we're in dev mode, commit more often for fun and profit
     if (Director::isDev()) {
         Solr::service($indexName)->commit();
     }
     // This will slow down things a tiny bit, but it is done so that we don't timeout to the database during a reindex
     DB::query('SELECT 1');
 }
 /**
  * @inheritdoc
  *
  * @return array
  */
 function check()
 {
     $brokenCores = array();
     if (!class_exists('Solr')) {
         return array(EnvironmentCheck::ERROR, 'Class `Solr` not found. Is the fulltextsearch module installed?');
     }
     $service = Solr::service();
     foreach (Solr::get_indexes($this->indexClass) as $index) {
         $core = $index->getIndexName();
         if (!$service->coreIsActive($core)) {
             $brokenCores[] = $core;
         }
     }
     if (!empty($brokenCores)) {
         return array(EnvironmentCheck::ERROR, 'The following indexes are unavailable: ' . implode($brokenCores, ', '));
     }
     return array(EnvironmentCheck::OK, 'Expected indexes are available.');
 }
 /**
  * @return SolrService
  */
 public function getService()
 {
     if (!$this->service) {
         $this->service = Solr::service(get_class($this));
     }
     return $this->service;
 }
 /**
  * Update the index on the given store
  * 
  * @param SolrIndex $instance Instance
  * @param SolrConfigStore $store
  */
 protected function updateIndex($instance, $store)
 {
     $index = $instance->getIndexName();
     $this->getLogger()->info("Configuring {$index}.");
     // Upload the config files for this index
     $this->getLogger()->info("Uploading configuration ...");
     $instance->uploadConfig($store);
     // Then tell Solr to use those config files
     $service = Solr::service();
     if ($service->coreIsActive($index)) {
         $this->getLogger()->info("Reloading core ...");
         $service->coreReload($index);
     } else {
         $this->getLogger()->info("Creating core ...");
         $service->coreCreate($index, $store->instanceDir($index));
     }
     $this->getLogger()->info("Done");
 }
 public function run($request)
 {
     increase_time_limit_to();
     $self = get_class($this);
     $verbose = isset($_GET['verbose']);
     $originalState = SearchVariant::current_state();
     if (isset($_GET['start'])) {
         $this->runFrom(singleton($_GET['index']), $_GET['class'], $_GET['start'], json_decode($_GET['variantstate'], true));
     } else {
         foreach (array('framework', 'sapphire') as $dirname) {
             $script = sprintf("%s%s{$dirname}%scli-script.php", BASE_PATH, DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR);
             if (file_exists($script)) {
                 break;
             }
         }
         $class = get_class($this);
         foreach (Solr::get_indexes() as $index => $instance) {
             echo "Rebuilding {$instance->getIndexName()}\n\n";
             $classes = $instance->getClasses();
             if ($request->getVar('class')) {
                 $limitClasses = explode(',', $request->getVar('class'));
                 $classes = array_intersect_key($classes, array_combine($limitClasses, $limitClasses));
             }
             if ($classes) {
                 Solr::service($index)->deleteByQuery('ClassHierarchy:(' . implode(' OR ', array_keys($classes)) . ')');
             }
             foreach ($classes as $class => $options) {
                 $includeSubclasses = $options['include_children'];
                 foreach (SearchVariant::reindex_states($class, $includeSubclasses) as $state) {
                     if ($instance->variantStateExcluded($state)) {
                         continue;
                     }
                     SearchVariant::activate_state($state);
                     $filter = $includeSubclasses ? "" : '"ClassName" = \'' . $class . "'";
                     $singleton = singleton($class);
                     $query = $singleton->get($class, $filter, null);
                     $dtaQuery = $query->dataQuery();
                     $sqlQuery = $dtaQuery->getFinalisedQuery();
                     $singleton->extend('augmentSQL', $sqlQuery, $dtaQuery);
                     $total = $query->count();
                     $statevar = json_encode($state);
                     echo "Class: {$class}, total: {$total}";
                     echo $statevar ? " in state {$statevar}\n" : "\n";
                     if (strpos(PHP_OS, "WIN") !== false) {
                         $statevar = '"' . str_replace('"', '\\"', $statevar) . '"';
                     } else {
                         $statevar = "'" . $statevar . "'";
                     }
                     for ($offset = 0; $offset < $total; $offset += $this->stat('recordsPerRequest')) {
                         echo "{$offset}..";
                         $cmd = "php {$script} dev/tasks/{$self} index={$index} class={$class} start={$offset} variantstate={$statevar}";
                         if ($verbose) {
                             echo "\n  Running '{$cmd}'\n";
                             $cmd .= " verbose=1 2>&1";
                         }
                         $res = $verbose ? passthru($cmd) : `{$cmd}`;
                         if ($verbose) {
                             echo "  " . preg_replace('/\\r\\n|\\n/', '$0  ', $res) . "\n";
                         }
                         // If we're in dev mode, commit more often for fun and profit
                         if (Director::isDev()) {
                             Solr::service($index)->commit();
                         }
                         // This will slow down things a tiny bit, but it is done so that we don't timeout to the database during a reindex
                         DB::query('SELECT 1');
                     }
                     echo "\n";
                 }
             }
             Solr::service($index)->commit();
         }
     }
     $originalState = SearchVariant::current_state();
 }