-
Notifications
You must be signed in to change notification settings - Fork 39
/
utilities.inc
1493 lines (1409 loc) · 49.3 KB
/
utilities.inc
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* This file contains all the utility functions for paged content.
*
* There are functions for:
* Getting pages for a paged content object
* Generating pdfs
* Generating image derivatives
* Generating OCR and HOCR.
*/
/**
* Gets all the pages in the given paged content object.
*
* @param AbstractObject $object
* The paged content object to fetch the pages from.
*
* @return array
* All the pages in the given paged content object. Ordered by sequence
* number. Each an array containing info.
*/
function islandora_paged_content_get_pages(AbstractObject $object) {
$pages = array();
// Try to use Solr first if enabled, or default to the RI if the Solr query
// fails for some reason.
if (module_exists('islandora_solr') && variable_get('islandora_paged_content_use_solr_for_dimensions', FALSE)) {
$pages = islandora_paged_content_get_pages_solr($object);
if (empty($pages)) {
$pages = islandora_paged_content_get_pages_ri($object);
}
}
else {
$pages = islandora_paged_content_get_pages_ri($object);
}
// Sort the pages into their proper order.
$sort = function ($a, $b) {
$a = (is_array($a) && isset($a['page'])) ? $a['page'] : 0;
$b = (is_array($b) && isset($b['page'])) ? $b['page'] : 0;
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
};
uasort($pages, $sort);
return $pages;
}
/**
* Helper function to retrieve the pages and dimensions from the RI.
*
* @param AbstractObject $object
* An AbstractObject representing a book.
*
* @return array
* All the pages in the given paged content object. Ordered by sequence
* number. Each an array containing info.
*/
function islandora_paged_content_get_pages_ri(AbstractObject $object) {
$query = <<<EOQ
PREFIX islandora-rels-ext: <http://islandora.ca/ontology/relsext#>
SELECT ?pid ?page ?label ?width ?height
FROM <#ri>
WHERE {
?pid <fedora-rels-ext:isMemberOf> <info:fedora/{$object->id}> ;
<fedora-model:label> ?label ;
islandora-rels-ext:isSequenceNumber ?page ;
<fedora-model:state> <fedora-model:Active> .
OPTIONAL {
?pid <fedora-view:disseminates> ?dss .
?dss <fedora-view:disseminationType> <info:fedora/*/JP2> ;
islandora-rels-ext:width ?width ;
islandora-rels-ext:height ?height .
}
}
ORDER BY ?page
EOQ;
$results = $object->repository->ri->sparqlQuery($query);
// Get rid of the "extra" info...
$map = function ($o) {
foreach ($o as $key => &$info) {
$info = $info['value'];
}
$o = array_filter($o);
return $o;
};
$pages = array_map($map, $results);
// Grab the PIDs...
$get_pid = function ($o) {
return $o['pid'];
};
$pids = array_map($get_pid, $pages);
// If we have some pages, combine our remapped results to produce an array
// mapping pids to the values for that pid.
$pages = count($pids) ? array_combine($pids, $pages) : array();
return $pages;
}
/**
* Helper function to retrieve the pages and dimensions from Solr.
*
* @param AbstractObject $object
* An AbstractObject representing a book.
*
* @return array
* All the pages in the given paged content object. Ordered by sequence
* number. Each an array containing info.
*/
function islandora_paged_content_get_pages_solr(AbstractObject $object) {
$pages = array();
$qp = new islandoraSolrQueryProcessor();
$qp->buildQuery(format_string('!field:("!pid" OR "info:fedora/!pid")', array(
'!field' => variable_get('islandora_solr_member_of_field', 'RELS_EXT_isMemberOf_uri_ms'),
'!pid' => $object->id,
)
));
if (variable_get('islandora_paged_content_hide_pages_solr', FALSE)) {
$fq_to_remove = array(variable_get('islandora_paged_content_solr_fq', '-RELS_EXT_isPageOf_uri_ms:[* TO *]'));
$qp->solrParams['fq'] = array_diff($qp->solrParams['fq'], $fq_to_remove);
}
$label_field = variable_get('islandora_solr_object_label_field', 'fgs_label_s');
$sequence_field = variable_get('islandora_paged_content_sequence_number_field', 'RELS_EXT_isSequenceNumber_literal_ms');
$height_field = variable_get('islandora_paged_content_solr_height_field', 'RELS_INT_height_literal_s');
$width_field = variable_get('islandora_paged_content_solr_width_field', 'RELS_INT_width_literal_s');
$qp->solrParams['fl'] = implode(', ', array(
'PID',
$label_field,
$sequence_field,
$height_field,
$width_field,
));
$qp->solrStart = 0;
$qp->solrLimit = 100000;
$qp->executeQuery(FALSE);
if ($qp->islandoraSolrResult['response']['numFound'] > 0) {
foreach ($qp->islandoraSolrResult['response']['objects'] as $page) {
$pages[$page['PID']] = array(
'pid' => $page['PID'],
'page' => (string) reset($page['solr_doc'][$sequence_field]),
'label' => $page['object_label'],
);
if (isset($page['solr_doc'][$width_field])) {
$pages[$page['PID']]['width'] = $page['solr_doc'][$width_field];
}
if (isset($page['solr_doc'][$height_field])) {
$pages[$page['PID']]['height'] = $page['solr_doc'][$height_field];
}
}
}
return $pages;
}
/**
* Function get_page_progression.
*
* Gets the page progression to be used in a viewer from the given paged
* content object.
*
* @param AbstractObject $object
* The paged content object to fetch the page progression from.
*
* @return string
* The page progression of the paged content object: either left to right
* 'lr' or right to left 'rl'.
*/
function islandora_paged_content_get_page_progression(AbstractObject $object) {
$relationships = $object->relationships->get(ISLANDORA_RELS_EXT_URI, 'hasPageProgression');
if (!empty($relationships)) {
$progression = reset($relationships);
return $progression['object']['value'];
}
else {
return 'lr';
}
}
/**
* Sets page progression for an object.
*
* @param AbstractObject $object
* An AbstractObject representing an object within Fedora.
* @param string $progression
* The page progression for the object.
*/
function islandora_paged_content_set_page_progression(AbstractObject $object, $progression) {
islandora_paged_content_unset_page_progression($object);
$object->relationships->add(ISLANDORA_RELS_EXT_URI, 'hasPageProgression', $progression, RELS_TYPE_PLAIN_LITERAL);
}
/**
* Unsets page progression for an object.
*
* @param AbstractObject $object
* An AbstractObject representing an object within Fedora.
*/
function islandora_paged_content_unset_page_progression(AbstractObject $object) {
$object->relationships->remove(ISLANDORA_RELS_EXT_URI, 'hasPageProgression');
}
/**
* Create/Update a datastream with the given parameters.
*
* @param AbstractObject $object
* The object to update.
* @param string $file
* The absolute path to the file to use as content for the datastream.
* @param string $dsid
* The datastream ID.
* @param string $label
* The datastream label.
* @param string $mimetype
* The datastream mimetype, if none given it will be guessed from the file
* extension.
* @param string $control_group
* The datastream control group.
*
* @return bool
* TRUE if successful, FALSE otherwise.
*/
function islandora_paged_content_update_datastream(AbstractObject $object, $file, $dsid, $label = NULL, $mimetype = NULL, $control_group = 'M', $copy = TRUE) {
if (!isset($mimetype)) {
$mime_detector = new MimeDetect();
$mimetype = $mime_detector->getMimetype($file);
}
$label = isset($label) ? $label : $dsid;
if (!isset($object[$dsid])) {
$ds = $object->constructDatastream($dsid, $control_group);
$ds->mimetype = $mimetype;
$ds->label = $label;
$ds->setContentFromFile($file, $copy);
$object->ingestDatastream($ds);
}
else {
$ds = $object[$dsid];
$ds->setContentFromFile($file, $copy);
if ($ds->label != $label) {
$ds->label = $label;
}
if ($ds->mimetype != $mimetype) {
$ds->mimetype = $mimetype;
}
}
return TRUE;
}
/**
* Gets the given relationship if one is defined.
*
* Assumes only one relationship matches the given parameters.
* Will return the given default if no relationship is found.
*
* @param object $relationships
* The Fedora relationships to check, either RELS-EXT or RELS-INT.
* @param string $predicate_uri
* The predicate URI.
* @param string $predicate
* The predicate.
* @param mixed $default
* The default value to return if no relationship was found.
*
* @return mixed
* The first matching relationships value if found, otherwise the given
* default it returned.
*/
function islandora_paged_content_get_relationship($relationships, $predicate_uri, $predicate, $default = NULL) {
$results = $relationships->get($predicate_uri, $predicate);
return count($results) > 0 ? $results[0]['object']['value'] : $default;
}
/**
* Sets a relationship removing any previous relationships.
*
* @param object $relationships
* The Fedora relationships to be modified, either RELS-EXT or RELS-INT.
* @param string $predicate_uri
* The predicate URI.
* @param string $predicate
* The predicate.
* @param string $object
* The object.
* @param bool $literal
* TRUE if the object is a literal, FALSE if it is an object.
*/
function islandora_paged_content_set_relationship($relationships, $predicate_uri, $predicate, $object, $literal = FALSE) {
$relationships->remove($predicate_uri, $predicate, NULL, $literal);
$relationships->add($predicate_uri, $predicate, $object, $literal);
}
/**
* Gets only the enabled ingest derivatives.
*
* As defined by the user in the admin form.
*
* @param string $namespace
* The solution pack namespace whose admin form we're checking.
*
* @return array
* The enabled ingest derivatives.
*/
function islandora_paged_content_get_enabled_ingest_derivatives($namespace) {
$default_derivatives = drupal_map_assoc(array('pdf', 'image', 'ocr'));
return variable_get("${namespace}_ingest_derivatives", $default_derivatives);
}
/**
* Gets the datastream ID of the intended source for the given derivative.
*
* @param string $dsid
* The derived datastream ID.
*
* @return string
* The datastream ID of the source for the given derivative.
*/
function islandora_paged_content_get_page_derivative_source_id($dsid) {
// @todo Change TIFF to OBJ, when done testing.
$source_datastreams = array(
'PDF' => 'OBJ',
'OCR' => 'OBJ',
'HOCR' => 'OBJ',
'TN' => 'OBJ',
'JPG' => 'OBJ',
'JP2' => 'OBJ',
);
return $source_datastreams[$dsid];
}
/**
* Checks if the given derivative datastream's intended source exists.
*
* @param AbstractObject $object
* The object to check.
* @param string $dsid
* The derived datastream ID.
*
* @return bool
* TRUE if the source datastream exists, FALSE otherwise.
*/
function islandora_paged_content_page_has_derivative_source(AbstractObject $object, $dsid) {
$source_dsid = islandora_paged_content_get_page_derivative_source_id($dsid);
return isset($object[$source_dsid]);
}
/**
* Gets the source file for the given, derivative datastream.
*
* @param AbstractObject $object
* The owner of the datastreams.
* @param string $dsid
* The derived datastream ID.
*
* @return string
* The path to the source file.
*/
function islandora_paged_content_get_page_derivative_source(AbstractObject $object, $dsid) {
if (!islandora_paged_content_can_derive($object, $dsid)) {
return FALSE;
}
$source_dsid = islandora_paged_content_get_page_derivative_source_id($dsid);
$mime_detector = new MimeDetect();
$ext = $mime_detector->getExtension($object[$source_dsid]->mimeType);
$base_name = "{$object->id}_{$source_dsid}.{$ext}";
// Colons mess with some programs...
$base_name = str_replace(':', '-', $base_name);
$source_file = file_create_filename($base_name, 'temporary://');
$object[$source_dsid]->getContent($source_file);
return drupal_realpath($source_file);
}
/**
* Checks if the given object can derive the given datastream.
*
* @param AbstractObject $object
* The object to check.
* @param string $dsid
* The derived datastream ID.
*
* @return bool
* TRUE if the datastream can be derived, FALSE otherwise.
*/
function islandora_paged_content_can_derive(AbstractObject $object, $dsid) {
$can_derive = FALSE;
$to_derive = array(
'islandora:newspaperCModel',
'islandora:bookCModel',
'islandora:pageCModel',
'islandora:bookPageCModel',
'islandora:newspaperPageCModel',
'islandora:manuscriptCModel',
'islandora:manuscriptPageCModel',
);
if (array_intersect($to_derive, $object->models)) {
$check_function = array(
'PDF' => 'islandora_paged_content_can_create_pdf',
'OCR' => 'islandora_ocr_can_derive_ocr',
'HOCR' => 'islandora_ocr_can_derive_ocr',
'TN' => 'islandora_paged_content_can_create_images',
'JP2' => 'islandora_paged_content_can_create_images',
'JPG' => 'islandora_paged_content_can_create_images',
);
if (isset($check_function[$dsid])) {
if ($dsid == "OCR" || $dsid == 'HOCR') {
module_load_include('inc', 'islandora_ocr', 'includes/utilities');
}
$can_derive = function_exists($check_function[$dsid]) && $check_function[$dsid]();
}
$can_derive = $can_derive && islandora_paged_content_page_has_derivative_source($object, $dsid);
}
return $can_derive;
}
/**
* Checks if it is possible to create image files.
*
* @return bool
* TRUE if it is possible, FALSE otherwise.
*/
function islandora_paged_content_can_create_images() {
return module_exists('islandora_large_image');
}
/**
* Checks if it is possible to create PDF files with imagemagick.
*
* @return bool
* TRUE if it is possible, FALSE otherwise.
*/
function islandora_paged_content_can_create_pdf() {
// @todo Should also check if imagemagick is configured correctly.
return module_exists('imagemagick');
}
/**
* Checks if it is possible to combined PDF files with GhostScript.
*
* @return bool
* TRUE if it is possible, FALSE otherwise.
*/
function islandora_paged_content_can_combine_pdf() {
$gs = variable_get('islandora_paged_content_gs', '/usr/bin/gs');
return is_executable($gs);
}
/**
* Appends onto a given PDF file a number of PDF files.
*
* @param string $file
* The absolute path of the PDF file to append onto.
* @param array $files
* The PDF files to append.
*
* @return bool
* TRUE if successful, FALSE otherwise.
*/
function islandora_paged_content_pdf_append($file, array $files) {
$temp_file = "$file.temp.pdf";
copy($file, $temp_file);
array_unshift($files, $temp_file);
$ret = islandora_paged_content_pdf_combine($files, $file);
file_unmanaged_delete($temp_file);
return $ret;
}
/**
* Combines the given PDF files into one output file.
*
* @param array $files
* The PDF files to be combined in order.
* @param string $out
* The absolute path to the combined PDF file.
*
* @return bool
* TRUE on success, FALSE otherwise.
*/
function islandora_paged_content_pdf_combine(array $files, $out) {
$gs = variable_get('islandora_paged_content_gs', '/usr/bin/gs');
$files = implode(' ', $files);
$command = "{$gs} -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile={$out} {$files}";
$output = array(); $ret = 0;
exec($command, $output, $ret);
if ($ret != 0) {
$variables = array(
'@ret' => $ret,
'@command' => $command,
'!output' => implode('<br/>', $output),
);
watchdog('islandora_paged_content', 'GhostScript failed to combine PDFs.<br/>Error: @ret<br/>Command: @command <br/>Output !output', $variables, WATCHDOG_ERROR);
return FALSE;
}
return TRUE;
}
/**
* Appends a series of OCR files to a consolidated OCR file.
*
* @param string $consolidated_ocr
* The consolidated OCR path.
* @param array $files
* The files to be appended to the OCR.
*
* @return bool
* TRUE on success, FALSE on failure.
*/
function islandora_paged_content_ocr_append($consolidated_ocr, array $files) {
$temp_file = drupal_tempnam("temporary://", "$consolidated_ocr.temp.txt");
copy($consolidated_ocr, $temp_file);
array_unshift($files, $temp_file);
$ret = islandora_paged_content_ocr_combine($files, $consolidated_ocr);
file_unmanaged_delete($temp_file);
return $ret;
}
/**
* Combines the given OCR files into one output file.
*
* @param array $files
* The OCR files to be combined, in order.
* @param string $out
* The absolute path to the consolidated OCR file.
*
* @return bool
* TRUE on success, FALSE on fail.
*/
function islandora_paged_content_ocr_combine(array $files, $out) {
$success = TRUE;
foreach ($files as $file) {
$ocr = file_get_contents($file);
if ($ocr == FALSE) {
$success = FALSE;
}
else {
$success = file_put_contents($out, $ocr, FILE_APPEND) == FALSE ? FALSE : $success;
}
}
return $success;
}
/**
* Creates a PDF derivative for the given Page object.
*
* @param AbstractObject $object
* The page object that the derivative will be generated from, and added to.
* @param array $options
* The options to be passed to convert.
*
* @return bool
* TRUE if successful, FALSE otherwise.
*/
function islandora_paged_content_page_derive_pdf_datastream(AbstractObject $object, array $options = array('-compress' => 'LZW')) {
$pdf_file = islandora_paged_content_page_derive_pdf($object, $options);
if (!$pdf_file) {
return FALSE;
}
$ret = islandora_paged_content_update_datastream($object, $pdf_file, 'PDF');
file_unmanaged_delete($pdf_file);
return $ret;
}
/**
* Creates a PDF derivative from the given Page object and convert options.
*
* @param AbstractObject $object
* The page object that the derivative will be generated from.
* @param array $options
* The options to be passed to convert.
*
* @return string
* The absolute path to the derived file if successful, FALSE otherwise.
*/
function islandora_paged_content_page_derive_pdf(AbstractObject $object, array $options = array('-compress' => 'LZW')) {
if (!islandora_paged_content_can_derive($object, 'PDF')) {
return FALSE;
}
$source_file = islandora_paged_content_get_page_derivative_source($object, 'PDF');
$pdf_file = islandora_paged_content_convert_to_pdf($source_file, $options);
file_unmanaged_delete($source_file);
return $pdf_file;
}
/**
* Converts the given image to a PDF file, using the given options.
*
* @param string $image_file
* The absolute path to the image file.
* @param array $options
* The options to be passed to convert.
*
* @return string
* The absolute path to the generated PDF if successful, FALSE otherwise.
*/
function islandora_paged_content_convert_to_pdf($image_file, array $options) {
// Convert options into a string.
$to_string = function (&$o, $k) {
$o = "$k $o";
};
array_walk($options, $to_string);
$options = implode(' ', $options);
$pdf_file = "$image_file.pdf";
$command_args = "{$options} {$image_file} {$pdf_file}";
$output = array(); $ret = 0;
if (_imagemagick_convert_exec($command_args, $output, $ret) !== TRUE) {
$message = 'ImageMagick failed to create a PDF.<br/>Error: @ret<br/>Command: @command<br/>Output: !output';
$variables = array(
'@ret' => $ret,
'@command' => "convert $command_args",
'!output' => implode('<br/>', $output),
);
watchdog('islandora_paged_content', 'ImageMagick failed to create a PDF.<br/>Error: @ret<br/>Command: @command<br/>Output: !output', $variables, WATCHDOG_ERROR);
return FALSE;
}
return $pdf_file;
}
/**
* Creates and adds all the image derivatives for the given Page object.
*
* @param AbstractObject $object
* The page object that the derivative will be generated for.
*
* @return bool
* TRUE if successful, FALSE otherwise.
*/
function islandora_paged_content_page_derive_image_datastreams(AbstractObject $object) {
module_load_include('inc', 'islandora_large_image', 'includes/derivatives');
$ret = islandora_large_image_create_all_derivatives($object);
if ($ret) {
// Add dimensions for the JP2.
islandora_paged_content_add_dimensions_relationships($object);
}
return $ret;
}
/**
* Add the dimensions to the RELS-INT for the JP2.
*
* This makes the dimensions accessible when we query for the pages.
*
* @param AbstractObject $object
* An AbstractObject representing an object within Fedora.
* @param bool $force
* Whether we are forcing the creation of dimensions or not.
*/
function islandora_paged_content_add_dimensions_relationships(AbstractObject $object, $force = FALSE) {
$rels = $object['JP2']->relationships;
$width_rels = $rels->get(ISLANDORA_RELS_EXT_URI, 'width');
if ($force || empty($width_rels)) {
// XXX: ... seems a little odd to grab them from the OBJ/tiff, but they're
// more widely supported...
$temp_file = drupal_tempnam('temporary://', 'dimensions');
$object['OBJ']->getContent($temp_file);
list($width, $height) = getimagesize($temp_file);
islandora_paged_content_set_relationship($rels, ISLANDORA_RELS_EXT_URI,
'width', $width, 1
);
islandora_paged_content_set_relationship($rels, ISLANDORA_RELS_EXT_URI,
'height', $height, 1
);
drupal_unlink($temp_file);
}
}
/**
* Checks if it is possible to update a paged content's thumbnail image.
*/
function islandora_paged_content_can_update_paged_content_thumbnail(AbstractObject $object) {
$pages = islandora_paged_content_get_pages($object);
if (count($pages)) {
$page = array_shift($pages);
$page = islandora_object_load($page['pid']);
return isset($page['TN']);
}
return FALSE;
}
/**
* Updates the thumbnail from the first page in the paged content.
*
* @param AbstractObject $object
* The paged content object to update.
*
* @return bool
* TRUE on success, FALSE otherwise.
*/
function islandora_paged_content_update_paged_content_thumbnail(AbstractObject $object) {
module_load_include('inc', 'islandora', 'includes/utilities');
if (!islandora_paged_content_can_update_paged_content_thumbnail($object)) {
return FALSE;
}
$pages = islandora_paged_content_get_pages($object);
$page = array_shift($pages);
$page = islandora_object_load($page['pid']);
$mime_detector = new MimeDetect();
$ext = $mime_detector->getExtension($page['TN']->mimeType);
// Windows will likely store temp data in a temp directory
// rather than in memory. Since the colon is forbidden in
// filenames, replace it with an underscore instead.
$thumbnail_id = ((islandora_deployed_on_windows()) ? str_replace(':', '_', $page->id) : $page->id);
$file = drupal_realpath("temporary://{$thumbnail_id}_TN.{$ext}");
$page['TN']->getContent($file);
$ret = islandora_paged_content_update_datastream($object,
$file,
'TN',
'Thumbnail');
file_unmanaged_delete($file);
return $ret;
}
/**
* Creates OCR and HOCR derivatives and ingests them.
*
* @param AbstractObject $object
* The page object that the derivatives will be generated for.
* @param array $options
* The options for tesseract/gimp.
*
* @return bool
* TRUE on success or FALSE on failure.
*/
function islandora_paged_content_page_derive_ocr_datastreams(AbstractObject $object, array $options = NULL) {
$options = islandora_paged_content_apply_language_relationship_to_options($object, $options);
module_load_include('inc', 'islandora_ocr', 'includes/derivatives');
$ret = islandora_ocr_derive_datastreams($object['OBJ'], $options);
islandora_paged_content_set_relationship($object->relationships,
ISLANDORA_RELS_EXT_URI,
'hasLanguage',
$options['language'],
TRUE);
return $ret;
}
/**
* Creates OCR and HOCR derivatives and returns references to them.
*
* @param AbstractObject $object
* The page object that the derivatives will be generated for.
* @param array $options
* The options for tesseract/gimp.
*
* @return array
* An associative array containing 'OCR' and 'HOCR', each of which either
* contain the path to the generated output from tesseract, or FALSE on
* failure.
*/
function islandora_paged_content_page_generate_ocr_datastreams(AbstractObject $object, array $options = NULL) {
$options = islandora_paged_content_apply_language_relationship_to_options($object, $options);
module_load_include('inc', 'islandora_ocr', 'includes/derivatives');
$ret = islandora_ocr_generate_derivatives($object['OBJ'], $options);
islandora_paged_content_set_relationship($object->relationships,
ISLANDORA_RELS_EXT_URI,
'hasLanguage',
$options['language'],
TRUE);
return $ret;
}
/**
* Helper function to create generation options from a language relationship.
*
* @param AbstractObject $object
* The object to check relationships for.
* @param array|null $options
* The current tesseract/gimp generation options.
*
* @return array
* An array that at least contains a 'language' option matching the object's
* hasLanguage relationship, or 'eng' if there wasn't one.
*/
function islandora_paged_content_apply_language_relationship_to_options(AbstractObject $object, array $options = NULL) {
$options = (array) $options;
if (!isset($options['language'])) {
$options['language'] = islandora_paged_content_get_relationship($object->relationships,
ISLANDORA_RELS_EXT_URI,
'hasLanguage',
'eng');
}
return $options;
}
/**
* Fetches the given page's dimensions.
*
* Firsts attempts to get it from the FITS metadata datastream, if that fails
* it will use Djatoka.
*
* @param AbstractObject $object
* The page object.
*
* @return array
* An associative array containing the following fields.
* - width: The width of the image in pixels.
* - height: The width of the image in pixels.
* If this function should fail for some reason all the fields will be 0.
*/
function islandora_paged_content_get_page_dimensions(AbstractObject $object) {
$dimensions = array('width' => 0, 'height' => 0);
$metadata = islandora_paged_content_get_technical_metadata($object);
$metadata = $metadata ? $metadata : islandora_paged_content_get_page_metadata_from_djatoka($object->id);
return $metadata ? array_intersect_key($metadata, $dimensions) : $dimensions;
}
/**
* Fetches the given page's technical metadata.
*
* The technical metadata is pull from the techincal metadata datastream created
* by islandora_fits.
*
* @param AbstractObject $object
* The page object.
*
* @return array
* An associative array containing the following fields.
* - width: The width of the image in pixels.
* - height: The width of the image in pixels.
* If this function fails FALSE is returned.
*/
function islandora_paged_content_get_technical_metadata(AbstractObject $object) {
$dsid = variable_get('islandora_fits_techmd_dsid', 'TECHMD');
if (empty($object[$dsid])) {
return FALSE;
}
$metadata = FALSE;
$file = file_create_filename("{$object->id}_{$dsid}.xml", 'temporary://');
$file = drupal_realpath($file);
if (strpos($object[$dsid]->mimetype, 'xml') !== FALSE && $object[$dsid]->getContent($file)) {
$doc = simplexml_load_file($file);
$doc->registerXPathNamespace('ns', 'http://hul.harvard.edu/ois/xml/ns/fits/fits_output');
$width_xpath = $doc->xpath('/ns:fits/ns:metadata/ns:image/ns:imageWidth');
$width = (int) array_pop($width_xpath);
$height_xpath = $doc->xpath('/ns:fits/ns:metadata/ns:image/ns:imageHeight');
$height = (int) array_pop($height_xpath);
$metadata = array('width' => $width, 'height' => $height);
}
file_unmanaged_delete($file);
return $metadata;
}
/**
* Fetches the given page's metadata via Djatoka.
*
* @param string $object_id
* The PID of the page to fetch the metadata from.
*
* @return array
* An associative array contatining the following string fields:
* - identifier: The URL to the resource.
* - imagefile: The path to the temp file being served.
* - width: The width of the image in pixels.
* - height: The width of the image in pixels.
* - dwtLevels: ???
* - levels: ???
* - compositingLayerCount: ???
* If the request to Djatoka fails then FALSE is returned.
*/
function islandora_paged_content_get_page_metadata_from_djatoka($object_id) {
module_load_include('inc', 'islandora', 'includes/authtokens');
$datastream_url = url("islandora/object/{$object_id}/datastream/JP2/view", array(
'absolute' => TRUE,
'query' => array(
'token' => islandora_get_object_token($object_id, 'JP2'),
),
));
$djatoka_url = variable_get('islandora_paged_content_djatoka_url', 'http://localhost:8080/adore-djatoka');
$djatoka_url .= (substr($djatoka_url, -1) == '/') ? '' : '/';
$djatoka_url = url($djatoka_url . "resolver", array(
'query' => array(
'url_ver' => 'Z39.88-2004',
'rft_id' => $datastream_url,
'svc_id' => 'info:lanl-repo/svc/getMetadata',
),
'external' => TRUE,
));
$request = drupal_http_request($djatoka_url);
if ($request->code == '200') {
return drupal_json_decode($request->data);
}
return FALSE;
}
/**
* Returns paged object derivatives.
*
* @param string $context
* Indicator of which solution pack calling this function.
*
* @return array
* Structured array of paged object derivatives
*/
function islandora_paged_content_paged_object_derivatives($context) {
$derive = islandora_paged_content_get_enabled_ingest_derivatives($context);
$paged_content_module_path = drupal_get_path('module', 'islandora_paged_content');
$derivatives = array();
if ($derive['image']) {
$derivatives[] = array(
'source_dsid' => NULL,
'destination_dsid' => 'TN',
'function' => array('islandora_paged_content_derive_thumbnail_of_parent'),
'file' => "$paged_content_module_path/includes/derivatives.inc",
);
}
if ($derive['pdf']) {
$derivatives = array_merge(
$derivatives,
array(
array(
'source_dsid' => NULL,
'destination_dsid' => 'PDF',
'function' => array('islandora_paged_content_aggregate_pdf_derivative'),
'file' => "$paged_content_module_path/includes/derivatives.inc",
),
)
);
}
if ($derive['ocr']) {
$derivatives = array_merge(
$derivatives,
array(
array(
'source_dsid' => NULL,
'destination_dsid' => 'OCR',
'function' => array('islandora_paged_content_aggregate_ocr_derivative'),
'file' => "$paged_content_module_path/includes/derivatives.inc",
),
)
);
}
return $derivatives;
}
/**
* Returns page derivatives.
*
* @param string $context
* Indicator of which solution pack calling this function.
*
* @return array
* Structured array of page derivatives
*/
function islandora_paged_content_page_derivatives($context) {
$large_image_module_path = drupal_get_path('module', 'islandora_large_image');
$paged_content_module_path = drupal_get_path('module', 'islandora_paged_content');
$ocr_module_path = drupal_get_path('module', 'islandora_ocr');
$derive = islandora_paged_content_get_enabled_ingest_derivatives($context);
$derivatives = array();
if (isset($derive['pdf']) && $derive['pdf']) {
$derivatives[] = array(
'source_dsid' => 'OBJ',
'destination_dsid' => 'PDF',
'function' => array('islandora_paged_content_derivatives_pdf'),
'file' => "$paged_content_module_path/includes/derivatives.inc",
);
}
if (isset($derive['ocr']) && $derive['ocr']) {
$derivatives[] = array(
'source_dsid' => 'OBJ',
'destination_dsid' => 'OCR',
'weight' => 0,
'function' => array('islandora_ocr_derive_ocr'),
'file' => "$ocr_module_path/includes/derivatives.inc",
);
$derivatives[] = array(
'source_dsid' => 'OBJ',
'destination_dsid' => 'HOCR',
'weight' => 0,
'function' => array('islandora_ocr_derive_hocr'),
'file' => "$ocr_module_path/includes/derivatives.inc",
);
$derivatives[] = array(
'source_dsid' => 'OBJ',
'destination_dsid' => NULL,
'weight' => 1,
'function' => array('islandora_ocr_remove_generating_rels_ext_statements'),
'file' => "$ocr_module_path/includes/derivatives.inc",
);
}
if (isset($derive['image']) && $derive['image']) {
$derivatives[] = array(
'source_dsid' => 'OBJ',