Skip to content

Commit 883a8fe

Browse files
author
DDEV User
committed
Address concerns about deleting files with multiple media references.
1 parent 36cfb1b commit 883a8fe

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

scripts/delete_media_file.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
continue;
4545
}
4646

47+
if (count($utils->getReferencingMedia($fid)) > 1) {
48+
echo "WARNING: File {$fid} has more than one media referenceing it, please investigate further. Skipping...\n";
49+
continue;
50+
}
4751
foreach ($utils->getReferencingMedia($fid) as $media) {
4852
if ($media) {
4953
echo "Deleted media ID: " . $media->id() . "\n";
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Script to delete media and associated file.
6+
*
7+
* Usage: drush php:script delete_media_file_dry_run.php "fid1,fid2,fid3"
8+
*/
9+
10+
use Drupal\file\Entity\File;
11+
12+
$utils = \Drupal::service('islandora.utils');
13+
14+
// Get command line arguments
15+
// $extra is used by drush scr command
16+
// For drush php:script, use $_SERVER['argv'].
17+
$input = $extra[0] ?? $_SERVER['argv'][1] ?? '';
18+
19+
if (empty($input)) {
20+
echo "Error: No input parameters provided.\n";
21+
echo "Usage: drush php:script delete_media_file_dry_run.php \"fid1,fid2,fid3\"\n";
22+
exit(1);
23+
}
24+
25+
// Parse comma-separated string into an array.
26+
$fids = array_map('trim', explode(',', $input));
27+
28+
// Remove empty values.
29+
$fids = array_filter($fids);
30+
31+
if (empty($fids)) {
32+
echo "Error: No valid file IDs provided.\n";
33+
exit(1);
34+
}
35+
36+
echo "Dry run -- processing " . count($fids) . " file(s)...\n\n";
37+
38+
foreach ($fids as $fid) {
39+
echo "--- Processing file: {$fid} ---\n";
40+
$file = File::load($fid);
41+
42+
if (!$file) {
43+
echo "Warning: Could not load file {$fid}\n";
44+
continue;
45+
}
46+
47+
if (count($utils->getReferencingMedia($fid)) > 1) {
48+
echo "WARNING: File {$fid} has more than one media referenceing it, please investigate further. Skipping...\n";
49+
continue;
50+
}
51+
foreach ($utils->getReferencingMedia($fid) as $media) {
52+
if ($media) {
53+
echo "Dry run - prod run will delete media ID: " . $media->id() . "\n";
54+
}
55+
}
56+
echo "Dry run - prod run will delete file ID: " . $file->id() . "\n";
57+
}
58+
59+
echo "\n";
60+
61+
echo "Processing complete.\n";

0 commit comments

Comments
 (0)