You are here

class SearchApiSolrAcquiaConnector in Acquia Connector 8

Acquia Search Plugin for Search API Solr.

@package Drupal\acquia_search\Plugin\SolrConnector

Plugin annotation


@SolrConnector(
  id = "solr_acquia_connector",
  label = @Translation("Acquia"),
  description = @Translation("Index items using an Acquia Apache Solr search server.")
)

Hierarchy

  • class \Drupal\acquia_search\Plugin\SolrConnector\SearchApiSolrAcquiaConnector extends \Drupal\search_api_solr\SolrConnector\SolrConnectorPluginBase

Expanded class hierarchy of SearchApiSolrAcquiaConnector

1 file declares its use of SearchApiSolrAcquiaConnector
AcquiaSearchOverrideTest.php in acquia_search/tests/src/Kernel/AcquiaSearchOverrideTest.php

File

acquia_search/src/Plugin/SolrConnector/SearchApiSolrAcquiaConnector.php, line 29

Namespace

Drupal\acquia_search\Plugin\SolrConnector
View source
class SearchApiSolrAcquiaConnector extends SolrConnectorPluginBase {

  /**
   * Event subscriber.
   *
   * @var \Drupal\acquia_search\EventSubscriber\SearchSubscriber
   */
  protected $searchSubscriber;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $plugin = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $plugin->searchSubscriber = $container
      ->get('acquia_search.search_subscriber');
    return $plugin;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    $configuration = parent::defaultConfiguration();
    $storage = new Storage();
    $configuration['index_id'] = $storage
      ->getIdentifier();
    $configuration['path'] = '/';
    $configuration['host'] = acquia_search_get_search_host();
    $configuration['scheme'] = empty($configuration['scheme']) ? 'http' : $configuration['scheme'];
    $default_port = $configuration['scheme'] == 'https' ? 443 : 80;
    $configuration['port'] = empty($configuration['port']) ? $default_port : $configuration['port'];
    unset($configuration['overridden_by_acquia_search']);

    // If auto-switch feature is turned off - do not attempt to determine the
    // preferred core.
    if (acquia_search_is_auto_switch_disabled()) {
      return $configuration;
    }

    // If the search config is overridden in settings.php, apply this config
    // to the Solr connection and don't attempt to determine the preferred
    // core.
    if (acquia_search_is_connection_config_overridden()) {
      $configuration = $this
        ->setOverriddenCore($configuration);
      return $configuration;
    }
    $preferred_core_service = acquia_search_get_core_service();

