-
Notifications
You must be signed in to change notification settings - Fork 11
/
Derivatives.php
319 lines (300 loc) · 15.7 KB
/
Derivatives.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
<?php
/**
* Class to create derivatives and ingets them into Fedora
*
* @author Richard Wincewicz
*/
include_once 'PREMIS.php';
class Derivative {
function __construct($fedora_object, $incoming_dsid, $extension = NULL, $log, $created_datastream) {
include_once 'message.php';
include_once 'fedoraConnection.php';
$this->log = $log;
$this->fedora_object = $fedora_object;
$this->object = $fedora_object->object;
$this->pid = $fedora_object->object->id;
$this->created_datastream = $created_datastream;
$this->incoming_dsid = $incoming_dsid;
$datastream_array = array();
foreach ($this->object as $datastream) {
$datastream_array[] = $datastream->id;
}
if (!in_array($dsid, $datastream_array)) {
print "Could not find the $dsid datastream!";
}
else {
$this->incoming_datastream = new FedoraDatastream($this->incoming_dsid, $this->fedora_object->object, $this->fedora_object->repository);
$this->mimetype = $this->incoming_datastream->mimetype;
}
$this->log->lwrite('Mimetype: ' . $this->mimetype, 'SERVER_INFO');
$this->extension = $extension;
if ($this->incoming_dsid != NULL) {
$this->temp_file = $fedora_object->saveDatastream($incoming_dsid, $extension);
}
$extension_array = explode('.', $this->temp_file);
$extension = $extension_array[1];
}
function __destruct() {
unlink($this->temp_file);
}
function OCR($dsid = 'OCR', $label = 'Scanned text', $language = 'eng') {
$this->log->lwrite('Starting processing', 'PROCESS_DATASTREAM', $this->pid, $dsid);
try {
if (file_exists($this->temp_file)) {
$output_file = $this->temp_file . '_OCR';
$command = "tesseract $this->temp_file $output_file -l $language -psm 1";
exec($command, $ocr_output, $return);
if (file_exists($output_file . '.txt')) {
$log_message = "$dsid derivative created by tesseract v3.0.1 using command - $command || SUCCESS";
$ingest = $this->add_derivative($dsid, $label, $output_file . '.txt', 'text/plain', $log_message);
}
else {
$convert_command = "convert -quality 99 " . $this->temp_file . " " . $this->temp_file . "_JPG2.jpg";
exec($convert_command, $convert_output, $return);
$command = "tesseract " . $this->temp_file . "_JPG2.jpg " . $output_file . " -l $language -psm 1";
exec($command, $ocr2_output, $return);
if (file_exists($output_file . '.txt')) {
$log_message = "$dsid derivative created by using ImageMagick to convert to jpg using command - $convert_command - and tesseract v3.0.1 using command - $command || SUCCESS ~~ OCR of original TIFF failed and so the image was converted to a JPG and reprocessed.";
$ingest = $this->add_derivative($dsid, $label, $output_file . '.txt', 'text/plain', $log_message);
}
else {
$this->log->lwrite("Could not find the file '$output_file.txt' for the $dsid derivative!\nTesseract output: " . implode(', ', $ocr_output) . " - Return value: $return", 'FAIL_DATASTREAM', $this->pid, $dsid, NULL, 'ERROR');
}
}
}
else {
$this->log->lwrite("Could not find the input file '$this->temp_file' for the $dsid derivative!", 'FAIL_DATASTREAM', $this->pid, $dsid, NULL, 'ERROR');
}
} catch (Exception $e) {
$this->log->lwrite("Could not create the $dsid derivative!", 'FAIL_DATASTREAM', $this->pid, $dsid, NULL, 'ERROR');
if ($ingest) {
unlink($output_file . '.txt');
}
}
return $return;
}
function HOCR($dsid = 'HOCR', $label = 'HOCR', $language = 'eng') {
$this->log->lwrite('Starting processing', 'PROCESS_DATASTREAM', $this->pid, $dsid);
try {
if (file_exists($this->temp_file)) {
$output_file = $this->temp_file . '_HOCR';
$command = "tesseract $this->temp_file $output_file -l $language -psm 1 hocr";
exec($command, $hocr_output, $return);
if (file_exists($output_file . '.html')) {
$log_message = "$dsid derivative created by tesseract v3.0.1 using command - $command || SUCCESS";
$ingest = $this->add_derivative($dsid, $label, $output_file . '.html', 'text/html', $log_message);
}
else {
$convert_command = "convert -quality 99 " . $this->temp_file . " " . $this->temp_file . "_JPG2.jpg";
exec($convert_command);
$command = "tesseract " . $this->temp_file . "_JPG2.jpg " . $output_file . " -l $language -psm 1 hocr";
exec($command, $hocr2_output, $return);
if (file_exists($output_file . '.html')) {
$log_message = "$dsid derivative created by using ImageMagick to convert to jpg using command - $convert_command - and tesseract v3.0.1 using command - $command || SUCCESS ~~ OCR of original TIFF failed and so the image was converted to a JPG and reprocessed.";
$ingest = $this->add_derivative($dsid, $label, $output_file . '.html', 'text/plain', $log_message);
}
else {
$this->log->lwrite("Could not find the file '$output_file.html' for the $dsid derivative!\nTesseract output: " . implode(', ', $ocr_output) . "\nReturn value: $return", 'FAIL_DATASTREAM', $this->pid, $dsid, NULL, 'ERROR');
}
}
}
else {
$this->log->lwrite("Could not find the input file '$this->temp_file' for the $dsid derivative!", 'FAIL_DATASTREAM', $this->pid, $dsid, NULL, 'ERROR');
}
} catch (Exception $e) {
$this->log->lwrite("Could not create the $dsid derivative!", 'FAIL_DATASTREAM', $this->pid, $dsid, NULL, 'ERROR');
if ($ingest) {
unlink($output_file . '.html');
}
}
return $return;
}
function ENCODED_OCR($dsid = 'ENCODED_OCR', $label = 'Encoded OCR', $language = 'eng') {
$this->log->lwrite('Starting processing', 'PROCESS_DATASTREAM', $this->pid, $dsid);
try {
$output_file = $this->temp_file . '_HOCR';
$command = "tesseract $this->temp_file $output_file -l $language -psm 1 hocr";
exec($command, $hocr_output, $return);
if (!file_exists($output_file . '.html')) {
exec("convert -quality 99 " . $this->temp_file . " " . $this->temp_file . "_JPG2.jpg");
$command = "tesseract " . $this->temp_file . "_JPG2.jpg " . $output_file . " -l $language -psm 1 hocr";
exec($command, $hocr2_output, $return);
if (!file_exists($output_file . '.html')) {
$this->log->lwrite("Could not create the $dsid derivative!", 'FAIL_DATASTREAM', $this->pid, $dsid, NULL, 'ERROR');
return $return;
}
}
$hocr_datastream = new NewFedoraDatastream("HOCR", 'M', $this->object, $this->fedora_object->repository);
$hocr_datastream->setContentFromFile($output_file . '.html');
$hocr_datastream->label = 'HOCR';
$hocr_datastream->mimetype = 'text/html';
$hocr_datastream->state = 'A';
$hocr_datastream->checksum = TRUE;
$hocr_datastream->checksumType = 'MD5';
$hocr_datastream->logMessage = "HOCR derivative created by tesseract v3.0.1 using command - $command || SUCCESS";
$this->object->ingestDatastream($hocr_datastream);
$hocr_xml = new DOMDocument();
$hocr_xml->load($output_file . '.html');
$xsl = new DOMDocument();
$xsl->load('hocr_to_lower.xslt');
$proc = new XSLTProcessor();
$proc->importStylesheet($xsl);
$encoded_xml = $proc->transformToXml($hocr_xml);
$encoded_xml = str_replace('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">', '<?xml version="1.0" encoding="UTF-8"?>', $encoded_xml);
$encoded_datastream = new NewFedoraDatastream($dsid, 'M', $this->object, $this->fedora_object->repository);
$encoded_datastream->setContentFromString($encoded_xml);
$encoded_datastream->label = $label;
$encoded_datastream->mimetype = 'text/xml';
$encoded_datastream->state = 'A';
$encoded_datastream->checksum = TRUE;
$encoded_datastream->checksumType = 'MD5';
$encoded_datastream->logMessage = "$dsid derivative created by tesseract v3.0.1 and transformed using hocr_to_lower.xslt || SUCCESS";
$this->object->ingestDatastream($encoded_datastream);
unlink($output_file . '.html');
$this->log->lwrite('Finished processing', 'COMPLETE_DATASTREAM', $this->pid, $dsid);
} catch (Exception $e) {
$this->log->lwrite("Could not create the $dsid derivative!", 'FAIL_DATASTREAM', $this->pid, $dsid, NULL, 'ERROR');
unlink($output_file . '.html');
}
return $return;
}
function JP2($dsid = 'JP2', $label = 'Compressed jp2') {
$this->log->lwrite('Starting processing', 'PROCESS_DATASTREAM', $this->pid, $dsid);
try {
$output_file = $this->temp_file . '_JP2.jp2';
$command = 'kdu_compress -i ' . $this->temp_file . ' -o ' . $output_file . ' -rate 0.5 Clayers=1 Clevels=7 Cprecincts=\{256,256\},\{256,256\},\{256,256\},\{128,128\},\{128,128\},\{64,64\},\{64,64\},\{32,32\},\{16,16\} Corder=RPCL ORGgen_plt=yes ORGtparts=R Cblk=\{32,32\} Cuse_sop=yes';
exec($command, $jp2_output, $return);
$log_message = "$dsid derivative created using kdu_compress with command - $command || SUCCESS";
$this->add_derivative($dsid, $label, $output_file, 'image/jp2', $log_message);
} catch (Exception $e) {
$this->log->lwrite("Could not create the $dsid derivative!", 'FAIL_DATASTREAM', $this->pid, $dsid, NULL, 'ERROR');
unlink($output_file);
}
return $return;
}
function TN($dsid = 'TN', $label = 'Thumbnail', $height = '200', $width = '200') {
$this->log->lwrite('Starting processing', 'PROCESS_DATASTREAM', $this->pid, $dsid);
try {
$output_file = $this->temp_file . '_TN.jpg';
$command = "convert -thumbnail " . $height . "x" . $width . " $this->temp_file $output_file";
exec($command, $tn_output, $return);
$log_message = "$dsid derivative created using ImageMagick with command - $command || SUCCESS";
$this->add_derivative($dsid, $label, $output_file, 'image/jpeg', $log_message);
} catch (Exception $e) {
$this->log->lwrite("Could not create the $dsid derivative!", 'FAIL_DATASTREAM', $this->pid, $dsid, NULL, 'ERROR');
unlink($output_file);
}
return $return;
}
function TN_department($dsid = 'TN', $label = 'Thumbnail', $height = '200', $width = '200') {
$this->log->lwrite('Starting processing', 'PROCESS_DATASTREAM', $this->pid, $dsid);
try {
$tn_filename = 'department_tn.png';
if (!file_exists($tn_filename)) {
$this->log->lwrite("Could not find thumbnail image!", 'FAIL_DATASTREAM', $this->pid, $dsid, NULL, 'ERROR');
return FALSE;
}
$log_message = "$dsid derivative uploaded from file system || SUCCESS";
$this->add_derivative($dsid, $label, $tn_filename, 'image/png', $log_message);
} catch (Exception $e) {
$this->log->lwrite("Could not create the $dsid derivative!", 'FAIL_DATASTREAM', $this->pid, $dsid, NULL, 'ERROR');
}
return TRUE;
}
function TN_faculty($dsid = 'TN', $label = 'Thumbnail', $height = '200', $width = '200') {
$this->log->lwrite('Starting processing', 'PROCESS_DATASTREAM', $this->pid, $dsid);
try {
$tn_filename = 'faculty_tn.png';
if (!file_exists($tn_filename)) {
$this->log->lwrite("Could not find thumbnail image!", 'ERROR');
return FALSE;
}
$log_message = "$dsid derivative uploaded from file system || SUCCESS";
$this->add_derivative($dsid, $label, $tn_filename, 'image/png', $log_message);
} catch (Exception $e) {
$this->log->lwrite("Could not create the $dsid derivative!", 'FAIL_DATASTREAM', $this->pid, $dsid, NULL, 'ERROR');
}
return TRUE;
}
function JPG($dsid = 'JPEG', $label = 'JPEG image', $resize = '800') {
$this->log->lwrite('Starting processing', 'PROCESS_DATASTREAM', $this->pid, $dsid);
try {
$output_file = $this->temp_file . '_JPG.jpg';
$command = "convert -resize $resize $this->temp_file $output_file";
exec($command, $jpg_output, $return);
$log_message = "$dsid derivative created using ImageMagick with command - $command || SUCCESS";
$this->add_derivative($dsid, $label, $output_file, 'image/jpeg', $log_message);
} catch (Exception $e) {
$this->log->lwrite("Could not create the $dsid derivative!", 'FAIL_DATASTREAM', $this->pid, $dsid, NULL, 'ERROR');
unlink($output_file);
}
return $return;
}
function TECHMD($dsid = 'TECHMD', $label = 'Technical metadata') {
$this->log->lwrite('Starting processing', 'PROCESS_DATASTREAM', $this->pid, $dsid);
try {
$output_file = $this->temp_file . '_TECHMD.xml';
$command = "/opt/fits/fits.sh -i $this->temp_file -o $output_file -xc";
exec($command, $techmd_output, $return);
$log_message = "$dsid derivative created using FITS with command - $command || SUCCESS";
$this->add_derivative($dsid, $label, $output_file, 'text/xml', $log_message);
} catch (Exception $e) {
$this->log->lwrite("Could not create the $dsid derivative!", 'FAIL_DATASTREAM', $this->pid, $dsid, NULL, 'ERROR');
unlink($output_file);
}
return $return;
}
function Scholar_PDFA($dsid = 'PDF', $label = 'PDF') {
if ($this->created_datastream == 'OBJ') {
$this->log->lwrite('Starting processing because the ' . $this->created_datastream . ' datastream was added', 'PROCESS_DATASTREAM', $this->pid, $dsid);
try {
$output_file = $this->temp_file . '_Scholar_PDFA.pdf';
if ($this->mimetype == 'application/pdf') {
$command = "gs -dPDFA -dBATCH -dNOPAUSE -dUseCIEColor -sProcessColorModel=DeviceCMYK -sDEVICE=pdfwrite -sPDFACompatibilityPolicy=1 -sOutputFile=$output_file $this->temp_file";
exec($command, $pdfa_output, $return);
$log_message = "$dsid derivative created using GhostScript with command - $command || SUCCESS";
}
else {
$command = "java -jar /opt/jodconverter-core-3.0-beta-4/lib/jodconverter-core-3.0-beta-4.jar $this->temp_file $output_file";
exec($command, $pdfa_output, $return);
$log_message = "$dsid derivative created using JODConverted and OpenOffice with command - $command || SUCCESS";
}
$this->add_derivative($dsid, $label, $output_file, 'application/pdf', $log_message);
} catch (Exception $e) {
$this->log->lwrite("Could not create the $dsid derivative!", 'FAIL_DATASTREAM', $this->pid, $dsid, NULL, 'ERROR');
unlink($output_file);
}
return $return;
}
}
function Scholar_Policy($dsid = 'POLICY', $label = "Embargo policy - Both") {
$this->log->lwrite('Starting processing', 'PROCESS_DATASTREAM', $this->pid, $dsid);
try {
$output_file = '/opt/php_listeners/document-embargo.xml';
$log_message = "$dsid derivative uploaded from the file system || SUCCESS";
$this->add_derivative($dsid, $label, $output_file, 'text/xml', $log_message, FALSE);
} catch (Exception $e) {
$this->log->lwrite("Could not create the $dsid derivative!", 'FAIL_DATASTREAM', $this->pid, $dsid, NULL, 'ERROR');
}
return TRUE;
}
private function add_derivative($dsid, $label, $output_file, $mimetype, $log_message = NULL, $delete = TRUE) {
$datastream = new NewFedoraDatastream($dsid, 'M', $this->object, $this->fedora_object->repository);
$datastream->setContentFromFile($output_file);
$datastream->label = $label;
$datastream->mimetype = $mimetype;
$datastream->state = 'A';
$datastream->checksum = TRUE;
$datastream->checksumType = 'MD5';
if ($log_message) {
$datastream->logMessage = $log_message;
}
$return = $this->object->ingestDatastream($datastream);
if ($delete) {
unlink($output_file);
}
$this->log->lwrite('Finished processing', 'COMPLETE_DATASTREAM', $this->pid, $dsid);
return $return;
}
}
?>