Skip to content

Instantly share code, notes, and snippets.

@sbreker
Last active June 26, 2020 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbreker/2d9aab48b3ecd55d5af2098ef85de459 to your computer and use it in GitHub Desktop.
Save sbreker/2d9aab48b3ecd55d5af2098ef85de459 to your computer and use it in GitHub Desktop.
<?php
// VERSION: 0.5
// delete empty info objs.
// In AtoM 2.4 is is possible to create empty Descriptions if a malformed
// csv is loaded. A common way to have this happen is to have blank lines
// present in the import CSV file.
//
// This script can be used to DELETE these empty Info Obj records.
//
// There is a $dryRun variable - set this to 'True' if you want to test
// what the sript finds WITHOUT DELETING. It will output a list of slugs
// it would have deleted. This is great for testing!
//
// The slugs created by this bug will look like 3 sets of 4 alphanum chars:
// e.g. n5ny-e7g3-w6te
//
// Setting $dryRun to 'False' will cause the script to delete any Descriptions
// it finds. MAKE BACKUPS FIRST!
//
// Setting $deleteChildren to 'True' will remove any child Descriptions found
// attached to these empty descriptions. BE VERY CAREFUL WITH THIS SETTING.
// TEST WITH DRY-RUN!!
//
// Command: php symfony tools:run <full_path_to_this_file>
//
// This script will:
// - Find all Descriptions that:
// -- Are not the root info obj (ID is NOT 1)
// -- That have a null (empty) identifier
// -- That have a null title
// -- That have a null repository ID
// -- That have a null LOD value
// -- Have the root info obj as a parent.
//
// - REBUILD ES INDEX AFTER RUNNING: php symfony search:populate
$dryRun = True;
$deleteChildDescriptions = False;
QubitSearch::disable();
function getChildren($id)
{
$query = 'SELECT io.id, slug.slug
FROM information_object io
JOIN slug on slug.object_id = io.id
WHERE io.parent_id = '.$id;
$results = QubitPdo::fetchAll($query, array(), array('fetchMode' => PDO::FETCH_ASSOC));
return $results;
}
$query = 'SELECT io.id, slug.slug, io.parent_id, io.identifier
FROM information_object io
JOIN information_object_i18n on io.id = information_object_i18n.id
JOIN object ON io.id = object.id
JOIN slug on slug.object_id = object.id
WHERE object.id != '.QubitInformationObject::ROOT_ID.
' AND io.parent_id = 1
AND information_object_i18n.title IS NULL
AND io.identifier IS NULL
AND io.level_of_description_id IS NULL
AND io.repository_id IS NULL
ORDER BY io.rgt ASC';
print("DELETING info obj records...\n");
$statement = QubitPdo::prepareAndExecute($query);
$counter = 0;
while ($row = $statement->fetch(PDO::FETCH_ASSOC))
{
print ("Deleting information object. ID: " . $row['id'] . ' ' . $row['slug'] . ' ' . $row['identifier'] . "\n");
try
{
$io = QubitInformationObject::getById($row['id']);
$childList = array();
if (null !== $children = getChildren($row['id']))
{
foreach ($children as $child)
{
$childList[] = $child["slug"];
}
if (0 < count($childList))
{
printf("Child recs found: %s\n", implode(",", $childList));
}
}
if (!$dryRun && $deleteChildDescriptions && 0 < count($childList))
{
printf("Deleting child recs: %s\n", implode(",", $childList));
$io->deleteFullHierarchy();
}
elseif (!$dryRun)
{
$io->delete();
}
print(" DELETED.\n");
$counter++;
}
catch (Exception $e)
{
print(" *ERROR - FAILED TO DELETE() OBJECT.\n");
print($e);
}
}
print("Deleted ".$counter." records.\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment