-
Notifications
You must be signed in to change notification settings - Fork 0
/
cals_importer.mappings.inc
executable file
·1925 lines (1708 loc) · 59.4 KB
/
cals_importer.mappings.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* parses various marc xml elements
*/
/**
* maps fieldnames to parser functions below
*
* @return
* an array of fieldname to functions and params
*
*/
function _cals_importer_repo_field_callback($fieldname) {
$arr = array(
//"title" => array('function' => '_cals_get_title_from_marc'),
"title_field" => array('function' => '_cals_get_title_from_marc'),
"field_title_alternate" => array('function' => '_cals_get_alternate_title_from_marc'),
"field_uniform_title" => array('function' => '_cals_get_uniform_title_from_marc'),
'body' => array('function' => '_cals_importer_get_basic_values_from_marc',
"tags" => array(
array('tag' => 520, 'code' => 'a'),
)
),
"field_iso_language" => array('function' => '_cals_importer_get_isolanguage_from_marcxml'),
"field_date" => array('function' => '_cals_get_date_issued_from_marc'),
"field_dc_coverage" => array('function' => '_cals_importer_get_coverage_marc'),
"field_dc_description" => array('function' => '_cals_get_descriptors_from_marc'),
"field_isbn" => array('function' => '_cals_importer_get_isbn_sysnums_from_marc'),
"field_isbn_audio" => array('function' => '_cals_importer_get_isbn_sysnums_from_marc'),
'field_performer' => array('function' => '_cals_importer_get_performer_from_marcxml'),
"field_subject" => array("function" => '_cals_get_subject_from_marc'),
"field_genre" => array("function" => '_cals_get_genre_from_marc'),
"field_genre_harmonized" => array("function" => '_cals_get_mapped_genre_from_marc'),
//'field_uri' => array('function' => '_cals_importer_get_uri_from_marc'),
'field_urls_external' => array('function' => '_cals_importer_get_urls_from_marc',
"patterns" => variable_get('cals_importer_url_patterns'),
"exceptions" => variable_get('cals_importer_url_patterns_ignore'),
),
'field_literary_format' => array('function' => '_cals_get_literary_form_from_marc'),
'field_access_rights' => array('function' => '_cals_importer_get_basic_values_from_marc',
"tags" => array(
array('tag' => 506, 'code' => 'a'),
array('tag' => 506, 'code' => 'd'),
array('tag' => 540, 'code' => 'a'),
array('tag' => 540, 'code' => 'd'),
)
),
'field_publisher_source' => array('function' => '_cals_importer_branch_on_publisher_tags',
"tags" => array(
array('tag' => 534, 'code' => 'c'),
array('tag' => 533, 'code' => 'a'),
array('tag' => 533, 'code' => 'b'),
array('tag' => 533, 'code' => 'c'),
array('tag' => 264, 'code' => 'a'),
array('tag' => 264, 'code' => 'b'),
array('tag' => 260, 'code' => 'a'),
array('tag' => 260, 'code' => 'b'),
)
),
'field_dc_creator' => array('function' => '_cals_importer_get_creator_from_marc',
"tags" => array(
array('tag' => 100, 'code' => 'a'),
array('tag' => 110, 'code' => 'a'),
array('tag' => 110, 'code' => 'b'),
array('tag' => 111, 'code' => 'a'),
)
),
'field_dc_contributor' => array('function' => '_cals_importer_get_contributor_from_marc',
"tags" => array(
//array('tag' => 700, 'code' => 'a'),
//array('tag' => 700, 'code' => 'e'),
//array('tag' => 700, 'code' => '4'),
array('tag' => 710, 'code' => 'a'),
array('tag' => 710, 'code' => 'b'),
array('tag' => 711, 'code' => 'a'),
)
),
'field_title_alternative' => array('function' => '_cals_importer_get_basic_values_from_marc',
"tags" => array(
array('tag' => 130, 'code' => 'a'),
array('tag' => 730, 'code' => 'a'),
)
),
//relations
'field_dc_relation' => array('function' => '_cals_importer_get_dc_relations_from_marc',
"tags" => array(
array('tag' => 250, 'code' => 'a', 'qualifier' => "type", 'enum' => "isVersionOf"),
array('tag' => 534, 'code' => 'b', 'qualifier' => "type", 'enum' => "isVersionOf"),
array('tag' => 800, 'code' => 'a', 'qualifier' => "type", 'enum' => "IsPartOf"),
array('tag' => 830, 'code' => 'a', 'qualifier' => "type", 'enum' => "IsPartOf"),
array('tag' => 760, 'code' => 'a', 'qualifier' => "type", 'enum' => "IsPartOf"),
array('tag' => 534, 'code' => 'e', 'qualifier' => "type", 'enum' => "IsFormatOf"),
)
),
'field_access_rights' => array('function' => '_cals_importer_get_basic_values_from_marc',
"tags" => array(
array('tag' => 540, 'code' => 'a',),
array('tag' => 540, 'code' => 'd',),
array('tag' => 506, 'code' => 'a',),
array('tag' => 506, 'code' => 'd',),
)
),
'field_rights_holder' => array('function' => '_cals_importer_get_basic_values_from_marc',
"tags" => array(
array('tag' => 542, 'code' => 'd'),
)
),
'field_access_restrictions' => array('function' => '_cals_importer_get_access_restrictions'),
'field_oclc_number' => array('function' => '_cals_importer_get_oclc_number_from_marc'),
'field_system_control_number' => array('function' => '_cals_importer_get_system_numbers_from_marc'),
'field_audience' => array('function' => '_cals_importer_get_audience_from_marc'),
//'field_cataloging_agency' => array('function' => "_cals_importer_get_cataloging_agency_from_marc"),
'field_contents' => array('function' => '_cals_importer_get_contents_from_marc'),
);
if($fieldname == 'all') return $arr;
if(array_key_exists($fieldname, $arr)) return $arr[$fieldname];
return '';
}
/**
* Maps fieldnames to parser functions for file resources.
*
* @return array
* An array of fieldnames to functions.
*/
function _cals_importer_file_resource_field_callback() {
return array(
"field_performer" => '_cals_reparse_performer_field',
);
}
/**
* Returns fields for repository item
*
* @return array
* an array of flattened field defs
*
* @todo
* wrap into a static variable?
*
*/
function _cals_get_fields_repository_item() {
$fields = field_info_instances("node", "repository_item");
$fieldnames = array_keys($fields);
$tmp = array(
"_autocomplete", "_with_summary", "_hidden", "_buttons", "_select", "_textfield", "_embed", "_textarea",
);
//create an array of taxonomies to be used in parsing field collections, etc.
foreach (taxonomy_get_vocabularies() as $k => $v) {
$vocabularies[$v->machine_name] = $k;
}
if (count($fieldnames)) {
foreach($fields as $k => $v) {
if (!in_array($k, $fieldnames)) {
unset($fields[$k]);
}
}
}
foreach($fields as $k => $v) {
$parser = _cals_importer_repo_field_callback($k);
if(!empty($parser)) {
$type = $v['widget']['type'];
$label = $v['label'];
$t2 = $type;
foreach($tmp as $strip) $t2 = str_replace($strip, '', $t2);
$req = $v['required'];
$map[$k] = array(
'label' => $label,
'type' => $t2,
'required' => $req,
'values' => array(),
'parser' => $parser,
);
if($t2 == 'options')
$map[$k]['allowed_values'] =
list_allowed_values(field_info_field($k));
if($t2 == 'taxonomy') {
$info = field_info_field($k);
foreach ($info['settings']['allowed_values'] as $item) {
if(!empty($item['vocabulary'])) $map[$k]['vid'] = $vocabularies[$item['vocabulary']];
}
}
//set a default if defined
$map[$k]['default'] = (isset($req) && isset($v['default_value'][0]['value'])) ? $v['default_value'][0]['value'] : '';
$types[$t2][] = $k;
}
}
$arr = array("types" => $types, "map" => $map);
return $arr;
}
/**
* Returns fields for file resource.
*
* @return array
* An array of fields that can be re-parsed.
*/
function _cals_get_fields_file_resource() {
$fields = field_info_instances('field_collection_item', 'field_file_resource');
$configured_fields = _cals_importer_file_resource_field_callback();
$fields = array_intersect_key($fields, $configured_fields);
$reparse_fields = array();
foreach ($fields as $field_name => $data) {
$reparse_fields[$field_name] = [
'name' => $data['label'],
'field_name' => $field_name,
'callback' => $configured_fields[$field_name],
];
}
return $reparse_fields;
}
/**
* helper function to parse xml path
* @param $record_xml
* the original xml string
* @param $tag
* the tag to parse
* @param $code
* the optional code to parse*
* @param $ind1
* The optional ind1 tag for the path.
* @param $ind2
* The optional ind2 tag for the path.
*
* @return
* an array of values
*
*/
function _cals_importer_xpath_value($record_xml, $fieldname, $tag, $code = NULL, $ind1 = NULL, $ind2 = NULL) {
$arr = array();
$path = ".//marc:{$fieldname}[@tag='" . $tag . "']";
if (!empty($ind1)) {
$path .= "[@ind1='" . $ind1 . "']";
}
if (isset($ind2)) {
$path .= "[@ind2='" . $ind2 . "']";
}
if (!empty($code)) {
$path .= "/marc:subfield[@code='" . $code . "']";
}
$tmp = $record_xml->xpath($path);
//dpm(__FUNCTION__, $path, $tmp);
if (count($tmp) >= 1) {
foreach ($tmp as $me){
$str = (string) strip_tags(trim($me[0]));
if (!empty($str)) {
$arr[] = $str;
}
}
}
return $arr;
}
function _get_tmp($xml) {}
function _cals_importer_xpath_control_field_value($xml, $tag, $code = NULL) {
$arr = array();
$path = "//marc:controlfield[@tag='" . $tag . "']";
if(!empty($code) ) $path .= "/marc:subfield[@code='" . $code . "']";
$tmp = $xml->xpath($path);
if (count($tmp) >= 1) {
foreach ($tmp as $me)
$arr[] = (string) trim($me[0]);
}
//if(!count($tmp)) return;
//printPre(__FUNCTION__, $arr, $path);
return $arr;
}
/**
* returns the title
*
*
* @return
* an array of MARC XML -> Drupal mappings
*
*/
function _cals_get_title_from_marc($xml, $k, &$map) {
$title = array();
$path = "//marc:datafield[@tag='245']/marc:subfield";
$tag = $xml->xpath($path);
foreach($tag as $subfield ) {
$code = (string) $subfield->attributes()->code;
if($code != 'c') {
if($code == 'b' && substr(rtrim($tmp[0]), -1) != ":") {
$title[0] .= " :";
}
$tmp = _cals_importer_xpath_value($xml, "datafield", 245, $code);
foreach ($tmp as $me) $title[] = $me;
}
}
$title = implode(" ", $title);
$title = _cals_get_truncated_value($title, 250);
if(strlen($title)) $map[$k]['values'][] = $title;
}
/*
* see:https://github.com/BCLibCoop/nnels/issues/99
*
* "Similarly, Alternate Title should pull from 246 if the first indicator is 0, 1 or 3, subfields $abnp
*
*/
function _cals_get_alternate_title_from_marc($xml, $k, &$map) {
$title = array();
$codes = array("a", "b", "n", "p");
$parent = $xml->xpath("//marc:datafield[@tag='246']"); //need to grab abbreviated path to find indicator
$inds = array(0, 1, 3);
if(count($parent)) {
foreach($parent as $i => $child) {
$ind = (string) $parent[$i]->attributes()->ind1;
if(in_array($ind, $inds)) {
$title = array(); //holds the title elements for this child
foreach ( $child->children('marc', true)->subfield as $subfield ) {
$code = (string) $subfield->attributes()->code;
if(in_array($code, $codes)) {
$title[] = (string) trim($subfield[0]);
}
}
if(count($title)) {
$title = implode(" ", $title);
$title = _cals_get_truncated_value($title, 250);
$map[$k]['values'][] = $title;
}
}
}
}
}
/*
* @see https://github.com/BCLibCoop/nnels/issues/99
*
* "Uniform title should pull from 130 $anp or if 130 does not exist, 240 where
* first indicator is 1, subfields $anp"
*
*/
function _cals_get_uniform_title_from_marc($xml, $k, &$map) {
$title = array();
$path = "//marc:datafield[@tag='130']/marc:subfield"; //reset path
$tag = $xml->xpath($path);
$codes = array("a", "n", "p");
foreach($tag as $subfield ) {
$code = (string) $subfield->attributes()->code;
if(in_array($code, $codes)) {
$path = "//marc:datafield[@tag='130']/marc:subfield[@code='{$code}']";
$tmp = $xml->xpath($path);
foreach ($tmp as $me) $title[] = (string) trim($me[0]);
}
}
if(!count($title)) {
$parent = $xml->xpath("//marc:datafield[@tag='240']"); //need to grab parent path to find indicator
if(!count($parent)) return;
$ind = (string) $parent[0]->attributes()->ind1;
$path = "//marc:datafield[@tag='240']/marc:subfield"; //reset path
$tag = $xml->xpath($path);
if($ind == 1) {
foreach($tag as $subfield ) {
$code = (string) $subfield->attributes()->code;
if(in_array($code, $codes)) {
$path = "//marc:datafield[@tag='240']/marc:subfield[@code='{$code}']";
$tmp = $xml->xpath($path);
foreach ($tmp as $me) $title[] = (string) trim($me[0]);
}
}
}
}
$title = implode(" ", $title);
$title = _cals_get_truncated_value($title, 250) ;
$vid = $map[$k]['vid'];
$map[$k]['values'][] = _cals_get_tid_by_name($vid, $title);
//_cals_get_taxonomy_terms_from_xpath($vid, $title, $k, $map);
}
/* Parse field_audience via 006/5 or 008/2
*
* @see https://github.com/BCLibCoop/nnels/issues/117
*/
function _cals_importer_get_audience_from_marc($xml, $k, &$map) {
$allowed_values = $map[$k]['allowed_values'];
$audience = '';
$arr = array("006" => 5, "008" => 22);
foreach($arr as $tag => $pos) {
//$tmp = _cals_importer_xpath_control_field_value($xml, $tag, $code = NULL);
$tmp = _cals_importer_xpath_value($xml, "controlfield", $tag, $code = NULL);
foreach($tmp as $val) {
$str = substr($val, $pos, 1);
if(array_key_exists($str, $allowed_values)) {
$audience = $str;
break;
}
}
if($audience != '') {
$map[$k]['values'][] = $audience;
break;
};
}
}
function _cals_importer_get_coverage_marc($xml, $k, &$map){
$path = "//marc:controlfield[@tag='008']";
$vals = $xml->xpath($path);
foreach ($vals as $val) {
$str = substr($val, 15, 3);
//field collection
$map[$k]['values'][] = array(
//'field_name' => 'field_dc_description',
'field_name' => 'field_dc_coverage',
'field_dc_coverage_type' => array(
LANGUAGE_NONE => array(array('value' => 'Spatial')),
),
'field_marc_tag' => array(
LANGUAGE_NONE => array(array('value' => '008')),
),
'field_dc_coverage_scheme' => array(
LANGUAGE_NONE => array(array('value' => "http://www.loc.gov/marc/countries")),
),
'field_value' => array(
LANGUAGE_NONE => array(array('value' => $str)),
),
);
}
$tags = array('043', '044');
foreach($tags as $tag) {
$path = "//marc:datafield[@tag='" . $tag . "']";
$vals = $xml->xpath($path);
foreach ($vals as $val) {
$str = (string) trim($val[0]);
if(strlen($str)) {
$map[$k]['values'][]= array(
'field_name' => 'field_dc_coverage',
'field_dc_coverage_type' => array(
LANGUAGE_NONE => array(array('value' => 'Spatial')),
),
'field_marc_tag' => array(
LANGUAGE_NONE => array(array('value' => $tag)),
),
'field_dc_coverage_scheme' => array(
LANGUAGE_NONE => array(array('value' => "ISO 3166")),
),
'field_value' => array(
LANGUAGE_NONE => array(array('value' => $str)),
),
);
}
}
}
$tag = 651; //??
$path = "//marc:datafield[@tag='651']/marc:subfield[@code='a']";
$vals = $xml->xpath($path);
$vals = _cals_importer_xpath_value($xml, "datafield", 651, "a");
foreach ($vals as $str) {
if(strlen($str)) {
$map[$k]['values'][]= array(
'field_name' => 'field_dc_coverage',
'field_dc_coverage_type' => array(
LANGUAGE_NONE => array(array('value' => 'Spatial')),
),
'field_marc_tag' => array(
LANGUAGE_NONE => array(array('value' => $tag)), //not sure what this is?
),
'field_dc_coverage_scheme' => array(
LANGUAGE_NONE => array(array('value' => "lcsh")),
),
'field_value' => array(
LANGUAGE_NONE => array(array('value' => $str)),
),
);
}
}
}
function _cals_get_date_issued_from_marc($xml, $k, &$map) {
$dates_dupes = array();
$dates = array();
//Background: https://github.com/BCLibCoop/nnels/issues/104#issuecomment-229739259
//Get common subfields for this section
$orig_pub_date_534c = _cals_importer_xpath_value($xml, "datafield", 534, "c");
$date_pub_264c = _cals_importer_xpath_value($xml, "datafield", 264, "c");
$date_pub_260c = _cals_importer_xpath_value($xml, "datafield", 260, "c");
if ( count ( $orig_pub_date_534c ) ) {
foreach ($orig_pub_date_534c as $orig_pub_date_part) {
//There can be more comma-separated parts in
//array returned from _cals_importer_xpath_value
$orig_pub_date_part = explode(',', trim( $orig_pub_date_part, '.' ) );
_cals_map_date_qualifiers
($orig_pub_date_part, 534, $dates, TRUE);
}
$reprod_tag = '533';
$reprod_date_533d = _cals_importer_xpath_value($xml, "datafield",
$reprod_tag, "d");
} else {
//No 533-4 so fallback is 008
$reprod_tag = '008';
$reprod_date_008c = _cals_importer_xpath_value($xml, "controlfield",
$reprod_tag,
NULL);
}
$reprod_date_shared = $reprod_date_533d ? $reprod_date_533d :
$reprod_date_008c;
if ( count( $date_pub_264c ) )
foreach ( $date_pub_264c as $pub_date_part ) {
$pub_date_parts = explode(',', trim( $pub_date_part, '.' ) );
foreach ( $pub_date_parts as $part ) _cals_map_date_qualifiers($part, 264, $dates, TRUE);
}
else if (count($date_pub_260c))
foreach ($date_pub_260c as $pub_date_part)
_cals_map_date_qualifiers($pub_date_part, 260, $dates, TRUE);
//Whatever was found for reproduction date
else if (count($reprod_date_shared))
foreach ($reprod_date_shared as $reprod_date_part)
_cals_map_date_qualifiers($reprod_date_part, $reprod_tag, $dates, TRUE);
foreach($dates as $qualifier => $date)
$map[$k]['values'][] = array(
'field_name' => "field_date",
'field_dc_date' => array(
LANGUAGE_NONE => array(array('value' => $date) ),
),
'field_qualifier_date' => array(
LANGUAGE_NONE => array(array('value' => $qualifier) ),
),
);
}
/**
* @param $str
* the date str to be parsed
* @param $tag
* the marc xml tag
* @param $dates
* the $dates array passed by reference
* @param $is_present
* boolean that tells us if there's a 534 tag included
*/
function _cals_map_date_qualifiers($str, $tag, &$dates, $is_present) {
if($tag == '008') {
$str = substr($str, 7, 4); // 008/07-10 should be numeric
}
else {
//Matches against c1999 or p1999 (UTF-8 for Ⓟ, sound recording copyright
// symbol)
$check_copyright = _cals_parse_date_issue_copyright($str);
}
$date_value = _cals_get_iso_year($str);
if(!empty($date_value)) {
if($check_copyright) {
$qualifier = "Copyrighted";
}
else {
if($is_present) //if there's 534 included, it takes "issued" qualifier, otherwise "created"
$qualifier = ($tag == 534) ? "Issued" : "Created";
else
$qualifier = "Issued";
}
$tmp[$qualifier] = $date_value;
//Replace the year w/ the most recent
if($tmp[$qualifier] > $dates[$qualifier] || !isset($dates[$qualifier]))
$dates[$qualifier] = $tmp[$qualifier];
}
}
/**
* @param $raw_date_value
* @return bool
*/
function _cals_parse_date_issue_copyright ($raw_date_value) {
//$copyright = "/^c\\d{4}/"; - original pattern
$pattern = "/[cp]\d{4}/"; //new pattern, modified
// 2016-06-30 - see:
// https://github.com/BCLibCoop/nnels/issues/104#issuecomment-229709076
preg_match($pattern, $raw_date_value, $matches);
if (count($matches))
return TRUE;
return FALSE;
}
/**
* @param $xml
* @param $k
* @param $map
*
* parse 041 to grab language values
* see: http://www.loc.gov/marc/bibliographic/bd041.html
*/
function _cals_importer_get_isolanguage_from_marcxml($xml, $k, &$map) {
//look for 041 tag code a
//$tmp = $xml->xpath("//marc:datafield[@tag='041']/marc:subfield[@code='a']");
$vals = _cals_importer_xpath_value($xml, "datafield", '041', 'a');
foreach ($vals as $val)
if(array_key_exists($val, $map[$k]['allowed_values'])) $map[$k]['values'][] = $val;
if( empty($map[$k]['values']) ) {
//$vals = _cals_importer_xpath_control_field_value($xml, "008", $code = NULL);
$vals = _cals_importer_xpath_value($xml, "controlfield", "008", $code = NULL);
foreach($vals as $val) {
$str = substr($val, 35, 3);
if(array_key_exists($str, $map[$k]['allowed_values'])) $map[$k]['values'][] = $str;
}
}
if( empty($map[$k]['values']) ) $map[$k]['values'][] = 'eng';
}
/**
* Parses 008 field to grap year value
* @param $str
* @return bool|int|mixed|string
*
*/
function _cals_get_date_from_008($str) {
return _cals_get_iso_date(substr($str, 7, 4));
}
/**
* Parse taxonomy terms: genre (vid = 2) and subject (vid = 3)
* @param $xpathvalue
* @param $fieldname
* @param $map
*/
function _cals_get_taxonomy_terms_from_xpath($xpathvalue, $fieldname, &$map) {
$vid = $map[$fieldname]['vid'];
$terms = array();
if (count($xpathvalue) >= 1) {
foreach ($xpathvalue as $me) {
$str = (string) $me[0];
$str = rtrim($str, ".");
if(!in_array($str, $terms) ) $terms[] = $str;
}
}
asort($terms);
foreach($terms as $term) $map[$fieldname]['values'][] = _cals_get_tid_by_name($vid, $term);
}
//function _cals_get_genre_from_marc(&$arr, $xml) {
function _cals_get_genre_from_marc($xml, $k, &$map) {
$xpathvalue = $xml->xpath("//marc:datafield[@tag='655'][@ind1=' ']/marc:subfield[@code='a']|//marc:datafield[@tag='655'][@ind2=' ']/marc:subfield[@code='a']");
_cals_get_taxonomy_terms_from_xpath($xpathvalue, $k, $map);
}
function _cals_get_mapped_genre_from_marc($xml, $k, &$map) {
$supplement_subjects = [];
// 655 is a takeover from field_genre (defunct).
$supplement_tags = array(
655 => 'a',
946 => 'a',
);
foreach ($supplement_tags as $tag => $code) {
$supplement_subjects[$tag] = $xml->xpath(
"//marc:datafield[@tag='{$tag}'][@ind1=' ']/marc:subfield[@code='{$code}']|//marc:datafield[@tag='{$tag}'][@ind2=' ']/marc:subfield[@code='{$code}']"
);
}
// Convert from SimpleXMlElement objects to strings for searching.
$flattened_supplement_subjects = array();
foreach ($supplement_tags as $tag => $code) {
if (isset($supplement_subjects[$tag])) {
foreach($supplement_subjects[$tag] as $key => $sxe) {
// Remove characters that do not exist in the harmonized genre terms.
$flattened_supplement_subjects[$key] = preg_replace('/[^a-zA-Z\s\(\)\-\,]/', '', $sxe[0]->__toString());
}
}
}
module_load_include('inc', 'cals_importer', 'cals_importer.term_parse');
try {
cals_importer_find_mapped_genre($flattened_supplement_subjects, $k, $map);
}
catch (Exception $e){
drupal_set_message(
"cals_import_find_mapped_genre encountered critical
error: " . $e->getMessage(), 'error', FALSE
);
}
}
/**
* To be deprecated
* @param $xml
* @param $k
* @param $map
*/
function _cals_importer_get_uri_from_marc($xml, $k, &$map) {
$vals = _cals_importer_xpath_value($xml, "datafield", '856', 'u');
$arr = array("nnels.ca", "license", "loc.gov");
foreach ($vals as $val) {
$add = 1;
foreach($arr as $ignore )
if(strpos($val, $ignore) !== FALSE) $add = 0;
if($add == 1) $map[$k]['values'][] = $val;
}
}
/**
* @param $xml
* @param $k
* @param $map
*/
function _cals_importer_get_urls_from_marc($xml, $k, &$map) {
$vals = _cals_importer_xpath_value($xml, "datafield", '856', 'u');
$arr = array("nnels.ca", "license", "loc.gov");
$exceptions = trim($map[$k]['parser']['exceptions']);
$exceptions = explode("\r\n", $exceptions);
$patterns = explode("\r\n", trim($map[$k]['parser']['patterns']));
foreach ($vals as $val) {
$val = strtolower($val);
$add = 1; //should we add this? default = yes
$added = 0; // track if we've added this already.
foreach($exceptions as $ignore )
if(strpos($val, $ignore) !== FALSE) $add = 0;
if($add == 1 && $added == 0) {
foreach($patterns as $pattern) {
$tmp = explode("|", $pattern);
if(strpos($val, trim($tmp[1]) ) !== FALSE ) {
$map[$k]['values'][] = array('url' => $val, "title" => trim($tmp[0]));
$added = 1;
}
}
if($added == 0) $map[$k]['values'][] = array('url' => $val);
}
}
}
/*
* Based on 006/16 or if 006 not present, 008/33;
* see: https://github.com/BCLibCoop/nnels/issues/113#event-656015813
*
* @todo
* decide if we need to loop thru $tmp, or just begin w/ $tmp[0]
*
*/
function _cals_get_literary_form_from_marc($xml, $k, &$map) {
//$ignore = array("u", "|", " ", '', 'n');
$allowed_values = $map[$k]['allowed_values'];
$litform = ''; //holds the value; blank if none
//$tmp = _cals_importer_xpath_control_field_value($xml, "006", $code = NULL);
//$tmp = _cals_importer_xpath_value($xml, "controlfield", "006", $code = NULL);
$arr = array("006" => 16, "008" => 33);
foreach($arr as $tag => $pos) {
//$tmp = _cals_importer_xpath_control_field_value($xml, $tag, $code = NULL);
$tmp = _cals_importer_xpath_value($xml, "controlfield", $tag, $code = NULL);
if(is_array($tmp) && count($tmp)) {
foreach($tmp as $val) {
$str = substr($val, $pos, 1);
//Careful here, string "0" is the MARC code.
//Fixes https://github.com/BCLibCoop/nnels/issues/201
if ($str === "0") $str = 'zero';
if(isset($str) && //substr() fails or finds empty char
$str !== '' &&
array_key_exists($str, $allowed_values)) {
$litform = $str;
break;
}
}
}
if($litform != '') {
$map[$k]['values'][] = $litform;
break;
};
}
//if(empty($litform)) $map[$k]['values'][] = 'u';
}
function _cals_get_subject_from_marc($xml, $k, &$map){
$paths = array();
$paths[] = "//marc:datafield[@tag='600']/marc:subfield[@code='a']";
//the codes we need to look for on 650:
$codes = array("a", "x", "v", "y", "z");
foreach ($codes as $code) $paths[] = "//marc:datafield[@tag='650']/marc:subfield[@code='" . $code . "']";
$path = implode("|", $paths);
$xpathvalue = $xml->xpath($path);
//Collapse on duplicates across a given subfield code
$deduped_xpath_values = array();
foreach($xpathvalue as $val) {
if (! in_array($val->__toString(), $deduped_xpath_values)) $deduped_xpath_values[] =
$val;
}
//Requires SimpleXMLElements passed in first parameter
_cals_get_taxonomy_terms_from_xpath($deduped_xpath_values, $k, $map);
}
/**
* Grabs table of contents per https://github.com/BCLibCoop/nnels/issues/121
*
* @param $xml
* @param $k
* @param $map
*/
function _cals_importer_get_contents_from_marc($xml, $k, &$map) {
$tag = 505;
$codes = array("a", "g", "r", "t");
$parent = $xml->xpath("//marc:datafield[@tag='505']");
$content = '';
foreach ($parent as $i => $child) {
//$ind = (string) $parent[$i]->attributes()->ind1;
$title = array(); //holds the title elements for this child
foreach ($child->children(NAME_SPACE)->subfield as $subfield) {
$code = (string) $subfield->attributes()->code;
if(in_array($code, $codes)) {
$str = (string) trim($subfield[0]);
//add blank just in case the subfield doesn't include spaces:
if(strlen($str)) $content .= " " . $str;
}
}
}
if(strlen($content)) {
$content = str_replace(" ", " ", $content);
$replacements = array(" -- ", "-- ", " --");
$content = str_replace($replacements, "--", $content);
$items = array_values(array_filter(explode("--", $content)));
$vars = array("title" => '', "type" => "ul", "items" => $items, "attributes" => array());
$map[$k]['values'][] = theme("item_list", $vars);
}
}
/*
* to deprecate??
*
* @param $xml
* @param $k
* @param $map
*/
function _cals_get_descriptors_from_marc($xml, $k, &$map) {
$fields = array(500, 504, 505, 511, 516, 533, 538, 586, 595, 598, 599, 541, );
foreach ($fields as $field) {
$path = "//marc:datafield[@tag='" . $field . "']";
$path = "//marc:datafield[@tag='" . $field . "']/marc:subfield";
$tmp = $xml->xpath($path);
if (count($tmp) >= 1) {
foreach ($tmp as $me) {
$val = (string) trim($me[0]);
$val = _cals_get_truncated_value($val, 1000);
$type = _cals_get_description_type($field);
$map[$k]['values'][] = array(
'field_name' => 'field_dc_description',
'field_dc_description_type' => array(
LANGUAGE_NONE => array(array('value' => $type)),
),
'field_value' => array(
LANGUAGE_NONE => array(array('value' => $val)),
),
);
}
}
}
}
/**
* @param $xml
* @param $k
* @param $map
* @param $flag
*/
//function _cals_importer_get_system_numbers_from_marc($xml, $k, &$map, $flag) {
//check w/ Jonathan as to the $flag parameter: can't find elsewhere in code
function _cals_importer_get_system_numbers_from_marc($xml, $k, &$map) {
$str = array();
$scn_targets = array(
'028' => 'a',
'035' => 'a',
'037' => 'a',
);
foreach ($scn_targets as $tag => $subfield) {
$tmp = _cals_importer_xpath_value($xml, "datafield", $tag, $subfield);
foreach($tmp as $value) {
$map[$k]['values'][] = $value;
}
unset($tmp);
}
}
function _cals_importer_get_oclc_number_from_marc($xml, $k, &$map) {
$arr = _cals_importer_xpath_value($xml, "datafield", '035', 'a');
foreach($arr as $value) {
//new check on valid oclc numbers
if (strpos($value, "(OCoLC)") !== false &&
_cals_isvalid_oclc_number($value) === 1 &&
count($map[$k]['values']) < 2 ) {
$map[$k]['values'][] = $value;
}
}
}
function _cals_importer_get_access_restrictions($xml, $k, &$map) {
//Default Requires Login see: https://github.com/BCLibCoop/nnels/issues/187
$map[$k]['values'][] = 1;
//Obsolete processing based on restricted string in MARCXML 506 a,d subfields
// $codes = array("a", "d");
// $title = array();
// $vals = array();
// foreach($codes as $code) {
// $tmp = _cals_importer_xpath_value($xml, "datafield", '506', $code);
// if (count($tmp) >= 1) {
// $vals[] = (string) trim($tmp[0]);
// if(count($vals)) {
// $str = implode(" ", $vals);
// $pos = strpos(strtolower($str),"restricted");
// $map[$k]['values'][] = (is_numeric($pos)) ? 1 : 0;
// }
// }
// }
}
function _cals_get_description_type($tag) {
$tmp = array(
505 => "tableofcontents",
520 => 'abstract',
);
/*
other possible enumerations
504 => "Bibliography",
511 => 'Performer',
516 => 'Type of file',
533 => 'Reproduction',
538 => 'System Details / Requirements',
586 => 'Awards Notes',
595 => 'Extent of Original',
598 => 'Keywords, Local',
599 => 'Local Notes',
541 => 'Accrual Method',
*/
if (array_key_exists($tag, $tmp)) return $tmp[$tag];
return "";
}
//function _cals_importer_get_isbn_sysnums_from_marc(&$arr, $xml) {
function _cals_importer_get_isbn_sysnums_from_marc($xml, $k, &$map) {
$path = ($k == 'field_isbn_audio') ? "//marc:datafield[@tag='020']/marc:subfield[@code='a']" :
"//marc:datafield[@tag='534']/marc:subfield[@code='z']" ;
$tmp = $xml->xpath($path);