You are here

class SolrCommandHelper in Search API Solr 8.3

Same name and namespace in other branches
  1. 4.x src/Utility/SolrCommandHelper.php \Drupal\search_api_solr\Utility\SolrCommandHelper

Provides functionality to be used by CLI tools.

Hierarchy

  • class \Drupal\search_api\Utility\CommandHelper implements \Psr\Log\LoggerAwareInterface uses \Psr\Log\LoggerAwareTrait

Expanded class hierarchy of SolrCommandHelper

2 files declare their use of SolrCommandHelper
SearchApiSolrCommands.php in src/Commands/SearchApiSolrCommands.php
SearchApiSolrDevelCommands.php in modules/search_api_solr_devel/src/Commands/SearchApiSolrDevelCommands.php

File

src/Utility/SolrCommandHelper.php, line 13

Namespace

Drupal\search_api_solr\Utility
View source
class SolrCommandHelper extends CommandHelper {

  /**
   * Re-install all Solr Field Types from their yml files.
   */
  public function reinstallFieldtypesCommand() {
    search_api_solr_install_missing_field_types();
  }

  /**
   * Gets the config for a Solr search server.
   *
   * @param string $server_id
   *   The ID of the server.
   * @param string $file_name
   *   The file name of the config zip that should be created.
   * @param string $solr_version
   *   The targeted Solr version.
   *
   * @throws \Drupal\search_api\SearchApiException
   * @throws \ZipStream\Exception\FileNotFoundException
   * @throws \ZipStream\Exception\FileNotReadableException
   * @throws \ZipStream\Exception\OverflowException
   */
  public function getServerConfigCommand($server_id, $file_name = NULL, $solr_version = NULL) {
    $servers = $this
      ->loadServers([
      $server_id,
    ]);
    $server = reset($servers);
    if (!$server) {
      throw new SearchApiSolrException('Unknown server');
    }
    if ($solr_version) {
      $config = $server
        ->getBackendConfig();

      // Temporarily switch the Solr version but don't save!
      $config['connector_config']['solr_version'] = $solr_version;
      $server
        ->setBackendConfig($config);
    }
    $solr_configset_controller = new SolrConfigSetController();
    $solr_configset_controller
      ->setServer($server);
    $archive_options = new Archive();
    $stream = FALSE;
    if ($file_name !== NULL) {

      // If no filename is provided, output stream is standard output.
      $stream = fopen($file_name, 'w+b');
      $archive_options
        ->setOutputStream($stream);
    }
    $zip = $solr_configset_controller
      ->getConfigZip($archive_options);
    $zip
      ->finish();
    if ($stream) {
      fclose($stream);
    }
  }

  /**
   * Finalizes one ore more indexes.
   *
   * @param string[]|null $indexIds
   *   (optional) An array of index IDs, or NULL if we should finalize all
   *   enabled indexes.
   * @param bool $force
   *   (optional) Force the finalization, even if the index isn't "dirty".
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   * @throws \Drupal\search_api\SearchApiException
   * @throws \Drupal\search_api_solr\SearchApiSolrException
   */
  public function finalizeIndexCommand(array $indexIds = NULL, $force = FALSE) {
    $servers = search_api_solr_get_servers();
    if ($force) {

      // It's important to mark all indexes as "dirty" before the first
      // finalization runs because there might be dependencies between the
      // indexes. Therefor we do the loop two times.
      foreach ($servers as $server) {
        foreach ($server
          ->getIndexes() as $index) {
          if ($index
            ->status() && !$index
            ->isReadOnly() && (!$indexIds || in_array($index
            ->id(), $indexIds))) {
            \Drupal::state()
              ->set('search_api_solr.' . $index
              ->id() . '.last_update', \Drupal::time()
              ->getRequestTime());
          }
        }
      }
    }
    foreach ($servers as $server) {

      /** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
      $backend = $server
        ->getBackend();
      foreach ($server
        ->getIndexes() as $index) {
        if ($index
          ->status() && !$index
          ->isReadOnly() && (!$indexIds || in_array($index
          ->id(), $indexIds))) {
          $backend
            ->finalizeIndex($index);
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommandHelper::$entityTypeManager protected property The entity type manager.
CommandHelper::$eventDispatcher protected property The event dispatcher.
CommandHelper::$indexStorage protected property The storage for search index entities.
CommandHelper::$moduleHandler protected property The module handler.
CommandHelper::$serverStorage protected property The storage for search server entities.
CommandHelper::$translationFunction protected property A callable for translating strings.
CommandHelper::clearIndexCommand public function Deletes all items from one or more indexes.
CommandHelper::clearServerCommand public function Clears all indexes on a server.
CommandHelper::disableIndexCommand public function Disables one or more enabled search indexes.
CommandHelper::disableServerCommand public function Disables a server.
CommandHelper::enableIndexCommand public function Enables one or more disabled search indexes.
CommandHelper::enableServerCommand public function Enables a server.
CommandHelper::getIndexCount public function Returns the total number of search indexes.
CommandHelper::indexItemsToIndexCommand public function Indexes items on one or more indexes.
CommandHelper::indexListCommand public function Lists all search indexes.
CommandHelper::indexStatusCommand public function Lists all search indexes with their status.
CommandHelper::loadIndexes public function Returns the indexes with the given IDs.
CommandHelper::loadServers public function Returns the servers with the given IDs.
CommandHelper::rebuildTrackerCommand public function Rebuilds the tracker for an index.
CommandHelper::reloadEntityOverrideFree public function Loads an override-free copy of a config entity, for saving.
CommandHelper::resetTrackerCommand public function Resets the tracker for an index, optionally filtering on entity types.
CommandHelper::searchIndexCommand public function Returns an array of results.
CommandHelper::serverListCommand public function Returns a list of servers created on the page.
CommandHelper::setIndexServerCommand public function Switches an index to another server.
CommandHelper::setIndexState public function Changes the state of a single index.
CommandHelper::t public function Translates a string using the set translation method.
CommandHelper::__construct public function Constructs a CommandHelper object.
SolrCommandHelper::finalizeIndexCommand public function Finalizes one ore more indexes.
SolrCommandHelper::getServerConfigCommand public function Gets the config for a Solr search server.
SolrCommandHelper::reinstallFieldtypesCommand public function Re-install all Solr Field Types from their yml files.