Last active
August 2, 2021 14:55
-
-
Save ajstanley/9498b7feace32dbaf4ab to your computer and use it in GitHub Desktop.
EMiC-327
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
*/ | |
function get_all_the_pids() { | |
$query = <<<EOT | |
SELECT DISTINCT ?pid FROM <#ri> | |
WHERE { | |
?pid <info:fedora/fedora-system:def/model#hasModel> ?cmodel ; | |
<fedora-model:state> <fedora-model:Active> ; | |
} | |
EOT; | |
$tuque = islandora_get_tuque_connection(); | |
$results = $tuque->repository->ri->query($query, 'sparql'); | |
$pids_to_update = array(); | |
foreach ($results as $result) { | |
if (substr_count( $result['pid']['value'], 'islandora:') > 0) { | |
$pids_to_update[] = $result['pid']['value']; | |
}; | |
} | |
return $pids_to_update; | |
} | |
function get_content_models() { | |
$query = <<<EOT | |
SELECT DISTINCT ?pid FROM <#ri> | |
WHERE { | |
?pid <info:fedora/fedora-system:def/model#hasModel> <info:fedora/fedora-system:ContentModel-3.0> ; | |
<fedora-model:state> <fedora-model:Active> . | |
} | |
EOT; | |
$tuque = islandora_get_tuque_connection(); | |
$results = $tuque->repository->ri->query($query, 'sparql'); | |
$content_models = array(); | |
foreach ($results as $result) { | |
$content_models[] = $result['pid']['value']; | |
} | |
return $content_models; | |
} | |
function emcidora_clone($pid) { | |
$old_object = islandora_object_load($pid); | |
$new_object = islandora_copy_object($old_object); | |
$new_object->id = str_replace('islandora:', 'emic:', $pid); | |
$rels = $old_object->relationships->get(); | |
foreach ($rels as $rel) { | |
$namespace = $rel['predicate']['namespace']; | |
$predicate = $rel['predicate']['value']; | |
$value = $rel['object']['value']; | |
$type = $rel['object']['literal']; | |
if ($predicate != 'hasModel') { | |
$value = str_replace('islandora:', 'emic:', $value); | |
} | |
$new_object->relationships->remove($namespace, $predicate); | |
$new_object->relationships->add($namespace, $predicate, $value, $type); | |
} | |
$clone = islandora_add_object($new_object); | |
if ($clone) { | |
$old_object->state = 'D'; | |
} | |
return $clone; | |
} | |
$all = get_all_the_pids(); | |
$models = get_content_models(); | |
$cleaned = array_diff($all, $models); | |
foreach ($cleaned as $pid) { | |
$clone = emcidora_clone($pid); | |
if (!$clone) { | |
print "$pid did not successfully replicate. \n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment