Skip to content

Commit

Permalink
Merge pull request #677 from UN-OCHA/RW-829
Browse files Browse the repository at this point in the history
[RW-829] Add script to retag MTI documents
  • Loading branch information
orakili authored Nov 8, 2023
2 parents bfac50e + de04cea commit 318d895
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions scripts/retagging/RW-829.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

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

$proceed = TRUE;
$save = TRUE;

$sources = [
1545 => 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);
}

0 comments on commit 318d895

Please sign in to comment.