You are here

public function SolrConfigSetController::getConfigFiles in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 src/Controller/SolrConfigSetController.php \Drupal\search_api_solr\Controller\SolrConfigSetController::getConfigFiles()

Returns the configuration files names and content.

Return value

array An associative array of files names and content.

Throws

\Drupal\search_api\SearchApiException

\Drupal\search_api_solr\SearchApiSolrException

1 call to SolrConfigSetController::getConfigFiles()
SolrConfigSetController::getConfigZip in src/Controller/SolrConfigSetController.php
Returns a ZipStream of all configuration files.

File

src/Controller/SolrConfigSetController.php, line 251

Class

SolrConfigSetController
Provides different listings of SolrFieldType.

Namespace

Drupal\search_api_solr\Controller

Code

public function getConfigFiles() : array {

  /** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
  $backend = $this
    ->getBackend();
  $connector = $backend
    ->getSolrConnector();
  $solr_branch = $real_solr_branch = $connector
    ->getSolrBranch($this->assumedMinimumVersion);
  $solr_major_version = $connector
    ->getSolrMajorVersion($this->assumedMinimumVersion);
  $template_path = drupal_get_path('module', 'search_api_solr') . '/solr-conf-templates/';
  $solr_configset_template_mapping = [
    '6.x' => $template_path . '6.x',
    '7.x' => $template_path . '7.x',
    '8.x' => $template_path . '8.x',
  ];
  $this
    ->moduleHandler()
    ->alter('search_api_solr_configset_template_mapping', $solr_configset_template_mapping);
  $search_api_solr_conf_path = $solr_configset_template_mapping[$solr_branch];
  $solrcore_properties_file = $search_api_solr_conf_path . '/solrcore.properties';
  if (file_exists($solrcore_properties_file) && is_readable($solrcore_properties_file)) {
    $solrcore_properties = parse_ini_file($solrcore_properties_file, FALSE, INI_SCANNER_RAW);
  }
  else {
    throw new SearchApiSolrException('solrcore.properties template could not be parsed.');
  }
  $files = [
    'schema_extra_types.xml' => $this
      ->getSchemaExtraTypesXml(),
    'schema_extra_fields.xml' => $this
      ->getSchemaExtraFieldsXml($backend
      ->getServer()),
    'solrconfig_extra.xml' => $this
      ->getSolrconfigExtraXml(),
    'solrconfig_index.xml' => $this
      ->getSolrconfigIndexXml(),
  ];
  if (!$backend
    ->isNonDrupalOrOutdatedConfigSetAllowed() && (empty($files['schema_extra_types.xml']) || empty($files['schema_extra_fields.xml']))) {
    throw new SearchApiSolrException(sprintf('The configs of the essential Solr field types are missing or broken for server "%s".', $backend
      ->getServer()
      ->id()));
  }
  if (version_compare($solr_major_version, '7', '>=')) {
    $files['solrconfig_query.xml'] = $this
      ->getSolrconfigQueryXml();
    $files['solrconfig_requestdispatcher.xml'] = $this
      ->getSolrconfigRequestDispatcherXml();
  }

  // Add language specific text files.
  $list_builder = $this
    ->getListBuilder('solr_field_type');
  $solr_field_types = $list_builder
    ->getEnabledEntities();

  /** @var \Drupal\search_api_solr\SolrFieldTypeInterface $solr_field_type */
  foreach ($solr_field_types as $solr_field_type) {
    $text_files = $solr_field_type
      ->getTextFiles();
    foreach ($text_files as $text_file_name => $text_file) {
      $text_file_name = Utility::completeTextFileName($text_file_name, $solr_field_type);
      $files[$text_file_name] = $text_file;
      $solrcore_properties['solr.replication.confFiles'] .= ',' . $text_file_name;
    }
  }
  $solrcore_properties['solr.luceneMatchVersion'] = $connector
    ->getLuceneMatchVersion($this->assumedMinimumVersion ?: '');
  if (!$connector
    ->isCloud()) {

    // @todo
    // $solrcore_properties['solr.replication.masterUrl']
    $solrcore_properties_string = '';
    foreach ($solrcore_properties as $property => $value) {
      $solrcore_properties_string .= $property . '=' . $value . "\n";
    }
    $files['solrcore.properties'] = $solrcore_properties_string;
  }

  // Now add all remaining static files from the conf dir that have not been
  // generated dynamically above.
  foreach (scandir($search_api_solr_conf_path) as $file) {
    if (strpos($file, '.') !== 0 && !array_key_exists($file, $files)) {
      $file_path = $search_api_solr_conf_path . '/' . $file;
      if (file_exists($file_path) && is_readable($file_path)) {
        $files[$file] = str_replace([
          'SEARCH_API_SOLR_SCHEMA_VERSION',
          'SEARCH_API_SOLR_BRANCH',
          'SEARCH_API_SOLR_JUMP_START_CONFIG_SET',
        ], [
          SolrBackendInterface::SEARCH_API_SOLR_SCHEMA_VERSION,
          $real_solr_branch,
          SEARCH_API_SOLR_JUMP_START_CONFIG_SET,
        ], file_get_contents($search_api_solr_conf_path . '/' . $file));
      }
      else {
        throw new SearchApiSolrException(sprintf('%s template is not readable.', $file));
      }
    }
  }
  if ($connector
    ->isCloud() && isset($files['solrconfig.xml'])) {

    // solrcore.properties won’t work in SolrCloud mode (it is not read from
    // ZooKeeper). Therefore we go for a more specific fallback to keep the
    // possibility to set the property as parameter of the virtual machine.
    // @see https://lucene.apache.org/solr/guide/8_6/configuring-solrconfig-xml.html
    $files['solrconfig.xml'] = preg_replace('/solr.luceneMatchVersion:LUCENE_\\d+/', 'solr.luceneMatchVersion:' . $solrcore_properties['solr.luceneMatchVersion'], $files['solrconfig.xml']);
    unset($files['solrcore.properties']);
  }
  $connector
    ->alterConfigFiles($files, $solrcore_properties['solr.luceneMatchVersion'], $this->serverId);
  $this
    ->moduleHandler()
    ->alter('search_api_solr_config_files', $files, $solrcore_properties['solr.luceneMatchVersion'], $this->serverId);
  return $files;
}