-
Notifications
You must be signed in to change notification settings - Fork 3
/
deposit-functions.php
1355 lines (1248 loc) · 54.6 KB
/
deposit-functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
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
/**
* New deposit and edit deposit transaction related support functions.
*
* @package HumCORE
* @subpackage Deposits
*/
/**
* Prepare the metadata sent to Fedora and Solr from $_POST input.
*
* @param string $user deposit user
* @param array $curr_val array of $_POST entries.
* @return array metadata content
*/
function prepare_user_entered_metadata( $user, $curr_val ) {
/**
* Prepare the metadata to be sent to Fedora and Solr.
*/
$metadata = array();
$metadata['title'] = wp_strip_all_tags( stripslashes( $curr_val['deposit-title-unchanged'] ) );
$metadata['title_unchanged'] = wp_kses(
stripslashes( $curr_val['deposit-title-unchanged'] ),
array(
'b' => array(),
'em' => array(),
'strong' => array(),
)
);
$metadata['abstract'] = wp_strip_all_tags( stripslashes( $curr_val['deposit-abstract-unchanged'] ) );
$metadata['abstract_unchanged'] = wp_kses(
stripslashes( $curr_val['deposit-abstract-unchanged'] ),
array(
'b' => array(),
'em' => array(),
'strong' => array(),
)
);
$metadata['genre'] = sanitize_text_field( $curr_val['deposit-genre'] );
$metadata['deposit_for_others'] = sanitize_text_field( $curr_val['deposit-for-others-flag'] );
$metadata['committee_deposit'] = sanitize_text_field( $curr_val['deposit-on-behalf-flag'] );
if ( ! empty( $curr_val['deposit-committee'] ) ) {
$metadata['committee_id'] = sanitize_text_field( $curr_val['deposit-committee'] );
} else {
$metadata['committee_id'] = '';
}
/**
* Get committee or author metadata.
*/
if ( 'yes' === $metadata['committee_deposit'] ) {
$committee = groups_get_group( array( 'group_id' => $metadata['committee_id'] ) );
$metadata['organization'] = strtoupper( humcore_get_current_society_id() );
$metadata['authors'][] = array(
'fullname' => $committee->name,
'given' => '',
'family' => '',
'uni' => $committee->slug,
'role' => 'creator',
'affiliation' => strtoupper( humcore_get_current_society_id() ),
);
} elseif ( 'submitter' !== sanitize_text_field( $curr_val['deposit-author-role'] ) ) {
$user_id = $user->ID;
$user_firstname = get_the_author_meta( 'first_name', $user_id );
$user_lastname = get_the_author_meta( 'last_name', $user_id );
$user_affiliation = bp_get_profile_field_data(
array(
'field' => 'Institutional or Other Affiliation',
'user_id' => $user_id,
)
);
$metadata['organization'] = $user_affiliation;
$metadata['authors'][] = array(
'fullname' => $user->display_name,
'given' => $user_firstname,
'family' => $user_lastname,
'uni' => $user->user_login,
'role' => sanitize_text_field( $curr_val['deposit-author-role'] ),
'affiliation' => $user_affiliation,
);
}
if ( ( ! empty( $curr_val['deposit-other-authors-first-name'] ) && ! empty( $curr_val['deposit-other-authors-last-name'] ) ) ) {
$other_authors = array_map(
function ( $first_name, $last_name, $role ) {
return array(
'first_name' => sanitize_text_field( $first_name ),
'last_name' => sanitize_text_field( $last_name ),
'role' => sanitize_text_field( $role ),
); },
$curr_val['deposit-other-authors-first-name'], $curr_val['deposit-other-authors-last-name'], $curr_val['deposit-other-authors-role']
);
foreach ( $other_authors as $author_array ) {
if ( ! empty( $author_array['first_name'] ) && ! empty( $author_array['last_name'] ) ) {
$mla_user = bp_activity_find_mentions( $author_array['first_name'] . $author_array['last_name'] );
if ( ! empty( $mla_user ) ) {
foreach ( $mla_user as $mla_userid => $mla_username ) {
break;
} // Only one, right?
$author_name = bp_core_get_user_displayname( $mla_userid );
$author_firstname = get_the_author_meta( 'first_name', $mla_userid );
$author_lastname = get_the_author_meta( 'last_name', $mla_userid );
$author_affiliation = bp_get_profile_field_data(
array(
'field' => 'Institutional or Other Affiliation',
'user_id' => $mla_userid,
)
);
$author_uni = $mla_username;
} else {
$author_firstname = $author_array['first_name'];
$author_lastname = $author_array['last_name'];
$author_name = trim( $author_firstname . ' ' . $author_lastname );
$author_uni = '';
$author_affiliation = '';
}
$metadata['authors'][] = array(
'fullname' => $author_name,
'given' => $author_firstname,
'family' => $author_lastname,
'uni' => $author_uni,
'role' => $author_array['role'],
'affiliation' => $author_affiliation,
);
}
}
}
usort(
$metadata['authors'], function( $a, $b ) {
return strcasecmp( $a['family'], $b['family'] );
}
);
/**
* Format author info for solr.
*/
$metadata['author_info'] = humcore_deposits_format_author_info( $metadata['authors'] );
if ( ! empty( $metadata['genre'] ) && in_array( $metadata['genre'], array( 'Dissertation', 'Technical report', 'Thesis', 'White paper' ) ) &&
! empty( $curr_val['deposit-institution'] ) ) {
$metadata['institution'] = sanitize_text_field( $curr_val['deposit-institution'] );
} elseif ( ! empty( $metadata['genre'] ) && in_array( $metadata['genre'], array( 'Dissertation', 'Technical report', 'Thesis', 'White paper' ) ) &&
empty( $curr_val['deposit-institution'] ) ) {
$metadata['institution'] = $metadata['organization'];
}
if ( ! empty( $metadata['genre'] ) && ( 'Conference proceeding' == $metadata['genre'] || 'Conference paper' == $metadata['genre'] || 'Conference poster' == $metadata['genre'] ) ) {
$metadata['conference_title'] = sanitize_text_field( $curr_val['deposit-conference-title'] );
$metadata['conference_organization'] = sanitize_text_field( $curr_val['deposit-conference-organization'] );
$metadata['conference_location'] = sanitize_text_field( $curr_val['deposit-conference-location'] );
$metadata['conference_date'] = sanitize_text_field( $curr_val['deposit-conference-date'] );
}
if ( ! empty( $metadata['genre'] ) && 'Presentation' == $metadata['genre'] ) {
$metadata['meeting_title'] = sanitize_text_field( $curr_val['deposit-meeting-title'] );
$metadata['meeting_organization'] = sanitize_text_field( $curr_val['deposit-meeting-organization'] );
$metadata['meeting_location'] = sanitize_text_field( $curr_val['deposit-meeting-location'] );
$metadata['meeting_date'] = sanitize_text_field( $curr_val['deposit-meeting-date'] );
}
$metadata['group'] = array();
$deposit_groups = $curr_val['deposit-group'];
if ( ! empty( $deposit_groups ) ) {
foreach ( $deposit_groups as $group_id ) {
$group = groups_get_group( array( 'group_id' => sanitize_text_field( $group_id ) ) );
$metadata['group'][] = $group->name;
$metadata['group_ids'][] = $group_id;
}
}
$metadata['subject'] = array();
$deposit_subjects = $curr_val['deposit-subject'];
if ( ! empty( $deposit_subjects ) ) {
foreach ( $deposit_subjects as $subject ) {
$metadata['subject'][] = sanitize_text_field( stripslashes( $subject ) );
// Subject ids will be set later.
}
}
$metadata['keyword'] = array();
$deposit_keywords = $curr_val['deposit-keyword'];
if ( ! empty( $deposit_keywords ) ) {
foreach ( $deposit_keywords as $keyword ) {
$metadata['keyword'][] = sanitize_text_field( stripslashes( $keyword ) );
// Keyword ids will be set later.
}
}
$metadata['type_of_resource'] = sanitize_text_field( $curr_val['deposit-resource-type'] );
$metadata['language'] = sanitize_text_field( $curr_val['deposit-language'] );
$metadata['notes'] = sanitize_text_field( stripslashes( $curr_val['deposit-notes-unchanged'] ) ); // Where do they go in MODS?
$metadata['notes_unchanged'] = wp_kses(
stripslashes( $curr_val['deposit-notes-unchanged'] ),
array(
'b' => array(),
'em' => array(),
'strong' => array(),
)
);
$metadata['type_of_license'] = sanitize_text_field( $curr_val['deposit-license-type'] );
$metadata['published'] = sanitize_text_field( $curr_val['deposit-published'] ); // Not stored in solr.
if ( ! empty( $curr_val['deposit-publication-type'] ) ) {
$metadata['publication-type'] = sanitize_text_field( $curr_val['deposit-publication-type'] ); // Not stored in solr.
} else {
$metadata['publication-type'] = 'none';
}
if ( 'book' == $metadata['publication-type'] ) {
$metadata['publisher'] = sanitize_text_field( $curr_val['deposit-book-publisher'] );
$metadata['date'] = sanitize_text_field( $curr_val['deposit-book-publish-date'] );
if ( ! empty( $metadata['date'] ) ) {
$metadata['date_issued'] = get_year_issued( $metadata['date'] );
} else {
$metadata['date_issued'] = date( 'Y', strtotime( 'today' ) );
}
$metadata['edition'] = sanitize_text_field( $curr_val['deposit-book-edition'] );
$metadata['volume'] = sanitize_text_field( $curr_val['deposit-book-volume'] );
$metadata['isbn'] = sanitize_text_field( $curr_val['deposit-book-isbn'] );
$metadata['doi'] = sanitize_text_field( $curr_val['deposit-book-doi'] );
} elseif ( 'book-chapter' == $metadata['publication-type'] ) {
$metadata['publisher'] = sanitize_text_field( $curr_val['deposit-book-chapter-publisher'] );
$metadata['date'] = sanitize_text_field( $curr_val['deposit-book-chapter-publish-date'] );
if ( ! empty( $metadata['date'] ) ) {
$metadata['date_issued'] = get_year_issued( $metadata['date'] );
} else {
$metadata['date_issued'] = date( 'Y', strtotime( 'today' ) );
}
$metadata['book_journal_title'] = sanitize_text_field( $curr_val['deposit-book-chapter-title'] );
$metadata['book_author'] = sanitize_text_field( $curr_val['deposit-book-chapter-author'] );
$metadata['chapter'] = sanitize_text_field( $curr_val['deposit-book-chapter-chapter'] );
$metadata['start_page'] = sanitize_text_field( $curr_val['deposit-book-chapter-start-page'] );
$metadata['end_page'] = sanitize_text_field( $curr_val['deposit-book-chapter-end-page'] );
$metadata['isbn'] = sanitize_text_field( $curr_val['deposit-book-chapter-isbn'] );
$metadata['doi'] = sanitize_text_field( $curr_val['deposit-book-chapter-doi'] );
} elseif ( 'book-review' == $metadata['publication-type'] ) {
$metadata['publisher'] = sanitize_text_field( $curr_val['deposit-book-review-publisher'] );
$metadata['date'] = sanitize_text_field( $curr_val['deposit-book-review-publish-date'] );
if ( ! empty( $metadata['date'] ) ) {
$metadata['date_issued'] = get_year_issued( $metadata['date'] );
} else {
$metadata['date_issued'] = date( 'Y', strtotime( 'today' ) );
}
$metadata['doi'] = sanitize_text_field( $curr_val['deposit-book-review-doi'] );
} elseif ( 'book-section' == $metadata['publication-type'] ) {
$metadata['publisher'] = sanitize_text_field( $curr_val['deposit-book-section-publisher'] );
$metadata['date'] = sanitize_text_field( $curr_val['deposit-book-section-publish-date'] );
if ( ! empty( $metadata['date'] ) ) {
$metadata['date_issued'] = get_year_issued( $metadata['date'] );
} else {
$metadata['date_issued'] = date( 'Y', strtotime( 'today' ) );
}
$metadata['book_journal_title'] = sanitize_text_field( $curr_val['deposit-book-section-title'] );
$metadata['book_author'] = sanitize_text_field( $curr_val['deposit-book-section-author'] );
$metadata['edition'] = sanitize_text_field( $curr_val['deposit-book-section-edition'] );
$metadata['start_page'] = sanitize_text_field( $curr_val['deposit-book-section-start-page'] );
$metadata['end_page'] = sanitize_text_field( $curr_val['deposit-book-section-end-page'] );
$metadata['isbn'] = sanitize_text_field( $curr_val['deposit-book-section-isbn'] );
$metadata['doi'] = sanitize_text_field( $curr_val['deposit-book-section-doi'] );
} elseif ( 'journal-article' == $metadata['publication-type'] ) {
$metadata['publisher'] = sanitize_text_field( $curr_val['deposit-journal-publisher'] );
$metadata['date'] = sanitize_text_field( $curr_val['deposit-journal-publish-date'] );
if ( ! empty( $metadata['date'] ) ) {
$metadata['date_issued'] = get_year_issued( $metadata['date'] );
} else {
$metadata['date_issued'] = date( 'Y', strtotime( 'today' ) );
}
$metadata['book_journal_title'] = sanitize_text_field( $curr_val['deposit-journal-title'] );
$metadata['volume'] = sanitize_text_field( $curr_val['deposit-journal-volume'] );
$metadata['issue'] = sanitize_text_field( $curr_val['deposit-journal-issue'] );
$metadata['start_page'] = sanitize_text_field( $curr_val['deposit-journal-start-page'] );
$metadata['end_page'] = sanitize_text_field( $curr_val['deposit-journal-end-page'] );
$metadata['issn'] = sanitize_text_field( $curr_val['deposit-journal-issn'] );
$metadata['doi'] = sanitize_text_field( $curr_val['deposit-journal-doi'] );
} elseif ( 'magazine-section' == $metadata['publication-type'] ) {
$metadata['date'] = sanitize_text_field( $curr_val['deposit-magazine-section-publish-date'] );
if ( ! empty( $metadata['date'] ) ) {
$metadata['date_issued'] = get_year_issued( $metadata['date'] );
} else {
$metadata['date_issued'] = date( 'Y', strtotime( 'today' ) );
}
$metadata['book_journal_title'] = sanitize_text_field( $curr_val['deposit-magazine-section-title'] );
$metadata['volume'] = sanitize_text_field( $curr_val['deposit-magazine-section-volume'] );
$metadata['start_page'] = sanitize_text_field( $curr_val['deposit-magazine-section-start-page'] );
$metadata['end_page'] = sanitize_text_field( $curr_val['deposit-magazine-section-end-page'] );
$metadata['url'] = sanitize_text_field( $curr_val['deposit-magazine-section-url'] );
} elseif ( 'monograph' == $metadata['publication-type'] ) {
$metadata['publisher'] = sanitize_text_field( $curr_val['deposit-monograph-publisher'] );
$metadata['date'] = sanitize_text_field( $curr_val['deposit-monograph-publish-date'] );
if ( ! empty( $metadata['date'] ) ) {
$metadata['date_issued'] = get_year_issued( $metadata['date'] );
} else {
$metadata['date_issued'] = date( 'Y', strtotime( 'today' ) );
}
$metadata['isbn'] = sanitize_text_field( $curr_val['deposit-monograph-isbn'] );
$metadata['doi'] = sanitize_text_field( $curr_val['deposit-monograph-doi'] );
} elseif ( 'newspaper-article' == $metadata['publication-type'] ) {
$metadata['date'] = sanitize_text_field( $curr_val['deposit-newspaper-article-publish-date'] );
if ( ! empty( $metadata['date'] ) ) {
$metadata['date_issued'] = get_year_issued( $metadata['date'] );
} else {
$metadata['date_issued'] = date( 'Y', strtotime( 'today' ) );
}
$metadata['book_journal_title'] = sanitize_text_field( $curr_val['deposit-newspaper-article-title'] );
$metadata['edition'] = sanitize_text_field( $curr_val['deposit-newspaper-article-edition'] );
$metadata['volume'] = sanitize_text_field( $curr_val['deposit-newspaper-article-volume'] );
$metadata['start_page'] = sanitize_text_field( $curr_val['deposit-newspaper-article-start-page'] );
$metadata['end_page'] = sanitize_text_field( $curr_val['deposit-newspaper-article-end-page'] );
$metadata['url'] = sanitize_text_field( $curr_val['deposit-newspaper-article-url'] );
} elseif ( 'online-publication' == $metadata['publication-type'] ) {
$metadata['publisher'] = sanitize_text_field( $curr_val['deposit-online-publication-publisher'] );
$metadata['date'] = sanitize_text_field( $curr_val['deposit-online-publication-publish-date'] );
if ( ! empty( $metadata['date'] ) ) {
$metadata['date_issued'] = get_year_issued( $metadata['date'] );
} else {
$metadata['date_issued'] = date( 'Y', strtotime( 'today' ) );
}
$metadata['book_journal_title'] = sanitize_text_field( $curr_val['deposit-online-publication-title'] );
$metadata['edition'] = sanitize_text_field( $curr_val['deposit-online-publication-edition'] );
$metadata['volume'] = sanitize_text_field( $curr_val['deposit-online-publication-volume'] );
$metadata['url'] = sanitize_text_field( $curr_val['deposit-online-publication-url'] );
} elseif ( 'podcast' == $metadata['publication-type'] ) {
$metadata['publisher'] = sanitize_text_field( $curr_val['deposit-podcast-publisher'] );
$metadata['date'] = sanitize_text_field( $curr_val['deposit-podcast-publish-date'] );
if ( ! empty( $metadata['date'] ) ) {
$metadata['date_issued'] = get_year_issued( $metadata['date'] );
} else {
$metadata['date_issued'] = date( 'Y', strtotime( 'today' ) );
}
$metadata['volume'] = sanitize_text_field( $curr_val['deposit-podcast-volume'] );
$metadata['url'] = sanitize_text_field( $curr_val['deposit-podcast-url'] );
} elseif ( 'proceedings-article' == $metadata['publication-type'] ) {
$metadata['publisher'] = sanitize_text_field( $curr_val['deposit-proceeding-publisher'] );
$metadata['date'] = sanitize_text_field( $curr_val['deposit-proceeding-publish-date'] );
if ( ! empty( $metadata['date'] ) ) {
$metadata['date_issued'] = get_year_issued( $metadata['date'] );
} else {
$metadata['date_issued'] = date( 'Y', strtotime( 'today' ) );
}
$metadata['book_journal_title'] = sanitize_text_field( $curr_val['deposit-proceeding-title'] );
$metadata['start_page'] = sanitize_text_field( $curr_val['deposit-proceeding-start-page'] );
$metadata['end_page'] = sanitize_text_field( $curr_val['deposit-proceeding-end-page'] );
$metadata['doi'] = sanitize_text_field( $curr_val['deposit-proceeding-doi'] );
} elseif ( 'none' == $metadata['publication-type'] ) {
$metadata['publisher'] = '';
$metadata['date'] = sanitize_text_field( $curr_val['deposit-non-published-date'] );
if ( ! empty( $metadata['date'] ) ) {
$metadata['date_issued'] = get_year_issued( $metadata['date'] );
} else {
$metadata['date_issued'] = date( 'Y', strtotime( 'today' ) );
}
}
$metadata['embargoed'] = sanitize_text_field( $curr_val['deposit-embargoed-flag'] );
if ( 'yes' === $metadata['embargoed'] ) {
$metadata['embargo_end_date'] = date( 'm/d/Y', strtotime( '+' . sanitize_text_field( $curr_val['deposit-embargo-length'] ) ) );
}
return $metadata;
}
/**
* Get the year from the date entered.
*
* @param string $date Date entered
* @return string Date in YYYY format
*/
function get_year_issued( $date_entered ) {
// The strtotime function will handle a wide variety of entries. First address some cases it will not handle.
$temp_date_entered = preg_replace(
'~^(winter(?:/|)|spring(?:/|)|summer(?:/|)|fall(?:/|)|autumn(?:/|))+\s(\d{4})$~i',
'Jan $2',
$date_entered
); // Custom publication date format.
$temp_date_entered = preg_replace(
'/^(\d{4})$/',
'Jan $1',
$temp_date_entered
); // Workaround for when only YYYY is entered.
$ambiguous_date = preg_match( '~^(\d{2})-(\d{2})-(\d{2}(?:\d{2})?)(?:\s.*?|)$~', $temp_date_entered, $matches );
if ( 1 === $ambiguous_date ) { // Just deal with slashes.
$temp_date_entered = sprintf( '%1$s/%2$s/%3$s', $matches[1], $matches[2], $matches[3] );
}
$ambiguous_date = preg_match( '~^(\d{2})/(\d{2})/(\d{2}(?:\d{2})?)(?:\s.*?|)$~', $temp_date_entered, $matches );
if ( 1 === $ambiguous_date && $matches[1] > 12 ) { // European date in d/m/y format will fail for dd > 12.
$temp_date_entered = sprintf( '%1$s/%2$s/%3$s', $matches[2], $matches[1], $matches[3] );
}
$date_value = strtotime( $temp_date_entered );
if ( false === $date_value ) {
return date( 'Y', strtotime( 'today' ) ); //TODO Real date edit message, kick back to user to fix. Meanwhile, this year is better than nothing.
}
return date( 'Y', $date_value );
}
/**
* Format the xml used to create the DC datastream for the aggregator object.
*
* @param array $args Array of arguments.
* @return WP_Error|string xml content
* @see wp_parse_args()
*/
function create_aggregator_xml( $args ) {
$defaults = array(
'pid' => '',
'creator' => 'HumCORE',
'title' => 'Generic Content Aggregator',
'type' => 'InteractiveResource',
);
$params = wp_parse_args( $args, $defaults );
$pid = $params['pid'];
$creator = $params['creator'];
$title = $params['title'];
$type = $params['type'];
if ( empty( $pid ) ) {
return new WP_Error( 'missingArg', 'PID is missing.' );
}
return '<oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
<dc:identifier>' . $pid . '</dc:identifier>
<dc:creator>' . $creator . '</dc:creator>
<dc:title>' . $title . '</dc:title>
<dc:type>' . $type . '</dc:type>
</oai_dc:dc>';
}
/**
* Format the rdf used to create the RELS-EXT datastream for the aggregator object.
*
* @param array $args Array of arguments.
* @return WP_Error|string rdf content
* @see wp_parse_args()
*/
function create_aggregator_rdf( $args ) {
$defaults = array(
'pid' => '',
'collectionPid' => '',
'isCollection' => false,
'fedoraModel' => 'ContentAggregator',
);
$params = wp_parse_args( $args, $defaults );
$pid = $params['pid'];
$collection_pid = $params['collectionPid'];
$is_collection = $params['isCollection'];
$fedora_model = $params['fedoraModel'];
if ( empty( $pid ) ) {
return new WP_Error( 'missingArg', 'PID is missing.' );
}
$member_of_markup = '';
if ( ! empty( $collection_pid ) ) {
$member_of_markup = sprintf( '<pcdm:memberOf rdf:resource="info:fedora/%1$s"></pcdm:memberOf>', $collection_pid );
}
$is_collection_markup = '';
if ( $is_collection ) {
$is_collection_markup = '<isCollection xmlns="info:fedora/fedora-system:def/relations-external#">true</isCollection>';
}
return '<rdf:RDF xmlns:fedora-model="info:fedora/fedora-system:def/model#"
xmlns:ore="http://www.openarchives.org/ore/terms/"
xmlns:pcdm="http://pcdm.org/models#"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="info:fedora/' . $pid . '">
<fedora-model:hasModel rdf:resource="info:fedora/ldpd:' . $fedora_model . '"></fedora-model:hasModel>
<rdf:type rdf:resource="http://pcdm.org/models#Object"></rdf:type>
' . $is_collection_markup . '
' . $member_of_markup . '
<cc:license rdf:resource="info:fedora/"></cc:license>
</rdf:Description>
</rdf:RDF>';
}
/**
* Format the xml used to create the DC datastream for the resource object.
*
* @param array $args Array of arguments.
* @return WP_Error|string xml content
* @see wp_parse_args()
*/
function create_resource_xml( $metadata, $filetype = '' ) {
if ( empty( $metadata ) ) {
return new WP_Error( 'missingArg', 'metadata is missing.' );
}
$pid = $metadata['pid'];
if ( empty( $pid ) ) {
return new WP_Error( 'missingArg', 'PID is missing.' );
}
$title = humcore_cleanup_utf8( htmlspecialchars( $metadata['title'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', false ) );
$type = humcore_cleanup_utf8( htmlspecialchars( $metadata['genre'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', false ) );
$description = humcore_cleanup_utf8( htmlspecialchars( $metadata['abstract'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', false ) );
$creator_list = '';
foreach ( $metadata['authors'] as $author ) {
if ( ( in_array( $author['role'], array( 'creator', 'author' ) ) ) && ! empty( $author['fullname'] ) ) {
$creator_list .= '
<dc:creator>' . $author['fullname'] . '</dc:creator>';
}
}
$subject_list = '';
foreach ( $metadata['subject'] as $subject ) {
$subject_list .= '
<dc:subject>' . humcore_cleanup_utf8( htmlspecialchars( $subject, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', false ) ) . '</dc:subject>';
}
if ( ! empty( $metadata['publisher'] ) ) {
$publisher = '<dc:publisher>' . humcore_cleanup_utf8( htmlspecialchars( $metadata['publisher'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', false ) ) . '</dc:publisher>';
} else {
$publisher = '';
}
if ( ! empty( $metadata['date_issued'] ) ) {
$date = '
<dc:date encoding="w3cdtf">' . $metadata['date_issued'] . '</dc:date>';
} else {
$date = '';
}
return '<oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
<dc:identifier>' . $pid . '</dc:identifier>
' . $creator_list . '
' . $date . '
<dc:title>' . $title . '</dc:title>
<dc:description>' . $description . '</dc:description>
' . $subject_list . '
' . $publisher . '
<dc:type>' . $type . '</dc:type>
<dc:format>' . $filetype . '</dc:format>
</oai_dc:dc>';
}
/**
* Format the rdf used to create the RELS-EXT datastream for the aggregator object.
*
* @param array $args Array of arguments.
* @return WP_Error|string rdf content
* @see wp_parse_args()
*/
function create_resource_rdf( $args ) {
$defaults = array(
'aggregatorPid' => '',
'resourcePid' => '',
'collectionPid' => '',
);
$params = wp_parse_args( $args, $defaults );
$aggregator_pid = $params['aggregatorPid'];
$resource_pid = $params['resourcePid'];
$collection_pid = $params['collectionPid'];
$collection_markup = '';
if ( empty( $aggregator_pid ) ) {
return new WP_Error( 'missingArg', 'PID is missing.' );
}
if ( empty( $resource_pid ) ) {
return new WP_Error( 'missingArg', 'PID is missing.' );
}
if ( ! empty( $collection_pid ) ) {
$collection_markup = sprintf( '<pcdm:memberOf rdf:resource="info:fedora/%1$s"></pcdm:memberOf>', $collection_pid );
}
return '<rdf:RDF xmlns:fedora-model="info:fedora/fedora-system:def/model#"
xmlns:dcmi="http://purl.org/dc/terms/"
xmlns:pcdm="http://pcdm.org/models#"
xmlns:rel="info:fedora/fedora-system:def/relations-external#"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="info:fedora/' . $resource_pid . '">
<fedora-model:hasModel rdf:resource="info:fedora/ldpd:Resource"></fedora-model:hasModel>
<rdf:type rdf:resource="http://pcdm.org/models#File"></rdf:type>
<pcdm:memberOf rdf:resource="info:fedora/' . $aggregator_pid . '"></pcdm:memberOf>
' . $collection_markup . '
<cc:license rdf:resource="info:fedora/"></cc:license>
</rdf:Description>
</rdf:RDF>';
}
/**
* Format the foxml used to create Fedora aggregator and resource objects.
*
* @param array $args Array of arguments.
* @return WP_Error|string foxml content
* @see wp_parse_args()
*/
function create_foxml( $args ) {
$defaults = array(
'pid' => '',
'label' => '',
'xmlContent' => '',
'state' => 'Active',
'rdfContent' => '',
);
$params = wp_parse_args( $args, $defaults );
$pid = $params['pid'];
$label = $params['label'];
$xml_content = $params['xmlContent'];
$state = $params['state'];
$rdf_content = $params['rdfContent'];
if ( empty( $pid ) ) {
return new WP_Error( 'missingArg', 'PID is missing.' );
}
if ( empty( $xml_content ) ) {
return new WP_Error( 'missingArg', 'XML string is missing.' );
}
if ( empty( $rdf_content ) ) {
return new WP_Error( 'missingArg', 'RDF string is missing.' );
}
$output = '<?xml version="1.0" encoding="UTF-8"?>
<foxml:digitalObject VERSION="1.1" PID="' . $pid . '"
xmlns:foxml="info:fedora/fedora-system:def/foxml#"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="info:fedora/fedora-system:def/foxml# http://www.fedora.info/definitions/1/0/foxml1-1.xsd">
<foxml:objectProperties>
<foxml:property NAME="info:fedora/fedora-system:def/model#state" VALUE="' . $state . '"/>
<foxml:property NAME="info:fedora/fedora-system:def/model#label" VALUE="' . $label . '"/>
</foxml:objectProperties>
<foxml:datastream ID="DC" STATE="A" CONTROL_GROUP="X" VERSIONABLE="true">
<foxml:datastreamVersion ID="DC1.0" LABEL="Dublin Core Record for this object"
CREATED="' . gmdate( 'Y-m-d\TH:i:s\Z' ) . '" MIMETYPE="text/xml"
FORMAT_URI="http://www.openarchives.org/OAI/2.0/oai_dc/" SIZE="' . strlen( $xml_content ) . '">
<foxml:xmlContent>' . $xml_content . '</foxml:xmlContent>
</foxml:datastreamVersion>
</foxml:datastream>
<foxml:datastream ID="RELS-EXT" STATE="A" CONTROL_GROUP="X" VERSIONABLE="true">
<foxml:datastreamVersion ID="RELS-EXT1.0" LABEL="RDF Statements about this object"
CREATED="' . gmdate( 'Y-m-d\TH:i:s\Z' ) . '" MIMETYPE="application/rdf+xml"
FORMAT_URI="info:fedora/fedora-system:FedoraRELSExt-1.0" SIZE="' . strlen( $rdf_content ) . '">
<foxml:xmlContent>' . $rdf_content . '</foxml:xmlContent>
</foxml:datastreamVersion>
</foxml:datastream>
</foxml:digitalObject>';
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false; // @codingStandardsIgnoreLine camelCase
if ( false === $dom->loadXML( $output ) ) {
humcore_write_error_log( 'error', '*****HumCORE Error - bad xml content*****' . var_export( $pid, true ) );
}
$dom->formatOutput = true; // @codingStandardsIgnoreLine camelCase
return $dom->saveXML();
}
/**
* Format the xml used to create the CONTENT datastream for the MODS metadata object.
*
* @param array $metadata
* @return WP_Error|string mods xml content
*/
function create_mods_xml( $metadata ) {
/**
* Format MODS xml fragment for one or more authors.
*/
$author_mods = '';
foreach ( $metadata['authors'] as $author ) {
if ( in_array( $author['role'], array( 'creator', 'author' ) ) ) {
if ( 'creator' === $author['role'] ) {
$author_mods .= '
<name type="corporate">';
} else {
if ( ! empty( $author['uni'] ) ) {
$author_mods .= '
<name type="personal" ID="' . $author['uni'] . '">';
} else {
$author_mods .= '
<name type="personal">';
}
}
if ( ( 'creator' !== $author['role'] ) && ( ! empty( $author['family'] ) || ! empty( $author['given'] ) ) ) {
$author_mods .= '
<namePart type="family">' . $author['family'] . '</namePart>
<namePart type="given">' . $author['given'] . '</namePart>';
// } else if ( 'creator' !== $author['role'] ) {
} else {
$author_mods .= '
<namePart>' . $author['fullname'] . '</namePart>';
}
if ( 'creator' === $author['role'] ) {
$author_mods .= '
<role>
<roleTerm type="text">creator</roleTerm>
</role>';
} else {
$author_mods .= '
<role>
<roleTerm type="text">' . $author['role'] . '</roleTerm>
</role>';
}
if ( ! empty( $author['affiliation'] ) ) {
$author_mods .= '
<affiliation>' . htmlspecialchars( $author['affiliation'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', false ) . '</affiliation>';
}
$author_mods .= '
</name>';
}
}
/**
* Format MODS xml fragment for organization affiliation.
*/
$org_mods = '';
if ( ! empty( $metadata['genre'] ) && in_array( $metadata['genre'], array( 'Dissertation', 'Technical report', 'Thesis', 'White paper' ) ) &&
! empty( $metadata['institution'] ) ) {
$org_mods .= '
<name type="corporate">
<namePart>
' . htmlspecialchars( $metadata['institution'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', false ) . '
</namePart>
<role>
<roleTerm type="text">originator</roleTerm>
</role>
</name>';
}
/**
* Format MODS xml fragment for date issued.
*/
$date_issued_mods = '';
if ( ! empty( $metadata['date_issued'] ) ) {
$date_issued_mods = '
<originInfo>
<dateIssued encoding="w3cdtf" keyDate="yes">' . $metadata['date_issued'] . '</dateIssued>
</originInfo>';
}
/**
* Format MODS xml fragment for resource type.
*/
$resource_type_mods = '';
if ( ! empty( $metadata['type_of_resource'] ) ) {
$resource_type_mods = '
<typeOfResource>' . $metadata['type_of_resource'] . '</typeOfResource>';
}
/**
* Format MODS xml fragment for language.
*/
$language_mods = '';
if ( ! empty( $metadata['language'] ) ) {
$term = wpmn_get_term_by( 'name', $metadata['language'], 'humcore_deposit_language' );
$language_mods = '
<language>
<languageTerm authority="iso639-3" >' . $term->slug . '</languageTerm>
</language>';
}
/**
* Format MODS xml fragment for genre.
*/
$genre_mods = '';
if ( ! empty( $metadata['genre'] ) ) {
$genre_mods = '
<genre>' . htmlspecialchars( $metadata['genre'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', false ) . '</genre>';
}
/**
* Format MODS xml fragment for one or more subjects.
*/
$full_subject_list = $metadata['subject'];
$subject_mods = '';
foreach ( $full_subject_list as $subject ) {
$subject_xml = '';
[$fast_id, $fast_subject, $fast_facet] = explode(":", $subject);
$fast_subject = htmlspecialchars( $fast_subject, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', false );
switch ($fast_facet) {
case "Topic":
case "Event":
$subject_xml = "<topic>{$fast_subject}</topic>";
break;
case "Form/Genre":
$subject_xml = "<form>{$fast_subject}</form>";
break;
case "Period": // same as chronological?
$subject_xml = "<temporal>{$fast_subject}</temporal>";
break;
case "Personal Name":
$subject_xml = "<name type=\"personal\"><namePart>{$fast_subject}</namePart></name>";
break;
case "Corporate Name":
$subject_xml = "<name type=\"corporate\"><namePart>{$fast_subject}</namePart></name>";
break;
case "Meeting":
$subject_xml = "<name type=\"conference\"><namePart>{$fast_subject}</namePart></name>";
break;
case "Uniform Title":
$subject_xml = "<titleInfo type=\"uniform\"><title>{$fast_subject}</title></titleInfo>";
break;
case "Geographic":
$subject_xml = "<geographic>{$fast_subject}</geographic>";
break;
default:
$subject_xml = "<topic>{$fast_subject}</topic>";
break;
}
// the outer <subject> tag is the same for all facets
$subject_xml = "<subject authority=\"fast\" authorityURI=\"http://id.worldcat.org/fast\" valueURI=\"http://id.worldcat.org/fast/{$fast_id}\">" .
"{$subject_xml}</subject>";
//
// concatenate all the subjects
$subject_mods .= $subject_xml;
}
$related_item_mods = '';
if ( 'journal-article' == $metadata['publication-type'] ) {
$related_item_mods = '
<relatedItem type="host">
<titleInfo>';
if ( ! empty( $metadata['book_journal_title'] ) ) {
$related_item_mods .= '
<title>' . htmlspecialchars( $metadata['book_journal_title'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', false ) . '</title>';
} else {
$related_item_mods .= '
<title/>';
}
$related_item_mods .= '
</titleInfo>';
if ( ! empty( $metadata['publisher'] ) ) {
$related_item_mods .= '
<originInfo>
<publisher>' . htmlspecialchars( $metadata['publisher'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', false ) . '</publisher>';
if ( ! empty( $metadata['date_issued'] ) ) {
$related_item_mods .= '
<dateIssued encoding="w3cdtf">' . $metadata['date_issued'] . '</dateIssued>';
}
$related_item_mods .= '
</originInfo>';
}
$related_item_mods .= '
<part>';
if ( ! empty( $metadata['volume'] ) ) {
$related_item_mods .= '
<detail type="volume">
<number>' . $metadata['volume'] . '</number>
</detail>';
}
if ( ! empty( $metadata['issue'] ) ) {
$related_item_mods .= '
<detail type="issue">
<number>' . $metadata['issue'] . '</number>
</detail>';
}
if ( ! empty( $metadata['start_page'] ) ) {
$related_item_mods .= '
<extent unit="page">
<start>' . $metadata['start_page'] . '</start>
<end>' . $metadata['end_page'] . '</end>
</extent>';
}
if ( ! empty( $metadata['date'] ) ) {
$related_item_mods .= '
<date>' . $metadata['date'] . '</date>';
}
$related_item_mods .= '
</part>';
if ( ! empty( $metadata['doi'] ) ) {
$related_item_mods .= '
<identifier type="doi">' . $metadata['doi'] . '</identifier>';
}
if ( ! empty( $metadata['issn'] ) ) {
$related_item_mods .= '
<identifier type="issn">' . $metadata['issn'] . '</identifier>';
}
$related_item_mods .= '
</relatedItem>';
} elseif ( 'book-chapter' == $metadata['publication-type'] ) {
$related_item_mods = '
<relatedItem type="host">
<titleInfo>';
if ( ! empty( $metadata['book_journal_title'] ) ) {
$related_item_mods .= '
<title>' . htmlspecialchars( $metadata['book_journal_title'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', false ) . '</title>';
} else {
$related_item_mods .= '
<title/>';
}
$related_item_mods .= '
</titleInfo>';
if ( ! empty( $metadata['book_author'] ) ) {
$related_item_mods .= '
<name type="personal">
<namePart>' . $metadata['book_author'] . '</namePart>
<role>
<roleTerm type="text">editor</roleTerm>
</role>
</name>';
}
if ( ! empty( $metadata['publisher'] ) ) {
$related_item_mods .= '
<originInfo>
<publisher>' . htmlspecialchars( $metadata['publisher'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', false ) . '</publisher>';
if ( ! empty( $metadata['date_issued'] ) ) {
$related_item_mods .= '
<dateIssued encoding="w3cdtf">' . $metadata['date_issued'] . '</dateIssued>';
}
$related_item_mods .= '
</originInfo>';
}
$related_item_mods .= '
<part>';
if ( ! empty( $metadata['chapter'] ) ) {
$related_item_mods .= '
<detail type="chapter">
<number>' . $metadata['chapter'] . '</number>
</detail>';
}
if ( ! empty( $metadata['start_page'] ) ) {
$related_item_mods .= '
<extent unit="page">
<start>' . $metadata['start_page'] . '</start>
<end>' . $metadata['end_page'] . '</end>
</extent>';
}
if ( ! empty( $metadata['date'] ) ) {
$related_item_mods .= '
<date>' . $metadata['date'] . '</date>';
}
$related_item_mods .= '
</part>';
if ( ! empty( $metadata['doi'] ) ) {
$related_item_mods .= '
<identifier type="doi">' . $metadata['doi'] . '</identifier>';
}
if ( ! empty( $metadata['isbn'] ) ) {
$related_item_mods .= '
<identifier type="isbn">' . $metadata['isbn'] . '</identifier>';
}
$related_item_mods .= '
</relatedItem>';
} elseif ( ! empty( $metadata['genre'] ) && ( 'Conference proceeding' == $metadata['genre'] || 'Conference paper' == $metadata['genre'] || 'Conference poster' == $metadata['genre'] ) ) {
$related_item_mods = '
<relatedItem type="host">
<titleInfo>';
if ( ! empty( $metadata['conference_title'] ) ) {
$related_item_mods .= '
<title>' . htmlspecialchars( $metadata['conference_title'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', false ) . '</title>';
} else {
$related_item_mods .= '
<title/>';
}
$related_item_mods .= '
</titleInfo>';
if ( ! empty( $metadata['publisher'] ) ) {
$related_item_mods .= '
<originInfo>
<publisher>' . htmlspecialchars( $metadata['publisher'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', false ) . '</publisher>';
if ( ! empty( $metadata['date_issued'] ) ) {
$related_item_mods .= '
<dateIssued encoding="w3cdtf">' . $metadata['date_issued'] . '</dateIssued>';
}
$related_item_mods .= '
</originInfo>';
}
$related_item_mods .= '
</relatedItem>';
}
/**
* Format the xml used to create the CONTENT datastream for the MODS metadata object.