forked from Islandora/islandora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectHelper.inc
1026 lines (912 loc) · 38.3 KB
/
ObjectHelper.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
// $Id$
/*
* Created on Feb 1, 2008
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
class ObjectHelper {
//allowed operations
public static $OBJECT_HELPER_VIEW_FEDORA = 'view fedora collection';
public static $EDIT_FEDORA_METADATA = 'edit fedora meta data';
public static $PURGE_FEDORA_OBJECTSANDSTREAMS = 'purge objects and datastreams';
public static $ADD_FEDORA_STREAMS = 'add fedora datastreams';
public static $INGEST_FEDORA_OBJECTS = 'ingest new fedora objects';
public static $EDIT_TAGS_DATASTREAM = 'edit tags datastream';
public static $VIEW_DETAILED_CONTENT_LIST = 'view detailed list of content';
public static $DISPLAY_ALWAYS = 0;
public static $DISPLAY_NEVER = 1;
public static $DISPLAY_NO_MODEL_OUTPUT = 2;
// TODO: Make this into a static member constant
public $availableDataStreamsText = 'Detailed list of content';
function ObjectHelper() {
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
module_load_include('inc', 'fedora_repository', 'ConnectionHelper');
$connectionHelper = new ConnectionHelper();
//$this->fedoraUser = $connectionHelper->getUser();
//$this->fedoraPass = $connectionHelper->getPassword();
}
/**
* Grabs a stream from fedora sets the mimetype and returns it. $dsID is the
* datastream id.
* @param $pid String
* @param $dsID String
*/
function makeObject($pid, $dsID, $asAttachment = FALSE, $label = NULL, $filePath=FALSE, $version=NULL) {
global $user;
module_load_include('inc','fedora_repository','ContentModel');
if ($pid == NULL || $dsID == NULL) {
drupal_set_message(t("no pid or dsid given to create an object with"), 'error');
return ' ';
}
$headers = module_invoke_all('file_download', "/fedora/repository/$pid/$dsID");
if (in_array(-1, $headers)) {
drupal_set_message('hello');
drupal_access_denied();
return ' ';
}
if (!fedora_repository_access(OBJECTHELPER :: $OBJECT_HELPER_VIEW_FEDORA, $pid, $user)) {
drupal_set_message(t("You do not have access Fedora objects within the attempted namespace."), 'error');
drupal_access_denied();
return ' ';
}
if (($cm = ContentModel::loadFromObject($pid)) == FALSE) {
drupal_set_message(t("You do not have access to objects without an Islandora Content Model."), 'error');
drupal_access_denied();
return ' ';
}
$cmDatastreams = $cm->listDatastreams();
if ( !((isset($user) && in_array('administrator',$user->roles)) || in_array($dsID,$cmDatastreams))) {
drupal_set_message(t("You do not have access to the specified datastream."), 'error');
drupal_access_denied();
return ' ';
}
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new Fedora_Item($pid);
if (isset($item->datastreams[$dsID])) {
$mimeType=$item->datastreams[$dsID]['MIMEType'];
if ($label == NULL) {
$label = $item->datastreams[$dsID]['label'];
}
} else {
drupal_not_found();
exit();
}
if ((!isset($user)) || $user->uid == 0) {
$fedoraUser = 'anonymous';
$fedoraPass = 'anonymous';
$contentSize= 0;
} else {
$fedoraUser = $user->name;
$fedoraPass = $user->pass;
$dataStreamInfo = $item->get_datastream_info($dsID);
$contentSize = $dataStreamInfo->datastream->size;
}
if (function_exists("curl_init")) {
if (!isset($mimeType)) {
$pid = variable_get('fedora_default_display_pid', 'demo:10');
$dsID = variable_get('fedora_default_display_dsid', 'TN');
$mimeType = 'image/jpeg';
}
$url = variable_get('fedora_base_url', 'http://localhost:8080/fedora') . '/get/' . $pid . '/' . $dsID;
if ($version) {
$url .= '/' . $version; //drupal_urlencode($version);
}
$ch = curl_init();
$user_agent = "Mozilla/4.0 pp(compatible; MSIE 5.01; Windows NT 5.0)";
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
//curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$fedoraUser:$fedoraPass");
// There seems to be a bug in Fedora 3.1's REST authentication, removing this line fixes the authorization denied error.
// curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); // return into a variable
curl_setopt($ch, CURLOPT_URL, $url);
if ($filePath !== FALSE) {
$fp = fopen($filePath, 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
fclose($fp);
} else {
header("Content-type: $mimeType");
if ($contentSize > 0) {
header("Content-length: $contentSize");
}
if ($asAttachment) {
$suggestedFileName = "$label";
$pos = strpos($suggestedFileName, '.');
/**
* Here we used to take an object of, say, type application/pdf with label, say, "My Document"
* and we assemble the output filename extension based on the post-slash portion of the mimetype.
* (If the label has a period anywhere in it, we leave it alone.)
*
* This is great for simple mimetypes like application/pdf, text/html, image/jpeg, etc.
* but it's terrible for, say, application/vnd.oasis.opendocument.text (.odt).
*
* Instead we'll use the get_extension function in MimeClass.inc to discover a valid extension
* for the mimetype in question.
*/
if ($pos === FALSE) {
module_load_include('inc', 'fedora_repository', 'MimeClass');
$mimeclass = new MimeClass();
$ext = $mimeclass->get_extension($mimeType);
$suggestedFileName = "$label.$ext";
}
header('Content-Disposition: attachment; filename="' . $suggestedFileName . '"');
}
curl_exec($ch);
}
curl_close($ch);
} else {
drupal_set_message(t('No curl support.'), 'error');
}
}
//Gets collection objects t
function getCollectionInfo($pid, $query = NULL) {
module_load_include('inc', 'fedora_repository', 'CollectionClass');
$collectionClass = new CollectionClass();
$results = $collectionClass->getRelatedItems($pid, $query);
return $results;
}
/**
* returns the mime type
*/
function getMimeType($pid, $dsID) {
global $user;
if (empty($pid) || empty($dsID)) {
drupal_set_message(t('You must specify an object pid and datastream ID.'), 'error');
return '';
}
if (!fedora_repository_access(ObjectHelper :: $OBJECT_HELPER_VIEW_FEDORA, $pid, $user)) {
drupal_set_message(t('You do not have the appropriate permissions'), 'error');
return;
}
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new fedora_item($pid);
$datastream_list = $item->get_datastreams_list_as_SimpleXML();
if (!isset($datastream_list)) {
//drupal_set_message( t("No datastreams available."), 'status' );
return ' ';
}
foreach ($datastream_list as $datastream) {
foreach ($datastream as $datastreamValue) {
if ($datastreamValue->ID == $dsID) {
return $datastreamValue->MIMEType;
}
}
}
return '';
}
function getDatastreamInfo($pid, $dsID) {
global $user;
if (empty($pid) || empty($dsID)) {
drupal_set_message(t('You must specify an object pid and datastream ID.'), 'error');
return '';
}
if (!fedora_repository_access(ObjectHelper :: $OBJECT_HELPER_VIEW_FEDORA, $pid, $user)) {
drupal_set_message(t('You do not have the appropriate permissions'), 'error');
return;
}
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new fedora_item($pid);
$datastream_list = $item->get_datastreams_list_as_SimpleXML();
if (!isset($datastream_list)) {
//drupal_set_message( t("No datastreams available."), 'status' );
return ' ';
}
foreach ($datastream_list as $datastream) {
foreach ($datastream as $datastreamValue) {
if ($datastreamValue->ID == $dsID) {
return $datastreamValue;
}
}
}
return '';
}
/**
* internal function
* @param $pid String
* @param $dataStreamValue Object
*/
function create_link_for_ds($pid, $dataStreamValue) {
global $base_url;
$path = drupal_get_path('module', 'fedora_repository');
require_once($path . '/api/fedora_item.inc');
$item = new Fedora_Item($pid);
if (user_access(ObjectHelper :: $PURGE_FEDORA_OBJECTSANDSTREAMS)) {
$allow=TRUE;
if (module_exists('fedora_fesl')) {
$allow= fedora_fesl_check_roles($pid,'write');
}
if ($allow) {
$purgeImage = '<a title="purge datastream ' . $dataStreamValue->label . '" href="' . $base_url . '/fedora/repository/purgeStream/' .
$pid . '/' . $dataStreamValue->ID . '/' . $dataStreamValue->label . '"><img src="' . $base_url . '/' . $path .
'/images/purge.gif" alt="purge datastream" /></a>';
}
} else {
$purgeImage = ' ';
}
$fullPath = base_path() . $path;
// Add an icon to replace a datastream
// @TODO Note: using l(theme_image(..), ...); for these image links (and other links) may remove the need to have clean urls enabled.
$replaceImage= ' ';
if (user_access(ObjectHelper :: $ADD_FEDORA_STREAMS)) {
$allow=TRUE;
if (module_exists('fedora_fesl')) {
$allow= fedora_fesl_check_roles($pid,'write');
}
if ($allow) {
$replaceImage = '<a title="' . t("Replace datastream") . " " . $dataStreamValue->label . '" href="' . $base_url . '/fedora/repository/replaceStream/' . $pid . '/' . $dataStreamValue->ID . '/' . $dataStreamValue->label . '"><img src="' . $base_url . '/' . $path . '/images/replace.png" alt="replace datastream" /></a>';
}
}
$content = '';
$id = $dataStreamValue->ID;
$label = $dataStreamValue->label;
$label = str_replace("_", " ", $label);
$mimeType = $dataStreamValue->MIMEType;
$view = '<a href="' . $base_url . '/fedora/repository/' . drupal_urlencode($pid) . '/' . $id . '/' . drupal_urlencode($label) .
'" target="_blank" >' . t('View') . '</a>';
$action = "$base_url/fedora/repository/object_download/" . drupal_urlencode($pid) . '/' . $id . '/' . drupal_urlencode(preg_replace('/\//i', '${1}_', $label)); // Necessary to handle the case of Datastream labels that contain slashes. Ugh.
$downloadVersion = '<form method="GET" action="' . $action . '"><input type="submit" value="' . t('Download') . '"></form>';
if (user_access(ObjectHelper :: $EDIT_FEDORA_METADATA)) {
$versions = $item->get_datastream_history($id);
if (is_array($versions)) {
$downloadVersion = '<form method="GET" action="' . $action . '" onsubmit="this.action=\'' . $action . '\' + \'/\'+this.version.value;">';
$downloadVersion .= '<select name="version">';
foreach ($versions as $version) {
$downloadVersion .= '<option>' . $version->createDate . '</option>';
}
$downloadVersion .= '</select><input type="submit" value="' . t('Download') . '"></form>';
}
}
$content .= "<tr><td>$label</td><td> $view</td><td> $downloadVersion</td><td> $mimeType</td><td> $replaceImage $purgeImage</td></tr>\n";
//$content .= "<tr><td><b>Mime Type :</b></td><td>$mimeType</td></tr>\n";
return $content;
}
/**
* Queries fedora for what we call the qualified dublin core. Currently only dc.coverage has
* any qualified fields
* Transforms the returned xml to html
* This is the default metadata view. With icons for searching a dublin core field
* @param $pid String
* @return String
*/
function getQDC($pid) {
global $base_url;
$path = drupal_get_path('module', 'fedora_repository');
module_load_include('inc', 'fedora_repository', 'ConnectionHelper');
$soapHelper = new ConnectionHelper();
$client = $soapHelper->getSoapClient(variable_get('fedora_soap_url', 'http://localhost:8080/fedora/services/access?wsdl'));
$dsId = 'QDC';
$params = array(
'pid' => "$pid",
'dsID' => "$dsId",
'asOfDateTime' => ""
);
try {
$object = $client->__soapCAll('getDatastreamDissemination', array(
'parameters' => $params
));
} catch (Exception $e) {
try { //probably no QDC so we will try for the DC stream.
$dsId = 'DC';
$params = array(
'pid' => "$pid",
'dsID' => "$dsId",
'asOfDateTime' => ""
);
$object = $client->__soapCAll('getDatastreamDissemination', array(
'parameters' => $params
));
} catch (exception $e2) {
drupal_set_message($e2->getMessage(), 'error');
return;
}
}
$xmlstr = $object->dissemination->stream;
try {
$proc = new XsltProcessor();
} catch (Exception $e) {
drupal_set_message($e->getMessage(), 'error');
return;
}
$proc->setParameter('', 'baseUrl', $base_url);
$proc->setParameter('', 'path', $base_url . '/' . $path);
$input = NULL;
$xsl = new DomDocument();
try {
$xsl->load($path . '/xsl/convertQDC.xsl');
$input = new DomDocument();
$input->loadXML(trim($xmlstr));
} catch (exception $e) {
watchdog(t("Fedora_Repository"), t("Problem loading XSL file: !e", array('!e' => $e)), NULL, WATCHDOG_ERROR);
}
$xsl = $proc->importStylesheet($xsl);
$newdom = $proc->transformToDoc($input);
$output = $newdom->saveXML();
$baseUrl = base_path();
//$baseUrl=substr($baseUrl, 0, (strpos($baseUrl, "/")-1));
if (user_access(ObjectHelper :: $EDIT_FEDORA_METADATA)) {
$allow=TRUE;
if (module_exists('fedora_fesl')) {
$allow= fedora_fesl_check_roles($pid,'write');
}
if ($allow) {
$output .= '<br /><a title = "' . t('Edit Meta Data') . '" href="' . $base_url . '/fedora/repository/' . 'editmetadata/' . $pid . '/' .
$dsId . '"><img src="' . $base_url . '/' . $path . '/images/edit.gif" alt="' . t('Edit Meta Data') . '" /></a>';
}
}
return $output;
}
/**
* Gets a list of datastreams from an object using its pid
*
* We make some assumptions here. We have implemented a policy that
* we ingest in our repository will have TN (thumbnail) datastream. Even audio
* will have a picture of a speaker or something. This is not critical
* but makes searches etc. look better if there is a TN stream.
* This diplays all the streams in a collapsed fieldset at the bottom of the object page.
* you can implement a content model if you would like certain streams displayed in certain ways.
* @param $object_pid String
* @return String
*
*/
function get_formatted_datastream_list($object_pid, $contentModels, &$fedoraItem) {
global $fedoraUser, $fedoraPass, $base_url;
module_load_include('inc', 'fedora_repository', 'ConnectionHelper');
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$path = drupal_get_path('module', 'fedora_repository');
$dataStreamBody = '';
$fedoraItem = new Fedora_Item($object_pid);
if (user_access(ObjectHelper :: $VIEW_DETAILED_CONTENT_LIST)) {
$availableDataStreamsText = 'Detailed List of Content';
//$metaDataText='Description';
$mainStreamLabel = NULL;
$object = $fedoraItem->get_datastreams_list_as_SimpleXML();
if (!isset($object)) {
drupal_set_message(t("No datastreams available"));
return ' ';
}
$hasOBJStream = NULL;
$hasTNStream = FALSE;
$dataStreamBody = "<br /><table>\n";
$dataStreamBody .= $this->get_parent_objects_asHTML($object_pid);
$dataStreamBody .= '<tr><th colspan="4"><h3>' . t("!text", array('!text' => $availableDataStreamsText)) . '</h3></th></tr>';
foreach ($object as $datastream) {
foreach ($datastream as $datastreamValue) {
if ($datastreamValue->ID == 'OBJ') {
$hasOBJStream = '1';
$mainStreamLabel = $datastreamValue->label;
$mainStreamLabel = str_replace("_", " ", $mainStreamLabel);
}
if ($datastreamValue->ID == 'TN') {
$hasTNStream = TRUE;
}
//create the links to each datastream
$dataStreamBody .= $this->create_link_for_ds($object_pid, $datastreamValue); //"<tr><td><b>$key :</b></td><td>$value</td></tr>\n";
}
}
$dataStreamBody .= "</table>\n";
//if they have access let them add a datastream
if (user_access(ObjectHelper :: $ADD_FEDORA_STREAMS)) {
$allow=TRUE;
if (module_exists('fedora_fesl')) {
$allow= fedora_fesl_check_roles($object_pid,'write');
}
if ($allow) {
$dataStreamBody .= drupal_get_form('add_stream_form', $object_pid);
}
}
$fieldset = array(
'#title' => t("!text", array('!text' => $availableDataStreamsText)),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#value' => $dataStreamBody
);
$dataStreamBody = '<div class = "fedora_detailed_list">' . theme('fieldset', $fieldset) . '</div>';
}
$content = '';
if (empty($contentModels)) {
//only show this stuff if there are no contentmodels
if (isset($hasOBJStream)) {
$content .= '<a href="' . base_path() . 'fedora/repository/' . $object_pid . '/OBJ/' . $mainStreamLabel . '" target="_blank" >';
$content .= '<img src="' . base_path() . 'fedora/repository/' . $object_pid . '/TN" alt="Thumbnail"/>';
$content .= '</a>';
} else {
//don't use thumbnail as link, we don't know which datastream to link to
$content .= '<img src="' . base_path() . 'fedora/repository/' . $object_pid . '/TN" alt="' . $object_pid . '"/>';
}
}
foreach ($contentModels as $contentModel) {
$content .= $this->createExtraFieldsets($object_pid, $contentModel);
}
$content .= $dataStreamBody;
if (user_access(ObjectHelper :: $PURGE_FEDORA_OBJECTSANDSTREAMS)) {
$allow=TRUE;
if (module_exists('fedora_fesl')) {
$allow= fedora_fesl_check_roles($object_pid,'write');
}
if ($allow) {
$purgeObject = '<a title="' . t('Purge Object ') . $object_pid . '" href="' . base_path() . 'fedora/repository/purgeObject/' .
$object_pid . '"><img src="' . $base_url . '/' . $path . '/images/purge_big.png" alt="' . t('Purge Object') . '" class="icon">' . t('Purge Object') . '</a>';
}
} else {
$purgeObject = ' ';
}
$content .= $purgeObject;
$addObject = " ";
if (user_access(ObjectHelper :: $OBJECT_HELPER_VIEW_FEDORA)) {
$addObject = theme('add_to_basket_link', $object_pid);
}
$content .= $addObject;
return '<div class="fedora_datastream_content">' . $content . '</div>';
}
/**
* Queries a collection object for an xslt to format how the
* collection of objects is displayed.
*/
function getXslContent($pid, $path, $canUseDefault = TRUE) {
module_load_include('inc', 'fedora_repository', 'CollectionClass');
$collectionClass = new CollectionClass();
$xslContent = $collectionClass->getCollectionViewStream($pid);
if (!$xslContent && $canUseDefault) { //no xslt so we will use the default sent with the module
$xslContent = file_get_contents($path . '/xsl/sparql_to_html.xsl');
}
return $xslContent;
}
/**
* returns a stream from a fedora object given a pid and dsid
*
*/
function getStream($pid, $dsid, $showError = FALSE) {
module_load_include('inc', 'fedora_repository', 'ConnectionHelper');
$soapHelper = new ConnectionHelper();
try {
$client = $soapHelper->getSoapClient(variable_get('fedora_soap_url', 'http://localhost:8080/fedora/services/access?wsdl'));
$params = array(
'pid' => "$pid",
'dsID' => "$dsid",
'asOfDateTime' => ""
);
if (!isset($client)) {
drupal_set_message(t('Error connection to Fedora using soap client.'));
return NULL;
}
$object = $client->__soapCall('getDatastreamDissemination', array('parameters' => $params));
} catch (Exception $e) {
if ($showError) {
drupal_set_message(t('Error getting Datastream for %pid and %datastream<br />', array('%pid' => $pid, '%datastream' => $dsid)), 'error');
}
return NULL;
}
$content = $object->dissemination->stream;
$content = trim($content);
return $content;
}
/*
* gets the name of the content models for the specified object
* this now returns an array of pids as in Fedora 3 we can have more then one Cmodel for an object
*/
function get_content_models_list($pid, $include_fedora_system_content_models = FALSE) {
module_load_include('inc', 'fedora_repository', 'CollectionClass');
$collectionHelper = new CollectionClass();
$pids = array();
$query = 'select $object from <#ri>
where <info:fedora/' . $pid . '> <fedora-model:hasModel> $object
and $object <fedora-model:state> <info:fedora/fedora-system:def/model#Active>';
$content_models = $collectionHelper->getRelatedItems($pid, $query);
if (empty($content_models)) {
return $pids;
}
try {
$sxml = new SimpleXMLElement($content_models);
} catch (exception $e) {
watchdog(t("Fedora_Repository"), t("Could not find a parent object for %s", $pid), NULL, WATCHDOG_ERROR);
return $pids;
}
if (!isset($sxml)) {
return $pids;
}
foreach ($sxml->xpath('//@uri') as $uri) {
if (strpos($uri, 'fedora-system') != FALSE && $include_fedora_system_content_models == FALSE) {
continue;
}
$pids[] = substr(strstr($uri, '/'), 1);
}
return $pids;
}
/*
* determines whether we can see the object or not
*/
function fedora_repository_access($op, $pid) {
global $user;
$returnValue = FALSE;
if ($pid == NULL) {
$pid = variable_get('fedora_repository_pid', 'islandora:top');
}
$nameSpaceAllowed = explode(" ", variable_get('fedora_pids_allowed', 'default: demo: changeme: Islandora: ilives: '));
$pos = NULL;
foreach ($nameSpaceAllowed as $nameSpace) {
$pos = stripos($pid, $nameSpace);
if ($pos === 0) {
$returnValue = TRUE;
}
}
if ($returnValue) {
$user_access = user_access($op);
if ($user_access == NULL) {
return FALSE;
}
return $user_access;
} else {
return FALSE;
}
}
/**
* internal function
* uses an xsl to parse the sparql xml returned from the ITQL query
*
*
* @param $content String
*/
function parseContent($content, $pid, $dsId, $collection, $pageNumber = NULL) {
$path = drupal_get_path('module', 'fedora_repository');
global $base_url;
$collection_pid = $pid; //we will be changing the pid later maybe
//module_load_include('php', ''Fedora_Repository'', 'ObjectHelper');
$objectHelper = $this;
$parsedContent = NULL;
$contentModels = $objectHelper->get_content_models_list($pid);
$isCollection = FALSE;
//if this is a collection object store the $pid in the session as it will come in handy
//after a purge or ingest to return to the correct collection.
$fedoraItem = NULL;
$datastreams = $this->get_formatted_datastream_list($pid, $contentModels, $fedoraItem);
if (!empty($contentModels)) {
foreach ($contentModels as $contentModel) {
if ($contentModel == variable_get('fedora_collection_model_pid', 'islandora:collectionCModel')) {
$_SESSION['fedora_collection'] = $pid;
$isCollection = TRUE;
}
}
}
if ($fedoraItem !== NULL) {
$dslist = $fedoraItem->get_datastreams_list_as_array();
if (isset($dslist['COLLECTION_POLICY'])) {
$isCollection = TRUE;
}
}
//$label=$content;
$collectionName = $collection;
if (!$pageNumber) {
$pageNumber = 1;
}
if (!isset($collectionName)) {
$collectionName = variable_get('fedora_repository_name', 'Collection');
}
$xslContent = $this->getXslContent($pid, $path);
//get collection list and display using xslt-------------------------------------------
if (isset($content) && $content != FALSE) {
$input = new DomDocument();
$input->loadXML(trim($content));
$results = $input->getElementsByTagName('result');
if ($results->length > 0) {
try {
$proc = new XsltProcessor();
$proc->setParameter('', 'collectionPid', $collection_pid);
$proc->setParameter('', 'collectionTitle', $collectionName);
$proc->setParameter('', 'baseUrl', $base_url);
$proc->setParameter('', 'path', $base_url . '/' . $path);
$proc->setParameter('', 'hitPage', $pageNumber);
$proc->registerPHPFunctions();
$xsl = new DomDocument();
$xsl->loadXML($xslContent);
// php xsl does not seem to work with namespaces so removing it below
// I may have just been being stupid here
// $content = str_ireplace('xmlns="http://www.w3.org/2001/sw/DataAccess/rf1/result"', '', $content);
$xsl = $proc->importStylesheet($xsl);
$newdom = $proc->transformToDoc($input);
$objectList = $newdom->saveXML(); //is the xml transformed to html as defined in the xslt associated with the collection object
if (!$objectList) {
throw new Exception("Invalid XML.");
}
} catch (Exception $e) {
drupal_set_message(t('!e', array('!e' => $e->getMessage())), 'error');
return '';
}
}
} else {
drupal_set_message(t("No Objects in this collection or bad query."));
}
//--------------------------------------------------------------------------------
//show the collections datastreams
if ($results->length > 0 || $isCollection == TRUE) {
// if(strlen($objectList)>22||$contentModel=='Collection'||$contentModel=='Community')//length of empty dom still equals 22 because of <table/> etc
module_load_include('inc', 'Fedora_Repository', 'CollectionPolicy');
$collectionPolicyExists = $objectHelper->getMimeType($pid, CollectionPolicy::getDefaultDSID());
if (user_access(ObjectHelper :: $INGEST_FEDORA_OBJECTS) && $collectionPolicyExists) {
if (!empty($collectionPolicyExists)) {
$allow=TRUE;
if (module_exists('fedora_fesl')) {
$allow= fedora_fesl_check_roles($pid,'write');
}
if ($allow) {
// $ingestObject = '<a title="'. t('Ingest a New object into ') . $collectionName . ' '. $collection_pid . '" href="'. base_path() .
$ingestObject = '<a title="' . t('Ingest a New object into !collection_name PID !collection_pid', array('!collection_name' => $collectionName, '!collection_pid' => $collection_pid)) . '" href="' . base_path() .
'fedora/ingestObject/' . $collection_pid . '/' . $collectionName . '"><img src="' . $base_url . '/' . $path .
'/images/ingest.png" alt="' . t('Add a New Object') . '" class="icon">' . t('Add to this Collection') . '</a>';
}
}
}
else {
$ingestObject = ' ';
}
$datastreams .= $ingestObject;
$objectListOut = '';
if (isset($objectList)) {
$object_list_fieldset = array(
'#title' => t('Items in this collection'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#value' => (isset($objectList) ? $objectList : ''), //collection list
);
$objectListOut = theme('fieldset', $object_list_fieldset);
}
} else {
//$collectionName='';
$collection_fieldset = array(
'#title' => "",
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#value' => $datastreams,
);
$objectListOut = ''; //no collection objects to show so don't show field set
}
$output = '';
switch (variable_get('fedora_object_display_title', ObjectHelper::$DISPLAY_ALWAYS)) {
case ObjectHelper :: $DISPLAY_NEVER: break;
case ObjectHelper :: $DISPLAY_NO_MODEL_OUTPUT:
if (trim($datastreams) == '') {
$output .= '<strong>' . $collectionName . '</strong><br/>';
}
break;
case ObjectHelper :: $DISPLAY_ALWAYS:
default:
$output .= '<strong>' . $collectionName . '</strong><br/>';
break;
}
$output .= $datastreams;
$showDesc = FALSE;
switch (variable_get('fedora_object_display_description', ObjectHelper :: $DISPLAY_NO_MODEL_OUTPUT)) {
case ObjectHelper :: $DISPLAY_NEVER: break;
case ObjectHelper :: $DISPLAY_NO_MODEL_OUTPUT:
if (trim($datastreams) == '') {
$showDesc = TRUE;
}
break;
case ObjectHelper :: $DISPLAY_ALWAYS:
default:
$showDesc = TRUE;
break;
}
if ($showDesc) {
//just show default dc or qdc as we could not find a content model
$metaDataText = t('Description');
$body = $this->getQDC($pid);
$fieldset = array(
'#title' => t("!metaDataText", array('!metaDataText' => $metaDataText)),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#value' => $body
);
$output .= theme('fieldset', $fieldset);
}
switch (variable_get('fedora_collection_display_list', ObjectHelper::$DISPLAY_ALWAYS)) {
case ObjectHelper :: $DISPLAY_NEVER: break;
case ObjectHelper :: $DISPLAY_NO_MODEL_OUTPUT:
if (trim($datastreams) == '') {
$output .= $objectListOut . '<br/>';
}
break;
case ObjectHelper :: $DISPLAY_ALWAYS:
default:
$output .= $objectListOut . '<br/>';
break;
}
return $output;
}
/**
* Gets the parent objects that this object is related to
*
* @param unknown_type $pid
* @return unknown
*/
function get_parent_objects($pid) {
$query_string = 'select $object $title from <#ri>
where ($object <dc:title> $title
and <info:fedora/' . $pid . '> <fedora-rels-ext:isMemberOfCollection> $object
and $object <fedora-model:state> <info:fedora/fedora-system:def/model#Active>)
order by $title';
$objects = $this->getCollectionInfo($pid, $query_string);
return $objects;
}
function get_parent_objects_asHTML($pid) {
global $base_url;
$parent_collections = $this->get_parent_objects($pid);
try {
$parent_collections = new SimpleXMLElement($parent_collections);
} catch (exception $e) {
drupal_set_message(t('Error getting parent objects !e', array('!e' => $e->getMessage())));
return;
}
$parent_collections_HTML = '';
foreach ($parent_collections->results->result as $result) {
$collection_label = $result->title;
foreach ($result->object->attributes() as $a => $b) {
if ($a == 'uri') {
$uri = (string) $b;
$uri = $base_url . '/fedora/repository' . substr($uri, strpos($uri, '/')) . '/-/' . $collection_label;
}
}
$parent_collections_HTML .= '<a href="' . $uri . '">' . $collection_label . '</a><br />';
}
if (!empty($parent_collections_HTML)) {
$parent_collections_HTML = '<tr><td><h3>' . t("Belongs to these collections: ") . '</h3></td><td colspan="4">' . $parent_collections_HTML . '</td></tr>';
}
return $parent_collections_HTML;
}
/**
* gets a list of datastreams and related function that we should use to show datastreams in their own fieldsets
* from the content model associated with the object
*/
function createExtraFieldsets($pid, $contentModel) {
//$models = $collectionHelper->getContentModels($collectionPid, FALSE);
// possible problem in below if the collection policy has multiple content models
//with different pids but same dsid we could get wrong one, low probability and functionality
// will probably change for fedora version 3.
if (empty($contentModel)) {
return NULL;
}
$output = '';
module_load_include('inc', 'fedora_repository', 'ContentModel');
if (($cm = ContentModel :: loadFromModel($contentModel)) !== FALSE && $cm->validate()) {
$output .= $cm->displayExtraFieldset($pid);
}
return $output;
}
/**
* Look in the content model for rules to run on the specified datastream.
*
* @param string $pid
* @param string $dsid
* @return boolean
*/
function get_and_do_datastream_rules($pid, $dsid, $file = '') {
if (!user_access('ingest new fedora objects')) {
drupal_set_message(t('You do not have permission to add datastreams.'));
return FALSE;
}
module_load_include('inc', 'fedora_repository', 'ContentModel');
if ($dsid != NULL && $pid != NULL && ($cm = ContentModel::loadFromObject($pid)) !== FALSE) {
$cm->execAddDatastreamMethods($dsid, $file);
}
}
/**
* Get a tree of related pids - for the basket functionality
*/
function get_all_related_pids($pid) {
if (!$pid) {
return FALSE;
}
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
// Get title and descriptions for $pid
$query_string = 'select $title $desc from <#ri>
where $o <dc:title> $title
and $o <dc:description> $desc
and $o <mulgara:is> <info:fedora/' . $pid . '>';
$url = variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch');
$url .= "?type=tuples&flush=true&format=csv&limit=1000&lang=itql&stream=on&query=";
$content = do_curl($url . htmlentities(urlencode($query_string)));
$rows = explode("\n", $content);
$fields = explode(',', $rows[1]);
$pids[$pid] = array('title' => $fields[0], 'description' => $fields[1]);
// $pids += $this->get_child_pids(array($pid));
return $pids;
}
/**
* Get children of PID - but only 2 levels deep
*/
function get_child_pids($pids) {
// Get pid, title and description for children of object $pid
$query_string = 'select $o $title from <#ri> ' .
// $query_string = 'select $o $title $desc from <#ri> '.
'where $s <info:fedora/fedora-system:def/relations-external#hasMember> $o ' .
'and $o <dc:title> $title ' .
// 'and $o <dc:description> $desc '.
'and ( ';
foreach ($pids as $pid) {
$query_string .= '$s <mulgara:is> <info:fedora/' . $pid . '> or ';
}
$query_string = substr($query_string, 0, -3) . ' )';
$url = variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch');
$url .= "?type=tuples&flush=true&format=csv&limit=1000&lang=itql&stream=on&query=";
$url .= htmlentities(urlencode($query_string));
$content = $this->doCurl($url);
$rows = explode("\n", $content);
// Knock of the first heading row
array_shift($rows);
$child_pids = array();
if (count($rows)) {
// iterate through each row
foreach ($rows as $row) {
if ($row == "") {
continue;
}
$fields = explode(',', $row);
$child_pid = substr($fields[0], 12);
$child_pids[$child_pid] = array('title' => $fields[1], 'description' => $fields[2]);
}
if (!empty($child_pids)) {
$child_pids += $this->get_child_pids(array_keys($child_pids));
}
}
return $child_pids;
}
/**
* Returns XML description of the object (export).
*/
function getObject($pid, $context = 'archive', $format = FOXML_11) {
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
$url = variable_get('fedora_base_url', 'http://localhost:8080/fedora') . '/objects/' . $pid . '/export?context=' . $context . '&format=' . $format;
$result_data = do_curl($url);
return $result_data;
}
/**
* Builds an array of drupal links for use in breadcrumbs.
*/
function getBreadcrumbs($pid, &$breadcrumbs, $level=10) {
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
$query_string = 'select $parentObject $title $content from <#ri>
where (<info:fedora/' . $pid . '> <dc:title> $title
and $parentObject <fedora-model:hasModel> $content
and (<info:fedora/' . $pid . '> <fedora-rels-ext:isMemberOfCollection> $parentObject
or <info:fedora/' . $pid . '> <fedora-rels-ext:isMemberOf> $parentObject)
and $parentObject <fedora-model:state> <info:fedora/fedora-system:def/model#Active>)
minus $content <mulgara:is> <info:fedora/fedora-system:FedoraObject-3.0>
order by $title';
$query_string = htmlentities(urlencode($query_string));
$url = variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch');
$url .= "?type=tuples&flush=TRUE&format=CSV&limit=1&offset=0&lang=itql&stream=on&query=" . $query_string;
$result = preg_split('/[\r\n]+/',do_curl($url));
array_shift($result); // throw away first line
$matches =str_getcsv(join("\n",$result));
if ($matches !== NULL) {