Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#177: Port search_api_solr_devel solr index clean-up command #178

Open
wants to merge 1 commit into
base: 8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions drush.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ services:
arguments: [ "@logger.factory", "@search_api_pantheon.pantheon_guzzle", "@search_api_pantheon.endpoint", "@search_api_pantheon.solarium_client" ]
tags:
- { name: drush.command }
search_api_pantheon.index_management:
class: Drupal\search_api_pantheon\Commands\IndexManagement
arguments:
- '@entity_type.manager'
- '@module_handler'
- '@event_dispatcher'
- '@search_api_solr.configset_controller'
tags:
- { name: drush.command }
83 changes: 83 additions & 0 deletions src/Commands/IndexManagement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Drupal\search_api_pantheon\Commands;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\search_api\SearchApiException;
use Drupal\search_api_solr\Controller\SolrConfigSetController;
use Drupal\search_api_solr\SearchApiSolrException;
use Drupal\search_api_solr\SolrBackendInterface;
use Drupal\search_api_solr\Utility\SolrCommandHelper;
use Drush\Commands\DrushCommands;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Defines Drush commands for managing Pantheon's Solr index.
*
* @see \Drupal\search_api_solr_devel\Commands\SearchApiSolrDevelCommands
*/
final class IndexManagement extends DrushCommands {

private SolrCommandHelper $commandHelper;

Check notice on line 25 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L25

Line indented incorrectly; expected 4 spaces, found 2

Check notice on line 25 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L25

Missing member variable doc comment

public function __construct(EntityTypeManagerInterface $entityTypeManager, ModuleHandlerInterface $moduleHandler, EventDispatcherInterface $eventDispatcher, SolrConfigSetController $solrConfigSetController) {

Check notice on line 27 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L27

Avoid excessively long variable names like $solrConfigSetController. Keep variable name length under 20.

Check notice on line 27 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L27

Expected 2 blank lines before function; 1 found

Check notice on line 27 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L27

Missing doc comment for function __construct()

Check notice on line 27 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L27

Opening brace should be on a new line
parent::__construct();
$this->commandHelper = new SolrCommandHelper($entityTypeManager, $moduleHandler, $eventDispatcher, $solrConfigSetController);
}

Check notice on line 30 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L30

Expected //end __construct()

Check notice on line 30 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L30

Line indented incorrectly; expected 4 spaces, found 2

/**

Check notice on line 32 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L32

Doc comment for parameter "$logger" missing

Check notice on line 32 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L32

Line indented incorrectly; expected at least 4 spaces, found 2
* {@inheritdoc}
*/
public function setLogger(LoggerInterface $logger): void {
parent::setLogger($logger);
$this->commandHelper->setLogger($logger);
}

/**
* Deletes *all* documents on a Solr search server (including all indexes).
*
* @param string $server_id

Check notice on line 43 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L43

Missing parameter comment
* The ID of the server.
*
* @command search-api-pantheon:sapi-delete-all
*
* @usage search-api-pantheon:sapi-delete-all server_id
* Deletes *all* documents on server_id.
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \Drupal\search_api_solr\SearchApiSolrException
* @throws \Drupal\search_api\SearchApiException
*/
public function deleteAll(string $server_id): void {

Check notice on line 55 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L55

Line indented incorrectly; expected 4 spaces, found 2
$servers = $this->commandHelper->loadServers([$server_id]);
if ($server = \reset($servers)) {

Check notice on line 57 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L57

Line indented incorrectly; expected 8 spaces, found 4
$backend = $server->getBackend();
if ($backend instanceof SolrBackendInterface) {

Check notice on line 59 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L59

Line indented incorrectly; expected 12 spaces, found 6
$connector = $backend->getSolrConnector();

Check notice on line 60 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L60

Line indented incorrectly; expected at least 16 spaces, found 8
$update_query = $connector->getUpdateQuery();

Check notice on line 61 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L61

Line indented incorrectly; expected at least 16 spaces, found 8
$update_query->addDeleteQuery('*:*');

Check notice on line 62 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L62

Line indented incorrectly; expected at least 16 spaces, found 8
$connector->update($update_query);

Check notice on line 63 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L63

Line indented incorrectly; expected at least 16 spaces, found 8

foreach ($server->getIndexes() as $index) {
if ($index->status() && !$index->isReadOnly()) {

Check notice on line 66 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L66

Implicit true comparisons prohibited; use === TRUE instead

Check notice on line 66 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L66

Line indented incorrectly; expected 20 spaces, found 10
if ($connector->isCloud()) {
$connector->update($update_query, $backend->getCollectionEndpoint($index));

Check notice on line 68 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L68

Line indented incorrectly; expected at least 28 spaces, found 14
}
$index->reindex();

Check notice on line 70 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L70

Line indented incorrectly; expected at least 24 spaces, found 12
}
}
}

Check notice on line 73 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L73

Expected 1 space after closing brace; newline found
else {

Check notice on line 74 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L74

The method deleteAll uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
throw new SearchApiSolrException("The given server ID doesn't use the Solr backend.");
}
}
else {
throw new SearchApiException("The given server ID doesn't exist.");

Check notice on line 79 in src/Commands/IndexManagement.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Commands/IndexManagement.php#L79

Line indented incorrectly; expected at least 12 spaces, found 6
}
}

}