Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
ISAICP-6530: Provide status report.
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiu-cristea committed Jun 4, 2021
1 parent ad4d2ce commit 84698dc
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions web/modules/custom/joinup_search/joinup_search.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* @file
* Install, update and uninstall functions for Joinup Search module.
*/

declare(strict_types = 1);

use Drupal\Component\Serialization\Json;
use Drupal\search_api_solr\Utility\Utility;
use Laminas\Diactoros\Uri;

/**
* Implements hook_requirements().
*/
function joinup_search_requirements(string $phase): array {
$requirements = [];
if ($phase !== 'runtime') {
return $requirements;
}

$http_client = \Drupal::httpClient();
$site_hash = Utility::getSiteHash();
foreach (['published', 'unpublished'] as $index_id) {
$endpoint = \Drupal::config("search_api.server.solr_{$index_id}")
->get('backend_config.connector_config');
$uri = (string) (new Uri())
->withScheme($endpoint['scheme'])
->withHost($endpoint['host'])
->withPort($endpoint['port'])
->withPath(rtrim($endpoint['path'], '/') . "/{$endpoint['core']}/select")
->withQuery(http_build_query([
'fq' => "-hash:{$site_hash}",
'q' => '*:*',
'wt' => 'json',
]));

try {
$response = $http_client->get($uri);
}
catch (\Throwable $exception) {
// Ignore server not available. Already treated by search_api_solr.
// @see search_api_solr_requirements()
}

$num_found = Json::decode($response->getBody()->getContents())['response']['numFound'];
if ($num_found) {
$requirements["solr_data:{$index_id}"] = [
'severity' => REQUIREMENT_ERROR,
'title' => t("Solr index %index", ['%index' => $index_id]),
'value' => t('Found @count entries with invalid site hash', [
'@count' => $num_found,
]),
'description' => t('The valid site hash is %hash. Retrieve the invalid entries with this <a href=":uri">query</a>.', [
'%hash' => $site_hash,
':uri' => $uri,
]),
];
}
}

return $requirements;
}

0 comments on commit 84698dc

Please sign in to comment.