From de04cea4ef8418ca1309ea45497c406825730361 Mon Sep 17 00:00:00 2001 From: orakili Date: Tue, 7 Nov 2023 23:15:12 +0000 Subject: [PATCH] chore: add script to retag MTI documents Refs: RW-829 --- scripts/retagging/RW-829.php | 61 ++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 scripts/retagging/RW-829.php diff --git a/scripts/retagging/RW-829.php b/scripts/retagging/RW-829.php new file mode 100644 index 000000000..e14b2d9bd --- /dev/null +++ b/scripts/retagging/RW-829.php @@ -0,0 +1,61 @@ + 2929, +]; + +$nids = \Drupal::database()->query(" + SELECT DISTINCT n.nid + FROM {node_field_data} AS n + INNER JOIN {node__field_source} AS fs + ON fs.entity_id = n.nid + WHERE fs.field_source_target_id IN (:sources[]) + ORDER BY nid ASC +", [ + ":sources[]" => array_keys($sources), +])->fetchCol(); + +$total = count($nids); + +echo "Found " . $total . " nodes to update" . PHP_EOL; + +if (!empty($proceed)) { + $storage = \Drupal::entityTypeManager()->getStorage("node"); + $chunk_size = 100; + $now = time(); + $progress = 1; + + $results = []; + foreach (array_chunk($nids, $chunk_size) as $chunk) { + foreach ($storage->loadMultiple($chunk) as $node) { + foreach ($node->field_source as $item) { + if (isset($sources[$item->target_id])) { + $results[$node->bundle()][$item->target_id] = ($results[$node->bundle()][$item->target_id] ?? 0) + 1; + $item->target_id = $sources[$item->target_id]; + } + } + + $node->notifications_content_disable = TRUE; + $node->setRevisionLogMessage("Automatic retagging of source (Ref: RW-829)."); + $node->setRevisionUserId(2); + $node->setRevisionCreationTime($now); + $node->setNewRevision(TRUE); + if ($save) { + $node->save(); + } + + echo "Progress: $progress / $total..." . PHP_EOL; + $progress++; + } + } + + print_r($results); +}