Skip to content

Commit

Permalink
Issue #3406269 by mkalkbrenner: Fix phpcs, phpstan, stylelint findings
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Kalkbrenner authored and mkalkbrenner committed Dec 5, 2023
1 parent ad88dba commit 8cd5a9c
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 67 deletions.
2 changes: 1 addition & 1 deletion modules/search_api_solr_admin/css/solr_field_analysis.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#analysis-result > div {
display: table;
margin-bottom: 10px;
border-spacing: 10px;
border-width: 2px;
border-style: solid;
border-color: black;
margin-bottom: 10px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\search_api\ServerInterface;
use Drupal\search_api_solr\SolrFieldTypeInterface;
use Solarium\Core\Query\Result\ResultInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand Down Expand Up @@ -85,6 +86,7 @@ public function buildForm(array $form, FormStateInterface $form_state, ServerInt
// Get solr field lists.
$list_builder = $this->entityTypeManager->getListBuilder('solr_field_type');
$list_builder->setServer($search_api_server);
/** @var SolrFieldTypeInterface[] $solr_field_types */
$solr_field_types = $list_builder->load();

$solr_fields = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace Drupal\search_api_solr_autocomplete\Plugin\search_api_autocomplete\suggester;

use Drupal\search_api\IndexInterface;
use Drupal\search_api\LoggerTrait;
use Drupal\search_api_solr\SolrBackendInterface;

/**
* Provides a helper method for loading the search backend.
*/
trait BackendTrait {

use LoggerTrait;

/**
* Retrieves the backend for the given index, if it supports autocomplete.
*
Expand All @@ -33,7 +36,7 @@ protected static function getBackend(IndexInterface $index): ?SolrBackendInterfa
}
}
catch (\Exception $e) {
watchdog_exception('search_api', $e);
$this->logException($e);
}
return NULL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected function getSpellcheckSuggestions(SolrBackendInterface $backend, Query
$this->filterDuplicateAutocompleteSuggestions($suggestions);
}
catch (SearchApiException $e) {
watchdog_exception('search_api_solr', $e);
$this->logException($e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Drupal\search_api_solr_autocomplete\Event\PreSuggesterQueryEvent;
use Solarium\Component\ComponentAwareQueryInterface;
use Solarium\Core\Query\Result\ResultInterface;
use Solarium\QueryType\Select\Result\Result;

/**
* Provides a suggester plugin that retrieves suggestions from the server.
Expand Down Expand Up @@ -184,7 +185,7 @@ protected function getSuggesterSuggestions(SolrBackendInterface $backend, QueryI
$this->filterDuplicateAutocompleteSuggestions($suggestions);
}
catch (SearchApiException $e) {
watchdog_exception('search_api_solr', $e);
$this->logException($e);
}
}

Expand Down Expand Up @@ -258,21 +259,20 @@ protected function setAutocompleteSuggesterQuery(QueryInterface $query, Autocomp
/**
* Get the term suggestions from the autocomplete query result.
*
* @param \Solarium\Core\Query\Result\ResultInterface $result
* @param \Solarium\QueryType\Select\Result\Result $result
* An autocomplete query result.
* @param \Drupal\search_api_autocomplete\Suggestion\SuggestionFactory $suggestion_factory
* The suggestion factory.
*
* @return \Drupal\search_api_autocomplete\Suggestion\SuggestionInterface[]
* An array of suggestions.
*/
protected function getAutocompleteSuggesterSuggestions(ResultInterface $result, SuggestionFactory $suggestion_factory) {
protected function getAutocompleteSuggesterSuggestions(Result $result, SuggestionFactory $suggestion_factory) {
$suggestions = [];
if ($phrases_result = $result->getComponent(ComponentAwareQueryInterface::COMPONENT_SUGGESTER)) {
/** @var \Solarium\Component\Result\Suggester\Result $phrases_result */
$dictionaries = array_keys($phrases_result->getResults());
foreach ($phrases_result->getAll() as $dictionary_index => $phrases) {
/** @var \Solarium\QueryType\Suggester\Result\Term $phrases */
foreach ($phrases->getSuggestions() as $phrase) {
$suggestion = $suggestion_factory->createFromSuggestedKeys($phrase['term']);
if (method_exists($suggestion, 'setDictionary')) {
Expand Down
46 changes: 28 additions & 18 deletions modules/search_api_solr_devel/src/Controller/DevelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
Expand All @@ -15,6 +16,7 @@
use Drupal\search_api\Event\IndexingItemsEvent;
use Drupal\search_api\Event\SearchApiEvents;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Plugin\search_api\tracker\Basic;
use Drupal\search_api\ServerInterface;
use Drupal\search_api\Utility\FieldsHelperInterface;
use Drupal\search_api\Utility\Utility;
Expand Down Expand Up @@ -146,6 +148,8 @@ protected function getBackends() {
*
* @return array
* Array of page elements to render.
*
* @throws \Drupal\search_api\SearchApiException
*/
public function entitySolr(RouteMatchInterface $route_match) {
$output_details = [];
Expand All @@ -155,7 +159,7 @@ public function entitySolr(RouteMatchInterface $route_match) {
$parameter_name = $route_match->getRouteObject()->getOption('_devel_entity_type_id');
$entity = $route_match->getParameter($parameter_name);

if ($entity && $entity instanceof EntityInterface) {
if ($entity instanceof ContentEntityInterface) {
foreach ($this->getBackends() as $backend_id) {
/** @var \Drupal\search_api\ServerInterface[] $servers */
$servers = $this->storage->loadByProperties([
Expand All @@ -166,14 +170,13 @@ public function entitySolr(RouteMatchInterface $route_match) {
/** @var \Drupal\search_api\ServerInterface $server */
/** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
$backend = $server->getBackend();
/** @var \Drupal\search_api\IndexInterface[] $indexes */
$indexes = $server->getIndexes();
$solr = $backend->getSolrConnector();
foreach ($indexes as $index) {
if ($index->status()) {
foreach ($index->getDatasourceIds() as $datasource_id) {
[, $entity_type] = Utility::splitPropertyPath($datasource_id);
if ($entity->getEntityTypeId() == $entity_type) {
if ($entity->getEntityTypeId() === $entity_type) {

foreach (array_keys($entity->getTranslationLanguages()) as $langcode) {
// @todo improve that ID generation?
Expand Down Expand Up @@ -229,6 +232,7 @@ public function entitySolr(RouteMatchInterface $route_match) {
$query->setFields('*');
try {
// @todo Run a timer on this process and report it?
/** @var \Solarium\QueryType\Select\Result\Result $results */
$results = $solr->execute($query, $backend->getCollectionEndpoint($index));
$num_found = $results->getNumFound();
$summary_row['solr_exists'] = $this->t('yes');
Expand All @@ -243,7 +247,7 @@ public function entitySolr(RouteMatchInterface $route_match) {
}

// If no item found in Solr, report it.
if ($num_found == 0) {
if ($num_found === 0) {
$summary_row['solr_exists'] = $this->t('no');
$output_details[$details_id][] = [
'#markup' => $this->t(
Expand All @@ -252,7 +256,7 @@ public function entitySolr(RouteMatchInterface $route_match) {
),
];
}
if ($num_found == 1) {
if ($num_found === 1) {
// Show Solr documents for this item.
$solr_documents = $results->getDocuments();
$fields = $solr_documents[0]->getFields();
Expand Down Expand Up @@ -393,22 +397,28 @@ protected function getBaseRow(ServerInterface $server, IndexInterface $index, $d

// Fetch tracker information.
$tracker = $index->getTrackerInstance();
$select = $tracker->getDatabaseConnection()
->select('search_api_item', 'sai');
$select->condition('index_id', $index->id());
$select->condition('datasource', $datasource_id);
$select->condition('item_id', $item_id);
$select->fields('sai', ['item_id', 'status', 'changed']);
$tracker_data = $select->execute()->fetch();
// Add tracker information to row.
if ($tracker_data) {
$base_row['tracked'] = $this->t('yes');
$base_row['changed'] = $this->showTimeAndTimeAgo($tracker_data->changed);
$base_row['status'] = $tracker_data->status ? $this->t('no') : $this->t('yes');
if ($tracker instanceof Basic) {
$select = $tracker->getDatabaseConnection()
->select('search_api_item', 'sai');
$select->condition('index_id', $index->id());
$select->condition('datasource', $datasource_id);
$select->condition('item_id', $item_id);
$select->fields('sai', ['item_id', 'status', 'changed']);
$tracker_data = $select->execute()->fetch();
// Add tracker information to row.
if ($tracker_data) {
$base_row['tracked'] = $this->t('yes');
$base_row['changed'] = $this->showTimeAndTimeAgo($tracker_data->changed);
$base_row['status'] = $tracker_data->status ? $this->t('no') : $this->t('yes');
}
else {
$base_row['tracked'] = $this->t('no');
}
}
else {
$base_row['tracked'] = $this->t('no');
$base_row['tracked'] = $this->t('unsupported tracker');
}

return $base_row;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ protected function getDevelSolrRoute(EntityTypeInterface $entity_type) {

return $route;
}

return NULL;
}

/**
Expand Down
14 changes: 7 additions & 7 deletions search_api_solr.install
Original file line number Diff line number Diff line change
Expand Up @@ -1506,13 +1506,13 @@ function search_api_solr_update_8320() {
$save = TRUE;
}
}
}
if ('str' === $key) {
// An previously erroneous implementation of
// search_api_solr_update_8315() added that entry which needs to
// be removed now.
unset($component['lst']['str']);
$save = TRUE;
if ('str' === $key) {
// A previously erroneous implementation of
// search_api_solr_update_8315() added that entry which needs to
// be removed now.
unset($component['lst']['str']);
$save = TRUE;
}
}
}
}
Expand Down
41 changes: 22 additions & 19 deletions search_api_solr.module
Original file line number Diff line number Diff line change
Expand Up @@ -74,37 +74,37 @@ function search_api_solr_cron() {
}

foreach (search_api_solr_get_servers(TRUE) as $server) {
/** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
$backend = $server->getBackend();
try {
/** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
$backend = $server->getBackend();
$connector = $backend->getSolrConnector();
$is_drupal_only_writeable = [];
$is_drupal_only_writeable[$server->id()] = FALSE;
$last_update_on_server[$server->id()] = 0;

if ($indexes = $server->getIndexes(['status' => TRUE])) {
$document_counts[$server->id()] = $backend->getDocumentCounts();
$is_drupal_only_writeable[$server->id()] = TRUE;
$last_update_on_server[$server->id()] = 0;
$optimized = FALSE;

foreach ($indexes as $index) {
if ($backend->finalizeIndex($index)) {
/** @var \Drupal\search_api\Backend\BackendPluginBase $backend */
$backend->getLogger()->info('Cron finalized Solr server @server.', ['@server' => $server->label()]);
}
$is_drupal_only_writeable[$server->id()] &= !Utility::hasIndexJustSolrDocumentDatasource($index);
$last_update = \Drupal::state()->get('search_api_solr.' . $index->id() . '.last_update', 0);
if ($last_update > $last_update_on_server[$server->id()]) {
$last_update_on_server[$server->id()] = $last_update;
}
if (!$optimized && $end_of_day && $backend->isOptimizeEnabled()) {
$endpoint = $backend->getCollectionEndpoint($index);
$connector->optimize($endpoint);
$optimized = TRUE;
/** @var \Drupal\search_api\Backend\BackendPluginBase $backend */
$backend->getLogger()
->info('Optimized Solr server @server.', ['@server' => $server->label()]);
}
}

if ($end_of_day && $backend->isOptimizeEnabled()) {
$endpoint = $backend->getCollectionEndpoint($index);
$connector->optimize($endpoint);
$backend->getLogger()
->info('Optimized Solr server @server.', ['@server' => $server->label()]);
}
}
else {
$is_drupal_only_writeable[$server->id()] = FALSE;
}

if ($is_drupal_only_writeable[$server->id()] && \Drupal::state()->get('search_api_solr.' . $server->id() . '.last_build', 0) < $last_update_on_server[$server->id()]) {
Expand All @@ -127,7 +127,8 @@ function search_api_solr_cron() {
}
}
catch (SearchApiException $e) {
watchdog_exception('search_api', $e, '%type while maintaining Solr server @server: @message in %function (line %line of %file).', ['@server' => $server->label()]);
/** @var \Drupal\search_api\Backend\BackendPluginBase $backend */
\Drupal\Core\Utility\Error::logException($backend->getLogger(), $e, '%type while maintaining Solr server @server: @message in %function (line %line of %file).', ['@server' => $server->label()]);
}
}

Expand Down Expand Up @@ -231,8 +232,10 @@ function search_api_solr_form_search_api_index_form_alter(&$form, FormStateInter
}

$settings = [];
/** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
$form_object = $form_state->getFormObject();
/** @var \Drupal\search_api\IndexInterface $index */
$index = $form_state->getFormObject()->getEntity();
$index = $form_object->getEntity();
if (!$index->isNew()) {
$settings = $index->getThirdPartySettings('search_api_solr');
}
Expand Down Expand Up @@ -326,6 +329,7 @@ function search_api_solr_form_search_api_index_form_alter(&$form, FormStateInter
'#default_value' => $settings['multilingual']['use_language_undefined_as_fallback_language'],
];

$langcode_options = [];
foreach (\Drupal::languageManager()->getLanguages() as $language) {
$langcode_options[$language->getId()] = $language->getName();
}
Expand Down Expand Up @@ -595,13 +599,12 @@ function search_api_solr_form_search_api_index_form_alter(&$form, FormStateInter
'#default_value' => $settings['advanced']['collection'],
];

$zones = system_time_zones(TRUE, TRUE);
$form['third_party_settings']['search_api_solr']['advanced']['timezone'] = [
'#type' => 'select',
'#title' => t('Time zone'),
'#description' => t("For correct date calculations the time zone to use is sent to the Solr server. By default the individual time zone of the current user will be used. If not available the site's default time zone will be used as fallback. But by setting a time zone here you can force a time zone for every query targeting this index."),
'#default_value' => $settings['advanced']['timezone'],
'#options' => $zones,
'#options' => \Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList(TRUE),
];
}
}
Expand Down Expand Up @@ -649,7 +652,7 @@ function search_api_solr_form_search_api_index_form_validate_server(array &$elem
function search_api_solr_form_search_api_index_fields_alter(&$form, FormStateInterface $form_state, $form_id) {
/** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
$form_object = $form_state->getFormObject();
/** @var \Drupal\search_api\IndexInterface $this_index */
/** @var \Drupal\search_api\IndexInterface $index */
$index = $form_object->getEntity();

if (Utility::hasIndexSolrDatasources($index)) {
Expand Down
17 changes: 1 addition & 16 deletions src/Plugin/search_api/backend/SearchApiSolrBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -4505,22 +4505,7 @@ protected function setGrouping(Query $solarium_query, QueryInterface $query, arr
}

/**
* Adds spellcheck features to the search query.
*
* @todo Scope is public to be accessible from
* \Drupal\search_api_solr_autocomplete\Plugin\search_api_autocomplete\Spellcheck::setAutocompleteSpellCheckQuery().
* Maybe some refactoring is required.
*
* @param \Solarium\Component\ComponentAwareQueryInterface $solarium_query
* The Solarium query.
* @param \Drupal\search_api\Query\QueryInterface $query
* The Search API query.
* @param array $spellcheck_options
* The spellcheck options to add.
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \Drupal\search_api\SearchApiException
* @throws \Drupal\search_api_solr\SearchApiSolrException
* {@inheritdoc}
*/
public function setSpellcheck(ComponentAwareQueryInterface $solarium_query, QueryInterface $query, array $spellcheck_options = []) {
/** @var \Solarium\Component\Spellcheck $spellcheck */
Expand Down
Loading

0 comments on commit 8cd5a9c

Please sign in to comment.