Skip to content

Commit

Permalink
chore: add script used to retag OCHA product
Browse files Browse the repository at this point in the history
Refs: RW-1175
  • Loading branch information
orakili committed Feb 17, 2025
1 parent 1a40286 commit 7c1c6e2
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions scripts/retagging/RW-1175.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* @file
* Retagging script for RW-1175.
*/

$proceed = TRUE;
$save = TRUE;

$terms = [
12350 => "Press Release",
12352 => "Statement/Speech",
12353 => "Other",
];

$nids = \Drupal::database()->query("
SELECT DISTINCT n.nid
FROM {node_field_data} AS n
INNER JOIN {node__field_ocha_product} AS fs
ON fs.entity_id = n.nid
WHERE fs.field_ocha_product_target_id IS NOT NULL
AND fs.field_ocha_product_target_id NOT IN (:terms[])
ORDER BY nid ASC
", [
":terms[]" => array_keys($terms),
])->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_ocha_product as $item) {
if (!isset($terms[$item->target_id])) {
$results[$node->bundle()][$item->target_id] = ($results[$node->bundle()][$item->target_id] ?? 0) + 1;
$item->target_id = 12353;
}
}

$node->notifications_content_disable = TRUE;
$node->setRevisionLogMessage("Automatic retagging of OCHA product (Ref: RW-1175).");
$node->setRevisionUserId(2);
$node->setRevisionCreationTime($now);
$node->setNewRevision(TRUE);
if ($save) {
$node->save();
}

echo "Progress: $progress / $total..." . PHP_EOL;
$progress++;
}
}

print_r($results);
}

0 comments on commit 7c1c6e2

Please sign in to comment.