This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsharedshelf-to-iiif-s3.php
197 lines (171 loc) · 6.87 KB
/
sharedshelf-to-iiif-s3.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
<?php
// sharedshelf-to-iiif-s3.php - move sharedshelf collection into static iiif on s3
require_once 'SharedShelfService.php';
require_once 'SharedShelfToSolrLogger.php';
require_once 'image-to-iiif-s3.php';
function usage()
{
global $argv;
echo PHP_EOL;
echo 'Usage: php '.$argv[0].' [--help] [--force] [-p NNN] [-s NNN]'.PHP_EOL;
echo '--help - show this info'.PHP_EOL;
echo '--force - ignore timestamps and rewrite all solr records'.PHP_EOL;
echo '-p - only process SharedShelf collection (project number) NNN (NNN must be numeric) - see listProjects.php'.PHP_EOL;
echo '-s - only process one of the images in the collection - id NNN'.PHP_EOL;
echo '--startdate yyyy-mm-dd - process only Forum assets with updated_on this date or later'.PHP_EOL;
echo '--enddate yyyy-mm-dd - process only Forum assets with updated_on this date or earlier'.PHP_EOL;
exit(0);
}
$log = false;
$options = getopt('p:s:', ['help', 'force', 'startdate:', 'enddate:']);
if (isset($options['help'])) {
usage();
}
$force_replacement = isset($options['force']);
if (isset($options['p'])) {
if (is_numeric($options['p'])) {
$single_collection = $options['p'];
} else {
usage();
}
} else {
$single_collection = false;
}
if (isset($options['s'])) {
if (is_numeric($options['s'])) {
$single_item = $options['s'];
} else {
usage();
}
} else {
$single_item = false;
}
$startdate = false;
if (isset($options['startdate'])) {
$match = preg_match('/\d{4}-\d{2}-\d{2}/', $options['startdate']);
if (1 === $match) {
$startdate = $options['startdate'];
}
}
$enddate = false;
if (isset($options['enddate'])) {
$match = preg_match('/\d{4}-\d{2}-\d{2}/', $options['enddate']);
if (1 === $match) {
$enddate = $options['enddate'];
}
}
try {
$supported_image_formats = ['png', 'jpg', 'gif', 'tif'];
// batch process information
$task = parse_ini_file('sharedshelf-to-iiif-s3.ini', true);
if (false === $task) {
echo "Need sharedshelf-to-iiif-s3.ini\n";
exit(1);
}
// open log
if (empty($task['process']['log_file_prefix'])) {
echo "Need log_file_prefix\n";
exit(1);
}
// sharedshelf user
$user = parse_ini_file('ssUser.ini');
if (false === $user) {
throw new Exception('Need to create ssUser.ini. See README.md', 1);
}
if (!isset($task['process']['cookie_jar_path'])) {
throw new Exception('Expecting cookie_jar_path in .ini file', 1);
}
$ss = new SharedShelfService($user['email'], $user['password'], $task['process']['cookie_jar_path']);
foreach ($task['configuration_files']['config'] as $config) {
$project = parse_ini_file($config);
if (false === $project) {
throw new Exception("Missing configuration file: $config", 1);
}
if (false !== $single_collection) {
if ($project['project'] != $single_collection) {
continue;
}
}
$project_id = $project['project'];
// find the publishing target id for this project
$publishing_target_id = $ss->find_publishing_target_id($project_id);
// create a log file for this collection
$log_file_prefix = $task['process']['log_file_prefix'].'-'.$project_id;
$log = new SharedShelfToSolrLogger($log_file_prefix);
$log->task("$config-$project_id-iiif");
$log->note('project_asset_ids');
$asset_count = $ss->project_assets_count($project_id);
$log->note("asset_count:$asset_count");
echo "IIIF: $config asset count: $asset_count ".$log->log_file_name().PHP_EOL;
$per_page = 25;
for ($start = 0; $start < $asset_count; $start += $per_page) {
$assets = $ss->project_assets($project_id, $start, $per_page);
$counter = $start;
foreach ($assets as $asset) {
try {
$ss_id = $asset['id'];
if ($single_item && $ss_id != $single_item) {
continue;
}
$log->item("asset $ss_id");
$pct = sprintf('%01.2f', $counter++ * 100.0 / (float) $asset_count);
$log->note("Completed:$pct");
if (false !== $startdate) {
if (strncmp($asset['updated_on'], $startdate, strlen($startdate)) < 0) {
$log->note('skipping : before startdate : '.$asset['updated_on']);
continue;
}
}
if (false !== $enddate) {
if (strncmp($asset['updated_on'], $enddate, strlen($enddate)) > 0) {
$log->note('skipping : after enddate : '.$asset['updated_on']);
continue;
}
}
// determine publishing status - status_ssi
$asset_full = $ss->asset($ss_id);
if (isset($asset_full['publishing_status']["$publishing_target_id"]['status'])) {
$cul_publishing_status = $asset_full['publishing_status']["$publishing_target_id"]['status'];
} else {
$cul_publishing_status = 'Unpublished';
}
if (0 != strcmp($cul_publishing_status, 'Published')) {
$log->note("Publishing status: $cul_publishing_status");
}
$url = $ss->media_url($ss_id);
$ext = $ss->media_file_extension($ss_id);
if (!in_array(strtolower($ext), $supported_image_formats)) {
throw new Exception("Unsupported image format: '$ext' ", 1);
}
$s3_path = "$project_id/$ss_id";
try {
image_to_iiif_s3($url, $ext, $s3_path, $force_replacement);
} catch (\Exception $e2) {
// just log the error and continue
$log->error("Asset $s3_path conversion to iiif failed: ".$e2->getMessage());
}
if (false) {
throw new Exception('shortcut to exit', 1);
}
} catch (\Exception $e) {
$error = 'Caught exception: '.$e->getMessage()." - skipping this asset\n";
if (false !== $log) {
$log->error($error);
} else {
echo $error;
}
}
}
}
$log->task("Done. Project $project_id.");
}
} catch (Exception $e) {
$error = 'Caught exception: '.$e->getMessage()."\n";
if (false !== $log) {
$log->error($error);
} else {
echo $error;
}
exit(1);
}
exit(0);