/**
  * @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.');
 }
 public function run($request)
 {
     parent::run($request);
     // Find the IndexStore handler, which will handle uploading config files to Solr
     $store = $this->getSolrConfigStore();
     $indexes = Solr::get_indexes();
     foreach ($indexes as $instance) {
         try {
             $this->updateIndex($instance, $store);
         } catch (Exception $e) {
             // We got an exception. Warn, but continue to next index.
             $this->getLogger()->error("Failure: " . $e->getMessage());
         }
     }
 }
 public function runReindex(LoggerInterface $logger, $batchSize, $taskName, $classes = null)
 {
     foreach (Solr::get_indexes() as $indexInstance) {
         $this->processIndex($logger, $indexInstance, $batchSize, $taskName, $classes);
     }
 }
 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();
 }