-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.php
328 lines (288 loc) · 9.87 KB
/
fetch.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
/**
* @file
* fetch.php, a utility for generating Bags via the Islandora REST interface.
*
* See the README.md file for more information.
*/
require_once 'vendor/autoload.php';
require 'vendor/scholarslab/bagit/lib/bagit.php';
use Monolog\Logger;
use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
// Read the .ini file.
if (file_exists(trim($argv[1]))) {
$config = parse_ini_file(trim($argv[1]), true);
} else {
print "Cannot find configuration file " . trim($argv[1]) . "\n";
exit;
}
/**
* Set up configuration values and log file.
*/
$temp_dir = $config['general']['temp_dir'];
$output_dir = $config['general']['output_dir'];
$islandora_base_url = rtrim($config['general']['islandora_base_url'], '/');
$config['bag']['compression'] = isset($config['bag']['compression']) ?
$config['bag']['compression'] : 'tgz';
$name_template = isset($config['general']['name_template']) ?
$config['general']['name_template'] : '[PID]';
$pid_separator = isset($config['general']['pid_separator']) ?
$config['general']['pid_separator'] : '_';
$path_to_log = isset($config['general']['path_to_log']) ?
$config['general']['path_to_log'] : 'fetch_bags.log';
$log = new Monolog\Logger('Islandora Fetch Bags');
$log_stream_handler= new Monolog\Handler\StreamHandler($path_to_log, Logger::INFO);
$log->pushHandler($log_stream_handler);
$log->addInfo("fetch.php started generating Bags from " . $islandora_base_url . " starting at ". date("F j, Y, g:i a"));
/**
* Get PIDs of objects to create Bags for, loop through
* the list, call Islandora's REST interface to get info
* on each object, and generate its Bag.
*/
$pids = array();
if (isset($config['objects']['solr_query'])) {
$solr_query = $config['objects']['solr_query'];
$pids = get_pids_from_solr($islandora_base_url, $solr_query);
}
if (isset($config['objects']['pid_file'])) {
$pid_file = $config['objects']['pid_file'];
$pids = get_pids_from_file($pid_file);
}
if (count($pids) == 0) {
print "No objects to generate Bags for, exiting.\n";
exit;
}
@mkdir($temp_dir);
@mkdir($output_dir);
foreach ($pids as $pid) {
describe_object($pid, $islandora_base_url);
}
$log->addInfo("fetch.php finished exporting Bags at ". date("F j, Y, g:i a"));
/**
* Functions.
*/
/**
* Gets the list of datastreams from Islandora's REST interface.
*
* @param string $pid
* The object's PID.
* @param string $islandora_base_url
* The site's base URL.
*/
function describe_object($pid, $islandora_base_url) {
$object_url = $islandora_base_url . '/islandora/rest/v1/object/' . $pid;
try {
$client = new GuzzleHttp\Client();
$object_response = $client->request('GET', $object_url, [
'headers' => [
'Accept' => 'application/json',
// 'X-Authorization-User' => $user . ':' . $token,
]
]);
} catch (Exception $e) {
if ($e instanceof RequestException or $e instanceof ClientException or $e instanceof ServerException ) {
$log->addError(Psr7\str($e->getRequest()));
if ($e->hasResponse()) {
$log->addError(Psr7\str($e->getResponse()));
print Psr7\str($e->getResponse()) . "\n";
}
exit;
}
}
$object_response_body = json_decode($object_response->getBody());
fetch_datastreams($object_response_body, $islandora_base_url);
}
/**
* Gets all of the datastream content files and save them in a directory.
*
* @param object $object_response_body
* The body of the REST request to describe the Islandora object.
* @param string $islandora_base_url
* The site's base URL.
*/
function fetch_datastreams($object_response_body, $islandora_base_url) {
global $log;
global $name_template;
global $pid_separator;
$raw_pid = $object_response_body->pid;
$datastreams = $object_response_body->datastreams;
// Add some custom mimetype -> extension mappings.
$builder = \Mimey\MimeMappingBuilder::create();
$builder->add('text/xml', 'xml');
$builder->add('image/jp2', 'jp2');
$mimes = new \Mimey\MimeTypes($builder->getMapping());
$bag_name = get_bag_name($raw_pid);
$bag_temp_dir = get_bag_temp_dir($bag_name);
mkdir($bag_temp_dir);
$client = new GuzzleHttp\Client();
$data_files = array();
foreach ($datastreams as $ds) {
$ds_url = $islandora_base_url . '/islandora/rest/v1/object/' . $raw_pid . '/datastream/' . $ds->dsid;
$ds_response = $client->request('GET', $ds_url, [
'headers' => [
// 'X-Authorization-User' => $user . ':' . $token,
]
]);
$mimeTypes = $mimes->getAllExtensions($ds->mimeType);
$file_path = $bag_temp_dir . DIRECTORY_SEPARATOR . $ds->dsid . '.' . $mimeTypes[0];
file_put_contents($file_path, $ds_response->getBody());
$data_files[] = $file_path;
}
generate_bag($object_response_body, $data_files);
}
/**
* Generates a Bag from the object's datastream content files.
*
* @param object $object_response_body
* The body of the REST request to describe the Islandora object.
* @param array $files
* Array of file paths to the saved files.
*/
function generate_bag($object_response_body, $files) {
global $log;
global $output_dir;
global $islandora_base_url;
global $config;
$pid = $object_response_body->pid;
$object_url = $islandora_base_url . '/islandora/object/' . $pid;
$bag_info = array();
foreach ($config['bag-info']['tags'] as $bag_info_tag) {
list($tag, $value) = explode(':', $bag_info_tag);
$bag_info[$tag] = trim($value);
}
$bag_name = get_bag_name($pid);
$bag_dir = $output_dir . DIRECTORY_SEPARATOR . $bag_name;
$bag = new BagIt($bag_dir, true, true, true, $bag_info);
foreach ($files as $file) {
$bag->addFile($file, basename($file));
}
if (isset($config['bag']['fetch'])) {
foreach ($config['bag']['fetch'] as $fetch_url) {
$bag->fetch->add($fetch_url, basename(parse_url($fetch_url, PHP_URL_PATH)));
}
}
if (isset($config['bag']['plugins'])) {
foreach ($config['bag']['plugins'] as $plugin) {
$plugin_name = '\ifb\plugins\\' . $plugin;
$bag_plugin = new $plugin_name($config);
$bag = $bag_plugin->execute($bag, $object_response_body);
}
}
$bag->update();
$bag_output_dir = $output_dir . DIRECTORY_SEPARATOR . $bag_name;
if ($config['bag']['compression'] == 'tgz' or $config['bag']['compression'] == 'zip') {
$bag->package($bag_output_dir, $config['bag']['compression']);
cleanup_temp_files($bag_output_dir);
}
$message = "Bag for $object_url saved in $output_dir";
$log->addInfo($message);
print $message . "\n";
$bag_temp_dir = get_bag_temp_dir($bag_name);
cleanup_temp_files($bag_temp_dir);
}
/**
* Gets the Bag's name, which is the name of the directory where
* the Bag will be output to, or the name of the zip/tar file
* if compression is enabled.
*
* @param string $pid
* The object's PID.
*
* @return string
* The Bag's name.
*/
function get_bag_name($pid) {
global $name_template;
global $pid_separator;
$pid_for_bag_dir = preg_replace('/' . ':' .'/', $pid_separator, $pid);
$bag_name = preg_replace('/\[PID\]/', $pid_for_bag_dir, $name_template);
return $bag_name;
}
/**
* Gets the temporary directory path where the Bag's files
* are stored.
*
* @param string $bag_name
* The Bag's name, which is the name of the directory where
* the Bag will be output to, or the name of the zip/tar file
* if compression is enabled.
*
* @return string
* The full path to the temporary directory.
*/
function get_bag_temp_dir($bag_name) {
global $temp_dir;
return $temp_dir . DIRECTORY_SEPARATOR . $bag_name;
}
/**
* Deletes a directory and all its children.
*
* @param string $dir
* The directory to delete.
*/
function cleanup_temp_files($dir) {
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
$child = $dir . DIRECTORY_SEPARATOR . $file;
is_dir($child) ? cleanup_temp_files($child) : unlink($child);
}
rmdir($dir);
}
/**
* Returns a list of PIDs from Solr.
*
* @param string $islandora_base_url
* The target Islandora instance's base URL.
* @param string $solr_query
* The query for retrieving PIDs.
*
* @return array
* A list of PIDs.
*/
function get_pids_from_solr($islandora_base_url, $solr_query) {
$pids = array();
$solr_url = $islandora_base_url . '/islandora/rest/v1/solr/' . $solr_query;
$client = new GuzzleHttp\Client();
$solr_response = $client->request('GET', $solr_url, [
'headers' => [
'Accept' => 'application/json',
// 'X-Authorization-User' => $user . ':' . $token,
]
]);
$solr_response_body = $solr_response->getBody();
$solr_results = json_decode($solr_response_body);
$docs = $solr_results->response->docs;
$count = count($docs);
print "Retrieved $count object PIDs from Solr, starting to fetch content files and generate Bags.\n";
foreach ($docs as $doc) {
$pids[] = $doc->PID;
}
return $pids;
}
/**
* Returns a list of PIDs from a PID file.
*
* @param string $pid_file_path
* The absolute path to the PID file.
*
* @return array
* A list of PIDs.
*/
function get_pids_from_file($pid_file_path) {
$pids = array();
$lines = file($pid_file_path);
foreach ($lines as $pid) {
$pid = trim($pid);
// Skip commented out rows.
if (!preg_match('!(#|//)!', $pid)) {
$pids[] = $pid;
}
}
$count = count($pids);
print "Retrieved $count object PIDs from $pid_file_path, starting to fetch content files and generate Bags.\n";
return $pids;
}