-
Notifications
You must be signed in to change notification settings - Fork 257
/
Record.pm
997 lines (797 loc) · 33.5 KB
/
Record.pm
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
package C4::Record;
#
# Copyright 2006 (C) LibLime
# Parts copyright 2010 BibLibre
# Part copyright 2015 Universidad de El Salvador
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
# please specify in which methods a given module is used
use MARC::Record; # marc2marcxml, marcxml2marc, changeEncoding
use MARC::File::XML; # marc2marcxml, marcxml2marc, changeEncoding
use Biblio::EndnoteStyle;
use Unicode::Normalize qw( NFC ); # _entity_encode
use C4::Biblio qw( GetFrameworkCode );
use C4::Koha; #marc2csv
use C4::XSLT;
use YAML::XS; #marcrecords2csv
use Encode;
use Template;
use Text::CSV::Encoded; #marc2csv
use Koha::Items;
use Koha::SimpleMARC qw( read_field );
use Koha::XSLT::Base;
use Koha::CsvProfiles;
use Koha::AuthorisedValues;
use Koha::TemplateUtils qw( process_tt );
use Carp qw( carp croak );
use vars qw(@ISA @EXPORT);
@ISA = qw(Exporter);
# only export API methods
@EXPORT = qw(
marc2endnote
marc2marc
marc2marcxml
marcxml2marc
marc2dcxml
marc2modsxml
marc2madsxml
marc2bibtex
marc2csv
marc2cites
marcrecord2csv
changeEncoding
);
=head1 NAME
C4::Record - MARC, MARCXML, DC, MODS, XML, etc. Record Management Functions and API
=head1 SYNOPSIS
New in Koha 3.x. This module handles all record-related management functions.
=head1 API (EXPORTED FUNCTIONS)
=head2 marc2marc - Convert from one flavour of ISO-2709 to another
my ($error,$newmarc) = marc2marc($marc,$to_flavour,$from_flavour,$encoding);
Returns an ISO-2709 scalar
=cut
sub marc2marc {
my ($marc,$to_flavour,$from_flavour,$encoding) = @_;
my $error;
if ($to_flavour && $to_flavour =~ m/marcstd/) {
my $marc_record_obj;
if ($marc =~ /^MARC::Record/) { # it's already a MARC::Record object
$marc_record_obj = $marc;
} else { # it's not a MARC::Record object, make it one
eval { $marc_record_obj = MARC::Record->new_from_usmarc($marc) }; # handle exceptions
# conversion to MARC::Record object failed, populate $error
if ($@) { $error .="\nCreation of MARC::Record object failed: ".$MARC::File::ERROR };
}
unless ($error) {
my @privatefields;
foreach my $field ($marc_record_obj->fields()) {
if ($field->tag() =~ m/9/ && ($field->tag() != '490' || C4::Context->preference("marcflavour") eq 'UNIMARC')) {
push @privatefields, $field;
} elsif (! ($field->is_control_field())) {
$field->delete_subfield(code => '9') if ($field->subfield('9'));
}
}
$marc_record_obj->delete_field($_) for @privatefields;
$marc = $marc_record_obj->as_usmarc();
}
} else {
$error = "Feature not yet implemented\n";
}
return ($error,$marc);
}
=head2 marc2marcxml - Convert from ISO-2709 to MARCXML
my ($error,$marcxml) = marc2marcxml($marc,$encoding,$flavour);
Returns a MARCXML scalar
C<$marc> - an ISO-2709 scalar or MARC::Record object
C<$encoding> - UTF-8 or MARC-8 [UTF-8]
C<$flavour> - MARC21 or UNIMARC
C<$dont_entity_encode> - a flag that instructs marc2marcxml not to entity encode the xml before returning (optional)
=cut
sub marc2marcxml {
my ($marc,$encoding,$flavour,$dont_entity_encode) = @_;
my $error; # the error string
my $marcxml; # the final MARCXML scalar
# test if it's already a MARC::Record object, if not, make it one
my $marc_record_obj;
if ($marc =~ /^MARC::Record/) { # it's already a MARC::Record object
$marc_record_obj = $marc;
} else { # it's not a MARC::Record object, make it one
eval { $marc_record_obj = MARC::Record->new_from_usmarc($marc) }; # handle exceptions
# conversion to MARC::Record object failed, populate $error
if ($@) { $error .="\nCreation of MARC::Record object failed: ".$MARC::File::ERROR };
}
# only proceed if no errors so far
unless ($error) {
# check the record for warnings
my @warnings = $marc_record_obj->warnings();
if (@warnings) {
warn "\nWarnings encountered while processing ISO-2709 record with title \"".$marc_record_obj->title()."\":\n";
foreach my $warn (@warnings) { warn "\t".$warn };
}
unless($encoding) {$encoding = "UTF-8"}; # set default encoding
unless($flavour) {$flavour = C4::Context->preference("marcflavour")}; # set default MARC flavour
# attempt to convert the record to MARCXML
eval { $marcxml = $marc_record_obj->as_xml_record($flavour) }; #handle exceptions
# record creation failed, populate $error
if ($@) {
$error .= "Creation of MARCXML failed:".$MARC::File::ERROR;
$error .= "Additional information:\n";
my @warnings = $@->warnings();
foreach my $warn (@warnings) { $error.=$warn."\n" };
# record creation was successful
} else {
# check the record for warning flags again (warnings() will be cleared already if there was an error, see above block
@warnings = $marc_record_obj->warnings();
if (@warnings) {
warn "\nWarnings encountered while processing ISO-2709 record with title \"".$marc_record_obj->title()."\":\n";
foreach my $warn (@warnings) { warn "\t".$warn };
}
}
# only proceed if no errors so far
unless ($error) {
# entity encode the XML unless instructed not to
unless ($dont_entity_encode) {
my ($marcxml_entity_encoded) = _entity_encode($marcxml);
$marcxml = $marcxml_entity_encoded;
}
}
}
# return result to calling program
return ($error,$marcxml);
}
=head2 marcxml2marc - Convert from MARCXML to ISO-2709
my ($error,$marc) = marcxml2marc($marcxml,$encoding,$flavour);
Returns an ISO-2709 scalar
C<$marcxml> - a MARCXML record
C<$encoding> - UTF-8 or MARC-8 [UTF-8]
C<$flavour> - MARC21 or UNIMARC
=cut
sub marcxml2marc {
my ($marcxml,$encoding,$flavour) = @_;
my $error; # the error string
my $marc; # the final ISO-2709 scalar
unless($encoding) {$encoding = "UTF-8"}; # set the default encoding
unless($flavour) {$flavour = C4::Context->preference("marcflavour")}; # set the default MARC flavour
# attempt to do the conversion
eval { $marc = MARC::Record->new_from_xml($marcxml,$encoding,$flavour) }; # handle exceptions
# record creation failed, populate $error
if ($@) {$error .="\nCreation of MARCXML Record failed: ".$@;
$error.=$MARC::File::ERROR if ($MARC::File::ERROR);
};
# return result to calling program
return ($error,$marc);
}
=head2 marc2dcxml - Convert from ISO-2709 to Dublin Core
my dcxml = marc2dcxml ($marc, $xml, $biblionumber, $format);
EXAMPLE
my dcxml = marc2dcxml (undef, undef, 1, "oaidc");
Convert MARC or MARCXML to Dublin Core metadata (XSLT Transformation),
optionally can get an XML directly from biblio_metadata
without item information. This method take into consideration the syspref
'marcflavour' (UNIMARC or MARC21).
Return an XML file with the format defined in C<$format>
C<$marc> - an ISO-2709 scalar or MARC::Record object
C<$xml> - a MARCXML file
C<$biblionumber> - biblionumber for database access
C<$format> - accept three type of DC formats (oaidc, srwdc, and rdfdc )
=cut
sub marc2dcxml {
my ( $marc, $xml, $biblionumber, $format ) = @_;
# global variables
my ( $marcxml, $record, $output );
# set the default path for intranet xslts
# differents xslts to process (OAIDC, SRWDC and RDFDC)
my $xsl = C4::Context->config('intrahtdocs') . '/prog/en/xslt/' .
C4::Context->preference('marcflavour') . 'slim2' . uc ( $format ) . '.xsl';
if ( defined $marc ) {
# no need to catch errors or warnings marc2marcxml do it instead
$marcxml = C4::Record::marc2marcxml( $marc );
} elsif ( not defined $xml and defined $biblionumber ) {
# get MARCXML biblio directly without item information
$marcxml = C4::Biblio::GetXmlBiblio( $biblionumber );
} else {
$marcxml = $xml;
}
eval { $record = MARC::Record->new_from_xml(
$marcxml,
'UTF-8',
C4::Context->preference('marcflavour')
);
};
# conversion to MARC::Record object failed
if ( $@ ) {
croak "Creation of MARC::Record object failed.";
} elsif ( $record->warnings() ) {
carp "Warnings encountered while processing ISO-2709 record.\n";
my @warnings = $record->warnings();
foreach my $warn (@warnings) {
carp "\t". $warn;
};
} elsif ( $record =~ /^MARC::Record/ ) { # if OK makes xslt transformation
my $xslt_engine = Koha::XSLT::Base->new;
if ( $format =~ /^(dc|oaidc|srwdc|rdfdc)$/i ) {
$output = $xslt_engine->transform( $marcxml, $xsl );
} else {
croak "The format argument ($format) not accepted.\n" .
"Please pass a valid format (oaidc, srwdc, or rdfdc)\n";
}
my $err = $xslt_engine->err; # error code
if ( $err ) {
croak "Error $err while processing\n";
} else {
return $output;
}
}
}
=head2 marc2modsxml - Convert from ISO-2709 to MODS
my $modsxml = marc2modsxml($marc);
Returns a MODS scalar
=cut
sub marc2modsxml {
my ($marc) = @_;
return _transformWithStylesheet($marc, "/prog/en/xslt/MARC21slim2MODS3-1.xsl");
}
=head2 marc2madsxml - Convert from ISO-2709 to MADS
my $madsxml = marc2madsxml($marc);
Returns a MADS scalar
=cut
sub marc2madsxml {
my ($marc) = @_;
return _transformWithStylesheet($marc, "/prog/en/xslt/MARC21slim2MADS.xsl");
}
=head2 _transformWithStylesheet - Transform a MARC record with a stylesheet
my $xml = _transformWithStylesheet($marc, $stylesheet)
Returns the XML scalar result of the transformation. $stylesheet should
contain the path to a stylesheet under intrahtdocs.
=cut
sub _transformWithStylesheet {
my ($marc, $stylesheet) = @_;
# grab the XML, run it through our stylesheet, push it out to the browser
my $xmlrecord = marc2marcxml($marc);
my $xslfile = C4::Context->config('intrahtdocs') . $stylesheet;
return C4::XSLT::engine->transform($xmlrecord, $xslfile);
}
sub marc2endnote {
my ($marc) = @_;
my $marc_rec_obj = MARC::Record->new_from_usmarc($marc);
my ( $abstract, $f260a, $f710a );
my $f260 = $marc_rec_obj->field('260');
if ($f260) {
$f260a = $f260->subfield('a') if $f260;
}
my $f710 = $marc_rec_obj->field('710');
if ($f710) {
$f710a = $f710->subfield('a');
}
my $f500 = $marc_rec_obj->field('500');
if ($f500) {
$abstract = $f500->subfield('a');
}
my $fields = {
DB => C4::Context->preference("LibraryName"),
Title => $marc_rec_obj->title(),
Author => $marc_rec_obj->author(),
Publisher => $f710a,
City => $f260a,
Year => $marc_rec_obj->publication_date,
Abstract => $abstract,
};
my $style = Biblio::EndnoteStyle->new();
my $template;
$template.= "DB - DB\n" if C4::Context->preference("LibraryName");
$template.="T1 - Title\n" if $marc_rec_obj->title();
$template.="A1 - Author\n" if $marc_rec_obj->author();
$template.="PB - Publisher\n" if $f710a;
$template.="CY - City\n" if $f260a;
$template.="Y1 - Year\n" if $marc_rec_obj->publication_date;
$template.="AB - Abstract\n" if $abstract;
my ($text, $errmsg) = $style->format($template, $fields);
return ($text);
}
=head2 marc2csv - Convert several records from UNIMARC to CSV
my ($csv) = marc2csv($biblios, $csvprofileid, $itemnumbers);
Pre and postprocessing can be done through a YAML file
Returns a CSV scalar
C<$biblio> - a list of biblionumbers
C<$csvprofileid> - the id of the CSV profile to use for the export (see export_format.export_format_id)
C<$itemnumbers> - a list of itemnumbers to export
=cut
sub marc2csv {
my ($biblios, $id, $itemnumbers) = @_;
$itemnumbers ||= [];
my $output;
my $csv = Text::CSV::Encoded->new( { formula => "empty" } );
# Getting yaml file
my $configfile = "../tools/csv-profiles/$id.yaml";
my ($preprocess, $postprocess, $fieldprocessing);
if (-e $configfile){
($preprocess,$postprocess, $fieldprocessing) = YAML::XS::LoadFile($configfile);
}
# Preprocessing
eval $preprocess if ($preprocess); ## no critic (StringyEval)
my $firstpass = 1;
if ( @$itemnumbers ) {
for my $itemnumber ( @$itemnumbers) {
my $item = Koha::Items->find( $itemnumber );
my $biblionumber = $item->biblio->biblionumber;
$output .= marcrecord2csv( $biblionumber, $id, $firstpass, $csv, $fieldprocessing, [$itemnumber] ) // '';
$firstpass = 0;
}
} else {
foreach my $biblio (@$biblios) {
$output .= marcrecord2csv( $biblio, $id, $firstpass, $csv, $fieldprocessing ) // '';
$firstpass = 0;
}
}
# Postprocessing
eval $postprocess if ($postprocess); ## no critic (StringyEval)
return $output;
}
=head2 marcrecord2csv - Convert a single record from UNIMARC to CSV
my ($csv) = marcrecord2csv($biblio, $csvprofileid, $header);
Returns a CSV scalar
C<$biblio> - a biblionumber
C<$csvprofileid> - the id of the CSV profile to use for the export (see export_format.export_format_id)
C<$header> - true if the headers are to be printed (typically at first pass)
C<$csv> - an already initialised Text::CSV object
C<$fieldprocessing>
C<$itemnumbers> a list of itemnumbers to export
=cut
sub marcrecord2csv {
my ($biblionumber, $id, $header, $csv, $fieldprocessing, $itemnumbers) = @_;
my $output;
# Getting the record
my $biblio = Koha::Biblios->find($biblionumber);
return unless $biblio;
my $record = eval { $biblio->metadata->record( { embed_items => 1, itemnumbers => $itemnumbers } ); };
return unless $record;
# Getting the framework
my $frameworkcode = $biblio->frameworkcode;
# Getting information about the csv profile
my $profile = Koha::CsvProfiles->find($id);
# Getting output encoding
my $encoding = $profile->encoding || 'utf8';
# Getting separators
my $csvseparator = $profile->csv_separator || ',';
my $fieldseparator = $profile->field_separator || '#';
my $subfieldseparator = $profile->subfield_separator || '|';
# TODO: Be more generic (in case we have to handle other protected chars or more separators)
if ($csvseparator eq '\t') { $csvseparator = "\t" }
if ($fieldseparator eq '\t') { $fieldseparator = "\t" }
if ($subfieldseparator eq '\t') { $subfieldseparator = "\t" }
if ($csvseparator eq '\n') { $csvseparator = "\n" }
if ($fieldseparator eq '\n') { $fieldseparator = "\n" }
if ($subfieldseparator eq '\n') { $subfieldseparator = "\n" }
$csv = $csv->encoding_out($encoding) ;
$csv->sep_char($csvseparator);
# Getting the marcfields
my $marcfieldslist = $profile->content;
# Getting the marcfields as an array
my @marcfieldsarray = split('\|', $marcfieldslist);
# Separating the marcfields from the user-supplied headers
my @csv_structures;
foreach (@marcfieldsarray) {
my @result = split('=', $_, 2);
my $content = ( @result == 2 )
? $result[1]
: $result[0];
my @fields;
while ( $content =~ m|(\d{3})\$?(.)?|g ) {
my $fieldtag = $1;
my $subfieldtag = $2;
push @fields, { fieldtag => $fieldtag, subfieldtag => $subfieldtag };
}
if ( @result == 2) {
push @csv_structures, { header => $result[0], content => $content, fields => \@fields };
} else {
push @csv_structures, { content => $content, fields => \@fields }
}
}
my ( @marcfieldsheaders, @csv_rows );
my $dbh = C4::Context->dbh;
my $field_list;
for my $field ( $record->fields ) {
my $fieldtag = $field->tag;
my $values;
if ( $field->is_control_field ) {
$values = $field->data();
} else {
$values->{indicator}{1} = $field->indicator(1);
$values->{indicator}{2} = $field->indicator(2);
for my $subfield ( $field->subfields ) {
my $subfieldtag = $subfield->[0];
my $value = $subfield->[1];
push @{ $values->{$subfieldtag} }, $value;
}
}
# We force the key as an integer (trick for 00X and OXX fields)
push @{ $field_list->{fields}{0+$fieldtag} }, $values;
}
# For each field or subfield
foreach my $csv_structure (@csv_structures) {
my @field_values;
my $tags = $csv_structure->{fields};
my $content = $csv_structure->{content};
if ( $header ) {
# If we have a user-supplied header, we use it
if ( exists $csv_structure->{header} ) {
push @marcfieldsheaders, $csv_structure->{header};
} else {
# If not, we get the matching tag name from koha
my $tag = $tags->[0];
if (defined $tag->{subfieldtag} ) {
my $query = "SELECT liblibrarian FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield=?";
my @results = $dbh->selectrow_array( $query, {}, $tag->{fieldtag}, $tag->{subfieldtag} );
push @marcfieldsheaders, $results[0];
} else {
my $query = "SELECT liblibrarian FROM marc_tag_structure WHERE tagfield=?";
my @results = $dbh->selectrow_array( $query, {}, $tag->{fieldtag} );
push @marcfieldsheaders, $results[0];
}
}
}
# TT tags exist
if ( $content =~ m|\[\%.*\%\]| ) {
# Replace 00X and 0XX with X or XX
$content =~ s|fields.00(\d)|fields.$1|g;
$content =~ s|fields.0(\d{2})|fields.$1|g;
my $tt_output = process_tt( $content, $field_list );
push @csv_rows, $tt_output;
} else {
for my $tag ( @$tags ) {
my @fields = $record->field( $tag->{fieldtag} );
# If it is a subfield
my @loop_values;
if (defined $tag->{subfieldtag} ) {
my $av_description_mapping = Koha::AuthorisedValues->get_descriptions_by_marc_field(
{
frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag},
tagsubfield => $tag->{subfieldtag},
}
);
# For each field
foreach my $field (@fields) {
my @subfields = $field->subfield( $tag->{subfieldtag} );
foreach my $subfield (@subfields) {
push @loop_values, (defined $av_description_mapping->{$subfield}) ? $av_description_mapping->{$subfield} : $subfield;
}
}
# Or a field
} else {
foreach my $field ( @fields ) {
my $value;
# If it is a control field
if ($field->is_control_field) {
my $authvalues = Koha::AuthorisedValues->get_descriptions_by_marc_field(
{ frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag}, } );
$value = defined $authvalues->{$field->as_string} ? $authvalues->{$field->as_string} : $field->as_string;
} else {
# If it is a field, we gather all subfields, joined by the subfield separator
my @subvaluesarray;
my @subfields = $field->subfields;
foreach my $subfield (@subfields) {
my $authvalues = Koha::AuthorisedValues->get_descriptions_by_marc_field(
{
frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag},
tagsubfield => $subfield->[0],
}
);
push (@subvaluesarray, defined $authvalues->{$subfield->[1]} ? $authvalues->{$subfield->[1]} : $subfield->[1]);
}
$value = join ($subfieldseparator, @subvaluesarray);
}
# Field processing
my $marcfield = $tag->{fieldtag}; # This line fixes a retrocompatibility concern
# The "processing" could be based on the $marcfield variable.
eval $fieldprocessing if ($fieldprocessing); ## no critic (StringyEval)
push @loop_values, $value;
}
}
push @field_values, {
fieldtag => $tag->{fieldtag},
subfieldtag => $tag->{subfieldtag},
values => \@loop_values,
};
}
for my $field_value ( @field_values ) {
if ( $field_value->{subfieldtag} ) {
push @csv_rows, join( $subfieldseparator, @{ $field_value->{values} } );
} else {
push @csv_rows, join( $fieldseparator, @{ $field_value->{values} } );
}
}
}
}
if ( $header ) {
$csv->combine(@marcfieldsheaders);
$output = $csv->string() . "\n";
}
$csv->combine(@csv_rows);
$output .= $csv->string() . "\n";
return $output;
}
=head2 changeEncoding - Change the encoding of a record
my ($error, $newrecord) = changeEncoding($record,$format,$flavour,$to_encoding,$from_encoding);
Changes the encoding of a record
C<$record> - the record itself can be in ISO-2709, a MARC::Record object, or MARCXML for now (required)
C<$format> - MARC or MARCXML (required)
C<$flavour> - MARC21 or UNIMARC, if MARC21, it will change the leader (optional) [defaults to Koha system preference]
C<$to_encoding> - the encoding you want the record to end up in (optional) [UTF-8]
C<$from_encoding> - the encoding the record is currently in (optional, it will probably be able to tell unless there's a problem with the record)
FIXME: the from_encoding doesn't work yet
FIXME: better handling for UNIMARC, it should allow management of 100 field
FIXME: shouldn't have to convert to and from xml/marc just to change encoding someone needs to re-write MARC::Record's 'encoding' method to actually alter the encoding rather than just changing the leader
=cut
sub changeEncoding {
my ($record,$format,$flavour,$to_encoding,$from_encoding) = @_;
my $newrecord;
my $error;
unless($flavour) {$flavour = C4::Context->preference("marcflavour")};
unless($to_encoding) {$to_encoding = "UTF-8"};
# ISO-2709 Record (MARC21 or UNIMARC)
if (lc($format) =~ /^marc$/o) {
# if we're converting encoding of an ISO2709 file, we need to roundtrip through XML
# because MARC::Record doesn't directly provide us with an encoding method
# It's definitely less than idea and should be fixed eventually - kados
my $marcxml; # temporary storage of MARCXML scalar
($error,$marcxml) = marc2marcxml($record,$to_encoding,$flavour);
unless ($error) {
($error,$newrecord) = marcxml2marc($marcxml,$to_encoding,$flavour);
}
# MARCXML Record
} elsif (lc($format) =~ /^marcxml$/o) { # MARCXML Record
my $marc;
($error,$marc) = marcxml2marc($record,$to_encoding,$flavour);
unless ($error) {
($error,$newrecord) = marc2marcxml($record,$to_encoding,$flavour);
}
} else {
$error.="Unsupported record format:".$format;
}
return ($error,$newrecord);
}
=head2 marc2bibtex - Convert from MARC21 and UNIMARC to BibTex
my ($bibtex) = marc2bibtex($record, $id);
Returns a BibTex scalar
C<$record> - a MARC::Record object
C<$id> - an id for the BibTex record (might be the biblionumber)
=cut
sub marc2bibtex {
my ($record, $id) = @_;
my $tex;
my $marcflavour = C4::Context->preference("marcflavour");
# Authors
my $author;
my @texauthors;
my @authorFields = ('100','110','111','700','710','711');
@authorFields = ('700','701','702','710','711','721') if ( $marcflavour eq "UNIMARC" );
foreach my $field ( @authorFields ) {
# author formatted surname, firstname
my $texauthor = '';
if ( $marcflavour eq "UNIMARC" ) {
$texauthor = join ', ',
( $record->subfield($field,"a"), $record->subfield($field,"b") );
} else {
$texauthor = $record->subfield($field,"a");
}
push @texauthors, $texauthor if $texauthor;
}
$author = join ' and ', @texauthors;
# Defining the conversion array according to the marcflavour
my @bh;
if ( $marcflavour eq "UNIMARC" ) {
# FIXME, TODO : handle repeatable fields
# TODO : handle more types of documents
# Unimarc to bibtex array
@bh = (
# Mandatory
author => $author,
title => $record->subfield("200", "a") || "",
editor => $record->subfield("210", "g") || "",
publisher => $record->subfield("210", "c") || "",
year => $record->subfield("210", "d") || $record->subfield("210", "h") || "",
# Optional
volume => $record->subfield("200", "v") || "",
series => $record->subfield("225", "a") || "",
address => $record->subfield("210", "a") || "",
edition => $record->subfield("205", "a") || "",
note => $record->subfield("300", "a") || "",
url => $record->subfield("856", "u") || ""
);
} else {
# Marc21 to bibtex array
@bh = (
# Mandatory
author => $author,
title => $record->subfield("245", "a") || "",
editor => $record->subfield("260", "f") || "",
publisher => $record->subfield("264", "b") || $record->subfield("260", "b") || "",
year => $record->subfield("264", "c") || $record->subfield("260", "c") || $record->subfield("260", "g") || "",
# Optional
# unimarc to marc21 specification says not to convert 200$v to marc21
series => $record->subfield("490", "a") || "",
address => $record->subfield("264", "a") || $record->subfield("260", "a") || "",
edition => $record->subfield("250", "a") || "",
note => $record->subfield("500", "a") || "",
url => $record->subfield("856", "u") || ""
);
}
my $additional_fields = C4::Context->yaml_preference('BibtexExportAdditionalFields');
if ( $additional_fields && $additional_fields->{'@'} ) {
my ( $f, $sf ) = split( /\$/, $additional_fields->{'@'} );
my ( $type ) = read_field( { record => $record, field => $f, subfield => $sf, field_numbers => [1] } );
if ($type) {
$tex .= '@' . $type . '{';
}
else {
$tex .= "\@book{";
}
}
else {
$tex .= "\@book{";
}
my @elt;
for ( my $i = 0 ; $i < scalar( @bh ) ; $i = $i + 2 ) {
next unless $bh[$i+1];
push @elt, qq|\t$bh[$i] = {$bh[$i+1]}|;
}
$tex .= join(",\n", $id, @elt);
if ($additional_fields) {
$tex .= ",\n";
foreach my $bibtex_tag ( keys %$additional_fields ) {
next if $bibtex_tag eq '@';
my @fields =
ref( $additional_fields->{$bibtex_tag} ) eq 'ARRAY'
? @{ $additional_fields->{$bibtex_tag} }
: $additional_fields->{$bibtex_tag};
for my $tag (@fields) {
my ( $f, $sf ) = split( /\$/, $tag );
my @values = read_field( { record => $record, field => $f, subfield => $sf } );
foreach my $v (@values) {
$tex .= qq(\t$bibtex_tag = {$v}\n);
}
}
}
}
else {
$tex .= "\n";
}
$tex .= "}\n";
return $tex;
}
=head2 marc2cites - Convert from MARC21 and UNIMARC to citations
my $cites = marc2cites($record);
Returns hashref of citation from MARC data, suitable to pass to templates.
Hash keys store citation system names, hash values citation string.
C<$record> - a MARC::Record object
=cut
sub marc2cites {
my $record = shift;
my $marcflavour = C4::Context->preference("marcflavour");
my %cites = ();
my @authors = ();
my $re_clean = qr/(^[\.,:;\/\-\s]+|[\.,:;\/\-\s]+$)/;
my @authorFields = ( '100', '110', '111', '700', '710', '711' );
@authorFields = ( '700', '701', '702', '710', '711', '721' ) if ( $marcflavour eq "UNIMARC" );
foreach my $ftag (@authorFields) {
foreach my $field ( $record->field($ftag) ) {
my $author = '';
if ( $marcflavour eq "UNIMARC" ) {
$author = join ', ',
( $field->subfield("a"), $field->subfield("b") );
} else {
$author = $field->subfield("a");
}
if ( $author =~ /([^,]+),?(.*)/ ) {
my %a;
( $a{'surname'} = $1 ) =~ s/$re_clean//g;
$a{'forenames'} = [ map { my $t = $_; $t =~ s/$re_clean//g; $t } split ' ', $2 ];
push( @authors, \%a );
}
}
}
my %publication;
if ( $marcflavour eq "UNIMARC" ) {
%publication = (
title => $record->subfield( "200", "a" ) || "",
place => $record->subfield( "210", "a" ) || "",
publisher => $record->subfield( "210", "c" ) || "",
date => $record->subfield( "210", "d" ) || $record->subfield( "210", "h" ) || ""
);
} else {
%publication = (
title => $record->subfield( "245", "a" ) || "",
place => $record->subfield( "264", "a" ) || $record->subfield( "260", "a" ) || "",
publisher => $record->subfield( "264", "b" ) || $record->subfield( "260", "b" ) || "",
date => $record->subfield( "264", "c" )
|| $record->subfield( "260", "c" )
|| $record->subfield( "260", "g" )
|| ""
);
}
$publication{$_} =~ s/$re_clean//g for keys %publication;
$publication{'date'} =~ s/[\D-]//g;
my $i = $#authors;
my $last = 0;
my $seclast = 0;
for my $author (@authors) {
$cites{'Harvard'} .= $author->{'surname'} . ' ';
$cites{'Harvard'} .= substr( $_, 0, 1 ) . '. ' for @{ $author->{'forenames'} };
$cites{'Harvard'} =~ s/\s+$//;
$cites{'Harvard'} .= $last ? '' : ( $seclast ? ' and ' : ', ' );
$cites{'Chicago'} .= $author->{'surname'} . ' ';
$cites{'Chicago'} .= $_ . ' ' for @{ $author->{'forenames'} };
$cites{'Chicago'} =~ s/\s+$//;
$cites{'Chicago'} .= $last ? '' : ( $seclast ? ' and ' : ', ' );
$cites{'MLA'} .= $author->{'surname'} . ' ';
$cites{'MLA'} .= $_ . ' ' for @{ $author->{'forenames'} };
$cites{'MLA'} =~ s/\s+$//;
$cites{'MLA'} .= $last ? '' : ( $seclast ? ' and ' : ', ' );
$cites{'APA'} .= $author->{'surname'} . ' ';
$cites{'APA'} .= substr( $_, 0, 1 ) . '. ' for @{ $author->{'forenames'} };
$cites{'APA'} =~ s/\s+$//;
$cites{'APA'} .= $last ? '' : ( $seclast ? ' & ' : ', ' );
$seclast = $#authors > 1 && $i-- == 2;
$last = $i == 0;
}
$cites{$_} =~ s/([^\.])$/$1./ for keys %cites;
if ( $publication{date} ) {
$cites{'Harvard'} .= ' (' . $publication{'date'} . '). ';
$cites{'Chicago'} .= ' ' . $publication{'date'} . '. ';
$cites{'MLA'} .= ' ' . $publication{'title'} . '. ';
$cites{'APA'} .= ' (' . $publication{'date'} . '). ';
}
$cites{'Harvard'} .= $publication{'title'} . '. ';
$cites{'Chicago'} .= $publication{'title'} . '. ';
$cites{'MLA'} .= $publication{'place'} . ': ';
$cites{'APA'} .= $publication{'title'} . '. ';
$cites{'Harvard'} .= $publication{'place'} . ': ';
$cites{'Chicago'} .= $publication{'place'} . ': ';
$cites{'MLA'} .= $publication{'publisher'} . '. ';
$cites{'APA'} .= $publication{'place'} . ': ';
$cites{'Harvard'} .= $publication{'publisher'};
$cites{'Chicago'} .= $publication{'publisher'};
$cites{'MLA'} .= $publication{'date'};
$cites{'APA'} .= $publication{'publisher'};
$cites{$_} =~ s/ +/ / for keys %cites;
$cites{$_} =~ s/([^\.])$/$1./ for keys %cites;
return \%cites;
}
=head1 INTERNAL FUNCTIONS
=head2 _entity_encode - Entity-encode an array of strings
my ($entity_encoded_string) = _entity_encode($string);
or
my (@entity_encoded_strings) = _entity_encode(@strings);
Entity-encode an array of strings
=cut
sub _entity_encode {
my @strings = @_;
my @strings_entity_encoded;
foreach my $string (@strings) {
my $nfc_string = NFC($string);
$nfc_string =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
push @strings_entity_encoded, $nfc_string;
}
return @strings_entity_encoded;
}
END { } # module clean-up code here (global destructor)
1;
__END__
=head1 AUTHOR
Joshua Ferraro <[email protected]>
=head1 MODIFICATIONS
=cut