forked from DGI-USC/usc_mirc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usc_mirc.module
951 lines (862 loc) · 32.3 KB
/
usc_mirc.module
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
<?php
/**
* autocomplete callback used to obtain streaming URLs.
*/
function usc_mirc_autocomplete_video_url($url_base, $filesystem_base, $string) {
module_load_include('inc', 'usc_mirc', 'usc_mirc.autocomplete');
$videos = _usc_mirc_traverse_directory(
$url_base, //variable_get('usc_mirc_streaming_url', 'http://dvr-streaming.mirc.sc.edu'),
$filesystem_base, //variable_get('usc_mirc_streaming_folder', '/mnt/mirc/mezz/access')
$string
);
$videos = array_filter($videos, '_usc_mirc_is_video_filename');
ksort($videos);
drupal_json($videos);
//header('Content-Type: text/javascript; charset=utf-8');
//echo json_encode($videos);
return;
}
/**
* implementation of islandora_content_model_forms' hook_islandora_content_model_forms_get_transforms()
*/
function usc_mirc_islandora_content_model_forms_get_transforms() {
$p = drupal_get_path('module', 'usc_mirc') . '/transforms';
return array(
"$p/eaccpf_to_dc.xsl" => "eaccpf_to_dc.xsl (usc_mirc)",
"$p/pbcore_to_dc.xsl" => "pbcore_to_dc.xsl (usc_mirc)"
);
}
/**
* implementation of hook_theme()
*/
function usc_mirc_theme($existing, $type, $theme, $path) {
return array(
'usc_mirc_item' => array( //@deprecated... But anyway.
'arguments' => array('element' => NULL),
'file' => 'usc_mirc.theme.inc',
),
);
}
/**
* implementation of hook_islandora_solr_primary_display()
*/
function usc_mirc_islandora_solr_primary_display() {
return array(
'usc_mirc_display' => array(
'name' => t('USC display'),
'module' => 'usc_mirc',
'file' => 'SolrResults.inc',
'class' => 'USC_Results',
'function' => 'displayResults',
'description' => t('More specific output.'),
),
);
}
/**
* implementation of hook_required_fedora_objects()
*
* Sets up objects in Fedora, in the manner of a solution pack.
*/
function usc_mirc_required_fedora_objects() {
$mod_path = drupal_get_path('module', 'usc_mirc') . '/fedora_objects';
return array(
'usc_mirc' => array(
'module' => 'usc_mirc',
'title' => t('USC Video Content Models'),
'objects' => array(
array(
'pid' => 'usc:collectionCModel',
'label' => 'Video Collection Content Model',
//'cmodel' => 'info:fedora/islandora:collectionCModel',
'cmodel' => 'info:fedora/fedora-system:ContentModel-3.0',
'parent' => 'info:fedora/islandora:ContentModelsCollection',
'datastreams' => array(
array(
'dsid' => 'ISLANDORACM',
'label' => 'Islandora content model',
'datastream_file' => "./$mod_path/cmodels/video_collection/ISLANDORACM.xml",
'dsversion' => TRUE,
),
),
),
array(
'pid' => 'usc:mirc',
'label' => 'Moving Images Research Collections',
//'cmodel' => 'info:fedora/usc:mezzanineRootCollectionCModel',
'cmodel' => 'info:fedora/islandora:collectionCModel',
'parent' => 'islandora:root',
'datastreams' => array(
array(
'dsid' => 'COLLECTION_POLICY',
'label' => 'Islandora Collection Policy',
'datastream_file' => "./$mod_path/collections/mezz_top/COLLECTION_POLICY.xml",
'dsversion' => TRUE,
),
array(
'dsid' => 'TN',
'label' => 'Thumbnail',
'datastream_file' => './' . drupal_get_path('module', 'fedora_repository') . '/images/Crystal_Clear_filesystem_folder_grey.png',
'mimetype' => 'image/png',
),
),
),
array(
'pid' => variable_get('usc_mirc_cmodel_vro', 'usc:test-vro'),
'label' => 'USC Video Resource Object Content Model',
'cmodel' => 'info:fedora/fedora-system:ContentModel-3.0',
'parent' => 'info:fedora/islandora:ContentModelsCollection',
'datastreams' => array(
array(
'dsid' => 'ISLANDORACM',
'label' => 'Islandora content model',
'datastream_file' => "./$mod_path/cmodels/vro/ISLANDORACM.xml",
'dsversion' => TRUE,
),
),
),
array(
'pid' => variable_get('usc_mirc_cmodel_mezzanine', 'usc:test-mezzanine'),
'label' => 'USC Mezzanine Object Content Model',
'cmodel' => 'info:fedora/fedora-system:ContentModel-3.0',
'parent' => 'info:fedora/islandora:ContentModelsCollection',
'datastreams' => array(
array(
'dsid' => 'ISLANDORACM',
'label' => 'Islandora content model',
'datastream_file' => "./$mod_path/cmodels/mezzanine/ISLANDORACM.xml",
'dsversion' => TRUE,
),
),
),
array(
'pid' => variable_get('usc_mirc_cmodel_access', 'usc:test-access'),
'label' => 'USC Access Object Content Model',
'cmodel' => 'info:fedora/fedora-system:ContentModel-3.0',
'parent' => 'info:fedora/islandora:ContentModelsCollection',
'datastreams' => array(
array(
'dsid' => 'ISLANDORACM',
'label' => 'Islandora content model',
'datastream_file' => "./$mod_path/cmodels/access/ISLANDORACM.xml",
'dsversion' => TRUE,
),
),
),
),
)
);
}
/**
* php function used in XPath...
* @deprecated No longer necessary
*/
function usc_mirc_create_person_string($name, $role) {
$output = $name;
if (!empty($output) && !empty($role)) {
$output .= " ($role)";
}
//dsm($output, 'output');
return new DOMText($output);
}
/**
* php function used in XPath...
* @deprecated No longer necessary
*/
function usc_mirc_create_date_string($start, $end) {
$output = '';
if (!empty($start) || !empty($end)) {
if (strcmp($start, $end) === 0) { //They're provided and the same...
$output = $start;
}
else { //At least a partial range
$output = "$start/$end";
}
}
return new DOMText($output);
}
/**
* implemenetation of hook_tabs_alter()
* Really, just try to select the proper tab.
*/
function usc_mirc_tabs_alter(&$element) {
//Switch to the Edit tab if necessary
if (!empty($element['edit_metadata']['content']['pid'])) {
foreach (element_children($element) as $child) {
if ($element[$child]['#type'] === 'tabpage') {
$element[$child]['#selected'] = FALSE;
}
}
$element['edit_metadata']['#selected'] = TRUE;
}
//Remove the DC section in 'Object Details' tab...
//XXX: Relies upon a slight modification in FedoraObjectDetailedContent, which splits up the dc, ds list and purge form into separate children.
$dc = $element['fedora_object_details']['tabset']['view']['dc'];
if (!empty($dc) && $dc['#type'] === 'markup') {
//unset($element['fedora_object_details']['tabset']['view']['dc']);
}
}
/**
* Duplication of drupal_get_form; however, we exit early (before rendering the
* markup) so other fun can happen. (Also skip a bit of batch stuff)
* "Fun stuff" such as being able to switch tabs based on the contents of the
* form... Might be useful to take this into core?
*/
function _usc_mirc_get_unrendered_form($form_id) {
$form_state = array(
'storage' => NULL,
'submitted' => FALSE,
);
$args = func_get_args();
$cacheable = FALSE;
// If the incoming $_POST contains a form_build_id, we'll check the
// cache for a copy of the form in question. If it's there, we don't
// have to rebuild the form to proceed. In addition, if there is stored
// form_state data from a previous step, we'll retrieve it so it can
// be passed on to the form processing code.
if (isset($_POST['form_id']) && $_POST['form_id'] == $form_id && !empty($_POST['form_build_id'])) {
$form = form_get_cache($_POST['form_build_id'], $form_state);
}
// If the previous bit of code didn't result in a populated $form
// object, we're hitting the form for the first time and we need
// to build it from scratch.
if (!isset($form)) {
$form_state['post'] = $_POST;
// Use a copy of the function's arguments for manipulation
$args_temp = $args;
$args_temp[0] = &$form_state;
array_unshift($args_temp, $form_id);
$form = call_user_func_array('drupal_retrieve_form', $args_temp);
$form_build_id = 'form-' . md5(uniqid(mt_rand(), TRUE));
$form['#build_id'] = $form_build_id;
drupal_prepare_form($form_id, $form, $form_state);
// Store a copy of the unprocessed form for caching and indicate that it
// is cacheable if #cache will be set.
$original_form = $form;
$cacheable = TRUE;
unset($form_state['post']);
}
$form['#post'] = $_POST;
// Now that we know we have a form, we'll process it (validating,
// submitting, and handling the results returned by its submission
// handlers. Submit handlers accumulate data in the form_state by
// altering the $form_state variable, which is passed into them by
// reference.
drupal_process_form($form_id, $form, $form_state);
if ($cacheable && !empty($form['#cache'])) {
// Caching is done past drupal_process_form so #process callbacks can
// set #cache.
form_set_cache($form_build_id, $original_form, $form_state);
}
// Most simple, single-step forms will be finished by this point --
// drupal_process_form() usually redirects to another page (or to
// a 'fresh' copy of the form) once processing is complete. If one
// of the form's handlers has set $form_state['redirect'] to FALSE,
// the form will simply be re-rendered with the values still in its
// fields.
//
// If $form_state['storage'] or $form_state['rebuild'] has been set
// and input has been processed, we know that we're in a complex
// multi-part process of some sort and the form's workflow is NOT
// complete. We need to construct a fresh copy of the form, passing
// in the latest $form_state in addition to any other variables passed
// into drupal_get_form().
if ((!empty($form_state['storage']) || !empty($form_state['rebuild'])) && !empty($form_state['process_input']) && !form_get_errors()) {
$form = drupal_rebuild_form($form_id, $form_state, $args);
}
return $form;
}
/**
* implementation of hook_form_FORM-ID_alter(), on 'fedora_repository_ingest_form'
*
* Adds in values for PID namespace and relationship to the parent object, in
* lieu of using a COLLECTION_POLICY in almost every object (since VROs are a
* 'collection' of mezzanine files, and mezzanines are a 'collection' of access
* variants).
*/
function usc_mirc_form_fedora_repository_ingest_form_alter(&$form, &$form_state) {
$cm = $form_state['storage']['content_model'];
if ($form_state['storage']['step'] === 1) {
if (in_array($cm, array(
variable_get('usc_mirc_cmodel_vro', 'usc:test-vro'),
variable_get('usc_mirc_cmodel_mezzanine', 'usc:test-mezzanine'),
variable_get('usc_mirc_cmodel_access', 'usc:test-access'),
))) {
$form_state['storage']['pid_namespace'] = variable_get('usc_mirc_namespace', 'usc');
}
if (in_array($cm, array(
variable_get('usc_mirc_cmodel_mezzanine', 'usc:test-mezzanine'),
variable_get('usc_mirc_cmodel_access', 'usc:test-access'),
))) {
$form_state['storage']['parent_relation'] = variable_get('usc_mirc_relationship', 'isDerivativeOf');
}
elseif (strcmp($cm, variable_get('usc_mirc_cmodel_vro', 'usc:test-vro')) === 0) {
$form_state['storage']['parent_relation'] = variable_get('usc_mirc_collection_relationship', 'isMemberOfCollection');
}
}
elseif ($form_state['storage']['step'] === 2) {
//Nothing...
//TODO: Create a CModel for the top-level collection...
if ($form_state['values']['models'] === 'usc:collectionCModel/ISLANDORACM') {
$form['module'] = array(
'#type' => 'hidden',
'#default_value' => 'fedora_repository',
);
$form['thumbnail_file'] = array(
'#type' => 'hidden',
'#default_value' => 'Crystal_Clear_filesystem_folder_grey.png'
);
$form['image_directory'] = array(
'#type' => 'hidden',
'#default_value' => 'images',
);
}
}
}
/*
function fedora_item_load($pid) {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new Fedora_Item($pid);
if ($item->exists()) {
return $item;
}
else {
drupal_not_found();
exit();
}
}
function usc_mirc_islandora_addable($item) {
}
function usc_mirc_islandora_cmodel_displays() {
return array(
'usc:test-vro' => array(
'file_type' => 'inc',
'module' => 'usc_mirc',
'file' => 'displays/usc_vro',
'class' => '',
'method' => '',
//'function' => '',
),
);
}
function usc_mirc_view($item, $dsid = NULL) {
$displays = module_invoke_all('islandora_cmodel_displays');
//get the cmodel
$model = 'usc:test-vro';
if ($dsid) {
return makeObject($item, $dsid);
}
elseif (array_key_exists($model, $displays)) {
$info = $displays[$model];
if ($info['file_type'] && $info['module'] && $info['file']) {
module_load_include($info['file_type'], $info['module'], $info['file']);
}
if ($info['class'] && $info['method']) {
$instance = new $info['class']($item);
return $instance->$info['method']($dsid);
}
else {
return $info['function']($item);
}
}
else {
return repository_page($item->pid);
}
}
function usc_mirc_front_page() {
drupal_goto('usc/' . variable_get('usc_mirc_mezzanine_collection', 'usc:mirc'));
}
function usc_mirc_get_item_title($item) {
return $item->objectProfile->objLabel;
}
*/
/**
* implementation of hook_menu
*
* Adds in autocomplete callback paths and the admin page for this module.
*/
function usc_mirc_menu() {
return array(
/*
'usc' => array(
'title' => 'Digital repo',
'page callback' => 'usc_mirc_front_page',
'type' => MENU_NORMAL_PAGE,
),
'usc/%fedora_item' => array(
'title callback' => 'usc_mirc_get_item_title',
'title arguments' => array(1),
'type' => MENU_CALLBACK,
),
'usc/%fedora_item/view' => array(
'title' => 'View',
'page callback' => 'usc_mirc_view',
'page arguments' => array(1, 3),
'access arguments' => array('view fedora collections'),
'type' => MENU_DEFAULT_LOCAL_TASK,
),
'usc/%fedora_item/add' => array(
'title' => 'Add',
'page callback' => 'usc_mirc_add'
'type' => MENU_LOCAL_TASK,
),
'usc/%fedora_item/edit' => array(
'type' => MENU_LOCAL_TASK,
),
'usc/%fedora_item/delete' => array(
'type' => MENU_LOCAL_TASK,
),*/
'usc/streaming_autocomplete' => array( //Autocomplete path for access copies
'page callback' => 'usc_mirc_autocomplete_video_url',
'page arguments' => array(
variable_get('usc_mirc_streaming_url', 'http://dvr-streaming.mirc.sc.edu'), //URL base
variable_get('usc_mirc_streaming_folder', '/mnt/mirc/mezz/access'), //Filesystem base
2, //String
),
'access arguments' => array('ingest new fedora objects'),
'type' => MENU_CALLBACK,
),
'usc/mezzanine_autocomplete' => array( //Autocomplete path for mezzanines.
'page callback' => 'usc_mirc_autocomplete_video_url',
'page arguments' => array(
variable_get('usc_mirc_mezzanine_url', '/mnt/mirc/mezz/mezzanine'), //URL base
variable_get('usc_mirc_mezzanine_folder', '/mnt/mirc/mezz/mezzanine'), //Filesystem base
2, //String
),
'access arguments' => array('ingest new fedora objects'),
'type' => MENU_CALLBACK,
),
'admin/settings/fedora_repository/usc_mirc' => array(
'title' => t('MIRC Islandora configuration'),
'description' => t('A number of configuration parameters used in the display of MIRC\'s video collections'),
'page callback' => 'drupal_get_form',
'page arguments' => array('usc_mirc_admin_form'),
'file' => 'usc_mirc.admin.inc',
'access arguments' => array('administer site configuration'),
'type' => MENU_LOCAL_TASK,
),
);
}
function usc_mirc_imagecache_default_presets() {
return array(
'usc_mirc_thumbnail' => array(
'presetname' => 'usc_mirc_thumbnail',
'actions' => array(
0 => array(
'weight' => 0,
'module' => 'imagecache',
'action' => 'imagecache_scale',
'data' => array(
'width' => 200,
'height' => 200,
'upscale' => TRUE,
),
),
),
),
'usc_mirc_solr_thumbnail' => array(
'presetname' => 'usc_mirc_solr_thumbnail',
'actions' => array(
0 => array(
'weight' => 0,
'module' => 'imagecache',
'action' => 'imagecache_scale',
'data' => array(
'width' => 100,
'height' => 100,
'upscale' => TRUE,
),
),
),
),
'featured_content_image' => array(
'presetname' => 'featured_content_image',
'actions' => array(
0 => array(
'weight' => 0,
'module' => 'imagecache',
'action' => 'imagecache_scale_and_crop',
'data' => array(
'width' => 350,
'height' => 260,
),
),
),
),
);
}
function _usc_mirc_get_mezzanine_types(&$form_state) {
if (module_exists('taxonomy')) {
$vocabs = taxonomy_get_vocabularies();
foreach($vocabs as $id => $voc) {
if ($voc->name == variable_get('usc_mirc_mezzanine_type_vocab', 'usc_mirc_mezzanine_type_vocab')) {
return _taxonomy_term_select(
t('Derivative type'),
NULL,
$form_state['values']['derivative_type'],
$id,
t('Whether this mezzanine is edited, or so on. (This list can be edited/expanded on via !link)', array('!link' => l(t('the type taxonomy vocab'), "admin/content/taxonomy/edit/vocabulary/$id"))),
FALSE,
NULL
);
}
}
}
return array(
'#type' => 'select',
'#title' => t('Derivative type'),
'#description' => t('Whether this mezzanine is edited, or so on.'),
'#options' => array( //TODO: Built list programatically... Allow user configurable values (changable in admin form?)
'original' => t('Original'),
'edited' => t('Edited'),
//'featured' => t('Featured'),
),
);
}
function _usc_mirc_get_mezzanine_formats(&$form_state) {
if (module_exists('taxonomy')) {
$vocabs = taxonomy_get_vocabularies();
foreach($vocabs as $id => $voc) {
if ($voc->name == variable_get('usc_mirc_mezzanine_format_vocab', 'usc_mirc_mezzanine_format_vocab')) {
return _taxonomy_term_select(
t('Derivative format'),
NULL,
$form_state['values']['derivative_format'],
$id,
t('Whether this mezzanine is edge-to-edge, cropped, or so on. (This list can be edited/expanded on via !link)', array('!link' => l(t('the format taxonomy vocab'), "admin/content/taxonomy/edit/vocabulary/$id"))),
FALSE,
NULL
);
}
}
}
return array(
'#type' => 'select',
'#title' => t('Derivative format'),
'#description' => t('Whether this mezzanine is edge-to-edge, cropped, or so on.'),
'#options' => array(
'edge-to-edge' => t('Edge-to-edge/Full frame (including perfs)'),
'cropped' => t('Cropped'),
),
'#default_value' => $form_state['values']['derivative_format'],
);
}
function usc_mirc_add_mezzanine_form(&$form_state, $parent_pid, $update_pid = FALSE) {
return array(
'update_pid' => array(
'#type' => 'hidden',
'#value' => $update_pid,
),
'parent_pid' => array(
'#type' => 'hidden',
'#value' => $parent_pid,
),
'file_name' => array(
'#type' => 'textfield',
'#title' => t('Mezzanine file path'),
'#description' => t('Used to extract technical metadata.'),
'#autocomplete_path' => 'usc/mezzanine_autocomplete',
'#weight' => -20,
'#default_value' => $form_state['values']['file_name'],
),
'derivative_type' => _usc_mirc_get_mezzanine_types($form_state),
'derivative_format' => _usc_mirc_get_mezzanine_formats($form_state),
'submit' => array(
'#type' => 'submit',
'#value' => t('Submit'),
),
);
}
function usc_mirc_add_mezzanine_form_validate($form, &$form_state) {
$values = $form_state['values'];
$file = $values['file_name'];
if (!(is_file($file) && is_readable($file))) {
form_set_error('file_name', t('The file %file_name (at %file_path) does not seem to exist or is not readable!', array(
'%file_path' => $file,
'%file_name' => $form['file_name']['#options'][$file],
)));
}
else {
$old_level = error_reporting(-1);
try {
//2. Grab media info output for the given file...
$command = 'mediainfo --Full --Output=XML ' . escapeshellarg($values['file_name']);
$descriptor = array(
1 => array('pipe', 'w'),
);
$pipes = array();
$command = escapeshellcmd($command);
$media_proc = proc_open($command, $descriptor, $pipes);
$mediainfo_output = '';
if (!is_resource($media_proc)) {
form_set_error('file_name', t('Error extracting metadata with Mediainfo!'));
throw new Exception('Get outta here!');
}
else {
$mediainfo_output = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$return_value = proc_close($media_proc);
}
$mediainfo_doc = DOMDocument::loadXML($mediainfo_output);
if (!$mediainfo_doc) {
form_set_error('', t('Mediainfo output failed to parse with DOMDocument!'));
throw new Exception('Get outta here!');
}
//3. Transform the mediainfo file to pbcore
$pbcore_transform = DOMDocument::load(drupal_get_path('module', 'usc_mirc') . '/transforms/mediainfo_to_pbcoreInstantiation.xsl');
if ($pbcore_transform === FALSE) {
watchdog('usc_mirc', 'Failed to load transform!', array(), WATCHDOG_ERROR);
throw new Exception('Get outta here!');
}
$transformer = new XSLTProcessor();
$transformer->importStylesheet($pbcore_transform);
$intermediate_pbcore = $transformer->transformToDoc($mediainfo_doc);
if (!$intermediate_pbcore) {
form_set_error('', t('Failed transforming Mediainfo XML to PBCore!'));
throw new Exception('Get outta here!');
}
unset($mediainfo_doc); //No longer need this (have the transformed version), free it up.
$pbcore_xpath = new DOMXPath($intermediate_pbcore);
$pbcore_doc = $pbcore_xpath->document;
$pbcore_xpath->registerNamespace('pb', 'http://www.pbcore.org/PBCore/PBCoreNamespace.html');
if (is_callable('taxonomy_get_term')) {
if ($f = taxonomy_get_term($values['derivative_format'])) {
$values['derivative_format'] = $f->name;
}
if ($t = taxonomy_get_term($values['derivative_type'])) {
$values['derivative_type'] = $t->name;
}
}
//4. Add the title, type and format fields
$title = _usc_mirc_get_title($values['parent_pid']);
//$title .= ' (' . $values['derivative_format'] . ', ' . $values['derivative_type'] . ')';
$form_state['storage']['usc_mirc']['title'] = $title;
$title_el = $pbcore_doc->createElement('instantiationAnnotation', $title);
$title_el->setAttribute('annotationType', 'instantiation_title');
foreach($pbcore_xpath->query('//pb:instantiationEssenceTrack[pb:essenceTrackType/text()="Video"]/pb:essenceTrackFrameSize') as $format_el) {
$format_el->setAttribute('annotation', $values['derivative_format']);
}
$type_el = $pbcore_doc->createElement('instantiationGenerations', $values['derivative_type']);
$doc_el = $pbcore_doc->documentElement;
$doc_el->appendChild($title_el);
$doc_el->appendChild($type_el);
$pbcore_doc->normalizeDocument();
$form_state['storage']['usc_mirc']['pbcore'] = $pbcore = $pbcore_doc->saveXML();
//5. Create DC transformation of PBCore.
$dc_transform = DOMDocument::load(drupal_get_path('module', 'usc_mirc') . '/transforms/pbcore_to_dc.xsl');
if (!$dc_transform) {
form_set_error('', t('Failed loading PBCore to DC transform!'));
throw new Exception('Get outta here!');
}
$transformer = new XSLTProcessor();
$transformer->importStyleSheet($dc_transform);
$pbcore_doc = DOMDocument::loadXML($pbcore); //XXX: This seems to be necessary for some reason... It seems to fail to get the newly added elements for the transform otherwise. DOMDocument, y u no work!?
$form_state['storage']['usc_mirc']['dc'] = $dc = $transformer->transformToXML($pbcore_doc);
if (!$dc) {
form_set_error('', t('Failed transforming PBCore XML to DC!'));
throw new Exception('Get outta here!');
}
unset($pbcore_doc); //No longer need this (have it in a string), free it up.
}
catch (Exception $e) {
//Do nothing...
}
error_reporting($old_level);
}
}
function _usc_mirc_get_title($pid) {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new Fedora_Item($pid);
return $item->objectProfile->objLabel;
}
function usc_mirc_add_mezzanine_form_submit($form, &$form_state) {
global $user;
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$values = $form_state['values'];
$pbcore = $form_state['storage']['usc_mirc']['pbcore'];
$dc = $form_state['storage']['usc_mirc']['dc'];
$title = $form_state['storage']['usc_mirc']['title'];
//6. If we've properly created the metadata, create the Fedora Object
if (!$values['update_pid']) {
$pid_namespace = variable_get('usc_mirc_namespace', 'usc');
$pid = Fedora_Item::get_next_PID_in_namespace($pid_namespace);
$new_item = Fedora_Item::ingest_new_item($pid, 'A', $title, $user->name);
//7. Relate the new object to it's parent
$new_item->add_relationship(variable_get('usc_mirc_relationship', 'isDerivativeOf'), $values['parent_pid']);
//8. Relate to the CModel.
$new_item->add_relationship('hasModel', variable_get('usc_mirc_cmodel_mezzanine', 'usc:test-mezzanine'), 'info:fedora/fedora-system:def/model#');
//9. Add the derived metadata.
$new_item->add_datastream_from_string($pbcore, 'PBCORE', 'PBCore Instantiation info', 'text/xml', 'X', 'Created via Islandora API');
$new_item->modify_datastream($dc, 'DC', 'Dublin Core Metadata', 'text/xml');
}
elseif (($item = new Fedora_Item($values['update_pid'])) && $item->exists()) {
$item->modify_object($title);
$item->modify_datastream($pbcore, 'PBCORE', 'PBCore Instantiation info', 'text/xml');
$item->modify_datastream($dc, 'DC', 'Dublin Core Metadata', 'text/xml');
}
}
/**
* Specific to mezzanines...
*/
function _usc_mirc_get_sub_collections($collection = NULL) {
if (empty($collection)) { //XXX: Make into configuration?
$collection = variable_get('usc_mirc_mezzanine_collection', variable_get('fedora_repository_pid', 'usc:mirc'));
}
//Get direct children of the collection (with their titles)
module_load_include('inc', 'usc_mirc', 'displays/base_cmodel');
$results = BaseContentModel::_perform_sparql_query(<<<EOQ
PREFIX fre: <info:fedora/fedora-system:def/relations-external#>
PREFIX fm: <info:fedora/fedora-system:def/model#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?subcollection ?title
FROM <#ri>
WHERE {
?subcollection fre:isMemberOfCollection <info:fedora/$collection> ;
fm:hasModel <info:fedora/usc:mezzanineCollectionCModel> ;
fm:state fm:Active ;
dc:title ?title .
}
EOQ
);
$options = array(
NULL => '(Remove collection association)',
);
foreach($results as $result) {
$options[$result['subcollection']] = $result['title'];
}
return $options;
}
/**
* Form for migrating a mezzanine to a new collection
*/
function usc_mirc_switch_mezzanine_collection_form(&$form_state, $object_pid) {
return array(
'object_pid' => array(
'#type' => 'hidden',
'#value' => $object_pid,
),
'collection' => array(
'#type' => 'select',
'#title' => t('New collection'),
'#description' => t('The collection to which this object should be moved.'),
'#options' => _usc_mirc_get_sub_collections(),
),
'submit' => array(
'#type' => 'submit',
'#value' => t('Move!')
)
);
}
/**
* Submission handler for migrating mezzanines.
* XXX: This probably won't be necessary, as the VRO will know what collection it belongs to...
*/
function usc_mirc_switch_mezzanine_collection_form_submit($form, &$form_state) {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$values = $form_state['values'];
$item = new Fedora_Item($values['object_pid']);
$item->purge_relationships('isMemberOfCollection', '');
if (!empty($values['collection'])) {
$item->add_relationship('isMemberOfCollection', $values['collection']);
}
}
/**
* form definition to create access variants.
*/
function usc_mirc_redirect_form(&$form_state, $parent_pid) {
return array(
'parent_pid' => array(
'#type' => 'hidden',
'#value' => $parent_pid,
),
'title' => array(
'#type' => 'textfield',
'#title' => t('Object label'),
'#required' => TRUE,
),
'streaming_url' => array(
'#type' => 'textfield',
'#title' => t('Streaming URL'),
'#required' => TRUE,
'#autocomplete_path' => 'usc/streaming_autocomplete',
),
'submit' => array(
'#type' => 'submit',
'#value' => 'Ingest',
)
);
}
/**
* Submission handler which creates a new object and adds a Redirect stream.
* Creates 'access' variants.
*/
function usc_mirc_redirect_form_submit($form, &$form_state) {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$values = $form_state['values'];
$pid_namespace = variable_get('usc_mirc_namespace', 'usc');
$pid = Fedora_Item::get_next_PID_in_namespace($pid_namespace);
global $user;
$new_item = Fedora_Item::ingest_new_item($pid, 'A', $values['title'], $user->name);
$new_item->add_datastream_from_url($values['streaming_url'], 'VIDEO', 'Video Redirect', 'video/mp4', 'R', 'Added from Islandora');
$new_item->add_relationship(variable_get('usc_mirc_relationship', 'isDerivativeOf'), $values['parent_pid']);
$new_item->add_relationship('fedora-model:hasModel', variable_get('usc_mirc_cmodel_access', 'usc:test-access'));
}
/**
* Get the instatiation to use to create a mezzanine from a preservation master.
* TODO: Finish implementing.
* @deprecated Don't think this is actually necessary.
*/
function _usc_mirc_get_instantiation_array() {
$toReturn = array(
'none' => t('')
);
//1. get the parent PBCore
//1.1 identify the parent(s)
module_load_include('inc', 'usc_mirc', 'mezzanine');
$parents = BaseVideoDisplay::_perform_sparql_query(<<<EOQ
PREFIX fre: <info:fedora/fedora-system:def/relations-external#>
PREFIX fm: <info:fedora/fedora-system:def/model#>
PREFIX fv: <info:fedora/fedora-system:def/view#>
SELECT ?parent
FROM <#ri>
WHERE {
?this fre:isDerivativeOf ?parent .
?parent fm:state fm:Active ;
fv:disseminates ?DSs .
?DSs fv:disseminationType <info:fedora/*/PBCORE> ;
fm:state fm:Active .
}
EOQ
);
$num_parents = count($parents);
$parent = NULL;
if ($num_parents > 1) {
$uri = $parents[0]['parent'];
$parent = substr($uri, strrpos($uri, '/'));
watchdog('usc_mirc', 'More than one parent!? Using first (%parent_pid)', array('%parent_pid' => $parent), WATCHDOG_WARNING);
}
elseif ($num_parents === 1) {
$uri = $parents[0]['parent'];
$parent = substr($uri, strrpos($uri, '/'));
}
$pbcore = DOMDocument::load(url("fedora/repository/$parent/PBCORE"));
if ($pbcore !== FALSE) {
$pbcore = new DOMXPath($pbcore);
//add each instantiation to the array to return.
$pbcore->registerNamespace('pb', 'http://www.pbcore.org/PBCore/PBCoreNamespace.html');
foreach ($pbcore->query('//pb:pbcoreInstantiation') as $node) {
$toReturn[] = 'asdf';
}
}
return $toReturn;
}
/**
* implementation of hook_perm()
*/
function usc_mirc_perm() {
return array(
'access preservation masters'
);
}
//function usc_mirc_islandora_tabs_alter(&$tabset, $parameters) {
//Find every pager href, and add in the fragment ID of the tab dif
//}