This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30bf0c5
commit 664e84c
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?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 error, it's already covered by search_api_solr module. | ||
// @see search_api_solr_requirements() | ||
continue; | ||
} | ||
|
||
$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; | ||
} |