diff --git a/web/modules/custom/joinup_core/joinup_core.deploy.php b/web/modules/custom/joinup_core/joinup_core.deploy.php index c3ad4bc8e9eef1eb709d411d6852218cd6d3ca3e..a362578d6faa653c3b4f670eedda130b626a0bc8 100644 --- a/web/modules/custom/joinup_core/joinup_core.deploy.php +++ b/web/modules/custom/joinup_core/joinup_core.deploy.php @@ -14,21 +14,10 @@ declare(strict_types=1); -use Drupal\Component\Utility\Variable; use Drupal\Core\Cache\Cache; use Drupal\Core\Entity\Sql\SqlEntityStorageInterface; -use Drupal\Core\Session\AccountSwitcherInterface; -use Drupal\Core\Utility\UpdateException; use Drupal\joinup_core\DeployAndSmokeTestHelper; use Drupal\joinup_document\Entity\DocumentInterface; -use Drupal\joinup_workflow\WorkflowHelperInterface; -use Drupal\solution\Entity\Solution; -use Drupal\user\Entity\User; -use Drupal\user\UserInterface; - -// Temporary constant. -// phpcs:ignore DrupalPractice.Constants.GlobalConstant.GlobalConstant -const SOLUTION_ARCHIVAL_ACTOR_ID = 744195; /** * Create Interoperable Europe Act document as custom page. @@ -91,97 +80,3 @@ function joinup_core_deploy_110901(): string { Cache::invalidateTags($node->getCacheTagsToInvalidate()); return "Orphan archived revision of document node with ID '{$node->id()}' was programmatically changed to published."; } - -/** - * Archives solutions specified by Functional Team. - */ -function joinup_core_deploy_110902(&$sandbox): string { - if (!isset($sandbox['remaining'])) { - $stream = fopen(__DIR__ . '/solutions-to-archive.csv', 'r'); - // CSV is small enough to store it in memory. - while (is_array($item = fgetcsv($stream))) { - $sandbox['remaining'][$item[0]] = $item[1] ?? $item[0]; - } - // Drop header. - unset($sandbox['remaining']['ID']); - fclose($stream); - $sandbox['total'] = count($sandbox['remaining']); - $sandbox['processed'] = 0; - } - - $accountSwitcher = \Drupal::service('account_switcher'); - assert($accountSwitcher instanceof AccountSwitcherInterface); - $storage = \Drupal::entityTypeManager()->getStorage('rdf_entity'); - $storage->resetCache(); - $logger = Drupal::logger('joinup_core'); - $workflowHelper = \Drupal::service('joinup_workflow.workflow_helper'); - assert($workflowHelper instanceof WorkflowHelperInterface); - - // We need a moderator account to perform the workflow state change. - if (!($actor = User::load(SOLUTION_ARCHIVAL_ACTOR_ID)) instanceof UserInterface) { - throw new UpdateException(sprintf( - "Cannot find the predefined actor for the operation. Was looking for user account with id '%s'.", - SOLUTION_ARCHIVAL_ACTOR_ID - )); - } - if (!$actor->hasRole('moderator')) { - throw new UpdateException('The predefined actor does not have the moderator role.'); - } - - $items = array_splice($sandbox['remaining'], 0, 50); - $solutions = $storage->loadMultiple(array_keys($items)); - $notLoadableSolutions = array_diff_key($items, $solutions); - $accountSwitcher->switchTo($actor); - - foreach ($solutions as $id => $solution) { - if (!$solution instanceof Solution) { - $logger->warning(sprintf( - "The RDF entity with ID '%s' is not a solution (referred with title '%s' in the spreadsheet)", - $id, - $items[$id], - )); - continue; - } - - if ($solution->isArchived() || !$solution->isTargetWorkflowStateAllowed('archived')) { - $logger->warning(sprintf( - "The solution with ID '%s' (referred with title '%s' in the spreadsheet) cannot be archived. Its current state is '%s'.", - $id, - $items[$id], - $solution->getWorkflowState(), - )); - continue; - } - - try { - $solution->skip_notification = TRUE; - $solution->notifyContactOnly = TRUE; - $workflowHelper->archiveEntity($solution); - } - catch (\Exception $exception) { - $logger->error(sprintf( - "The solution with ID '%s' (referred with title '%s' in the spreadsheet) cannot be archived:\n%s", - $id, - $items[$id], - $exception->getMessage(), - )); - } - } - - if (!empty($notLoadableSolutions)) { - $logger->warning(sprintf( - "The following solutions were not found:\n%s", - Variable::export($notLoadableSolutions), - )); - } - - $accountSwitcher->switchBack(); - - $sandbox['processed'] += count($items); - $sandbox['#finished'] = (int) empty($sandbox['remaining']); - return sprintf( - "Processed %s solutions from total %s items to archive.", - $sandbox['processed'], - $sandbox['total'], - ); -} diff --git a/web/modules/custom/joinup_rdf/joinup_rdf.deploy.php b/web/modules/custom/joinup_rdf/joinup_rdf.deploy.php index 5182398a576d3079376089e1762ba8b2249ab42d..cf2cb06869ee959fb9cccb3a3275bf7378740c39 100644 --- a/web/modules/custom/joinup_rdf/joinup_rdf.deploy.php +++ b/web/modules/custom/joinup_rdf/joinup_rdf.deploy.php @@ -7,11 +7,22 @@ declare(strict_types=1); +use Drupal\Component\Utility\Variable; +use Drupal\Core\Session\AccountSwitcherInterface; +use Drupal\Core\Utility\UpdateException; use Drupal\joinup_taxonomy\Entity\MappedTerm; +use Drupal\joinup_workflow\WorkflowHelperInterface; use Drupal\node\Entity\Node; use Drupal\pathauto\PathautoState; use Drupal\search_api\Entity\Index; +use Drupal\solution\Entity\Solution; use Drupal\sparql_entity_storage\UriEncoder; +use Drupal\user\Entity\User; +use Drupal\user\UserInterface; + +// Temporary constant. +// phpcs:ignore DrupalPractice.Constants.GlobalConstant.GlobalConstant +const SOLUTION_ARCHIVAL_ACTOR_ID = 744195; /** * Disable RDF Sync for the deployment. @@ -277,3 +288,97 @@ function _joinup_rdf_deploy_flatten_rdf_values(array $data): array { } return $values; } + +/** + * Archives solutions specified by Functional Team. + */ +function joinup_rdf_deploy_110906(&$sandbox): string { + if (!isset($sandbox['remaining'])) { + $stream = fopen(__DIR__ . '/solutions-to-archive.csv', 'r'); + // CSV is small enough to store it in memory. + while (is_array($item = fgetcsv($stream))) { + $sandbox['remaining'][$item[0]] = $item[1] ?? $item[0]; + } + // Drop header. + unset($sandbox['remaining']['ID']); + fclose($stream); + $sandbox['total'] = count($sandbox['remaining']); + $sandbox['processed'] = 0; + } + + $accountSwitcher = \Drupal::service('account_switcher'); + assert($accountSwitcher instanceof AccountSwitcherInterface); + $storage = \Drupal::entityTypeManager()->getStorage('rdf_entity'); + $storage->resetCache(); + $logger = Drupal::logger('joinup_core'); + $workflowHelper = \Drupal::service('joinup_workflow.workflow_helper'); + assert($workflowHelper instanceof WorkflowHelperInterface); + + // We need a moderator account to perform the workflow state change. + if (!($actor = User::load(SOLUTION_ARCHIVAL_ACTOR_ID)) instanceof UserInterface) { + throw new UpdateException(sprintf( + "Cannot find the predefined actor for the operation. Was looking for user account with id '%s'.", + SOLUTION_ARCHIVAL_ACTOR_ID + )); + } + if (!$actor->hasRole('moderator')) { + throw new UpdateException('The predefined actor does not have the moderator role.'); + } + + $items = array_splice($sandbox['remaining'], 0, 50); + $solutions = $storage->loadMultiple(array_keys($items)); + $notLoadableSolutions = array_diff_key($items, $solutions); + $accountSwitcher->switchTo($actor); + + foreach ($solutions as $id => $solution) { + if (!$solution instanceof Solution) { + $logger->warning(sprintf( + "The RDF entity with ID '%s' is not a solution (referred with title '%s' in the spreadsheet)", + $id, + $items[$id], + )); + continue; + } + + if ($solution->isArchived() || !$solution->isTargetWorkflowStateAllowed('archived')) { + $logger->warning(sprintf( + "The solution with ID '%s' (referred with title '%s' in the spreadsheet) cannot be archived. Its current state is '%s'.", + $id, + $items[$id], + $solution->getWorkflowState(), + )); + continue; + } + + try { + $solution->skip_notification = TRUE; + $solution->notifyContactOnly = TRUE; + $workflowHelper->archiveEntity($solution); + } + catch (\Exception $exception) { + $logger->error(sprintf( + "The solution with ID '%s' (referred with title '%s' in the spreadsheet) cannot be archived:\n%s", + $id, + $items[$id], + $exception->getMessage(), + )); + } + } + + if (!empty($notLoadableSolutions)) { + $logger->warning(sprintf( + "The following solutions were not found:\n%s", + Variable::export($notLoadableSolutions), + )); + } + + $accountSwitcher->switchBack(); + + $sandbox['processed'] += count($items); + $sandbox['#finished'] = (int) empty($sandbox['remaining']); + return sprintf( + "Processed %s solutions from total %s items to archive.", + $sandbox['processed'], + $sandbox['total'], + ); +} diff --git a/web/modules/custom/joinup_core/solutions-to-archive.csv b/web/modules/custom/joinup_rdf/solutions-to-archive.csv similarity index 100% rename from web/modules/custom/joinup_core/solutions-to-archive.csv rename to web/modules/custom/joinup_rdf/solutions-to-archive.csv