|
<?php |
|
|
|
use Drupal\asset\Entity\Asset; |
|
use Drupal\log\Entity\Log; |
|
|
|
// Debug flag, leave TRUE to not perform any actions. |
|
$debug = TRUE; |
|
#$debug = FALSE; |
|
|
|
// Hosts to to not run this script on. |
|
$denylist = [ |
|
//'farmos.ddev.site', |
|
]; |
|
|
|
$host = \Drupal::request()->getHost(); |
|
if (in_array($host, $denylist)) { |
|
log_message('Host in denylist.'); |
|
return; |
|
} |
|
|
|
print(PHP_EOL); |
|
|
|
// Get the canonical and merge arguments. |
|
$canonical_id = $extra[0]; |
|
$merge_id = $extra[1]; |
|
if (empty($canonical_id) || empty($merge_id)) { |
|
log_message("Missing arguments. Need canonical id and merge id.", 'ERROR'); |
|
return; |
|
} |
|
|
|
// Load each asset. |
|
$canonical_asset = Asset::load($canonical_id); |
|
if (empty($canonical_asset)) { |
|
log_message("Canonical asset ID '$canonical_id' not found.", 'ERROR'); |
|
return; |
|
} |
|
$canonical_label = $canonical_asset->label(); |
|
log_message("Found canonical asset $canonical_id: '$canonical_label'"); |
|
|
|
$merge_asset = Asset::load($merge_id); |
|
if (empty($merge_asset)) { |
|
log_message("Merge asset ID '$merge_id' not found.", 'ERROR'); |
|
return; |
|
} |
|
$merge_label = $merge_asset->label(); |
|
log_message("Found merge asset $merge_id: '$merge_label'"); |
|
|
|
// Load logs that reference the merge asset. |
|
$log_ids = \Drupal::entityQuery('log') |
|
->condition('asset.entity:asset.id', $merge_id) |
|
->execute(); |
|
$logs = Log::loadMultiple($log_ids); |
|
|
|
// Remove the merge asset id from each log and add the canonical asset id. |
|
$log_count = count($logs); |
|
log_message("$log_count logs need update:"); |
|
foreach ($logs as $log) { |
|
|
|
/** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $asset_field */ |
|
$asset_field = $log->get('asset'); |
|
$log_assets = $asset_field->getValue(); |
|
|
|
// Find the existing asset_id. |
|
$index = array_search($merge_id, array_column($log_assets, 'target_id')); |
|
if (is_int($index)) { |
|
// Remove the index. |
|
$asset_field->removeItem($index); |
|
} |
|
|
|
// Add the canonical if doesn't already exist. |
|
$index = array_search((int) $canonical_id, array_column($log_assets, 'target_id')); |
|
if ($index === FALSE) { |
|
$asset_field->appendItem($canonical_id); |
|
} |
|
|
|
// Finally, save the log. |
|
$log_id = $log->id(); |
|
$log_name = $log->label(); |
|
if (!$debug) { |
|
$log->save(); |
|
log_message("Updated log $log_id: '$log_name'", 'operation'); |
|
} |
|
else { |
|
log_message("Log $log_id: '$log_name'"); |
|
} |
|
} |
|
|
|
// Delete the merge asset if not debugging. |
|
if (!$debug) { |
|
try { |
|
$merge_asset->delete(); |
|
log_message("Deleted asset $merge_id: '$merge_label'", 'operation'); |
|
} catch (Exception $e) { |
|
log_message("Could not delete asset $merge_id: '$merge_label'. Reason: " . $e->getMessage(), 'error'); |
|
return; |
|
} |
|
} |
|
|
|
print(PHP_EOL); |
|
|
|
/** |
|
* Helper function to print/log messages. |
|
* |
|
* @param $message |
|
* The message to print. |
|
* @param $level |
|
* The message level prefix. |
|
*/ |
|
function log_message($message, $level = 'info') { |
|
$prefix = [ |
|
'info' => '', |
|
'operation' => ' OP: ', |
|
'warning' => 'WARNING: ', |
|
'error' => 'ERROR: ', |
|
]; |
|
print($prefix[$level] . $message . PHP_EOL); |
|
} |
|
|