You are here

public function SearchApiSolrBackend::viewSettings in Search API Solr 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::viewSettings()
  2. 8.2 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::viewSettings()
  3. 4.x src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::viewSettings()

Throws

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\search_api\SearchApiException

Overrides BackendPluginBase::viewSettings

File

src/Plugin/search_api/backend/SearchApiSolrBackend.php, line 656

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

public function viewSettings() {

  /** @var \Drupal\search_api_solr\Plugin\SolrConnector\StandardSolrCloudConnector $connector */
  $connector = $this
    ->getSolrConnector();
  $cloud = $connector instanceof SolrCloudConnectorInterface;
  $info[] = [
    'label' => $this
      ->t('Solr connector plugin'),
    'info' => $connector
      ->label(),
  ];
  $info[] = [
    'label' => $this
      ->t('Solr server URI'),
    'info' => $connector
      ->getServerLink(),
  ];
  if ($cloud) {
    $info[] = [
      'label' => $this
        ->t('Solr collection URI'),
      'info' => $connector
        ->getCollectionLink(),
    ];
  }
  else {
    $info[] = [
      'label' => $this
        ->t('Solr core URI'),
      'info' => $connector
        ->getCoreLink(),
    ];
  }

  // Add connector-specific information.
  $info = array_merge($info, $connector
    ->viewSettings());
  if ($this->server
    ->status()) {

    // If the server is enabled, check whether Solr can be reached.
    try {
      $ping_server = $connector
        ->pingServer();
    } catch (\Exception $e) {
      $ping_server = FALSE;
    }
    if ($ping_server) {
      $msg = $this
        ->t('The Solr server could be reached.');
    }
    else {
      $msg = $this
        ->t('The Solr server could not be reached or is protected by your service provider.');
    }
    $info[] = [
      'label' => $this
        ->t('Server Connection'),
      'info' => $msg,
      'status' => $ping_server ? 'ok' : 'error',
    ];
    try {
      $ping = $connector
        ->pingCore();
    } catch (\Exception $e) {
      $ping = FALSE;
    }
    if ($ping) {
      $msg = $this
        ->t('The Solr @core could be accessed (latency: @millisecs ms).', [
        '@core' => $cloud ? 'collection' : 'core',
        '@millisecs' => $ping * 1000,
      ]);
    }
    else {
      $msg = $this
        ->t('The Solr @core could not be accessed. Further data is therefore unavailable.', [
        '@core' => $cloud ? 'collection' : 'core',
      ]);
    }
    $info[] = [
      'label' => $cloud ? $this
        ->t('Collection Connection') : $this
        ->t('Core Connection'),
      'info' => $msg,
      'status' => $ping ? 'ok' : 'error',
    ];
    $version = $connector
      ->getSolrVersion();
    $info[] = [
      'label' => $this
        ->t('Configured Solr Version'),
      'info' => $version,
      'status' => version_compare($version, '0.0.0', '>') ? 'ok' : 'error',
    ];
    if ($ping_server || $ping) {
      $info[] = [
        'label' => $this
          ->t('Detected Solr Version'),
        'info' => $connector
          ->getSolrVersion(TRUE),
        'status' => 'ok',
      ];
      try {
        $endpoints[0] = $connector
          ->getEndpoint();
        $endpoints_queried = [];
        foreach ($this
          ->getServer()
          ->getIndexes() as $index) {
          $endpoints[$index
            ->id()] = $this
            ->getCollectionEndpoint($index);
        }

        /** @var \Solarium\Core\Client\Endpoint $endpoint */
        foreach ($endpoints as $index_id => $endpoint) {
          try {
            $key = $endpoint
              ->getBaseUri();
          } catch (UnexpectedValueException $e) {
            if ($cloud && 0 === $index_id) {
              $info[] = [
                'label' => $this
                  ->t('Default Collection'),
                'info' => $this
                  ->t("Default collection isn't set. Ensure that the collections are properly set on the indexes in their advanced section od the Solr specific index options."),
                'status' => 'error',
              ];
            }
            else {
              $info[] = [
                'label' => $this
                  ->t('Additional information'),
                'info' => $this
                  ->t('Collection or core configuration for index %index is wrong or missing: %msg', [
                  '%index' => $index_id,
                  '%msg' => $e
                    ->getMessage(),
                ]),
                'status' => 'error',
              ];
            }
            continue;
          }
          if (!in_array($key, $endpoints_queried)) {
            $endpoints_queried[] = $key;
            if ($cloud) {
              $connector
                ->setCollectionNameFromEndpoint($endpoint);
            }
            $data = $connector
              ->getLuke();
            if (isset($data['index']['numDocs'])) {

              // Collect the stats.
              $stats_summary = $connector
                ->getStatsSummary();
              $pending_msg = $stats_summary['@pending_docs'] ? $this
                ->t('(@pending_docs sent but not yet processed)', $stats_summary) : '';
              $index_msg = $stats_summary['@index_size'] ? $this
                ->t('(@index_size on disk)', $stats_summary) : '';
              $indexed_message = $this
                ->t('@num items @pending @index_msg', [
                '@num' => $data['index']['numDocs'],
                '@pending' => $pending_msg,
                '@index_msg' => $index_msg,
              ]);
              $info[] = [
                'label' => $this
                  ->t('%key: Indexed', [
                  '%key' => $key,
                ]),
                'info' => $indexed_message,
              ];
              if (!empty($stats_summary['@deletes_total'])) {
                $info[] = [
                  'label' => $this
                    ->t('%key: Pending Deletions', [
                    '%key' => $key,
                  ]),
                  'info' => $stats_summary['@deletes_total'],
                ];
              }
              $info[] = [
                'label' => $this
                  ->t('%key: Delay', [
                  '%key' => $key,
                ]),
                'info' => $this
                  ->t('@autocommit_time before updates are processed.', $stats_summary),
              ];
              $status = 'ok';
              if (!$this
                ->isNonDrupalOrOutdatedConfigSetAllowed()) {
                $variables[':url'] = Url::fromUri('internal:/' . drupal_get_path('module', 'search_api_solr') . '/INSTALL.md')
                  ->toString();
                if (strpos($stats_summary['@schema_version'], 'search-api') === 0 || strpos($stats_summary['@schema_version'], 'drupal') === 0) {
                  if (strpos($stats_summary['@schema_version'], 'drupal-' . SolrBackendInterface::SEARCH_API_SOLR_MIN_SCHEMA_VERSION) !== 0) {
                    \Drupal::messenger()
                      ->addError($this
                      ->t('You are using outdated Solr configuration set. Please follow the instructions described in the <a href=":url">INSTALL.md</a> file for setting up Solr.', $variables));
                    $status = 'error';
                  }
                }
                else {
                  \Drupal::messenger()
                    ->addError($this
                    ->t('You are using an incompatible Solr schema. Please follow the instructions described in the <a href=":url">INSTALL.md</a> file for setting up Solr.', $variables));
                  $status = 'error';
                }
              }
              $info[] = [
                'label' => $this
                  ->t('%key: Schema', [
                  '%key' => $key,
                ]),
                'info' => $stats_summary['@schema_version'],
                'status' => $status,
              ];
              if (!empty($stats_summary['@collection_name'])) {
                $info[] = [
                  'label' => $this
                    ->t('%key: Solr Collection Name', [
                    '%key' => $key,
                  ]),
                  'info' => $stats_summary['@collection_name'],
                ];
              }
              elseif (!empty($stats_summary['@core_name'])) {
                $info[] = [
                  'label' => $this
                    ->t('%key: Solr Core Name', [
                    '%key' => $key,
                  ]),
                  'info' => $stats_summary['@core_name'],
                ];
              }
            }
          }
        }
        try {
          foreach ($this
            ->getMaxDocumentVersions() as $site_hash => $indexes) {
            if ('#total' === $site_hash) {
              $info[] = [
                'label' => $this
                  ->t('Max document _version_ for this server'),
                'info' => $indexes,
              ];
            }
            else {
              foreach ($indexes as $index => $datasources) {
                foreach ($datasources as $datasource => $max_version) {
                  $info[] = [
                    'label' => $this
                      ->t('Max _version_ for datasource %datasource in index %index on site %site', [
                      '%datasource' => $datasource,
                      '%index' => $index,
                      '%site' => $site_hash,
                    ]),
                    'info' => $max_version,
                  ];
                }
              }
            }
          }
        } catch (UnexpectedValueException $e) {
          $info[] = [
            'label' => $this
              ->t('Max document _version_ for this server'),
            'info' => $this
              ->t('Collection or core configuration for at least one index on this server is wrong or missing: %msg', [
              '%index' => $index_id,
              '%msg' => $e
                ->getMessage(),
            ]),
            'status' => 'error',
          ];
        }
      } catch (SearchApiException $e) {
        $info[] = [
          'label' => $this
            ->t('Additional information'),
          'info' => $this
            ->t('An error occurred while trying to retrieve additional information from the Solr server: %msg', [
            '%msg' => $e
              ->getMessage(),
          ]),
          'status' => 'error',
        ];
      }
    }
  }
  $info[] = [
    'label' => $this
      ->t('Targeted content domain'),
    'info' => $this
      ->getDomain(),
  ];
  if (!empty($this->configuration['disabled_field_types'])) {
    \Drupal::messenger()
      ->addWarning($this
      ->t('You disabled some Solr Field Types for this server.'));
    $info[] = [
      'label' => $this
        ->t('Disabled Solr Field Types'),
      'info' => implode(', ', $this->configuration['disabled_field_types']),
    ];
  }
  $info[] = [
    'label' => $this
      ->t('Targeted environment'),
    'info' => $this
      ->getEnvironment(),
  ];
  if (!empty($this->configuration['disabled_caches'])) {
    \Drupal::messenger()
      ->addWarning($this
      ->t('You disabled some Solr Caches for this server.'));
    $info[] = [
      'label' => $this
        ->t('Disabled Solr Caches'),
      'info' => implode(', ', $this->configuration['disabled_caches']),
    ];
  }
  return $info;
}