From 78aca96394e0bda1f1578dcda59241fd7e828ce4 Mon Sep 17 00:00:00 2001 From: berliner Date: Fri, 22 Nov 2024 10:27:48 +0100 Subject: [PATCH] HPC-9890: Fix some issues with the changed detection in content migrations --- ....migration.articles_hpc_content_module.yml | 8 ++--- ....migration.articles_hpc_content_module.yml | 6 ++-- .../src/ContentManager/BaseContentManager.php | 7 +++- .../RemoteSourceBaseHpcContentModule.php | 34 +++++++++---------- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/config/migrate_plus.migration.articles_hpc_content_module.yml b/config/migrate_plus.migration.articles_hpc_content_module.yml index 866a9e47c..cb1fda8f2 100644 --- a/config/migrate_plus.migration.articles_hpc_content_module.yml +++ b/config/migrate_plus.migration.articles_hpc_content_module.yml @@ -45,9 +45,9 @@ source: label: Tags selector: tags - - name: content_space_title + name: content_space label: 'Content space' - selector: content_space/title + selector: content_space ids: id: type: integer @@ -92,11 +92,11 @@ process: - plugin: skip_on_empty method: process - source: content_space_title + source: content_space - plugin: entity_lookup entity_type: taxonomy_term - source: content_space_title + source: content_space bundle_key: vid value_key: name bundle: content_space diff --git a/html/modules/custom/ghi_content/config/install/migrate_plus.migration.articles_hpc_content_module.yml b/html/modules/custom/ghi_content/config/install/migrate_plus.migration.articles_hpc_content_module.yml index 506b10861..2684faf09 100644 --- a/html/modules/custom/ghi_content/config/install/migrate_plus.migration.articles_hpc_content_module.yml +++ b/html/modules/custom/ghi_content/config/install/migrate_plus.migration.articles_hpc_content_module.yml @@ -40,7 +40,7 @@ source: - name: content_space label: 'Content space' - selector: content_space/title + selector: content_space ids: id: type: integer @@ -85,11 +85,11 @@ process: - plugin: skip_on_empty method: process - source: content_space/title + source: content_space - plugin: entity_lookup entity_type: taxonomy_term - source: content_space/title + source: content_space bundle_key: vid value_key: name bundle: content_space diff --git a/html/modules/custom/ghi_content/src/ContentManager/BaseContentManager.php b/html/modules/custom/ghi_content/src/ContentManager/BaseContentManager.php index a56ebf3f5..3f83b0b38 100644 --- a/html/modules/custom/ghi_content/src/ContentManager/BaseContentManager.php +++ b/html/modules/custom/ghi_content/src/ContentManager/BaseContentManager.php @@ -526,8 +526,13 @@ abstract public function isUpToDateWithRemote(NodeInterface $node); * A normalized array based on the given node object. */ protected function normalizeContentNodeData(NodeInterface $node) { + $migration = $this->getMigration($node); + $expected_fields = array_map(function ($item) { + return str_contains($item, '/') ? explode('/', $item)[0] : $item; + }, array_keys($migration->getProcess())); + $data = $node->toArray(); - unset($data['changed']); + $data = array_intersect_key($data, array_flip($expected_fields)); ArrayHelper::sortMultiDimensionalArrayByKeys($data); ArrayHelper::reduceArray($data); $this->moduleHandler->alter('normalize_content', $data); diff --git a/html/modules/custom/ghi_content/src/RemoteSource/RemoteSourceBaseHpcContentModule.php b/html/modules/custom/ghi_content/src/RemoteSource/RemoteSourceBaseHpcContentModule.php index b47f7c3e4..e92ec36f3 100644 --- a/html/modules/custom/ghi_content/src/RemoteSource/RemoteSourceBaseHpcContentModule.php +++ b/html/modules/custom/ghi_content/src/RemoteSource/RemoteSourceBaseHpcContentModule.php @@ -12,7 +12,9 @@ use Drupal\hpc_api\Traits\SimpleCacheTrait; use GuzzleHttp\Cookie\CookieJar; use GuzzleHttp\Exception\ClientException; +use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\ServerException; +use GuzzleHttp\RequestOptions; /** * HPC Content Module specific remote source base class. @@ -435,23 +437,19 @@ public function getContentUrl($id, $type = 'canonical') { * {@inheritdoc} */ public function getFileSize($uri) { - $options = [ - 'http' => [ - 'method' => 'HEAD', - ], - ]; if ($basic_auth = $this->getRemoteBasicAuth()) { - $options['http'] = [ - 'header' => 'Authorization: Basic ' . base64_encode($basic_auth['user'] . ':' . $basic_auth['pass']), + $options[RequestOptions::AUTH] = [ + $basic_auth['user'], + $basic_auth['pass'], ]; } - $context = stream_context_create($options); - $headers = @get_headers($uri, 1, $context); - if (!$headers) { + try { + $response = $this->httpClient->head($uri, $options); + } + catch (GuzzleException $e) { return NULL; } - $headers = array_change_key_case($headers); - return $headers['content-length'] ?? NULL; + return $response->getHeader('content-length') ?? NULL; } /** @@ -460,13 +458,13 @@ public function getFileSize($uri) { public function getFileContent($uri) { $options = []; if ($basic_auth = $this->getRemoteBasicAuth()) { - $options['http'] = [ - 'method' => 'GET', - 'header' => 'Authorization: Basic ' . base64_encode($basic_auth['user'] . ':' . $basic_auth['pass']), + $options[RequestOptions::AUTH] = [ + $basic_auth['user'], + $basic_auth['pass'], ]; } - $context = stream_context_create($options); - return @file_get_contents($uri, FALSE, $context); + $response = $this->httpClient->get($uri, $options); + return $response->getBody(); } /** @@ -534,6 +532,8 @@ public function getImportMetaData($type, ?array $tags) { title title_short summary + content_space + tags created updated status