    // If the preferred core available, set it.
    if ($preferred_core_service
      ->isPreferredCoreAvailable()) {
      $configuration = $this
        ->setPreferredCore($configuration, $preferred_core_service);
    }
    else {

      // This means we can't detect which search core should be used, so we
      // need to protect it by setting read-only mode but only if it applies.
      if (acquia_search_should_set_read_only_mode()) {
        $configuration = $this
          ->setReadOnlyMode($configuration);
      }
    }
    return $configuration;
  }

  /**
   * Sets the preferred core in the given Solr config.
   *
   * @param array $configuration
   *   Solr connection configuration.
   * @param \Drupal\acquia_search\PreferredSearchCoreService $preferred_core_service
   *   Service for determining the preferred search core.
   *
   * @return array
   *   Updated Solr connection configuration.
   */
  protected function setPreferredCore(array $configuration, PreferredSearchCoreService $preferred_core_service) {
    $configuration['host'] = $preferred_core_service
      ->getPreferredCoreHostname();
    $configuration['core'] = $preferred_core_service
      ->getPreferredCoreId();
    $configuration['index_id'] = $preferred_core_service
      ->getPreferredCoreId();
    $configuration['overridden_by_acquia_search'] = ACQUIA_SEARCH_OVERRIDE_AUTO_SET;
    return $configuration;
  }

  /**
   * Sets the current connection overrides to the given Solr config.
   *
   * @param array $configuration
   *   Solr connection configuration.
   *
   * @return array
   *   Updated Solr connection configuration.
   */
  protected function setOverriddenCore(array $configuration) {
    $override = \Drupal::config('acquia_search.settings')
      ->get('connection_override');
    $configuration['overridden_by_acquia_search'] = ACQUIA_SEARCH_EXISTING_OVERRIDE;
    $configuration['path'] = '/';
    return array_merge($configuration, $override);
  }

  /**
   * Sets read-only mode to the given Solr config.
   *
   * We enforce read-only mode in 2 ways:
   * - The module implements hook_search_api_index_load() and alters indexes'
   * read-only flag.
   * - In this plugin, we "emulate" read-only mode by overriding
   * $this->getUpdateQuery() and avoiding all updates just in case something
   * is still attempting to directly call a Solr update.
   *
   * @param array $configuration
   *   Solr connection configuration.
   *
   * @return array
   *   Updated Solr connection configuration.
   */
  protected function setReadOnlyMode(array $configuration) {
    $configuration['overridden_by_acquia_search'] = ACQUIA_SEARCH_AUTO_OVERRIDE_READ_ONLY;
    return $configuration;
  }

  /**
   * {@inheritdoc}
   *
   * Acquia-specific: 'admin/info/system' path is protected by Acquia.
   * Use admin/system instead.
   */
  public function pingServer() {
    return $this
      ->doPing([
      'handler' => 'admin/system',
    ], 'core');
  }

  /**
   * {@inheritdoc}
   */
  public function getServerInfo($reset = FALSE) {
    $this
      ->useTimeout();
    return $this
      ->getDataFromHandler('admin/system', $reset);
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    unset($form['host']);
    unset($form['port']);
    unset($form['path']);
    unset($form['core']);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {

    // Turn off connection check of parent class.
  }

  /**
   * {@inheritdoc}
   */
  protected function connect() {
    if ($this->solr) {
      return;
    }
    $this->solr = new Client(new Http(), $this->eventDispatcher);

    // Scheme should always be https and port 443.
    $this->configuration['scheme'] = 'https';
    $this->configuration['port'] = 443;
    $this->configuration['key'] = 'core';
    $this->configuration['path'] = '/';
    $this->configuration[self::QUERY_TIMEOUT] = $this->configuration['timeout'];
    $this->solr
      ->createEndpoint($this->configuration, TRUE);
    $this->solr
      ->registerPlugin('acquia_search_subscriber', $this->searchSubscriber);
  }

  /**
   * {@inheritdoc}
   */
  protected function getServerUri() {
    $this
      ->connect();
    return $this->solr
      ->getEndpoint()
      ->getServerUri();
  }

  /**
   * {@inheritdoc}
   *
   * Avoid providing an valid Update query if module determines this server
   * should be locked down (as indicated by the overridden_by_acquia_search
   * server option).
   *
   * @throws \Exception
   *   If the Search API Server is currently in read-only mode.
   */
  public function getUpdateQuery() {
    $this
      ->connect();
    $overridden = $this->solr
      ->getEndpoint('core')
      ->getOption('overridden_by_acquia_search');
    if ($overridden === ACQUIA_SEARCH_AUTO_OVERRIDE_READ_ONLY) {
      $message = 'The Search API Server serving this index is currently in read-only mode.';
      \Drupal::logger('acquia search')
        ->error($message);
      throw new \Exception($message);
    }
    return $this->solr
      ->createUpdate();
  }

  /**
   * {@inheritdoc}
   */
  public function getExtractQuery() {
    $this
      ->connect();
    $query = $this->solr
      ->createExtract();
    $query
      ->setHandler('extract/tika');
    return $query;
  }

  /**
   * {@inheritdoc}
   */
  public function getMoreLikeThisQuery() {
    $this
      ->connect();
    $query = $this->solr
      ->createMoreLikeThis();
    $query
      ->setHandler('select');
    $query
      ->addParam('qt', 'mlt');
    return $query;
  }

  /**
   * {@inheritdoc}
   */
  public function viewSettings() {
    $uri = Url::fromUri('https://docs.acquia.com/acquia-search/', [
      'absolute' => TRUE,
    ]);
    $link = Link::fromTextAndUrl($this
      ->t('Acquia Search'), $uri);
    $message = $this
      ->t('Search is provided by @acquia_search.', [
      '@acquia_search' => $link
        ->toString(),
    ]);
    $this
      ->messenger()
      ->addStatus($message);
    return parent::viewSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function reloadCore() {
    return FALSE;
  }
  public function getAutocompleteQuery() {
    return parent::getAutocompleteQuery();
  }
  public function autocomplete(AutocompleteQuery $query, ?Endpoint $endpoint = NULL) {
    return parent::autocomplete($query, $endpoint);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SearchApiSolrAcquiaConnector::$searchSubscriber protected property Event subscriber.
SearchApiSolrAcquiaConnector::autocomplete public function
SearchApiSolrAcquiaConnector::buildConfigurationForm public function
SearchApiSolrAcquiaConnector::connect protected function
SearchApiSolrAcquiaConnector::create public static function
SearchApiSolrAcquiaConnector::defaultConfiguration public function
SearchApiSolrAcquiaConnector::getAutocompleteQuery public function
SearchApiSolrAcquiaConnector::getExtractQuery public function
SearchApiSolrAcquiaConnector::getMoreLikeThisQuery public function
SearchApiSolrAcquiaConnector::getServerInfo public function
SearchApiSolrAcquiaConnector::getServerUri protected function
SearchApiSolrAcquiaConnector::getUpdateQuery public function Avoid providing an valid Update query if module determines this server should be locked down (as indicated by the overridden_by_acquia_search server option).
SearchApiSolrAcquiaConnector::pingServer public function Acquia-specific: 'admin/info/system' path is protected by Acquia. Use admin/system instead.
SearchApiSolrAcquiaConnector::reloadCore public function
SearchApiSolrAcquiaConnector::setOverriddenCore protected function Sets the current connection overrides to the given Solr config.
SearchApiSolrAcquiaConnector::setPreferredCore protected function Sets the preferred core in the given Solr config.
SearchApiSolrAcquiaConnector::setReadOnlyMode protected function Sets read-only mode to the given Solr config.
SearchApiSolrAcquiaConnector::validateConfigurationForm public function
SearchApiSolrAcquiaConnector::viewSettings public function