From 37397fcad3762dbe84a5da0530bbf89067537308 Mon Sep 17 00:00:00 2001
From: Claudiu Cristea <clau.cristea@gmail.com>
Date: Fri, 4 Jun 2021 16:34:27 +0300
Subject: [PATCH 1/4] ISAICP-6530: Clean orphan Solr document entries.

---
 .../custom/joinup_core/joinup_core.deploy.php | 24 +++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/web/modules/custom/joinup_core/joinup_core.deploy.php b/web/modules/custom/joinup_core/joinup_core.deploy.php
index 0402a709ad..588e6b2ccb 100644
--- a/web/modules/custom/joinup_core/joinup_core.deploy.php
+++ b/web/modules/custom/joinup_core/joinup_core.deploy.php
@@ -14,6 +14,8 @@
 
 declare(strict_types = 1);
 
+use Laminas\Diactoros\Uri;
+
 /**
  * Update fields in specific solutions.
  */
@@ -99,3 +101,25 @@ function joinup_core_deploy_0107200(array &$sandbox): string {
   $sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max']) ? 1 : (float) $sandbox['progress'] / (float) $sandbox['max'];
   return "Processed {$sandbox['progress']} out of {$sandbox['max']} items.";
 }
+
+/**
+ * Clean orphan Solr document entries.
+ */
+function joinup_deploy_0107201(array &$sandbox = NULL): void {
+  $http_client = \Drupal::httpClient();
+  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']}/update")
+      ->withQuery(http_build_query([
+        'stream.body' => '<delete><query>-hash:tb6juv</query></delete>',
+        'commit' => 'true',
+        'wt' => 'json',
+       ]));
+    $http_client->get($uri);
+  }
+}

From 30bf0c52e1854c9565ef27e357363d8ea993e410 Mon Sep 17 00:00:00 2001
From: Claudiu Cristea <clau.cristea@gmail.com>
Date: Fri, 4 Jun 2021 17:25:18 +0300
Subject: [PATCH 2/4] ISAICP-6530: Fix function name.

---
 web/modules/custom/joinup_core/joinup_core.deploy.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/web/modules/custom/joinup_core/joinup_core.deploy.php b/web/modules/custom/joinup_core/joinup_core.deploy.php
index 588e6b2ccb..38160d4961 100644
--- a/web/modules/custom/joinup_core/joinup_core.deploy.php
+++ b/web/modules/custom/joinup_core/joinup_core.deploy.php
@@ -105,7 +105,7 @@ function joinup_core_deploy_0107200(array &$sandbox): string {
 /**
  * Clean orphan Solr document entries.
  */
-function joinup_deploy_0107201(array &$sandbox = NULL): void {
+function joinup_core_deploy_0107201(array &$sandbox = NULL): void {
   $http_client = \Drupal::httpClient();
   foreach (['published', 'unpublished'] as $index_id) {
     $endpoint = \Drupal::config("search_api.server.solr_{$index_id}")

From 664e84c01475208921ef2b73803c3b106aa01948 Mon Sep 17 00:00:00 2001
From: Claudiu Cristea <clau.cristea@gmail.com>
Date: Fri, 4 Jun 2021 17:25:41 +0300
Subject: [PATCH 3/4] ISAICP-6530: Provide status report.

---
 .../joinup_search/joinup_search.install       | 65 +++++++++++++++++++
 1 file changed, 65 insertions(+)
 create mode 100644 web/modules/custom/joinup_search/joinup_search.install

diff --git a/web/modules/custom/joinup_search/joinup_search.install b/web/modules/custom/joinup_search/joinup_search.install
new file mode 100644
index 0000000000..9be6bc178e
--- /dev/null
+++ b/web/modules/custom/joinup_search/joinup_search.install
@@ -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;
+}

From 566d861aecdaa803fc992432a436b04d5c055f96 Mon Sep 17 00:00:00 2001
From: Claudiu Cristea <clau.cristea@gmail.com>
Date: Mon, 7 Jun 2021 12:56:11 +0300
Subject: [PATCH 4/4] ISAICP-6530: PHP CS.

---
 web/modules/custom/joinup_core/joinup_core.deploy.php  | 4 ++--
 web/modules/custom/joinup_search/joinup_search.install | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/web/modules/custom/joinup_core/joinup_core.deploy.php b/web/modules/custom/joinup_core/joinup_core.deploy.php
index 38160d4961..01e4a8257f 100644
--- a/web/modules/custom/joinup_core/joinup_core.deploy.php
+++ b/web/modules/custom/joinup_core/joinup_core.deploy.php
@@ -105,7 +105,7 @@ function joinup_core_deploy_0107200(array &$sandbox): string {
 /**
  * Clean orphan Solr document entries.
  */
-function joinup_core_deploy_0107201(array &$sandbox = NULL): void {
+function joinup_core_deploy_0107201(?array &$sandbox = NULL): void {
   $http_client = \Drupal::httpClient();
   foreach (['published', 'unpublished'] as $index_id) {
     $endpoint = \Drupal::config("search_api.server.solr_{$index_id}")
@@ -119,7 +119,7 @@ function joinup_core_deploy_0107201(array &$sandbox = NULL): void {
         'stream.body' => '<delete><query>-hash:tb6juv</query></delete>',
         'commit' => 'true',
         'wt' => 'json',
-       ]));
+      ]));
     $http_client->get($uri);
   }
 }
diff --git a/web/modules/custom/joinup_search/joinup_search.install b/web/modules/custom/joinup_search/joinup_search.install
index 9be6bc178e..5110fd3482 100644
--- a/web/modules/custom/joinup_search/joinup_search.install
+++ b/web/modules/custom/joinup_search/joinup_search.install
@@ -34,7 +34,7 @@ function joinup_search_requirements(string $phase): array {
         'fq' => "-hash:{$site_hash}",
         'q' => '*:*',
         'wt' => 'json',
-       ]));
+      ]));
 
     try {
       $response = $http_client->get($uri);
@@ -56,7 +56,7 @@ function joinup_search_requirements(string $phase): array {
         'description' => t('The valid site hash is %hash. Retrieve the invalid entries with this <a href=":uri">query</a>.', [
           '%hash' => $site_hash,
           ':uri' => $uri,
-         ]),
+        ]),
       ];
     }
   }