Skip to content

Conversation

@nhart
Copy link

@nhart nhart commented Dec 31, 2025

Summary by CodeRabbit

  • New Features
    • Added command-line scripts to delete file records and associated media, plus a dry-run mode.
    • Accepts comma-separated file IDs and validates input.
    • Warns and skips files referenced by multiple media; logs per-file/media actions.
    • Reports progress, errors, and a final completion message.

✏️ Tip: You can customize this high-level summary in your review settings.

@nhart nhart added the minor Added functionality that is backwards compatible. label Dec 31, 2025
@coderabbitai
Copy link

coderabbitai bot commented Dec 31, 2025

📝 Walkthrough

Walkthrough

Adds two new Drush PHP scripts: scripts/delete_media_file.php deletes Media entities referencing given File IDs and then deletes the files; scripts/delete_media_file_dry_run.php reports the same deletions without performing them. Both accept comma-separated file IDs, validate input, and emit per-file logs/warnings.

Changes

Cohort / File(s) Summary
Delete script
scripts/delete_media_file.php
New CLI Drush script that parses comma-separated file IDs, loads File entities, finds referencing Media via Islandora utilities, deletes referencing Media (unless multiple references), deletes the File entity, and logs progress/warnings to stdout.
Dry-run script
scripts/delete_media_file_dry_run.php
New CLI Drush dry-run script that parses file IDs and reports which Media and File deletions would occur, skipping any actual deletions; includes input validation and per-file warnings for missing files or multiple references.

Sequence Diagram(s)

sequenceDiagram
    participant Operator as Operator (Drush)
    participant Script as delete_media_file(.php)
    participant Islandora as Islandora Utils
    participant Drupal as Drupal Entity API
    participant DB as Database

    Operator->>Script: invoke with comma-separated file IDs
    Script->>Drupal: File::load(fileId)
    alt file not found
        Drupal-->>Script: null
        Script->>Operator: log warning (file not found)
    else file found
        Drupal-->>Script: File entity
        Script->>Islandora: find referencing Media for File
        Islandora-->>Script: list of Media IDs
        alt multiple referencing media (>1)
            Script->>Operator: log warning and skip file
        else single or multiple handled for deletion
            loop for each media
                Script->>Drupal: load Media entity
                Drupal-->>Script: Media entity
                alt dry-run
                    Script->>Operator: log "would delete media {id}"
                else execute
                    Script->>Drupal: delete Media entity
                    Drupal-->>DB: remove media record
                    Drupal-->>Script: deletion confirmation
                    Script->>Operator: log media deletion
                end
            end
            alt dry-run
                Script->>Operator: log "would delete file {id}"
            else execute
                Script->>Drupal: delete File entity
                Drupal-->>DB: remove file record
                Drupal-->>Script: file deletion confirmation
                Script->>Operator: log file deletion
            end
        end
    end
    Script->>Operator: processing complete
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 I hop through FIDs with nimble feet,
Listing what to prune or what to delete.
Dry-run whispers, real run clears the heap,
Islandora dreams while Drush counts sheep. 🥕✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately and concisely describes the main change: adding scripts for deleting media entities and files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings

Warning

Review ran into problems

🔥 Problems

Errors were encountered while retrieving linked issues.

Errors (1)
  • JIRA integration encountered authorization issues. Please disconnect and reconnect the integration in the CodeRabbit UI.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🧹 Nitpick comments (1)
scripts/delete_media_file.php (1)

38-55: Consider transaction handling and structured logging.

The script performs destructive operations without transaction handling. If a media deletion fails partway through the loop, the file may still be deleted, leaving an inconsistent state. Additionally, consider using Drupal's logger service instead of echo statements for better observability in production environments.

This is an optional enhancement for improved reliability and observability:

  • Wrap the deletion logic in a database transaction that can be rolled back on failure.
  • Replace echo statements with \Drupal::logger('delete_media_file')->info() for structured logging.
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Jira integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between d36bfe5 and fee94c4.

📒 Files selected for processing (1)
  • scripts/delete_media_file.php
🧰 Additional context used
🪛 GitHub Actions: Code Linting
scripts/delete_media_file.php

[warning] 10-10: PHPCS warning: Unused use statement. (Drupal.Classes.UnusedUseStatement.UnusedUse)


[error] 11-11: PHPCS error: There must be one blank line after the last USE statement; 0 found. (PSR2.Namespaces.UseDeclaration.SpaceAfterLastUse)


[error] 16-16: PHPCS error: Inline comments must end in full-stops, exclamation marks, question marks, colons, or closing parentheses. (Drupal.Commenting.InlineComment.InvalidEndChar)


[error] 17-17: PHPCS error: Use null coalesce operator instead of ternary operator. (SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator.NullCoalesceOperatorNotUsed)


[error] 17-17: PHPCS error: Use null coalesce operator instead of ternary operator. (SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator.NullCoalesceOperatorNotUsed)


[error] 53-53: PHPCS error: Line indented incorrectly; expected 2 spaces, found 4. (Drupal.WhiteSpace.ScopeIndent.IncorrectExact)


[error] 54-54: PHPCS error: Line indented incorrectly; expected 2 spaces, found 4. (Drupal.WhiteSpace.ScopeIndent.IncorrectExact)


[error] 57-57: PHPCS error: Line indented incorrectly; expected 0 spaces, found 2. (Drupal.WhiteSpace.ScopeIndent.IncorrectExact)

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Fix all issues with AI Agents
In @scripts/delete_media_file_dry_run.php:
- Line 48: The warning message echo that prints "WARNING: File {$fid} has more
than one media referenceing it, please investigate further. Skipping...\n"
contains a typo; update the string to use "referencing" instead of
"referenceing" so the echo reads "WARNING: File {$fid} has more than one media
referencing it, please investigate further. Skipping...\n".
- Around line 47-56: Cache the result of $utils->getReferencingMedia($fid) into
a local variable (e.g., $referencing = $utils->getReferencingMedia($fid)) and
use $referencing for the count() check and the foreach loop instead of calling
getReferencingMedia() twice; update the conditional that currently uses
count($utils->getReferencingMedia($fid)) to use count($referencing) and iterate
over $referencing to echo the media IDs and avoid redundant queries.
🧹 Nitpick comments (1)
scripts/delete_media_file_dry_run.php (1)

25-34: Consider validating that file IDs are numeric.

While File::load() may handle non-numeric values gracefully, adding explicit validation would make errors clearer and prevent potential issues.

🔎 Proposed validation enhancement
 // Remove empty values.
 $fids = array_filter($fids);
+
+// Validate that all values are numeric.
+$fids = array_filter($fids, 'is_numeric');
 
 if (empty($fids)) {
   echo "Error: No valid file IDs provided.\n";
   exit(1);
 }
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Jira integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 36cfb1b and 883a8fe.

📒 Files selected for processing (2)
  • scripts/delete_media_file.php
  • scripts/delete_media_file_dry_run.php
🚧 Files skipped from review as they are similar to previous changes (1)
  • scripts/delete_media_file.php
🔇 Additional comments (6)
scripts/delete_media_file_dry_run.php (6)

1-8: LGTM!

The file header and usage documentation are clear and accurate.


14-23: LGTM!

The argument parsing correctly handles both Drush command formats and provides clear error messaging.


36-37: LGTM!

Clear messaging indicating the dry-run mode and file count.


38-45: LGTM!

The file loading and null check are appropriate, with clear warning messages for missing files.


59-61: LGTM!

Clear completion message.


10-12: islandora.utils service usage is correct.

The islandora.utils service and getReferencingMedia() method are provided by the Islandora module, which is a declared requirement in README.md. The method calls at lines 47 and 51 properly pass the file ID and iterate over the returned media objects. No issues found.

Comment on lines +47 to +56
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 "Dry run - prod run will delete media ID: " . $media->id() . "\n";
}
}
echo "Dry run - prod run will delete file ID: " . $file->id() . "\n";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Optimize by caching the result of getReferencingMedia().

The script calls getReferencingMedia($fid) twice: once on Line 47 to check the count and again on Line 51 to iterate. This is inefficient and could impact performance, especially if the method involves database queries.

🔎 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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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 "Dry run - prod run will delete media ID: " . $media->id() . "\n";
}
}
echo "Dry run - prod run will delete file ID: " . $file->id() . "\n";
$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 ($referencing_media 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";
🤖 Prompt for AI Agents
In @scripts/delete_media_file_dry_run.php around lines 47 - 56, Cache the result
of $utils->getReferencingMedia($fid) into a local variable (e.g., $referencing =
$utils->getReferencingMedia($fid)) and use $referencing for the count() check
and the foreach loop instead of calling getReferencingMedia() twice; update the
conditional that currently uses count($utils->getReferencingMedia($fid)) to use
count($referencing) and iterate over $referencing to echo the media IDs and
avoid redundant queries.

}

if (count($utils->getReferencingMedia($fid)) > 1) {
echo "WARNING: File {$fid} has more than one media referenceing it, please investigate further. Skipping...\n";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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";
🤖 Prompt for AI Agents
In @scripts/delete_media_file_dry_run.php at line 48, The warning message echo
that prints "WARNING: File {$fid} has more than one media referenceing it,
please investigate further. Skipping...\n" contains a typo; update the string to
use "referencing" instead of "referenceing" so the echo reads "WARNING: File
{$fid} has more than one media referencing it, please investigate further.
Skipping...\n".

@nhart
Copy link
Author

nhart commented Jan 8, 2026

no longer needed.. closing pr

@nhart nhart closed this Jan 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor Added functionality that is backwards compatible.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants