diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 53040d9a9c531ddc7f570b0578cffd5508fa9224..76b76e644e4749d654b9495bc8b67a3bd0255b47 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,5 +1,6 @@ Search API 1.x, dev (xxxx-xx-xx): --------------------------------- +- #2933811 by drunken monkey: Fixed coding standards in new Drush code. - #2932347 by drunken monkey, ghaya: Fixed case insensitive matching for highlighting non-ASCII text. - #2938288 by drunken monkey: Fixed problems with PHP 7.2. diff --git a/src/Commands/SearchApiCommands.php b/src/Commands/SearchApiCommands.php index ae502a973898afc740bcaf3e98a846ee6b3150b1..f7fdcba88e55a162fdbe4095e0c452f93869dc74 100644 --- a/src/Commands/SearchApiCommands.php +++ b/src/Commands/SearchApiCommands.php @@ -66,6 +66,9 @@ class SearchApiCommands extends DrushCommands { * * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields * The table rows. + * + * @throws \Drupal\search_api\SearchApiException + * Thrown if an index has a server which couldn't be loaded. */ public function listCommand() { $rows = $this->commandHelper->indexListCommand(); @@ -85,6 +88,9 @@ class SearchApiCommands extends DrushCommands { * Enable the search index with the ID node_index. * * @aliases sapi-en,search-api-enable + * + * @throws \Drupal\search_api\ConsoleException + * Thrown if no indexes could be loaded. */ public function enable($indexId) { $this->commandHelper->enableIndexCommand([$indexId]); @@ -101,6 +107,9 @@ class SearchApiCommands extends DrushCommands { * Alias to enable all disabled indexes. * * @aliases sapi-ena,search-api-enable-all + * + * @throws \Drupal\search_api\ConsoleException + * Thrown if no indexes could be loaded. */ public function enableAll() { $this->commandHelper->enableIndexCommand(); @@ -139,6 +148,9 @@ class SearchApiCommands extends DrushCommands { * Alias to disable all enabled indexes. * * @aliases sapi-disa,search-api-disable-all + * + * @throws \Drupal\search_api\ConsoleException + * Thrown if no indexes could be loaded. */ public function disableAll() { $this->commandHelper->disableIndexCommand(); @@ -170,6 +182,9 @@ class SearchApiCommands extends DrushCommands { * * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields * The table rows. + * + * @throws \Drupal\search_api\SearchApiException + * Thrown if one of the affected indexes had an invalid tracker set. */ public function status($indexId = NULL) { $rows = $this->commandHelper->indexStatusCommand([$indexId]); @@ -242,6 +257,10 @@ class SearchApiCommands extends DrushCommands { * Schedule the search index with the ID node_index for reindexing. * * @aliases search-api-mark-all,search-api-reindex,sapi-r,search-api-reset-tracker + * + * @throws \Drupal\search_api\SearchApiException + * Thrown if one of the affected indexes had an invalid tracker set, or some + * other internal error occurred. */ public function resetTracker($indexId = NULL, array $options = ['entity-types' => []]) { $this->commandHelper->resetTrackerCommand([$indexId], $options['entity-types']); @@ -264,6 +283,10 @@ class SearchApiCommands extends DrushCommands { * Clear the search index with the ID node_index. * * @aliases sapi-c,search-api-clear + * + * @throws \Drupal\search_api\SearchApiException + * Thrown if one of the affected indexes had an invalid tracker set, or some + * other internal error occurred. */ public function clear($indexId = NULL) { $this->commandHelper->clearIndexCommand([$indexId]); @@ -292,6 +315,12 @@ class SearchApiCommands extends DrushCommands { * * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields * The table rows. + * + * @throws \Drupal\search_api\ConsoleException + * Thrown if searching failed for any reason. + * @throws \Drupal\search_api\SearchApiException + * Thrown if no search query could be created for the given index, for + * example because it is disabled or its server could not be loaded. */ public function search($indexId, $keyword) { $rows = $this->commandHelper->searchIndexCommand($indexId, $keyword); @@ -318,6 +347,9 @@ class SearchApiCommands extends DrushCommands { * * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields * The table rows. + * + * @throws \Drupal\search_api\ConsoleException + * Thrown if no servers could be loaded. */ public function serverList() { $rows = $this->commandHelper->serverListCommand(); @@ -339,6 +371,11 @@ class SearchApiCommands extends DrushCommands { * Alias to enable the my_solr_server search server. * * @aliases sapi-se,search-api-server-enable + * + * @throws \Drupal\search_api\ConsoleException + * Thrown if the server couldn't be loaded. + * @throws \Drupal\Core\Entity\EntityStorageException + * Thrown if an internal error occurred when saving the server. */ public function serverEnable($serverId) { $this->commandHelper->enableServerCommand($serverId); @@ -358,6 +395,11 @@ class SearchApiCommands extends DrushCommands { * Alias to disable the my_solr_server search server. * * @aliases sapi-sd,search-api-server-disable + * + * @throws \Drupal\search_api\ConsoleException + * Thrown if the server couldn't be loaded. + * @throws \Drupal\Core\Entity\EntityStorageException + * Thrown if an internal error occurred when saving the server. */ public function serverDisable($serverId) { $this->commandHelper->disableServerCommand($serverId); @@ -377,6 +419,12 @@ class SearchApiCommands extends DrushCommands { * Alias to clear all search indexes on the search server my_solr_server. * * @aliases sapi-sc,search-api-server-clear + * + * @throws \Drupal\search_api\ConsoleException + * Thrown if the server couldn't be loaded. + * @throws \Drupal\search_api\SearchApiException + * Thrown if one of the affected indexes had an invalid tracker set, or some + * other internal error occurred. */ public function serverClear($serverId) { $this->commandHelper->clearServerCommand($serverId); diff --git a/src/Utility/CommandHelper.php b/src/Utility/CommandHelper.php index 7c3ad351a624eae91b418cd925687199c0a0a6a4..ad6fc207ed0966e76227a95acb0413822a031dcf 100644 --- a/src/Utility/CommandHelper.php +++ b/src/Utility/CommandHelper.php @@ -2,7 +2,9 @@ namespace Drupal\search_api\Utility; +use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; use Drupal\Core\Config\Entity\ConfigEntityInterface; +use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\search_api\ConsoleException; @@ -52,24 +54,28 @@ class CommandHelper implements LoggerAwareInterface { * * @var callable */ - protected $translationMethod; + protected $translationFunction; /** - * Constructs a new CommandHelper object. + * Constructs a CommandHelper object. * - * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. - * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. - * @param string $translationMethod - * A callable for translating strings. + * @param string|callable $translation_function + * (optional) A callable for translating strings. + * + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * Thrown if the "search_api_index" or "search_api_server" entity types are + * unknown. */ - public function __construct(EntityTypeManagerInterface $entityTypeManager, ModuleHandlerInterface $moduleHandler, $translationMethod = 'dt') { - $this->entityTypeManager = $entityTypeManager; - $this->indexStorage = $entityTypeManager->getStorage('search_api_index'); - $this->serverStorage = $entityTypeManager->getStorage('search_api_server'); - $this->moduleHandler = $moduleHandler; - $this->translationMethod = $translationMethod; + public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, $translation_function = 'dt') { + $this->entityTypeManager = $entity_type_manager; + $this->indexStorage = $entity_type_manager->getStorage('search_api_index'); + $this->serverStorage = $entity_type_manager->getStorage('search_api_server'); + $this->moduleHandler = $module_handler; + $this->translationFunction = $translation_function; } /** @@ -88,6 +94,9 @@ class CommandHelper implements LoggerAwareInterface { * tracked in the index. * - status: Either "enabled" or "disabled". * - limit: The number of items that are processed in a single cron run. + * + * @throws \Drupal\search_api\SearchApiException + * Thrown if an index has a server which couldn't be loaded. */ public function indexListCommand() { $indexes = $this->loadIndexes(); @@ -137,6 +146,9 @@ class CommandHelper implements LoggerAwareInterface { * - complete: a percentage of indexation. * - indexed: The amount of indexed items. * - total: The total amount of items. + * + * @throws \Drupal\search_api\SearchApiException + * Thrown if one of the affected indexes had an invalid tracker set. */ public function indexStatusCommand(array $indexId = NULL) { $indexes = $this->loadIndexes($indexId); @@ -202,7 +214,7 @@ class CommandHelper implements LoggerAwareInterface { * all indexes will be disabled. * * @throws \Drupal\search_api\ConsoleException - * If no indexes are defined. + * Thrown if no indexes could be loaded. */ public function disableIndexCommand(array $index_ids = NULL) { if (!$this->getIndexCount()) { @@ -240,6 +252,8 @@ class CommandHelper implements LoggerAwareInterface { * * @throws \Drupal\search_api\ConsoleException * Thrown if an indexing batch process could not be created. + * @throws \Drupal\search_api\SearchApiException + * Thrown if one of the affected indexes had an invalid tracker set. */ public function indexItemsToIndexCommand(array $indexIds = NULL, $limit = NULL, $batchSize = NULL) { $indexes = $this->loadIndexes($indexIds); @@ -316,6 +330,10 @@ class CommandHelper implements LoggerAwareInterface { * * @return bool * TRUE if any index was affected, FALSE otherwise. + * + * @throws \Drupal\search_api\SearchApiException + * Thrown if one of the affected indexes had an invalid tracker set, or some + * other internal error occurred. */ public function resetTrackerCommand(array $indexIds = NULL, array $entityTypes = []) { $indexes = $this->loadIndexes($indexIds); @@ -361,6 +379,10 @@ class CommandHelper implements LoggerAwareInterface { * * @return bool * TRUE when the clearing was successful, FALSE when no indexes were found. + * + * @throws \Drupal\search_api\SearchApiException + * Thrown if one of the affected indexes had an invalid tracker set, or some + * other internal error occurred. */ public function clearIndexCommand(array $indexIds = NULL) { $indexes = $this->loadIndexes($indexIds); @@ -393,6 +415,9 @@ class CommandHelper implements LoggerAwareInterface { * * @throws \Drupal\search_api\ConsoleException * Thrown if searching failed for any reason. + * @throws \Drupal\search_api\SearchApiException + * Thrown if no search query could be created for the given index, for + * example because it is disabled or its server could not be loaded. */ public function searchIndexCommand($indexId, $keyword = NULL) { $indexes = $this->loadIndexes([$indexId]); @@ -442,10 +467,9 @@ class CommandHelper implements LoggerAwareInterface { * - status: The enabled status of the server. * * @throws \Drupal\search_api\ConsoleException - * Thrown if there aren't any servers yet. + * Thrown if no servers could be loaded. */ public function serverListCommand() { - /** @var \Drupal\search_api\ServerInterface[] $servers */ $servers = $this->loadServers(); if (count($servers) === 0) { throw new ConsoleException($this->t('There are no servers present.')); @@ -471,14 +495,16 @@ class CommandHelper implements LoggerAwareInterface { * * @throws \Drupal\search_api\ConsoleException * Thrown if the server couldn't be loaded. + * @throws \Drupal\Core\Entity\EntityStorageException + * Thrown if an internal error occurred when saving the server. */ public function enableServerCommand($serverId) { - $server = $this->loadServers([$serverId]); - if (empty($server)) { + $servers = $this->loadServers([$serverId]); + if (empty($servers)) { throw new ConsoleException($this->t('The server could not be loaded.')); } /** @var \Drupal\search_api\ServerInterface $server */ - $server = $this->reloadEntityOverrideFree(reset($server)); + $server = $this->reloadEntityOverrideFree(reset($servers)); $server->setStatus(TRUE)->save(); } @@ -490,14 +516,16 @@ class CommandHelper implements LoggerAwareInterface { * * @throws \Drupal\search_api\ConsoleException * Thrown if the server couldn't be loaded. + * @throws \Drupal\Core\Entity\EntityStorageException + * Thrown if an internal error occurred when saving the server. */ public function disableServerCommand($serverId) { - $server = $this->loadServers([$serverId]); - if (empty($server)) { + $servers = $this->loadServers([$serverId]); + if (empty($servers)) { throw new ConsoleException($this->t('The server could not be loaded.')); } /** @var \Drupal\search_api\ServerInterface $server */ - $server = $this->reloadEntityOverrideFree(reset($server)); + $server = $this->reloadEntityOverrideFree(reset($servers)); $server->setStatus(FALSE)->save(); } @@ -509,14 +537,17 @@ class CommandHelper implements LoggerAwareInterface { * * @throws \Drupal\search_api\ConsoleException * Thrown if the server couldn't be loaded. + * @throws \Drupal\search_api\SearchApiException + * Thrown if one of the affected indexes had an invalid tracker set, or some + * other internal error occurred. */ public function clearServerCommand($serverId) { - $server = $this->loadServers([$serverId]); - if (empty($server)) { + $servers = $this->loadServers([$serverId]); + if (empty($servers)) { throw new ConsoleException($this->t('The server could not be loaded.')); } /** @var \Drupal\search_api\ServerInterface $server */ - $server = $this->reloadEntityOverrideFree(reset($server)); + $server = $this->reloadEntityOverrideFree(reset($servers)); foreach ($server->getIndexes() as $index) { $index->clear(); @@ -557,7 +588,7 @@ class CommandHelper implements LoggerAwareInterface { $index->save(); $this->logger->info($this->t('Index @index has been set to use server @server and items have been queued for indexing.', ['@index' => $indexId, '@server' => $serverId])); } - catch (SearchApiException $e) { + catch (EntityStorageException $e) { $this->logger->warning($e->getMessage()); $this->logger->warning($this->t('There was an error setting index @index to use server @server, or this index is already configured to use this server.', ['@index' => $indexId, '@server' => $serverId])); } @@ -568,13 +599,14 @@ class CommandHelper implements LoggerAwareInterface { * * @param array|null $indexIds * (optional) The IDs of the search indexes to return, or NULL to load all - * indexes. + * indexes. An array with a single NULL value is interpreted the same way as + * passing NULL. * * @return \Drupal\search_api\IndexInterface[] * An array of search indexes. */ public function loadIndexes(array $indexIds = NULL) { - if (count($indexIds) === 1 && $indexIds === [NULL]) { + if ($indexIds === [NULL]) { $indexIds = NULL; } return $this->indexStorage->loadMultiple($indexIds); @@ -641,9 +673,14 @@ class CommandHelper implements LoggerAwareInterface { * loaded. */ public function reloadEntityOverrideFree(ConfigEntityInterface $entity) { - /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */ - $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId()); - return $storage->loadOverrideFree($entity->id()); + try { + /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */ + $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId()); + return $storage->loadOverrideFree($entity->id()); + } + catch (InvalidPluginDefinitionException $e) { + return NULL; + } } /** @@ -658,7 +695,7 @@ class CommandHelper implements LoggerAwareInterface { * The translated message. */ public function t($message, array $arguments = []) { - return call_user_func_array($this->translationMethod, [ + return call_user_func_array($this->translationFunction, [ $message, $arguments, ]);