Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions scripts/delete_media_file.php
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";
61 changes: 61 additions & 0 deletions scripts/delete_media_file_dry_run.php
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";
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".

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
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.

}

echo "\n";

echo "Processing complete.\n";