-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete_resource_sample.php
executable file
·66 lines (60 loc) · 3.83 KB
/
delete_resource_sample.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/php
<?php
// This script removes a given resource/collection
// config
$resourceId = 'https://id.acdh.oeaw.ac.at/resourceIwantToDelete';
$recursively = false; // should collection children be removed as well (doesn't count if you remember a binary resource)
$removeTombstone = true; // should tombstone be removed as well
$removeReferences = false; // should metadata references to removed resource(s) be removed as well (when `false` and such references exist, the removal will fail)
// advanced config (generally shouldn't need adjustments)
$configLocation = '/ARCHE/config.yaml';
$composerLocation = '/ARCHE'; // directory where you run "composer update"; if doesn't exist, the script's directory will be used instead
$recursiveProperty = null; // RDF property used for recursive deletion (if null repository's parent property is used)
$runComposerUpdate = true; // should `composer update` be run in $composerLocation dir (makes ingestion initialization longer but releases us from remembering about running `composer update` by hand)
// NO CHANGES NEEDED BELOW THIS LINE
$composerLocation = getenv('COMPOSER_DIR') ?: (file_exists($composerLocation) ? $composerLocation : __DIR__);
if ($runComposerUpdate && count($argv) < 2) {
echo "\n######################################################\nUpdating libraries\n######################################################\n";
exec('cd ' . escapeshellarg($composerLocation) . ' && composer update --no-dev');
echo "\n######################################################\nUpdate ended\n######################################################\n\n";
}
use acdhOeaw\arche\lib\Repo;
use acdhOeaw\arche\lib\exception\ExceptionUtil;
use zozlak\argparse\ArgumentParser;
require_once "$composerLocation/vendor/autoload.php";
if (count($argv) > 1) {
$parser = new ArgumentParser();
$parser->addArgument('--recursively', action: ArgumentParser::ACTION_STORE_TRUE, default: false);
$parser->addArgument('--recursiveProperty', default: '');
$parser->addArgument('--tombstone', action: ArgumentParser::ACTION_STORE_TRUE, default: false);
$parser->addArgument('--references', action: ArgumentParser::ACTION_STORE_TRUE, default: false);
$parser->addArgument('resourceId');
$parser->addArgument('repoUrl');
$parser->addArgument('user');
$parser->addArgument('password');
$args = $parser->parseArgs();
$recursively = $args->recursively;
$recursiveProperty = $args->recursiveProperty;
$removeTombstone = $args->tombstone;
$removeReferences = $args->references;
$resourceId = $args->resourceId;
$auth = [$args->user, $args->password];
$repo = Repo::factoryFromUrl($args->repoUrl, ['auth' => $auth]);
} else {
$repo = Repo::factoryInteractive(empty($configLocation) ? null : $configLocation);
}
$recursiveProperty = empty($recursiveProperty) ? $repo->getSchema()->parent : $recursiveProperty;
$recursiveProperty = $recursively ? $recursiveProperty : '';
try {
echo "\n######################################################\nDeleting\n######################################################\n";
$res = $repo->getResourceById($resourceId);
$repo->begin();
echo "deleting " . $res->getUri() . " tombstone: " . (int) $removeTombstone . " references: " . (int) $removeReferences . " recusively following: $recursiveProperty\n";
$res->delete($removeTombstone, $removeReferences, $recursiveProperty);
$repo->commit();
echo "\n######################################################\nDeleted\n######################################################\n";
} catch (Throwable $e) {
echo "\n######################################################\nDeletion failed\n######################################################\n";
echo ExceptionUtil::unwrap($e, false);
exit(1);
}