3 # Copyright 2006 (C) LibLime
4 # Parts copyright 2010 BibLibre
5 # Part copyright 2015 Universidad de El Salvador
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 # please specify in which methods a given module is used
25 use MARC::Record; # marc2marcxml, marcxml2marc, changeEncoding
26 use MARC::File::XML; # marc2marcxml, marcxml2marc, changeEncoding
27 use Biblio::EndnoteStyle;
28 use Unicode::Normalize qw( NFC ); # _entity_encode
29 use C4::Biblio qw( GetFrameworkCode );
30 use C4::Koha; #marc2csv
32 use YAML::XS; #marcrecords2csv
35 use Text::CSV::Encoded; #marc2csv
37 use Koha::SimpleMARC qw( read_field );
39 use Koha::CsvProfiles;
40 use Koha::AuthorisedValues;
41 use Koha::TemplateUtils qw( process_tt );
42 use Carp qw( carp croak );
44 use vars qw(@ISA @EXPORT);
49 # only export API methods
67 C4::Record - MARC, MARCXML, DC, MODS, XML, etc. Record Management Functions and API
71 New in Koha 3.x. This module handles all record-related management functions.
73 =head1 API (EXPORTED FUNCTIONS)
75 =head2 marc2marc - Convert from one flavour of ISO-2709 to another
77 my ($error,$newmarc) = marc2marc($marc,$to_flavour,$from_flavour,$encoding);
79 Returns an ISO-2709 scalar
84 my ($marc,$to_flavour,$from_flavour,$encoding) = @_;
86 if ($to_flavour && $to_flavour =~ m/marcstd/) {
88 if ($marc =~ /^MARC::Record/) { # it's already a MARC::Record object
89 $marc_record_obj = $marc;
90 } else { # it's not a MARC::Record object, make it one
91 eval { $marc_record_obj = MARC::Record->new_from_usmarc($marc) }; # handle exceptions
93 # conversion to MARC::Record object failed, populate $error
94 if ($@) { $error .="\nCreation of MARC::Record object failed: ".$MARC::File::ERROR };
98 foreach my $field ($marc_record_obj->fields()) {
99 if ($field->tag() =~ m/9/ && ($field->tag() != '490' || C4::Context->preference("marcflavour") eq 'UNIMARC')) {
100 push @privatefields, $field;
101 } elsif (! ($field->is_control_field())) {
102 $field->delete_subfield(code => '9') if ($field->subfield('9'));
105 $marc_record_obj->delete_field($_) for @privatefields;
106 $marc = $marc_record_obj->as_usmarc();
109 $error = "Feature not yet implemented\n";
111 return ($error,$marc);
114 =head2 marc2marcxml - Convert from ISO-2709 to MARCXML
116 my ($error,$marcxml) = marc2marcxml($marc,$encoding,$flavour);
118 Returns a MARCXML scalar
120 C<$marc> - an ISO-2709 scalar or MARC::Record object
122 C<$encoding> - UTF-8 or MARC-8 [UTF-8]
124 C<$flavour> - MARC21 or UNIMARC
126 C<$dont_entity_encode> - a flag that instructs marc2marcxml not to entity encode the xml before returning (optional)
131 my ($marc,$encoding,$flavour,$dont_entity_encode) = @_;
132 my $error; # the error string
133 my $marcxml; # the final MARCXML scalar
135 # test if it's already a MARC::Record object, if not, make it one
137 if ($marc =~ /^MARC::Record/) { # it's already a MARC::Record object
138 $marc_record_obj = $marc;
139 } else { # it's not a MARC::Record object, make it one
140 eval { $marc_record_obj = MARC::Record->new_from_usmarc($marc) }; # handle exceptions
142 # conversion to MARC::Record object failed, populate $error
143 if ($@) { $error .="\nCreation of MARC::Record object failed: ".$MARC::File::ERROR };
145 # only proceed if no errors so far
148 # check the record for warnings
149 my @warnings = $marc_record_obj->warnings();
151 warn "\nWarnings encountered while processing ISO-2709 record with title \"".$marc_record_obj->title()."\":\n";
152 foreach my $warn (@warnings) { warn "\t".$warn };
154 unless($encoding) {$encoding = "UTF-8"}; # set default encoding
155 unless($flavour) {$flavour = C4::Context->preference("marcflavour")}; # set default MARC flavour
157 # attempt to convert the record to MARCXML
158 eval { $marcxml = $marc_record_obj->as_xml_record($flavour) }; #handle exceptions
160 # record creation failed, populate $error
162 $error .= "Creation of MARCXML failed:".$MARC::File::ERROR;
163 $error .= "Additional information:\n";
164 my @warnings = $@->warnings();
165 foreach my $warn (@warnings) { $error.=$warn."\n" };
167 # record creation was successful
170 # check the record for warning flags again (warnings() will be cleared already if there was an error, see above block
171 @warnings = $marc_record_obj->warnings();
173 warn "\nWarnings encountered while processing ISO-2709 record with title \"".$marc_record_obj->title()."\":\n";
174 foreach my $warn (@warnings) { warn "\t".$warn };
178 # only proceed if no errors so far
181 # entity encode the XML unless instructed not to
182 unless ($dont_entity_encode) {
183 my ($marcxml_entity_encoded) = _entity_encode($marcxml);
184 $marcxml = $marcxml_entity_encoded;
188 # return result to calling program
189 return ($error,$marcxml);
192 =head2 marcxml2marc - Convert from MARCXML to ISO-2709
194 my ($error,$marc) = marcxml2marc($marcxml,$encoding,$flavour);
196 Returns an ISO-2709 scalar
198 C<$marcxml> - a MARCXML record
200 C<$encoding> - UTF-8 or MARC-8 [UTF-8]
202 C<$flavour> - MARC21 or UNIMARC
207 my ($marcxml,$encoding,$flavour) = @_;
208 my $error; # the error string
209 my $marc; # the final ISO-2709 scalar
210 unless($encoding) {$encoding = "UTF-8"}; # set the default encoding
211 unless($flavour) {$flavour = C4::Context->preference("marcflavour")}; # set the default MARC flavour
213 # attempt to do the conversion
214 eval { $marc = MARC::Record->new_from_xml($marcxml,$encoding,$flavour) }; # handle exceptions
216 # record creation failed, populate $error
217 if ($@) {$error .="\nCreation of MARCXML Record failed: ".$@;
218 $error.=$MARC::File::ERROR if ($MARC::File::ERROR);
220 # return result to calling program
221 return ($error,$marc);
224 =head2 marc2dcxml - Convert from ISO-2709 to Dublin Core
226 my dcxml = marc2dcxml ($marc, $xml, $biblionumber, $format);
230 my dcxml = marc2dcxml (undef, undef, 1, "oaidc");
232 Convert MARC or MARCXML to Dublin Core metadata (XSLT Transformation),
233 optionally can get an XML directly from biblio_metadata
234 without item information. This method take into consideration the syspref
235 'marcflavour' (UNIMARC or MARC21).
236 Return an XML file with the format defined in C<$format>
238 C<$marc> - an ISO-2709 scalar or MARC::Record object
240 C<$xml> - a MARCXML file
242 C<$biblionumber> - biblionumber for database access
244 C<$format> - accept three type of DC formats (oaidc, srwdc, and rdfdc )
249 my ( $marc, $xml, $biblionumber, $format ) = @_;
252 my ( $marcxml, $record, $output );
254 # set the default path for intranet xslts
255 # differents xslts to process (OAIDC, SRWDC and RDFDC)
256 my $xsl = C4::Context->config('intrahtdocs') . '/prog/en/xslt/' .
257 C4::Context->preference('marcflavour') . 'slim2' . uc ( $format ) . '.xsl';
259 if ( defined $marc ) {
260 # no need to catch errors or warnings marc2marcxml do it instead
261 $marcxml = C4::Record::marc2marcxml( $marc );
262 } elsif ( not defined $xml and defined $biblionumber ) {
263 # get MARCXML biblio directly without item information
264 $marcxml = C4::Biblio::GetXmlBiblio( $biblionumber );
269 eval { $record = MARC::Record->new_from_xml(
272 C4::Context->preference('marcflavour')
276 # conversion to MARC::Record object failed
278 croak "Creation of MARC::Record object failed.";
279 } elsif ( $record->warnings() ) {
280 carp "Warnings encountered while processing ISO-2709 record.\n";
281 my @warnings = $record->warnings();
282 foreach my $warn (@warnings) {
285 } elsif ( $record =~ /^MARC::Record/ ) { # if OK makes xslt transformation
286 my $xslt_engine = Koha::XSLT::Base->new;
287 if ( $format =~ /^(dc|oaidc|srwdc|rdfdc)$/i ) {
288 $output = $xslt_engine->transform( $marcxml, $xsl );
290 croak "The format argument ($format) not accepted.\n" .
291 "Please pass a valid format (oaidc, srwdc, or rdfdc)\n";
293 my $err = $xslt_engine->err; # error code
295 croak "Error $err while processing\n";
302 =head2 marc2modsxml - Convert from ISO-2709 to MODS
304 my $modsxml = marc2modsxml($marc);
306 Returns a MODS scalar
312 return _transformWithStylesheet($marc, "/prog/en/xslt/MARC21slim2MODS3-1.xsl");
315 =head2 marc2madsxml - Convert from ISO-2709 to MADS
317 my $madsxml = marc2madsxml($marc);
319 Returns a MADS scalar
325 return _transformWithStylesheet($marc, "/prog/en/xslt/MARC21slim2MADS.xsl");
328 =head2 _transformWithStylesheet - Transform a MARC record with a stylesheet
330 my $xml = _transformWithStylesheet($marc, $stylesheet)
332 Returns the XML scalar result of the transformation. $stylesheet should
333 contain the path to a stylesheet under intrahtdocs.
337 sub _transformWithStylesheet {
338 my ($marc, $stylesheet) = @_;
339 # grab the XML, run it through our stylesheet, push it out to the browser
340 my $xmlrecord = marc2marcxml($marc);
341 my $xslfile = C4::Context->config('intrahtdocs') . $stylesheet;
342 return C4::XSLT::engine->transform($xmlrecord, $xslfile);
347 my $marc_rec_obj = MARC::Record->new_from_usmarc($marc);
348 my ( $abstract, $f260a, $f710a );
349 my $f260 = $marc_rec_obj->field('260');
351 $f260a = $f260->subfield('a') if $f260;
353 my $f710 = $marc_rec_obj->field('710');
355 $f710a = $f710->subfield('a');
357 my $f500 = $marc_rec_obj->field('500');
359 $abstract = $f500->subfield('a');
362 DB => C4::Context->preference("LibraryName"),
363 Title => $marc_rec_obj->title(),
364 Author => $marc_rec_obj->author(),
367 Year => $marc_rec_obj->publication_date,
368 Abstract => $abstract,
370 my $style = Biblio::EndnoteStyle->new();
372 $template.= "DB - DB\n" if C4::Context->preference("LibraryName");
373 $template.="T1 - Title\n" if $marc_rec_obj->title();
374 $template.="A1 - Author\n" if $marc_rec_obj->author();
375 $template.="PB - Publisher\n" if $f710a;
376 $template.="CY - City\n" if $f260a;
377 $template.="Y1 - Year\n" if $marc_rec_obj->publication_date;
378 $template.="AB - Abstract\n" if $abstract;
379 my ($text, $errmsg) = $style->format($template, $fields);
384 =head2 marc2csv - Convert several records from UNIMARC to CSV
386 my ($csv) = marc2csv($biblios, $csvprofileid, $itemnumbers);
388 Pre and postprocessing can be done through a YAML file
392 C<$biblio> - a list of biblionumbers
394 C<$csvprofileid> - the id of the CSV profile to use for the export (see export_format.export_format_id)
396 C<$itemnumbers> - a list of itemnumbers to export
401 my ($biblios, $id, $itemnumbers) = @_;
404 my $csv = Text::CSV::Encoded->new();
407 my $configfile = "../tools/csv-profiles/$id.yaml";
408 my ($preprocess, $postprocess, $fieldprocessing);
410 ($preprocess,$postprocess, $fieldprocessing) = YAML::XS::LoadFile($configfile);
414 eval $preprocess if ($preprocess); ## no critic (StringyEval)
417 if ( @$itemnumbers ) {
418 for my $itemnumber ( @$itemnumbers) {
419 my $item = Koha::Items->find( $itemnumber );
420 my $biblionumber = $item->biblio->biblionumber;
421 $output .= marcrecord2csv( $biblionumber, $id, $firstpass, $csv, $fieldprocessing, [$itemnumber] ) // '';
425 foreach my $biblio (@$biblios) {
426 $output .= marcrecord2csv( $biblio, $id, $firstpass, $csv, $fieldprocessing ) // '';
432 eval $postprocess if ($postprocess); ## no critic (StringyEval)
437 =head2 marcrecord2csv - Convert a single record from UNIMARC to CSV
439 my ($csv) = marcrecord2csv($biblio, $csvprofileid, $header);
443 C<$biblio> - a biblionumber
445 C<$csvprofileid> - the id of the CSV profile to use for the export (see export_format.export_format_id)
447 C<$header> - true if the headers are to be printed (typically at first pass)
449 C<$csv> - an already initialised Text::CSV object
453 C<$itemnumbers> a list of itemnumbers to export
458 my ($biblionumber, $id, $header, $csv, $fieldprocessing, $itemnumbers) = @_;
462 my $biblio = Koha::Biblios->find($biblionumber);
463 return unless $biblio;
465 $biblio->metadata->record(
466 { embed_items => 1, itemnumbers => $itemnumbers } );
468 return unless $record;
469 # Getting the framework
470 my $frameworkcode = $biblio->frameworkcode;
472 # Getting information about the csv profile
473 my $profile = Koha::CsvProfiles->find($id);
475 # Getting output encoding
476 my $encoding = $profile->encoding || 'utf8';
478 my $csvseparator = $profile->csv_separator || ',';
479 my $fieldseparator = $profile->field_separator || '#';
480 my $subfieldseparator = $profile->subfield_separator || '|';
482 # TODO: Be more generic (in case we have to handle other protected chars or more separators)
483 if ($csvseparator eq '\t') { $csvseparator = "\t" }
484 if ($fieldseparator eq '\t') { $fieldseparator = "\t" }
485 if ($subfieldseparator eq '\t') { $subfieldseparator = "\t" }
486 if ($csvseparator eq '\n') { $csvseparator = "\n" }
487 if ($fieldseparator eq '\n') { $fieldseparator = "\n" }
488 if ($subfieldseparator eq '\n') { $subfieldseparator = "\n" }
490 $csv = $csv->encoding_out($encoding) ;
491 $csv->sep_char($csvseparator);
493 # Getting the marcfields
494 my $marcfieldslist = $profile->content;
496 # Getting the marcfields as an array
497 my @marcfieldsarray = split('\|', $marcfieldslist);
499 # Separating the marcfields from the user-supplied headers
501 foreach (@marcfieldsarray) {
502 my @result = split('=', $_, 2);
503 my $content = ( @result == 2 )
507 while ( $content =~ m|(\d{3})\$?(.)?|g ) {
509 my $subfieldtag = $2;
510 push @fields, { fieldtag => $fieldtag, subfieldtag => $subfieldtag };
513 push @csv_structures, { header => $result[0], content => $content, fields => \@fields };
515 push @csv_structures, { content => $content, fields => \@fields }
519 my ( @marcfieldsheaders, @csv_rows );
520 my $dbh = C4::Context->dbh;
523 for my $field ( $record->fields ) {
524 my $fieldtag = $field->tag;
526 if ( $field->is_control_field ) {
527 $values = $field->data();
529 $values->{indicator}{1} = $field->indicator(1);
530 $values->{indicator}{2} = $field->indicator(2);
531 for my $subfield ( $field->subfields ) {
532 my $subfieldtag = $subfield->[0];
533 my $value = $subfield->[1];
534 push @{ $values->{$subfieldtag} }, $value;
537 # We force the key as an integer (trick for 00X and OXX fields)
538 push @{ $field_list->{fields}{0+$fieldtag} }, $values;
541 # For each field or subfield
542 foreach my $csv_structure (@csv_structures) {
544 my $tags = $csv_structure->{fields};
545 my $content = $csv_structure->{content};
548 # If we have a user-supplied header, we use it
549 if ( exists $csv_structure->{header} ) {
550 push @marcfieldsheaders, $csv_structure->{header};
552 # If not, we get the matching tag name from koha
553 my $tag = $tags->[0];
554 if (defined $tag->{subfieldtag} ) {
555 my $query = "SELECT liblibrarian FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield=?";
556 my @results = $dbh->selectrow_array( $query, {}, $tag->{fieldtag}, $tag->{subfieldtag} );
557 push @marcfieldsheaders, $results[0];
559 my $query = "SELECT liblibrarian FROM marc_tag_structure WHERE tagfield=?";
560 my @results = $dbh->selectrow_array( $query, {}, $tag->{fieldtag} );
561 push @marcfieldsheaders, $results[0];
567 if ( $content =~ m|\[\%.*\%\]| ) {
568 # Replace 00X and 0XX with X or XX
569 $content =~ s|fields.00(\d)|fields.$1|g;
570 $content =~ s|fields.0(\d{2})|fields.$1|g;
571 my $tt_output = process_tt( $content, $field_list );
572 push @csv_rows, $tt_output;
574 for my $tag ( @$tags ) {
575 my @fields = $record->field( $tag->{fieldtag} );
576 # If it is a subfield
578 if (defined $tag->{subfieldtag} ) {
579 my $av_description_mapping = Koha::AuthorisedValues->get_descriptions_by_marc_field(
581 frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag},
582 tagsubfield => $tag->{subfieldtag},
586 foreach my $field (@fields) {
587 my @subfields = $field->subfield( $tag->{subfieldtag} );
588 foreach my $subfield (@subfields) {
589 push @loop_values, (defined $av_description_mapping->{$subfield}) ? $av_description_mapping->{$subfield} : $subfield;
596 foreach my $field ( @fields ) {
599 # If it is a control field
600 if ($field->is_control_field) {
601 my $authvalues = Koha::AuthorisedValues->get_descriptions_by_marc_field(
602 { frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag}, } );
603 $value = defined $authvalues->{$field->as_string} ? $authvalues->{$field->as_string} : $field->as_string;
605 # If it is a field, we gather all subfields, joined by the subfield separator
607 my @subfields = $field->subfields;
608 foreach my $subfield (@subfields) {
609 my $authvalues = Koha::AuthorisedValues->get_descriptions_by_marc_field(
611 frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag},
612 tagsubfield => $subfield->[0],
615 push (@subvaluesarray, defined $authvalues->{$subfield->[1]} ? $authvalues->{$subfield->[1]} : $subfield->[1]);
617 $value = join ($subfieldseparator, @subvaluesarray);
621 my $marcfield = $tag->{fieldtag}; # This line fixes a retrocompatibility concern
622 # The "processing" could be based on the $marcfield variable.
623 eval $fieldprocessing if ($fieldprocessing); ## no critic (StringyEval)
625 push @loop_values, $value;
629 push @field_values, {
630 fieldtag => $tag->{fieldtag},
631 subfieldtag => $tag->{subfieldtag},
632 values => \@loop_values,
635 for my $field_value ( @field_values ) {
636 if ( $field_value->{subfieldtag} ) {
637 push @csv_rows, join( $subfieldseparator, @{ $field_value->{values} } );
639 push @csv_rows, join( $fieldseparator, @{ $field_value->{values} } );
647 $csv->combine(@marcfieldsheaders);
648 $output = $csv->string() . "\n";
650 $csv->combine(@csv_rows);
651 $output .= $csv->string() . "\n";
658 =head2 changeEncoding - Change the encoding of a record
660 my ($error, $newrecord) = changeEncoding($record,$format,$flavour,$to_encoding,$from_encoding);
662 Changes the encoding of a record
664 C<$record> - the record itself can be in ISO-2709, a MARC::Record object, or MARCXML for now (required)
666 C<$format> - MARC or MARCXML (required)
668 C<$flavour> - MARC21 or UNIMARC, if MARC21, it will change the leader (optional) [defaults to Koha system preference]
670 C<$to_encoding> - the encoding you want the record to end up in (optional) [UTF-8]
672 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)
674 FIXME: the from_encoding doesn't work yet
676 FIXME: better handling for UNIMARC, it should allow management of 100 field
678 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
683 my ($record,$format,$flavour,$to_encoding,$from_encoding) = @_;
686 unless($flavour) {$flavour = C4::Context->preference("marcflavour")};
687 unless($to_encoding) {$to_encoding = "UTF-8"};
689 # ISO-2709 Record (MARC21 or UNIMARC)
690 if (lc($format) =~ /^marc$/o) {
691 # if we're converting encoding of an ISO2709 file, we need to roundtrip through XML
692 # because MARC::Record doesn't directly provide us with an encoding method
693 # It's definitely less than idea and should be fixed eventually - kados
694 my $marcxml; # temporary storage of MARCXML scalar
695 ($error,$marcxml) = marc2marcxml($record,$to_encoding,$flavour);
697 ($error,$newrecord) = marcxml2marc($marcxml,$to_encoding,$flavour);
701 } elsif (lc($format) =~ /^marcxml$/o) { # MARCXML Record
703 ($error,$marc) = marcxml2marc($record,$to_encoding,$flavour);
705 ($error,$newrecord) = marc2marcxml($record,$to_encoding,$flavour);
708 $error.="Unsupported record format:".$format;
710 return ($error,$newrecord);
713 =head2 marc2bibtex - Convert from MARC21 and UNIMARC to BibTex
715 my ($bibtex) = marc2bibtex($record, $id);
717 Returns a BibTex scalar
719 C<$record> - a MARC::Record object
721 C<$id> - an id for the BibTex record (might be the biblionumber)
727 my ($record, $id) = @_;
729 my $marcflavour = C4::Context->preference("marcflavour");
734 my @authorFields = ('100','110','111','700','710','711');
735 @authorFields = ('700','701','702','710','711','721') if ( $marcflavour eq "UNIMARC" );
737 foreach my $field ( @authorFields ) {
738 # author formatted surname, firstname
740 if ( $marcflavour eq "UNIMARC" ) {
741 $texauthor = join ', ',
742 ( $record->subfield($field,"a"), $record->subfield($field,"b") );
744 $texauthor = $record->subfield($field,"a");
746 push @texauthors, $texauthor if $texauthor;
748 $author = join ' and ', @texauthors;
750 # Defining the conversion array according to the marcflavour
752 if ( $marcflavour eq "UNIMARC" ) {
754 # FIXME, TODO : handle repeatable fields
755 # TODO : handle more types of documents
757 # Unimarc to bibtex array
762 title => $record->subfield("200", "a") || "",
763 editor => $record->subfield("210", "g") || "",
764 publisher => $record->subfield("210", "c") || "",
765 year => $record->subfield("210", "d") || $record->subfield("210", "h") || "",
768 volume => $record->subfield("200", "v") || "",
769 series => $record->subfield("225", "a") || "",
770 address => $record->subfield("210", "a") || "",
771 edition => $record->subfield("205", "a") || "",
772 note => $record->subfield("300", "a") || "",
773 url => $record->subfield("856", "u") || ""
777 # Marc21 to bibtex array
782 title => $record->subfield("245", "a") || "",
783 editor => $record->subfield("260", "f") || "",
784 publisher => $record->subfield("264", "b") || $record->subfield("260", "b") || "",
785 year => $record->subfield("264", "c") || $record->subfield("260", "c") || $record->subfield("260", "g") || "",
788 # unimarc to marc21 specification says not to convert 200$v to marc21
789 series => $record->subfield("490", "a") || "",
790 address => $record->subfield("264", "a") || $record->subfield("260", "a") || "",
791 edition => $record->subfield("250", "a") || "",
792 note => $record->subfield("500", "a") || "",
793 url => $record->subfield("856", "u") || ""
797 my $BibtexExportAdditionalFields = C4::Context->preference('BibtexExportAdditionalFields');
798 my $additional_fields;
799 if ($BibtexExportAdditionalFields) {
800 $BibtexExportAdditionalFields = "$BibtexExportAdditionalFields\n\n";
801 $additional_fields = eval { YAML::XS::Load(Encode::encode_utf8($BibtexExportAdditionalFields)); };
803 warn "Unable to parse BibtexExportAdditionalFields : $@";
804 $additional_fields = undef;
808 if ( $additional_fields && $additional_fields->{'@'} ) {
809 my ( $f, $sf ) = split( /\$/, $additional_fields->{'@'} );
810 my ( $type ) = read_field( { record => $record, field => $f, subfield => $sf, field_numbers => [1] } );
813 $tex .= '@' . $type . '{';
824 for ( my $i = 0 ; $i < scalar( @bh ) ; $i = $i + 2 ) {
825 next unless $bh[$i+1];
826 push @elt, qq|\t$bh[$i] = {$bh[$i+1]}|;
828 $tex .= join(",\n", $id, @elt);
830 if ($additional_fields) {
832 foreach my $bibtex_tag ( keys %$additional_fields ) {
833 next if $bibtex_tag eq '@';
836 ref( $additional_fields->{$bibtex_tag} ) eq 'ARRAY'
837 ? @{ $additional_fields->{$bibtex_tag} }
838 : $additional_fields->{$bibtex_tag};
840 for my $tag (@fields) {
841 my ( $f, $sf ) = split( /\$/, $tag );
842 my @values = read_field( { record => $record, field => $f, subfield => $sf } );
843 foreach my $v (@values) {
844 $tex .= qq(\t$bibtex_tag = {$v}\n);
859 =head1 INTERNAL FUNCTIONS
861 =head2 _entity_encode - Entity-encode an array of strings
863 my ($entity_encoded_string) = _entity_encode($string);
867 my (@entity_encoded_strings) = _entity_encode(@strings);
869 Entity-encode an array of strings
875 my @strings_entity_encoded;
876 foreach my $string (@strings) {
877 my $nfc_string = NFC($string);
878 $nfc_string =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
879 push @strings_entity_encoded, $nfc_string;
881 return @strings_entity_encoded;
884 END { } # module clean-up code here (global destructor)
890 Joshua Ferraro <jmf@liblime.com>