SolrAdminCommandHelper.php in Search API Solr 4.x
File
modules/search_api_solr_admin/src/Utility/SolrAdminCommandHelper.php
View source
<?php
namespace Drupal\search_api_solr_admin\Utility;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\search_api_solr\SearchApiSolrException;
use Drupal\search_api_solr\Utility\SolrCommandHelper;
use Drupal\search_api_solr\Utility\Utility;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class SolrAdminCommandHelper extends SolrCommandHelper {
protected $fileSystem;
protected $messenger;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, EventDispatcherInterface $event_dispatcher, FileSystemInterface $fileSystem, MessengerInterface $messenger) {
parent::__construct($entity_type_manager, $module_handler, $event_dispatcher);
$this->fileSystem = $fileSystem;
$this->messenger = $messenger;
}
public function reload(string $server_id) : void {
$server = $this
->getServer($server_id);
$connector = Utility::getSolrConnector($server);
$result = $connector
->reloadCore();
if (!$result) {
throw new SearchApiSolrException(sprintf('Reloading %s for %s (%s) failed.', $connector
->isCloud() ? 'collection' : 'core', $server
->label(), $server_id));
}
$this
->reindex($server);
}
public function deleteCollection(string $server_id) : void {
$server = $this
->getServer($server_id);
$connector = Utility::getSolrCloudConnector($server);
$result = $connector
->deleteCollection();
if (!$result) {
throw new SearchApiSolrException(sprintf('Reloading %s for %s (%s) failed.', $connector
->isCloud() ? 'collection' : 'core', $server
->label(), $server_id));
}
$this
->reindex($server);
}
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) {
$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);
}
}
}