-
Notifications
You must be signed in to change notification settings - Fork 2
SUP-7425: add script for deleting media and files #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @file | ||
| * Script to delete media and associated file. | ||
| * | ||
| * Usage: drush php:script delete_media_file.php "fid1,fid2,fid3" | ||
| */ | ||
|
|
||
| use Drupal\file\Entity\File; | ||
|
|
||
| $utils = \Drupal::service('islandora.utils'); | ||
|
|
||
| // Get command line arguments | ||
| // $extra is used by drush scr command | ||
| // For drush php:script, use $_SERVER['argv']. | ||
| $input = $extra[0] ?? $_SERVER['argv'][1] ?? ''; | ||
|
|
||
| if (empty($input)) { | ||
| echo "Error: No input parameters provided.\n"; | ||
| echo "Usage: drush php:script delete_media_file.php \"fid1,fid2,fid3\"\n"; | ||
| exit(1); | ||
| } | ||
|
|
||
| // Parse comma-separated string into an array. | ||
| $fids = array_map('trim', explode(',', $input)); | ||
|
|
||
| // Remove empty values. | ||
| $fids = array_filter($fids); | ||
|
|
||
| if (empty($fids)) { | ||
| echo "Error: No valid file IDs provided.\n"; | ||
| exit(1); | ||
| } | ||
|
|
||
| echo "Processing " . count($fids) . " file(s)...\n\n"; | ||
|
|
||
| foreach ($fids as $fid) { | ||
| echo "--- Processing file: {$fid} ---\n"; | ||
| $file = File::load($fid); | ||
|
|
||
| if (!$file) { | ||
| echo "Warning: Could not load file {$fid}\n"; | ||
| continue; | ||
| } | ||
|
|
||
| if (count($utils->getReferencingMedia($fid)) > 1) { | ||
| echo "WARNING: File {$fid} has more than one media referenceing it, please investigate further. Skipping...\n"; | ||
| continue; | ||
| } | ||
| foreach ($utils->getReferencingMedia($fid) as $media) { | ||
| if ($media) { | ||
| echo "Deleted media ID: " . $media->id() . "\n"; | ||
| $media->delete(); | ||
| } | ||
| } | ||
| echo "Deleted file ID: " . $file->id() . "\n"; | ||
| $file->delete(); | ||
| } | ||
|
|
||
| echo "\n"; | ||
|
|
||
| echo "Processing complete.\n"; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,61 @@ | ||||||||||||||||||||||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||
| * @file | ||||||||||||||||||||||||||||||||||||||||||||
| * Script to delete media and associated file. | ||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||
| * Usage: drush php:script delete_media_file_dry_run.php "fid1,fid2,fid3" | ||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| use Drupal\file\Entity\File; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| $utils = \Drupal::service('islandora.utils'); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // Get command line arguments | ||||||||||||||||||||||||||||||||||||||||||||
| // $extra is used by drush scr command | ||||||||||||||||||||||||||||||||||||||||||||
| // For drush php:script, use $_SERVER['argv']. | ||||||||||||||||||||||||||||||||||||||||||||
| $input = $extra[0] ?? $_SERVER['argv'][1] ?? ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if (empty($input)) { | ||||||||||||||||||||||||||||||||||||||||||||
| echo "Error: No input parameters provided.\n"; | ||||||||||||||||||||||||||||||||||||||||||||
| echo "Usage: drush php:script delete_media_file_dry_run.php \"fid1,fid2,fid3\"\n"; | ||||||||||||||||||||||||||||||||||||||||||||
| exit(1); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // Parse comma-separated string into an array. | ||||||||||||||||||||||||||||||||||||||||||||
| $fids = array_map('trim', explode(',', $input)); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // Remove empty values. | ||||||||||||||||||||||||||||||||||||||||||||
| $fids = array_filter($fids); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if (empty($fids)) { | ||||||||||||||||||||||||||||||||||||||||||||
| echo "Error: No valid file IDs provided.\n"; | ||||||||||||||||||||||||||||||||||||||||||||
| exit(1); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| echo "Dry run -- processing " . count($fids) . " file(s)...\n\n"; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| foreach ($fids as $fid) { | ||||||||||||||||||||||||||||||||||||||||||||
| echo "--- Processing file: {$fid} ---\n"; | ||||||||||||||||||||||||||||||||||||||||||||
| $file = File::load($fid); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if (!$file) { | ||||||||||||||||||||||||||||||||||||||||||||
| echo "Warning: Could not load file {$fid}\n"; | ||||||||||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if (count($utils->getReferencingMedia($fid)) > 1) { | ||||||||||||||||||||||||||||||||||||||||||||
| echo "WARNING: File {$fid} has more than one media referenceing it, please investigate further. Skipping...\n"; | ||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the typo in the warning message. Line 48 contains a typo: "referenceing" should be "referencing". 🔎 Proposed fix- echo "WARNING: File {$fid} has more than one media referenceing it, please investigate further. Skipping...\n";
+ echo "WARNING: File {$fid} has more than one media referencing it, please investigate further. Skipping...\n";📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| foreach ($utils->getReferencingMedia($fid) as $media) { | ||||||||||||||||||||||||||||||||||||||||||||
| if ($media) { | ||||||||||||||||||||||||||||||||||||||||||||
| echo "Dry run - prod run will delete media ID: " . $media->id() . "\n"; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| echo "Dry run - prod run will delete file ID: " . $file->id() . "\n"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+47
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Optimize by caching the result of The script calls 🔎 Proposed optimization- if (count($utils->getReferencingMedia($fid)) > 1) {
+ $referencing_media = $utils->getReferencingMedia($fid);
+ if (count($referencing_media) > 1) {
echo "WARNING: File {$fid} has more than one media referencing it, please investigate further. Skipping...\n";
continue;
}
- foreach ($utils->getReferencingMedia($fid) as $media) {
+ foreach ($referencing_media as $media) {
if ($media) {
echo "Dry run - prod run will delete media ID: " . $media->id() . "\n";
}
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| echo "\n"; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| echo "Processing complete.\n"; | ||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.