This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tombstone
executable file
·120 lines (109 loc) · 3.43 KB
/
tombstone
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env php
<?php
/**
* Script for creating tombstone entries for Bags in ElasticSearch.
*
* Run './tombstone --help' for usage.
*/
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7;
use \wapmorgan\UnifiedArchive\UnifiedArchive;
$cmd = new Commando\Command();
$cmd->option('p')
->aka('path')
->describedAs('Absolute or relative path to the Bag filename to create the tombstone for. Use either this option or --id.')
->default('');
$cmd->option('e')
->aka('elasticsearch_url')
->describedAs('URL (including port number) of your Elasticsearch endpoint. Default is "http://localhost:9200".')
->default('http://localhost:9200');
$cmd->option('x')
->aka('elasticsearch_index')
->describedAs('Elasticsearch index. Default is "bags".')
->default('bags');
$cmd->option('i')
->aka('id')
->describedAs('The ID of the bag to create the tombstone for. Use either this option or --path.');
if (strlen($cmd['p'])) {
$bag_id = get_id_from_path($cmd['e'], $cmd['x'], $cmd['p']);
}
else {
$bag_id = $cmd['id'];
}
if (document_exists($cmd['elasticsearch_url'], $cmd['elasticsearch_index'], $bag_id)) {
$client = new Client([
'base_uri' => $cmd['elasticsearch_url'],
]);
try {
$update['doc'] = array('tombstone' => true, 'document_timestamp' => gmdate("Y-m-d\TH:i:s\Z"));
$response = $client->post($cmd['elasticsearch_index'] . '/bag/' . $bag_id . '/_update', ['json' => $update]);
}
catch (ClientException $e) {
print Psr7\str($e->getRequest());
print Psr7\str($e->getResponse());
}
}
else {
print "Bag " . $bag_id . " not found in " . $cmd['elasticsearch_url'] . '/' . $cmd['elasticsearch_index'] . PHP_EOL;
exit;
}
print "Tombstone for bag " . $bag_id . " added to " . $cmd['elasticsearch_url'] . '/' . $cmd['elasticsearch_index'] . PHP_EOL;
/**
* See if the document exists.
*
* @param string $base_url
* The Elasticsearch server's URL.
* @param string $bag_id
* The ID of the bag in the Elasticsearch index.
*
* @return bool
* True if the index exists, false if not.
*/
function document_exists($base_url, $index, $bag_id) {
$client = new Client(['base_uri' => $base_url]);
try {
$document_exists_response = $client->request('GET', $index . '/bag' . '/' . $bag_id);
}
catch (ClientException $e) {
if ($e->getResponse()->getStatusCode() == 404) {
return false;
}
}
return true;
}
/**
* Given a bag's path, get its ID in the index.
*
* @param string $base_url
* The Elasticsearch server's URL.
* @param string $bag_id
* The ID of the bag in the Elasticsearch index.
* @param string $bag_path
* The absolute path of the bag in the Elasticsearch index.
*
* @return string
* The bag's ID.
*/
function get_id_from_path($base_url, $index, $bag_path) {
$client = new Client(['base_uri' => $base_url]);
try {
$query = array('query' => array('term' => array('bag_location_exact' => $bag_path)));
$response = $client->request('GET', '/'. $index . '/bag' . '/_search', ['json' => $query]);
$result = json_decode($response->getBody());
if ($result->hits->total > 0) {
return $result->hits->hits[0]->_id;
}
else {
return false;
}
}
catch (ClientException $e) {
if ($e->getResponse()->getStatusCode() == 404) {
return false;
}
}
return true;
}