You are here

abstract class SolrConnectorPluginBase in Search API Solr 8.3

Same name and namespace in other branches
  1. 8 src/SolrConnector/SolrConnectorPluginBase.php \Drupal\search_api_solr\SolrConnector\SolrConnectorPluginBase
  2. 8.2 src/SolrConnector/SolrConnectorPluginBase.php \Drupal\search_api_solr\SolrConnector\SolrConnectorPluginBase
  3. 4.x src/SolrConnector/SolrConnectorPluginBase.php \Drupal\search_api_solr\SolrConnector\SolrConnectorPluginBase

Defines a base class for Solr connector plugins.

Plugins extending this class need to define a plugin definition array through annotation. These definition arrays may be altered through hook_search_api_solr_connector_info_alter(). The definition includes the following keys:

  • id: The unique, system-wide identifier of the backend class.
  • label: The human-readable name of the backend class, translated.
  • description: A human-readable description for the backend class, translated.

A complete plugin definition should be written as in this example:


@SolrConnector(
  id = "my_connector",
  label = @Translation("My connector"),
  description = @Translation("Authenticates with SuperAuth™.")
)

Hierarchy

Expanded class hierarchy of SolrConnectorPluginBase

See also

\Drupal\search_api_solr\Annotation\SolrConnector

\Drupal\search_api_solr\SolrConnector\SolrConnectorPluginManager

\Drupal\search_api_solr\SolrConnectorInterface

Plugin API

1 file declares its use of SolrConnectorPluginBase
StandardSolrConnector.php in src/Plugin/SolrConnector/StandardSolrConnector.php

File

src/SolrConnector/SolrConnectorPluginBase.php, line 56

Namespace

Drupal\search_api_solr\SolrConnector
View source
abstract class SolrConnectorPluginBase extends ConfigurablePluginBase implements SolrConnectorInterface, PluginFormInterface {
  use PluginFormTrait {
    submitConfigurationForm as traitSubmitConfigurationForm;
  }
  use LoggerTrait;

  /**
   * The event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * A connection to the Solr server.
   *
   * @var \Solarium\Client
   */
  protected $solr;

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

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'scheme' => 'http',
      'host' => 'localhost',
      'port' => 8983,
      'path' => '/',
      'core' => '',
      'timeout' => 5,
      self::INDEX_TIMEOUT => 5,
      self::OPTIMIZE_TIMEOUT => 10,
      self::FINALIZE_TIMEOUT => 30,
      'solr_version' => '',
      'http_method' => 'AUTO',
      'commit_within' => 1000,
      'jmx' => FALSE,
      'solr_install_dir' => '',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $configuration['port'] = (int) $configuration['port'];
    $configuration['timeout'] = (int) $configuration['timeout'];
    $configuration[self::INDEX_TIMEOUT] = (int) $configuration[self::INDEX_TIMEOUT];
    $configuration[self::OPTIMIZE_TIMEOUT] = (int) $configuration[self::OPTIMIZE_TIMEOUT];
    $configuration[self::FINALIZE_TIMEOUT] = (int) $configuration[self::FINALIZE_TIMEOUT];
    $configuration['commit_within'] = (int) $configuration['commit_within'];
    $configuration['jmx'] = (bool) $configuration['jmx'];
    parent::setConfiguration($configuration);
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['scheme'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('HTTP protocol'),
      '#description' => $this
        ->t('The HTTP protocol to use for sending queries.'),
      '#default_value' => isset($this->configuration['scheme']) ? $this->configuration['scheme'] : 'http',
      '#options' => [
        'http' => 'http',
        'https' => 'https',
      ],
    ];
    $form['host'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Solr host'),
      '#description' => $this
        ->t('The host name or IP of your Solr server, e.g. <code>localhost</code> or <code>www.example.com</code>.'),
      '#default_value' => isset($this->configuration['host']) ? $this->configuration['host'] : '',
      '#required' => TRUE,
    ];
    $form['port'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Solr port'),
      '#description' => $this
        ->t('The Jetty example server is at port 8983, while Tomcat uses 8080 by default.'),
      '#default_value' => isset($this->configuration['port']) ? $this->configuration['port'] : '',
      '#required' => TRUE,
    ];
    $form['path'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Solr path'),
      '#description' => $this
        ->t('The path that identifies the Solr instance to use on the server.'),
      '#default_value' => isset($this->configuration['path']) ? $this->configuration['path'] : '/',
    ];
    $form['core'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Solr core'),
      '#description' => $this
        ->t('The name that identifies the Solr core to use on the server.'),
      '#default_value' => isset($this->configuration['core']) ? $this->configuration['core'] : '',
      '#required' => TRUE,
    ];
    $form['timeout'] = [
      '#type' => 'number',
      '#min' => 1,
      '#max' => 180,
      '#title' => $this
        ->t('Query timeout'),
      '#description' => $this
        ->t('The timeout in seconds for search queries sent to the Solr server.'),
      '#default_value' => isset($this->configuration['timeout']) ? $this->configuration['timeout'] : 5,
      '#required' => TRUE,
    ];
    $form[self::INDEX_TIMEOUT] = [
      '#type' => 'number',
      '#min' => 1,
      '#max' => 180,
      '#title' => $this
        ->t('Index timeout'),
      '#description' => $this
        ->t('The timeout in seconds for indexing requests to the Solr server.'),
      '#default_value' => isset($this->configuration[self::INDEX_TIMEOUT]) ? $this->configuration[self::INDEX_TIMEOUT] : 5,
      '#required' => TRUE,
    ];
    $form[self::OPTIMIZE_TIMEOUT] = [
      '#type' => 'number',
      '#min' => 1,
      '#max' => 180,
      '#title' => $this
        ->t('Optimize timeout'),
      '#description' => $this
        ->t('The timeout in seconds for background index optimization queries on a Solr server.'),
      '#default_value' => isset($this->configuration[self::OPTIMIZE_TIMEOUT]) ? $this->configuration[self::OPTIMIZE_TIMEOUT] : 10,
      '#required' => TRUE,
    ];
    $form[self::FINALIZE_TIMEOUT] = [
      '#type' => 'number',
      '#min' => 1,
      '#max' => 180,
      '#title' => $this
        ->t('Finalize timeout'),
      '#description' => $this
        ->t('The timeout in seconds for index finalization queries on a Solr server.'),
      '#default_value' => isset($this->configuration[self::FINALIZE_TIMEOUT]) ? $this->configuration[self::FINALIZE_TIMEOUT] : 30,
      '#required' => TRUE,
    ];
    $form['commit_within'] = [
      '#type' => 'number',
      '#min' => 0,
      '#title' => $this
        ->t('Commit within'),
      '#description' => $this
        ->t('The limit in milliseconds within a (soft) commit on Solr is forced after any updating the index in any way. Setting the value to "0" turns off this dynamic enforcement and lets Solr behave like configured solrconf.xml.'),
      '#default_value' => isset($this->configuration['commit_within']) ? $this->configuration['commit_within'] : 1000,
      '#required' => TRUE,
    ];
    $form['workarounds'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Connector Workarounds'),
    ];
    $form['workarounds']['solr_version'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Solr version override'),
      '#description' => $this
        ->t('Specify the Solr version manually in case it cannot be retrived automatically. The version can be found in the Solr admin interface under "Solr Specification Version" or "solr-spec"'),
      '#options' => [
        '' => $this
          ->t('Determine automatically'),
        '6' => '6.x',
        '7' => '7.x',
        '8' => '8.x',
      ],
      '#default_value' => isset($this->configuration['solr_version']) ? $this->configuration['solr_version'] : '',
    ];
    $form['workarounds']['http_method'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('HTTP method'),
      '#description' => $this
        ->t('The HTTP method to use for sending queries. GET will often fail with larger queries, while POST should not be cached. AUTO will use GET when possible, and POST for queries that are too large.'),
      '#default_value' => isset($this->configuration['http_method']) ? $this->configuration['http_method'] : 'AUTO',
      '#options' => [
        'AUTO' => $this
          ->t('AUTO'),
        'POST' => 'POST',
        'GET' => 'GET',
      ],
    ];
    $form['advanced'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Advanced server configuration'),
    ];
    $form['advanced']['jmx'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable JMX'),
      '#description' => $this
        ->t('Enable JMX based monitoring.'),
      '#default_value' => isset($this->configuration['jmx']) ? $this->configuration['jmx'] : FALSE,
    ];
    $form['advanced']['solr_install_dir'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('solr.install.dir'),
      '#description' => $this
        ->t('The path where Solr is installed on the server, relative to the configuration or absolute. Some examples are "../../.." for Solr downloaded from apache.org, "/usr/local/opt/solr" for installations via homebrew on macOS or "/opt/solr" for some linux distributions and for the official Solr docker container. If you use different systems for development, testing and production you can use drupal config overwrites to adjust the value per environment or adjust the generated solrcore.properties per environment or use java virtual machine options (-D) to set the property. Modern Solr installations should set that virtual machine option correctly in their start script by themselves. In this case this field should be left empty!'),
      '#default_value' => isset($this->configuration['solr_install_dir']) ? $this->configuration['solr_install_dir'] : '',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    if (isset($values['port']) && (!is_numeric($values['port']) || $values['port'] < 0 || $values['port'] > 65535)) {
      $form_state
        ->setError($form['port'], $this
        ->t('The port has to be an integer between 0 and 65535.'));
    }
    if (!empty($values['path']) && strpos($values['path'], '/') !== 0) {
      $form_state
        ->setError($form['path'], $this
        ->t('If provided the path has to start with "/".'));
    }
    if (!empty($values['core']) && strpos($values['core'], '/') === 0) {
      $form_state
        ->setError($form['core'], $this
        ->t('Core or collection must not start with "/".'));
    }
    if (!$form_state
      ->hasAnyErrors()) {

      // Try to orchestrate a server link from form values.
      $values_copied = $values;
      $solr = $this
        ->createClient($values_copied);
      $solr
        ->createEndpoint($values_copied + [
        'key' => 'search_api_solr',
      ], TRUE);
      try {
        $this
          ->getServerLink();
      } catch (\InvalidArgumentException $e) {
        foreach ([
          'scheme',
          'host',
          'port',
          'path',
          'core',
        ] as $part) {
          $form_state
            ->setError($form[$part], $this
            ->t('The server link generated from the form values is illegal.'));
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();

    // Since the form is nested into another, we can't simply use #parents for
    // doing this array restructuring magic. (At least not without creating an
    // unnecessary dependency on internal implementation.)
    foreach ($values['workarounds'] as $key => $value) {
      $form_state
        ->setValue($key, $value);
    }
    foreach ($values['advanced'] as $key => $value) {
      $form_state
        ->setValue($key, $value);
    }

    // Clean-up the form to avoid redundant entries in the stored configuration.
    $form_state
      ->unsetValue('workarounds');
    $form_state
      ->unsetValue('advanced');
    $this
      ->traitSubmitConfigurationForm($form, $form_state);
  }

  /**
   * Prepares the connection to the Solr server.
   */
  protected function connect() {
    if (!$this->solr) {
      $configuration = $this->configuration;
      $this->solr = $this
        ->createClient($configuration);
      $this->solr
        ->createEndpoint($configuration + [
        'key' => 'search_api_solr',
      ], TRUE);
    }
  }

  /**
   * Create a Client.
   */
  protected function createClient(array &$configuration) {
    $configuration[self::QUERY_TIMEOUT] = $configuration['timeout'] ?? 5;
    if (Client::checkMinimal('5.2.0')) {
      $adapter = extension_loaded('curl') ? new Curl($configuration) : new Http($configuration);
      unset($configuration['timeout']);
      return new Client($adapter, $this->eventDispatcher);
    }
    return new Client(NULL, $this->eventDispatcher);
  }

  /**
   * {@inheritdoc}
   */
  public function isCloud() {
    return FALSE;
  }

  /**
   * Returns a the Solr server URI.
   */
  protected function getServerUri() {
    $this
      ->connect();
    $url_path = $this->solr
      ->getEndpoint()
      ->getServerUri();
    if ($this->configuration['host'] === 'localhost' && !empty($_SERVER['SERVER_NAME'])) {
      $url_path = str_replace('localhost', $_SERVER['SERVER_NAME'], $url_path);
    }
    return $url_path;
  }

  /**
   * {@inheritdoc}
   */
  public function getServerLink() {
    $url_path = $this
      ->getServerUri();
    $url = Url::fromUri($url_path);
    return Link::fromTextAndUrl($url_path, $url);
  }

  /**
   * {@inheritdoc}
   */
  public function getCoreLink() {
    $url_path = $this
      ->getServerUri() . 'solr/#/' . $this->configuration['core'];
    $url = Url::fromUri($url_path);
    return Link::fromTextAndUrl($url_path, $url);
  }

  /**
   * {@inheritdoc}
   */
  public function getSolrVersion($force_auto_detect = FALSE) {

    // Allow for overrides by the user.
    if (!$force_auto_detect && !empty($this->configuration['solr_version'])) {

      // In most cases the already stored solr_version is just the major version
      // number as integer. In this case we will expand it to the minimum
      // corresponding full version string.
      $min_version = [
        '0',
        '0',
        '0',
      ];
      $version = implode('.', explode('.', $this->configuration['solr_version']) + $min_version);
      return '4.0.0' === $version ? '4.5.0' : $version;
    }
    $info = [];
    try {
      $info = $this
        ->getCoreInfo();
    } catch (\Exception $e) {
      try {
        $info = $this
          ->getServerInfo();
      } catch (SearchApiSolrException $e) {
      }
    }

    // Get our solr version number.
    if (isset($info['lucene']['solr-spec-version'])) {
      return $info['lucene']['solr-spec-version'];
    }
    return '0.0.0';
  }

  /**
   * {@inheritdoc}
   */
  public function getSolrMajorVersion($version = '') : int {
    [
      $major,
    ] = explode('.', $version ?: $this
      ->getSolrVersion());
    return (int) $major;
  }

  /**
   * {@inheritdoc}
   */
  public function getSolrBranch($version = '') {
    return $this
      ->getSolrMajorVersion($version) . '.x';
  }

  /**
   * {@inheritdoc}
   */
  public function getLuceneMatchVersion($version = '') {
    [
      $major,
      $minor,
    ] = explode('.', $version ?: $this
      ->getSolrVersion());
    return $major . '.' . $minor;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getCoreInfo($reset = FALSE) {
    $this
      ->useTimeout();
    return $this
      ->getDataFromHandler($this->configuration['core'] . '/admin/system', $reset);
  }

  /**
   * {@inheritdoc}
   */
  public function getLuke() {
    $this
      ->useTimeout();
    return $this
      ->getDataFromHandler($this->configuration['core'] . '/admin/luke', TRUE);
  }

  /**
   * {@inheritdoc}
   */
  public function getSchemaVersionString($reset = FALSE) {
    return $this
      ->getCoreInfo($reset)['core']['schema'];
  }

  /**
   * {@inheritdoc}
   */
  public function getSchemaVersion($reset = FALSE) {
    $parts = explode('-', $this
      ->getSchemaVersionString($reset));
    return $parts[1];
  }

  /**
   * Gets data from a Solr endpoint using a given handler.
   *
   * @param string $handler
   *   The handler used for the API query.
   * @param bool $reset
   *   If TRUE the server will be asked regardless if a previous call is cached.
   *
   * @return array
   *   Response data with system information.
   *
   * @throws \Drupal\search_api_solr\SearchApiSolrException
   */
  protected function getDataFromHandler($handler, $reset = FALSE) {
    static $previous_calls = [];
    $this
      ->connect();

    // We keep the results in a state instead of a cache because we want to
    // access parts of this data even if Solr is temporarily not reachable and
    // caches are cleared.
    $state_key = 'search_api_solr.endpoint.data';
    $state = \Drupal::state();
    $endpoint_data = $state
      ->get($state_key);
    $server_uri = $this
      ->getServerUri();
    if (!isset($previous_calls[$server_uri][$handler]) || !isset($endpoint_data[$server_uri][$handler]) || $reset) {

      // Don't retry multiple times in case of an exception.
      $previous_calls[$server_uri][$handler] = TRUE;
      if (!is_array($endpoint_data) || !isset($endpoint_data[$server_uri][$handler]) || $reset) {
        $query = $this->solr
          ->createApi([
          'handler' => $handler,
          'version' => Request::API_V1,
        ]);
        $endpoint_data[$server_uri][$handler] = $this
          ->execute($query)
          ->getData();
        $state
          ->set($state_key, $endpoint_data);
      }
    }
    return $endpoint_data[$server_uri][$handler];
  }

  /**
   * {@inheritdoc}
   */
  public function pingCore(array $options = []) {
    $this
      ->connect();
    $this
      ->useTimeout();
    $query = $this->solr
      ->createPing();
    try {
      $start = microtime(TRUE);
      $result = $this->solr
        ->execute($query);
      if ($result
        ->getResponse()
        ->getStatusCode() == 200) {

        // Add 1 µs to the ping time so we never return 0.
        return microtime(TRUE) - $start + 1.0E-6;
      }
    } catch (HttpException $e) {

      // Don't handle the exception. Just return FALSE below.
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function pingServer() {
    return $this
      ->getServerInfo(TRUE);
  }

  /**
   * {@inheritdoc}
   */
  public function getStatsSummary() {
    $this
      ->connect();
    $this
      ->useTimeout();
    $summary = [
      '@pending_docs' => '',
      '@autocommit_time_seconds' => '',
      '@autocommit_time' => '',
      '@deletes_by_id' => '',
      '@deletes_by_query' => '',
      '@deletes_total' => '',
      '@schema_version' => '',
      '@core_name' => '',
      '@index_size' => '',
    ];
    $query = $this->solr
      ->createPing();
    $query
      ->setResponseWriter(Query::WT_PHPS);
    $query
      ->setHandler('admin/mbeans?stats=true');
    $stats = $this
      ->execute($query)
      ->getData();
    if (!empty($stats)) {
      $solr_version = $this
        ->getSolrVersion(TRUE);
      $max_time = -1;
      if (version_compare($solr_version, '7.0', '>=')) {
        $update_handler_stats = $stats['solr-mbeans']['UPDATE']['updateHandler']['stats'];
        $summary['@pending_docs'] = (int) $update_handler_stats['UPDATE.updateHandler.docsPending'];
        if (isset($update_handler_stats['UPDATE.updateHandler.softAutoCommitMaxTime'])) {
          $max_time = (int) $update_handler_stats['UPDATE.updateHandler.softAutoCommitMaxTime'];
        }
        $summary['@deletes_by_id'] = (int) $update_handler_stats['UPDATE.updateHandler.deletesById'];
        $summary['@deletes_by_query'] = (int) $update_handler_stats['UPDATE.updateHandler.deletesByQuery'];
        $summary['@core_name'] = $stats['solr-mbeans']['CORE']['core']['stats']['CORE.coreName'];
        $summary['@index_size'] = $stats['solr-mbeans']['CORE']['core']['stats']['INDEX.size'];
      }
      else {
        $update_handler_stats = $stats['solr-mbeans']['UPDATEHANDLER']['updateHandler']['stats'];
        $summary['@pending_docs'] = (int) $update_handler_stats['docsPending'];
        $max_time = (int) $update_handler_stats['autocommit maxTime'];
        $summary['@deletes_by_id'] = (int) $update_handler_stats['deletesById'];
        $summary['@deletes_by_query'] = (int) $update_handler_stats['deletesByQuery'];
        $summary['@core_name'] = $stats['solr-mbeans']['CORE']['core']['stats']['coreName'];
        if (version_compare($solr_version, '6.4', '>=')) {

          // @see https://issues.apache.org/jira/browse/SOLR-3990
          $summary['@index_size'] = $stats['solr-mbeans']['CORE']['core']['stats']['size'];
        }
        else {
          $summary['@index_size'] = $stats['solr-mbeans']['QUERYHANDLER']['/replication']['stats']['indexSize'];
        }
      }
      $summary['@autocommit_time_seconds'] = $max_time / 1000;
      $summary['@autocommit_time'] = \Drupal::service('date.formatter')
        ->formatInterval($max_time / 1000);
      $summary['@deletes_total'] = $summary['@deletes_by_id'] + $summary['@deletes_by_query'];
      $summary['@schema_version'] = $this
        ->getSchemaVersionString(TRUE);
    }
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function coreRestGet($path) {
    $this
      ->useTimeout();
    return $this
      ->restRequest($this->configuration['core'] . '/' . ltrim($path, '/'));
  }

  /**
   * {@inheritdoc}
   */
  public function coreRestPost($path, $command_json = '') {
    $this
      ->useTimeout(self::INDEX_TIMEOUT);
    return $this
      ->restRequest($this->configuration['core'] . '/' . ltrim($path, '/'), Request::METHOD_POST, $command_json);
  }

  /**
   * {@inheritdoc}
   */
  public function serverRestGet($path) {
    $this
      ->useTimeout();
    return $this
      ->restRequest($path);
  }

  /**
   * {@inheritdoc}
   */
  public function serverRestPost($path, $command_json = '') {
    $this
      ->useTimeout(self::INDEX_TIMEOUT);
    return $this
      ->restRequest($path, Request::METHOD_POST, $command_json);
  }

  /**
   * Sends a REST request to the Solr server endpoint and returns the result.
   *
   * @param string $handler
   *   The handler used for the API query.
   * @param string $method
   *   The HTTP request method.
   * @param string $command_json
   *   The command to send encoded as JSON.
   *
   * @return array
   *   The decoded response.
   *
   * @throws \Drupal\search_api_solr\SearchApiSolrException
   */
  protected function restRequest($handler, $method = Request::METHOD_GET, $command_json = '') {
    $this
      ->connect();
    $query = $this->solr
      ->createApi([
      'handler' => $handler,
      'accept' => 'application/json',
      'contenttype' => 'application/json',
      'method' => $method,
      'rawdata' => Request::METHOD_POST == $method ? $command_json : NULL,
    ]);
    $response = $this
      ->execute($query);
    $output = $response
      ->getData();

    // \Drupal::logger('search_api_solr')->info(print_r($output, true));.
    if (!empty($output['errors'])) {
      throw new SearchApiSolrException('Error trying to send a REST request.' . "\nError message(s):" . print_r($output['errors'], TRUE));
    }
    return $output;
  }

  /**
   * {@inheritdoc}
   */
  public function getUpdateQuery() {
    $this
      ->connect();
    return $this->solr
      ->createUpdate();
  }

  /**
   * {@inheritdoc}
   */
  public function getSelectQuery() {
    $this
      ->connect();
    return $this->solr
      ->createSelect();
  }

  /**
   * {@inheritdoc}
   */
  public function getMoreLikeThisQuery() {
    $this
      ->connect();
    return $this->solr
      ->createMoreLikeThis();
  }

  /**
   * {@inheritdoc}
   */
  public function getTermsQuery() {
    $this
      ->connect();
    return $this->solr
      ->createTerms();
  }

  /**
   * {@inheritdoc}
   */
  public function getSpellcheckQuery() {
    $this
      ->connect();
    return $this->solr
      ->createSpellcheck();
  }

  /**
   * {@inheritdoc}
   */
  public function getSuggesterQuery() {
    $this
      ->connect();
    return $this->solr
      ->createSuggester();
  }

  /**
   * {@inheritdoc}
   */
  public function getAutocompleteQuery() {
    $this
      ->connect();
    $this->solr
      ->registerQueryType('autocomplete', AutocompleteQuery::class);
    return $this->solr
      ->createQuery('autocomplete');
  }

  /**
   * {@inheritdoc}
   */
  public function getQueryHelper(QueryInterface $query = NULL) {
    if ($query) {
      return $query
        ->getHelper();
    }
    return \Drupal::service('solarium.query_helper');
  }

  /**
   * {@inheritdoc}
   */
  public function getExtractQuery() {
    $this
      ->connect();
    return $this->solr
      ->createExtract();
  }

  /**
   * Creates a CustomizeRequest object.
   *
   * @return \Solarium\Plugin\CustomizeRequest\CustomizeRequest|\Solarium\Core\Plugin\PluginInterface
   *   The Solarium CustomizeRequest object.
   */
  protected function customizeRequest() {
    $this
      ->connect();
    return $this->solr
      ->getPlugin('customizerequest');
  }

  /**
   * {@inheritdoc}
   */
  public function search(Query $query, ?Endpoint $endpoint = NULL) {
    $this
      ->connect();
    if (!$endpoint) {
      $endpoint = $this->solr
        ->getEndpoint();
    }
    $this
      ->useTimeout(self::QUERY_TIMEOUT, $endpoint);

    // Use the 'postbigrequest' plugin if no specific http method is
    // configured. The plugin needs to be loaded before the request is
    // created.
    if ($this->configuration['http_method'] === 'AUTO') {
      $this->solr
        ->getPlugin('postbigrequest');
    }

    // Use the manual method of creating a Solarium request so we can control
    // the HTTP method.
    $request = $this->solr
      ->createRequest($query);

    // Set the configured HTTP method.
    if ($this->configuration['http_method'] === 'POST') {
      $request
        ->setMethod(Request::METHOD_POST);
    }
    elseif ($this->configuration['http_method'] === 'GET') {
      $request
        ->setMethod(Request::METHOD_GET);
    }
    return $this
      ->executeRequest($request, $endpoint);
  }

  /**
   * {@inheritdoc}
   */
  public function createSearchResult(QueryInterface $query, Response $response) {
    return $this->solr
      ->createResult($query, $response);
  }

  /**
   * {@inheritdoc}
   */
  public function update(UpdateQuery $query, ?Endpoint $endpoint = NULL) {
    $this
      ->connect();
    if (!$endpoint) {
      $endpoint = $this->solr
        ->getEndpoint();
    }

    // The default timeout is set for search queries. The configured timeout
    // might differ and needs to be set now because solarium doesn't
    // distinguish between these types.
    $this
      ->useTimeout(self::INDEX_TIMEOUT, $endpoint);
    if ($this->configuration['commit_within']) {

      // Do a commitWithin since that is automatically a softCommit since Solr 4
      // and a delayed hard commit with Solr 3.4+.
      // By default we wait 1 second after the request arrived for solr to parse
      // the commit. This allows us to return to Drupal and let Solr handle what
      // it needs to handle.
      // @see http://wiki.apache.org/solr/NearRealtimeSearch

      /** @var \Solarium\Plugin\CustomizeRequest\CustomizeRequest $request */
      $request = $this
        ->customizeRequest();
      $request
        ->createCustomization('id')
        ->setType('param')
        ->setName('commitWithin')
        ->setValue($this->configuration['commit_within']);
    }
    return $this
      ->execute($query, $endpoint);
  }

  /**
   * {@inheritdoc}
   */
  public function execute(QueryInterface $query, ?Endpoint $endpoint = NULL) {
    $this
      ->connect();
    if (!$endpoint) {
      $endpoint = $this->solr
        ->getEndpoint();
    }
    try {
      return $this->solr
        ->execute($query, $endpoint);
    } catch (HttpException $e) {
      $this
        ->handleHttpException($e, $endpoint);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function executeRequest(Request $request, ?Endpoint $endpoint = NULL) {
    $this
      ->connect();
    if (!$endpoint) {
      $endpoint = $this->solr
        ->getEndpoint();
    }
    try {
      return $this->solr
        ->executeRequest($request, $endpoint);
    } catch (HttpException $e) {
      $this
        ->handleHttpException($e, $endpoint);
    }
  }

  /**
   * Converts a HttpException in an easier to read SearchApiSolrException.
   *
   * @param \Solarium\Exception\HttpException $e
   *   The HttpException object.
   * @param \Solarium\Core\Client\Endpoint $endpoint
   *   The Solarium endpoint.
   *
   * @throws \Drupal\search_api_solr\SearchApiSolrException
   */
  protected function handleHttpException(HttpException $e, Endpoint $endpoint) {
    $response_code = (int) $e
      ->getCode();
    switch ((string) $response_code) {
      case '404':
        $description = 'not found';
        break;
      case '401':
      case '403':
        $description = 'access denied';
        break;
      case '500':
      case '0':
        $description = 'internal Solr server error';
        break;
      default:
        $description = 'unreachable or returned unexpected response code';
    }
    throw new SearchApiSolrException(sprintf('Solr endpoint %s %s (%d). %s', $endpoint
      ->getServerUri(), $description, $response_code, $e
      ->getBody()), $response_code, $e);
  }

  /**
   * {@inheritdoc}
   */
  public function optimize(?Endpoint $endpoint = NULL) {
    $this
      ->connect();
    if (!$endpoint) {
      $endpoint = $this->solr
        ->getEndpoint();
    }

    // The default timeout is set for search queries. The configured timeout
    // might differ and needs to be set now because solarium doesn't
    // distinguish between these types.
    $this
      ->useTimeout(self::OPTIMIZE_TIMEOUT, $endpoint);
    $update_query = $this->solr
      ->createUpdate();
    $update_query
      ->addOptimize(TRUE, FALSE);
    $this
      ->execute($update_query, $endpoint);
  }

  /**
   * {@inheritdoc}
   */
  public function adjustTimeout(int $timeout, ?Endpoint &$endpoint = NULL) {
    $this
      ->connect();
    if (!$endpoint) {
      $endpoint = $this->solr
        ->getEndpoint();
    }
    $previous_timeout = $endpoint
      ->getOption(self::QUERY_TIMEOUT);
    $options = $endpoint
      ->getOptions();
    $options[self::QUERY_TIMEOUT] = $timeout;
    $endpoint = new Endpoint($options);
    return $previous_timeout;
  }

  /**
   * {@inheritdoc}
   */
  public function useTimeout(string $timeout = self::QUERY_TIMEOUT, ?Endpoint $endpoint = NULL) {
    $this
      ->connect();
    if (!$endpoint) {
      $endpoint = $this->solr
        ->getEndpoint();
    }
    $adpater = $this->solr
      ->getAdapter();
    if ($adpater instanceof TimeoutAwareInterface && ($seconds = $endpoint
      ->getOption($timeout))) {
      $adpater
        ->setTimeout($seconds);
    }
    else {
      $this
        ->getLogger()
        ->warning('The function SolrConnectorPluginBase::useTimeout() has no affect because you use a HTTP adapter that is not implementing TimeoutAwareInterface. You need to adjust your SolrConnector accordingly.');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getTimeout(?Endpoint $endpoint = NULL) {
    $this
      ->connect();
    if (!$endpoint) {
      $endpoint = $this->solr
        ->getEndpoint();
    }
    return $endpoint
      ->getOption(self::QUERY_TIMEOUT);
  }

  /**
   * {@inheritdoc}
   */
  public function getIndexTimeout(?Endpoint $endpoint = NULL) {
    $this
      ->connect();
    if (!$endpoint) {
      $endpoint = $this->solr
        ->getEndpoint();
    }
    return $endpoint
      ->getOption(self::INDEX_TIMEOUT);
  }

  /**
   * {@inheritdoc}
   */
  public function getOptimizeTimeout(?Endpoint $endpoint = NULL) {
    $this
      ->connect();
    if (!$endpoint) {
      $endpoint = $this->solr
        ->getEndpoint();
    }
    return $endpoint
      ->getOption(self::OPTIMIZE_TIMEOUT);
  }

  /**
   * {@inheritdoc}
   */
  public function getFinalizeTimeout(?Endpoint $endpoint = NULL) {
    $this
      ->connect();
    if (!$endpoint) {
      $endpoint = $this->solr
        ->getEndpoint();
    }
    return $endpoint
      ->getOption(self::FINALIZE_TIMEOUT);
  }

  /**
   * {@inheritdoc}
   */
  public function extract(QueryInterface $query, ?Endpoint $endpoint = NULL) {
    $this
      ->useTimeout(self::INDEX_TIMEOUT, $endpoint);
    return $this
      ->execute($query, $endpoint);
  }

  /**
   * {@inheritdoc}
   */
  public function getContentFromExtractResult(ExtractResult $result, $filepath) {
    $array_data = $result
      ->getData();
    if (isset($array_data[basename($filepath)])) {
      return $array_data[basename($filepath)];
    }

    // In most (or every) cases when an error happens we won't reach that point,
    // because a Solr exception is already pased through. Anyway, this exception
    // will be thrown if the solarium library surprises us again. ;-)
    throw new SearchApiSolrException('Unable to find extracted files within the Solr response body.');
  }

  /**
   * {@inheritdoc}
   */
  public function getEndpoint($key = 'search_api_solr') {
    $this
      ->connect();
    return $this->solr
      ->getEndpoint($key);
  }

  /**
   * {@inheritdoc}
   */
  public function createEndpoint(string $key, array $additional_configuration = []) {
    $this
      ->connect();
    $configuration = [
      'key' => $key,
      self::QUERY_TIMEOUT => $this->configuration['timeout'],
    ] + $additional_configuration + $this->configuration;
    if (Client::checkMinimal('5.2.0')) {
      unset($configuration['timeout']);
    }
    return $this->solr
      ->createEndpoint($configuration, TRUE);
  }

  /**
   * {@inheritdoc}
   */
  public function getFile($file = NULL) {
    $this
      ->connect();
    $query = $this->solr
      ->createApi([
      'handler' => $this->configuration['core'] . '/admin/file',
    ]);
    if ($file) {
      $query
        ->addParam('file', $file);
    }
    return $this
      ->execute($query)
      ->getResponse();
  }

  /**
   * {@inheritdoc}
   */
  public function viewSettings() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function __sleep() {

    // It's safe to unset the solr client completely before serialization
    // because connect() will set it up again correctly after deserialization.
    unset($this->solr);
    return parent::__sleep();
  }

  /**
   * {@inheritdoc}
   */
  public function alterConfigFiles(array &$files, string $lucene_match_version, string $server_id = '') {
    if (!empty($this->configuration['jmx'])) {
      $files['solrconfig_extra.xml'] .= "<jmx />\n";
    }
    if (!empty($this->configuration['solr_install_dir'])) {
      $files['solrcore.properties'] = preg_replace("/solr\\.install\\.dir.*\$/m", 'solr.install.dir=' . $this->configuration['solr_install_dir'], $files['solrcore.properties']);
    }
    else {
      $files['solrcore.properties'] = preg_replace("/solr\\.install\\.dir.*\$/m", '', $files['solrcore.properties']);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigurablePluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 6
ConfigurablePluginBase::calculatePluginDependencies Deprecated protected function Calculates and adds dependencies of a specific plugin instance.
ConfigurablePluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ConfigurablePluginBase::getDescription public function Returns the plugin's description. Overrides ConfigurablePluginInterface::getDescription
ConfigurablePluginBase::getPluginDependencies Deprecated protected function Calculates and returns dependencies of a specific plugin instance.
ConfigurablePluginBase::label public function Returns the label for use on the administration pages. Overrides ConfigurablePluginInterface::label
ConfigurablePluginBase::moduleHandler Deprecated protected function Wraps the module handler.
ConfigurablePluginBase::onDependencyRemoval public function Informs the plugin that some of its dependencies are being removed. Overrides ConfigurablePluginInterface::onDependencyRemoval 5
ConfigurablePluginBase::themeHandler Deprecated protected function Wraps the theme handler.
ConfigurablePluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 2
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__wakeup public function 2
DependencyTrait::$dependencies protected property The object's dependencies.
DependencyTrait::addDependencies protected function Adds multiple dependencies.
DependencyTrait::addDependency protected function Adds a dependency.
HideablePluginBase::isHidden public function Determines whether this plugin should be hidden in the UI. Overrides HideablePluginInterface::isHidden 1
LoggerTrait::$logger protected property The logging channel to use.
LoggerTrait::getLogger public function Retrieves the logger.
LoggerTrait::logException protected function Logs an exception.
LoggerTrait::setLogger public function Sets the logger.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginDependencyTrait::calculatePluginDependencies protected function Calculates and adds dependencies of a specific plugin instance. Aliased as: traitCalculatePluginDependencies 1
PluginDependencyTrait::getPluginDependencies protected function Calculates and returns dependencies of a specific plugin instance. Aliased as: traitGetPluginDependencies
PluginDependencyTrait::moduleHandler protected function Wraps the module handler. Aliased as: traitModuleHandler 1
PluginDependencyTrait::themeHandler protected function Wraps the theme handler. Aliased as: traitThemeHandler 1
PluginFormTrait::submitConfigurationForm public function Form submission handler. Aliased as: traitSubmitConfigurationForm 7
SolrConnectorInterface::FINALIZE_TIMEOUT constant
SolrConnectorInterface::INDEX_TIMEOUT constant
SolrConnectorInterface::OPTIMIZE_TIMEOUT constant
SolrConnectorInterface::QUERY_TIMEOUT constant
SolrConnectorInterface::reloadCore public function Reloads the Solr core. 1
SolrConnectorPluginBase::$eventDispatcher protected property The event dispatcher.
SolrConnectorPluginBase::$solr protected property A connection to the Solr server.
SolrConnectorPluginBase::adjustTimeout public function Sets a new timeout for queries, but not for indexing or optimization. Overrides SolrConnectorInterface::adjustTimeout
SolrConnectorPluginBase::alterConfigFiles public function Alter the newly assembled Solr configuration files. Overrides SolrConnectorInterface::alterConfigFiles 1
SolrConnectorPluginBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm 1
SolrConnectorPluginBase::connect protected function Prepares the connection to the Solr server.
SolrConnectorPluginBase::coreRestGet public function Sends a REST GET request to the Solr core and returns the result. Overrides SolrConnectorInterface::coreRestGet
SolrConnectorPluginBase::coreRestPost public function Sends a REST POST request to the Solr core and returns the result. Overrides SolrConnectorInterface::coreRestPost
SolrConnectorPluginBase::create public static function Creates an instance of the plugin. Overrides ConfigurablePluginBase::create
SolrConnectorPluginBase::createClient protected function Create a Client.
SolrConnectorPluginBase::createEndpoint public function Creates an endpoint. Overrides SolrConnectorInterface::createEndpoint
SolrConnectorPluginBase::createSearchResult public function Creates a result from a response. Overrides SolrConnectorInterface::createSearchResult
SolrConnectorPluginBase::customizeRequest protected function Creates a CustomizeRequest object.
SolrConnectorPluginBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurablePluginBase::defaultConfiguration 1
SolrConnectorPluginBase::execute public function Executes any query. Overrides SolrConnectorInterface::execute 2
SolrConnectorPluginBase::executeRequest public function Executes a request and returns the response. Overrides SolrConnectorInterface::executeRequest 2
SolrConnectorPluginBase::extract public function Executes an extract query. Overrides SolrConnectorInterface::extract
SolrConnectorPluginBase::getAutocompleteQuery public function Creates a new Solarium autocomplete query. Overrides SolrConnectorInterface::getAutocompleteQuery 1
SolrConnectorPluginBase::getContentFromExtractResult public function Gets the content from an extract query result. Overrides SolrConnectorInterface::getContentFromExtractResult
SolrConnectorPluginBase::getCoreInfo public function Gets information about the Solr Core. Overrides SolrConnectorInterface::getCoreInfo
SolrConnectorPluginBase::getCoreLink public function Returns a link to the Solr core, if the necessary options are set. Overrides SolrConnectorInterface::getCoreLink
SolrConnectorPluginBase::getDataFromHandler protected function Gets data from a Solr endpoint using a given handler.
SolrConnectorPluginBase::getEndpoint public function Returns an endpoint. Overrides SolrConnectorInterface::getEndpoint
SolrConnectorPluginBase::getExtractQuery public function Creates a new Solarium extract query. Overrides SolrConnectorInterface::getExtractQuery
SolrConnectorPluginBase::getFile public function Retrieves a config file or file list from the Solr server. Overrides SolrConnectorInterface::getFile
SolrConnectorPluginBase::getFinalizeTimeout public function Get the finalize timeout. Overrides SolrConnectorInterface::getFinalizeTimeout
SolrConnectorPluginBase::getIndexTimeout public function Get the index timeout. Overrides SolrConnectorInterface::getIndexTimeout
SolrConnectorPluginBase::getLuceneMatchVersion public function Gets the LuceneMatchVersion string. Overrides SolrConnectorInterface::getLuceneMatchVersion
SolrConnectorPluginBase::getLuke public function Gets meta-data about the index. Overrides SolrConnectorInterface::getLuke
SolrConnectorPluginBase::getMoreLikeThisQuery public function Creates a new Solarium more like this query. Overrides SolrConnectorInterface::getMoreLikeThisQuery 1
SolrConnectorPluginBase::getOptimizeTimeout public function Get the optimize timeout. Overrides SolrConnectorInterface::getOptimizeTimeout
SolrConnectorPluginBase::getQueryHelper public function Returns a Solarium query helper object. Overrides SolrConnectorInterface::getQueryHelper
SolrConnectorPluginBase::getSchemaVersion public function Gets the schema version number. Overrides SolrConnectorInterface::getSchemaVersion
SolrConnectorPluginBase::getSchemaVersionString public function Gets the full schema version string the core is using. Overrides SolrConnectorInterface::getSchemaVersionString
SolrConnectorPluginBase::getSelectQuery public function Creates a new Solarium update query. Overrides SolrConnectorInterface::getSelectQuery 1
SolrConnectorPluginBase::getServerInfo public function Gets information about the Solr server. Overrides SolrConnectorInterface::getServerInfo
SolrConnectorPluginBase::getServerLink public function Returns a link to the Solr server. Overrides SolrConnectorInterface::getServerLink
SolrConnectorPluginBase::getServerUri protected function Returns a the Solr server URI.
SolrConnectorPluginBase::getSolrBranch public function Gets the current Solr branch name. Overrides SolrConnectorInterface::getSolrBranch
SolrConnectorPluginBase::getSolrMajorVersion public function Gets the current Solr major version. Overrides SolrConnectorInterface::getSolrMajorVersion
SolrConnectorPluginBase::getSolrVersion public function Gets the current Solr version. Overrides SolrConnectorInterface::getSolrVersion
SolrConnectorPluginBase::getSpellcheckQuery public function Creates a new Solarium suggester query. Overrides SolrConnectorInterface::getSpellcheckQuery 1
SolrConnectorPluginBase::getStatsSummary public function Gets summary information about the Solr Core. Overrides SolrConnectorInterface::getStatsSummary 1
SolrConnectorPluginBase::getSuggesterQuery public function Creates a new Solarium suggester query. Overrides SolrConnectorInterface::getSuggesterQuery 1
SolrConnectorPluginBase::getTermsQuery public function Creates a new Solarium terms query. Overrides SolrConnectorInterface::getTermsQuery 1
SolrConnectorPluginBase::getTimeout public function Get the query timeout. Overrides SolrConnectorInterface::getTimeout
SolrConnectorPluginBase::getUpdateQuery public function Creates a new Solarium update query. Overrides SolrConnectorInterface::getUpdateQuery
SolrConnectorPluginBase::handleHttpException protected function Converts a HttpException in an easier to read SearchApiSolrException.
SolrConnectorPluginBase::isCloud public function Returns TRUE for Cloud. Overrides SolrConnectorInterface::isCloud 1
SolrConnectorPluginBase::optimize public function Optimizes the Solr index. Overrides SolrConnectorInterface::optimize
SolrConnectorPluginBase::pingCore public function Pings the Solr core to tell whether it can be accessed. Overrides SolrConnectorInterface::pingCore 1
SolrConnectorPluginBase::pingServer public function Pings the Solr server to tell whether it can be accessed. Overrides SolrConnectorInterface::pingServer
SolrConnectorPluginBase::restRequest protected function Sends a REST request to the Solr server endpoint and returns the result.
SolrConnectorPluginBase::search public function Executes a search query and returns the raw response. Overrides SolrConnectorInterface::search
SolrConnectorPluginBase::serverRestGet public function Sends a REST GET request to the Solr server and returns the result. Overrides SolrConnectorInterface::serverRestGet
SolrConnectorPluginBase::serverRestPost public function Sends a REST POST request to the Solr server and returns the result. Overrides SolrConnectorInterface::serverRestPost
SolrConnectorPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurablePluginBase::setConfiguration
SolrConnectorPluginBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
SolrConnectorPluginBase::update public function Executes an update query and applies some tweaks. Overrides SolrConnectorInterface::update
SolrConnectorPluginBase::useTimeout public function
SolrConnectorPluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormTrait::validateConfigurationForm
SolrConnectorPluginBase::viewSettings public function Returns additional, connector-specific information about this server. Overrides SolrConnectorInterface::viewSettings
SolrConnectorPluginBase::__sleep public function Overrides DependencySerializationTrait::__sleep
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.