You are here

public function SolrAdminCommandHelper::uploadConfigset in Search API Solr 4.x

Generates and uploads the configset for a Solr search server.

Parameters

string $server_id: The ID of the server.

int $num_shards:

bool $messages:

Throws

\Drupal\search_api\SearchApiException

\Drupal\search_api_solr\SearchApiSolrException

\ZipStream\Exception\FileNotFoundException

\ZipStream\Exception\FileNotReadableException

\ZipStream\Exception\OverflowException

File

modules/search_api_solr_admin/src/Utility/SolrAdminCommandHelper.php, line 107

Class

SolrAdminCommandHelper
Provides functionality to be used by CLI tools.

Namespace

Drupal\search_api_solr_admin\Utility

Code

public function uploadConfigset(string $server_id, int $num_shards = 3, bool $messages = FALSE) : void {
  $server = $this
    ->getServer($server_id);
  $connector = Utility::getSolrCloudConnector($server);
  if ($messages) {

    // Called via admin form. 'dt' is not available.
    $this->translationFunction = 't';
  }
  $filename = $this->fileSystem
    ->tempnam($this->fileSystem
    ->getTempDirectory(), 'configset_') . '.zip';
  $this
    ->getServerConfigCommand($server
    ->id(), $filename);
  $configset = $connector
    ->getConfigSetName();
  $collection_exists = (bool) $configset;
  if (!$collection_exists) {
    $configset = Utility::generateConfigsetName($server);
  }
  $connector
    ->uploadConfigset($configset, $filename);
  if ($messages) {
    $this->messenger
      ->addStatus($this
      ->t('Successfully uploaded configset %configset.', [
      '%configset' => $configset,
    ]));
  }
  if ($collection_exists) {
    $this
      ->reload($server_id);
    if ($messages) {
      $this->messenger
        ->addStatus($this
        ->t('Successfully reloaded collection %collection.', [
        '%collection' => $connector
          ->getCollectionName(),
      ]));
    }
  }
  else {
    $connector
      ->createCollection([
      'collection.configName' => $configset,
      'numShards' => $num_shards,
    ]);
    if ($messages) {
      $this->messenger
        ->addStatus($this
        ->t('Successfully created collection %collection.', [
        '%collection' => $connector
          ->getCollectionName(),
      ]));
    }
    $this
      ->reindex($server);
  }
}