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 GetMarcBiblio );
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 Carp qw( carp croak );
43 use vars qw(@ISA @EXPORT);
48 # only export API methods
66 C4::Record - MARC, MARCXML, DC, MODS, XML, etc. Record Management Functions and API
70 New in Koha 3.x. This module handles all record-related management functions.
72 =head1 API (EXPORTED FUNCTIONS)
74 =head2 marc2marc - Convert from one flavour of ISO-2709 to another
76 my ($error,$newmarc) = marc2marc($marc,$to_flavour,$from_flavour,$encoding);
78 Returns an ISO-2709 scalar
83 my ($marc,$to_flavour,$from_flavour,$encoding) = @_;
85 if ($to_flavour && $to_flavour =~ m/marcstd/) {
87 if ($marc =~ /^MARC::Record/) { # it's already a MARC::Record object
88 $marc_record_obj = $marc;
89 } else { # it's not a MARC::Record object, make it one
90 eval { $marc_record_obj = MARC::Record->new_from_usmarc($marc) }; # handle exceptions
92 # conversion to MARC::Record object failed, populate $error
93 if ($@) { $error .="\nCreation of MARC::Record object failed: ".$MARC::File::ERROR };
97 foreach my $field ($marc_record_obj->fields()) {
98 if ($field->tag() =~ m/9/ && ($field->tag() != '490' || C4::Context->preference("marcflavour") eq 'UNIMARC')) {
99 push @privatefields, $field;
100 } elsif (! ($field->is_control_field())) {
101 $field->delete_subfield(code => '9') if ($field->subfield('9'));
104 $marc_record_obj->delete_field($_) for @privatefields;
105 $marc = $marc_record_obj->as_usmarc();
108 $error = "Feature not yet implemented\n";
110 return ($error,$marc);
113 =head2 marc2marcxml - Convert from ISO-2709 to MARCXML
115 my ($error,$marcxml) = marc2marcxml($marc,$encoding,$flavour);
117 Returns a MARCXML scalar
119 C<$marc> - an ISO-2709 scalar or MARC::Record object
121 C<$encoding> - UTF-8 or MARC-8 [UTF-8]
123 C<$flavour> - MARC21 or UNIMARC
125 C<$dont_entity_encode> - a flag that instructs marc2marcxml not to entity encode the xml before returning (optional)
130 my ($marc,$encoding,$flavour,$dont_entity_encode) = @_;
131 my $error; # the error string
132 my $marcxml; # the final MARCXML scalar
134 # test if it's already a MARC::Record object, if not, make it one
136 if ($marc =~ /^MARC::Record/) { # it's already a MARC::Record object
137 $marc_record_obj = $marc;
138 } else { # it's not a MARC::Record object, make it one
139 eval { $marc_record_obj = MARC::Record->new_from_usmarc($marc) }; # handle exceptions
141 # conversion to MARC::Record object failed, populate $error
142 if ($@) { $error .="\nCreation of MARC::Record object failed: ".$MARC::File::ERROR };
144 # only proceed if no errors so far
147 # check the record for warnings
148 my @warnings = $marc_record_obj->warnings();
150 warn "\nWarnings encountered while processing ISO-2709 record with title \"".$marc_record_obj->title()."\":\n";
151 foreach my $warn (@warnings) { warn "\t".$warn };
153 unless($encoding) {$encoding = "UTF-8"}; # set default encoding
154 unless($flavour) {$flavour = C4::Context->preference("marcflavour")}; # set default MARC flavour
156 # attempt to convert the record to MARCXML
157 eval { $marcxml = $marc_record_obj->as_xml_record($flavour) }; #handle exceptions
159 # record creation failed, populate $error
161 $error .= "Creation of MARCXML failed:".$MARC::File::ERROR;
162 $error .= "Additional information:\n";
163 my @warnings = $@->warnings();
164 foreach my $warn (@warnings) { $error.=$warn."\n" };
166 # record creation was successful
169 # check the record for warning flags again (warnings() will be cleared already if there was an error, see above block
170 @warnings = $marc_record_obj->warnings();
172 warn "\nWarnings encountered while processing ISO-2709 record with title \"".$marc_record_obj->title()."\":\n";
173 foreach my $warn (@warnings) { warn "\t".$warn };
177 # only proceed if no errors so far
180 # entity encode the XML unless instructed not to
181 unless ($dont_entity_encode) {
182 my ($marcxml_entity_encoded) = _entity_encode($marcxml);
183 $marcxml = $marcxml_entity_encoded;
187 # return result to calling program
188 return ($error,$marcxml);
191 =head2 marcxml2marc - Convert from MARCXML to ISO-2709
193 my ($error,$marc) = marcxml2marc($marcxml,$encoding,$flavour);
195 Returns an ISO-2709 scalar
197 C<$marcxml> - a MARCXML record
199 C<$encoding> - UTF-8 or MARC-8 [UTF-8]
201 C<$flavour> - MARC21 or UNIMARC
206 my ($marcxml,$encoding,$flavour) = @_;
207 my $error; # the error string
208 my $marc; # the final ISO-2709 scalar
209 unless($encoding) {$encoding = "UTF-8"}; # set the default encoding
210 unless($flavour) {$flavour = C4::Context->preference("marcflavour")}; # set the default MARC flavour
212 # attempt to do the conversion
213 eval { $marc = MARC::Record->new_from_xml($marcxml,$encoding,$flavour) }; # handle exceptions
215 # record creation failed, populate $error
216 if ($@) {$error .="\nCreation of MARCXML Record failed: ".$@;
217 $error.=$MARC::File::ERROR if ($MARC::File::ERROR);
219 # return result to calling program
220 return ($error,$marc);
223 =head2 marc2dcxml - Convert from ISO-2709 to Dublin Core
225 my dcxml = marc2dcxml ($marc, $xml, $biblionumber, $format);
229 my dcxml = marc2dcxml (undef, undef, 1, "oaidc");
231 Convert MARC or MARCXML to Dublin Core metadata (XSLT Transformation),
232 optionally can get an XML directly from biblio_metadata
233 without item information. This method take into consideration the syspref
234 'marcflavour' (UNIMARC or MARC21).
235 Return an XML file with the format defined in C<$format>
237 C<$marc> - an ISO-2709 scalar or MARC::Record object
239 C<$xml> - a MARCXML file
241 C<$biblionumber> - biblionumber for database access
243 C<$format> - accept three type of DC formats (oaidc, srwdc, and rdfdc )
248 my ( $marc, $xml, $biblionumber, $format ) = @_;
251 my ( $marcxml, $record, $output );
253 # set the default path for intranet xslts
254 # differents xslts to process (OAIDC, SRWDC and RDFDC)
255 my $xsl = C4::Context->config('intrahtdocs') . '/prog/en/xslt/' .
256 C4::Context->preference('marcflavour') . 'slim2' . uc ( $format ) . '.xsl';
258 if ( defined $marc ) {
259 # no need to catch errors or warnings marc2marcxml do it instead
260 $marcxml = C4::Record::marc2marcxml( $marc );
261 } elsif ( not defined $xml and defined $biblionumber ) {
262 # get MARCXML biblio directly without item information
263 $marcxml = C4::Biblio::GetXmlBiblio( $biblionumber );
268 eval { $record = MARC::Record->new_from_xml(
271 C4::Context->preference('marcflavour')
275 # conversion to MARC::Record object failed
277 croak "Creation of MARC::Record object failed.";
278 } elsif ( $record->warnings() ) {
279 carp "Warnings encountered while processing ISO-2709 record.\n";
280 my @warnings = $record->warnings();
281 foreach my $warn (@warnings) {
284 } elsif ( $record =~ /^MARC::Record/ ) { # if OK makes xslt transformation
285 my $xslt_engine = Koha::XSLT::Base->new;
286 if ( $format =~ /^(dc|oaidc|srwdc|rdfdc)$/i ) {
287 $output = $xslt_engine->transform( $marcxml, $xsl );
289 croak "The format argument ($format) not accepted.\n" .
290 "Please pass a valid format (oaidc, srwdc, or rdfdc)\n";
292 my $err = $xslt_engine->err; # error code
294 croak "Error $err while processing\n";
301 =head2 marc2modsxml - Convert from ISO-2709 to MODS
303 my $modsxml = marc2modsxml($marc);
305 Returns a MODS scalar
311 return _transformWithStylesheet($marc, "/prog/en/xslt/MARC21slim2MODS3-1.xsl");
314 =head2 marc2madsxml - Convert from ISO-2709 to MADS
316 my $madsxml = marc2madsxml($marc);
318 Returns a MADS scalar
324 return _transformWithStylesheet($marc, "/prog/en/xslt/MARC21slim2MADS.xsl");
327 =head2 _transformWithStylesheet - Transform a MARC record with a stylesheet
329 my $xml = _transformWithStylesheet($marc, $stylesheet)
331 Returns the XML scalar result of the transformation. $stylesheet should
332 contain the path to a stylesheet under intrahtdocs.
336 sub _transformWithStylesheet {
337 my ($marc, $stylesheet) = @_;
338 # grab the XML, run it through our stylesheet, push it out to the browser
339 my $xmlrecord = marc2marcxml($marc);
340 my $xslfile = C4::Context->config('intrahtdocs') . $stylesheet;
341 return C4::XSLT::engine->transform($xmlrecord, $xslfile);
346 my $marc_rec_obj = MARC::Record->new_from_usmarc($marc);
347 my ( $abstract, $f260a, $f710a );
348 my $f260 = $marc_rec_obj->field('260');
350 $f260a = $f260->subfield('a') if $f260;
352 my $f710 = $marc_rec_obj->field('710');
354 $f710a = $f710->subfield('a');
356 my $f500 = $marc_rec_obj->field('500');
358 $abstract = $f500->subfield('a');
361 DB => C4::Context->preference("LibraryName"),
362 Title => $marc_rec_obj->title(),
363 Author => $marc_rec_obj->author(),
366 Year => $marc_rec_obj->publication_date,
367 Abstract => $abstract,
369 my $style = Biblio::EndnoteStyle->new();
371 $template.= "DB - DB\n" if C4::Context->preference("LibraryName");
372 $template.="T1 - Title\n" if $marc_rec_obj->title();
373 $template.="A1 - Author\n" if $marc_rec_obj->author();
374 $template.="PB - Publisher\n" if $f710a;
375 $template.="CY - City\n" if $f260a;
376 $template.="Y1 - Year\n" if $marc_rec_obj->publication_date;
377 $template.="AB - Abstract\n" if $abstract;
378 my ($text, $errmsg) = $style->format($template, $fields);
383 =head2 marc2csv - Convert several records from UNIMARC to CSV
385 my ($csv) = marc2csv($biblios, $csvprofileid, $itemnumbers);
387 Pre and postprocessing can be done through a YAML file
391 C<$biblio> - a list of biblionumbers
393 C<$csvprofileid> - the id of the CSV profile to use for the export (see export_format.export_format_id)
395 C<$itemnumbers> - a list of itemnumbers to export
400 my ($biblios, $id, $itemnumbers) = @_;
403 my $csv = Text::CSV::Encoded->new();
406 my $configfile = "../tools/csv-profiles/$id.yaml";
407 my ($preprocess, $postprocess, $fieldprocessing);
409 ($preprocess,$postprocess, $fieldprocessing) = YAML::XS::LoadFile($configfile);
413 eval $preprocess if ($preprocess); ## no critic (StringyEval)
416 if ( @$itemnumbers ) {
417 for my $itemnumber ( @$itemnumbers) {
418 my $item = Koha::Items->find( $itemnumber );
419 my $biblionumber = $item->biblio->biblionumber;
420 $output .= marcrecord2csv( $biblionumber, $id, $firstpass, $csv, $fieldprocessing, [$itemnumber] ) // '';
424 foreach my $biblio (@$biblios) {
425 $output .= marcrecord2csv( $biblio, $id, $firstpass, $csv, $fieldprocessing ) // '';
431 eval $postprocess if ($postprocess); ## no critic (StringyEval)
436 =head2 marcrecord2csv - Convert a single record from UNIMARC to CSV
438 my ($csv) = marcrecord2csv($biblio, $csvprofileid, $header);
442 C<$biblio> - a biblionumber
444 C<$csvprofileid> - the id of the CSV profile to use for the export (see export_format.export_format_id)
446 C<$header> - true if the headers are to be printed (typically at first pass)
448 C<$csv> - an already initialised Text::CSV object
452 C<$itemnumbers> a list of itemnumbers to export
457 my ($biblio, $id, $header, $csv, $fieldprocessing, $itemnumbers) = @_;
461 my $record = GetMarcBiblio({ biblionumber => $biblio });
462 return unless $record;
463 C4::Biblio::EmbedItemsInMarcBiblio({
464 marc_record => $record,
465 biblionumber => $biblio,
466 item_numbers => $itemnumbers });
467 # Getting the framework
468 my $frameworkcode = GetFrameworkCode($biblio);
470 # Getting information about the csv profile
471 my $profile = Koha::CsvProfiles->find($id);
473 # Getting output encoding
474 my $encoding = $profile->encoding || 'utf8';
476 my $csvseparator = $profile->csv_separator || ',';
477 my $fieldseparator = $profile->field_separator || '#';
478 my $subfieldseparator = $profile->subfield_separator || '|';
480 # TODO: Be more generic (in case we have to handle other protected chars or more separators)
481 if ($csvseparator eq '\t') { $csvseparator = "\t" }
482 if ($fieldseparator eq '\t') { $fieldseparator = "\t" }
483 if ($subfieldseparator eq '\t') { $subfieldseparator = "\t" }
484 if ($csvseparator eq '\n') { $csvseparator = "\n" }
485 if ($fieldseparator eq '\n') { $fieldseparator = "\n" }
486 if ($subfieldseparator eq '\n') { $subfieldseparator = "\n" }
488 $csv = $csv->encoding_out($encoding) ;
489 $csv->sep_char($csvseparator);
491 # Getting the marcfields
492 my $marcfieldslist = $profile->content;
494 # Getting the marcfields as an array
495 my @marcfieldsarray = split('\|', $marcfieldslist);
497 # Separating the marcfields from the user-supplied headers
499 foreach (@marcfieldsarray) {
500 my @result = split('=', $_, 2);
501 my $content = ( @result == 2 )
505 while ( $content =~ m|(\d{3})\$?(.)?|g ) {
507 my $subfieldtag = $2;
508 push @fields, { fieldtag => $fieldtag, subfieldtag => $subfieldtag };
511 push @csv_structures, { header => $result[0], content => $content, fields => \@fields };
513 push @csv_structures, { content => $content, fields => \@fields }
517 my ( @marcfieldsheaders, @csv_rows );
518 my $dbh = C4::Context->dbh;
521 for my $field ( $record->fields ) {
522 my $fieldtag = $field->tag;
524 if ( $field->is_control_field ) {
525 $values = $field->data();
527 $values->{indicator}{1} = $field->indicator(1);
528 $values->{indicator}{2} = $field->indicator(2);
529 for my $subfield ( $field->subfields ) {
530 my $subfieldtag = $subfield->[0];
531 my $value = $subfield->[1];
532 push @{ $values->{$subfieldtag} }, $value;
535 # We force the key as an integer (trick for 00X and OXX fields)
536 push @{ $field_list->{fields}{0+$fieldtag} }, $values;
539 # For each field or subfield
540 foreach my $csv_structure (@csv_structures) {
542 my $tags = $csv_structure->{fields};
543 my $content = $csv_structure->{content};
546 # If we have a user-supplied header, we use it
547 if ( exists $csv_structure->{header} ) {
548 push @marcfieldsheaders, $csv_structure->{header};
550 # If not, we get the matching tag name from koha
551 my $tag = $tags->[0];
552 if (defined $tag->{subfieldtag} ) {
553 my $query = "SELECT liblibrarian FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield=?";
554 my @results = $dbh->selectrow_array( $query, {}, $tag->{fieldtag}, $tag->{subfieldtag} );
555 push @marcfieldsheaders, $results[0];
557 my $query = "SELECT liblibrarian FROM marc_tag_structure WHERE tagfield=?";
558 my @results = $dbh->selectrow_array( $query, {}, $tag->{fieldtag} );
559 push @marcfieldsheaders, $results[0];
565 if ( $content =~ m|\[\%.*\%\]| ) {
566 my $tt = Template->new();
567 my $template = $content;
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;
572 $tt->process( \$content, $field_list, \$tt_output );
573 push @csv_rows, $tt_output;
575 for my $tag ( @$tags ) {
576 my @fields = $record->field( $tag->{fieldtag} );
577 # If it is a subfield
579 if (defined $tag->{subfieldtag} ) {
580 my $av = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag}, tagsubfield => $tag->{subfieldtag}, });
581 $av = $av->count ? $av->unblessed : [];
582 my $av_description_mapping = { map { ( $_->{authorised_value} => $_->{lib} ) } @$av };
584 foreach my $field (@fields) {
585 my @subfields = $field->subfield( $tag->{subfieldtag} );
586 foreach my $subfield (@subfields) {
587 push @loop_values, (defined $av_description_mapping->{$subfield}) ? $av_description_mapping->{$subfield} : $subfield;
593 my $av = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag}, });
594 $av = $av->count ? $av->unblessed : [];
595 my $authvalues = { map { ( $_->{authorised_value} => $_->{lib} ) } @$av };
597 foreach my $field ( @fields ) {
600 # If it is a control field
601 if ($field->is_control_field) {
602 $value = defined $authvalues->{$field->as_string} ? $authvalues->{$field->as_string} : $field->as_string;
604 # If it is a field, we gather all subfields, joined by the subfield separator
606 my @subfields = $field->subfields;
607 foreach my $subfield (@subfields) {
608 push (@subvaluesarray, defined $authvalues->{$subfield->[1]} ? $authvalues->{$subfield->[1]} : $subfield->[1]);
610 $value = join ($subfieldseparator, @subvaluesarray);
614 my $marcfield = $tag->{fieldtag}; # This line fixes a retrocompatibility concern
615 # The "processing" could be based on the $marcfield variable.
616 eval $fieldprocessing if ($fieldprocessing); ## no critic (StringyEval)
618 push @loop_values, $value;
622 push @field_values, {
623 fieldtag => $tag->{fieldtag},
624 subfieldtag => $tag->{subfieldtag},
625 values => \@loop_values,
628 for my $field_value ( @field_values ) {
629 if ( $field_value->{subfieldtag} ) {
630 push @csv_rows, join( $subfieldseparator, @{ $field_value->{values} } );
632 push @csv_rows, join( $fieldseparator, @{ $field_value->{values} } );
640 $csv->combine(@marcfieldsheaders);
641 $output = $csv->string() . "\n";
643 $csv->combine(@csv_rows);
644 $output .= $csv->string() . "\n";
651 =head2 changeEncoding - Change the encoding of a record
653 my ($error, $newrecord) = changeEncoding($record,$format,$flavour,$to_encoding,$from_encoding);
655 Changes the encoding of a record
657 C<$record> - the record itself can be in ISO-2709, a MARC::Record object, or MARCXML for now (required)
659 C<$format> - MARC or MARCXML (required)
661 C<$flavour> - MARC21 or UNIMARC, if MARC21, it will change the leader (optional) [defaults to Koha system preference]
663 C<$to_encoding> - the encoding you want the record to end up in (optional) [UTF-8]
665 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)
667 FIXME: the from_encoding doesn't work yet
669 FIXME: better handling for UNIMARC, it should allow management of 100 field
671 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
676 my ($record,$format,$flavour,$to_encoding,$from_encoding) = @_;
679 unless($flavour) {$flavour = C4::Context->preference("marcflavour")};
680 unless($to_encoding) {$to_encoding = "UTF-8"};
682 # ISO-2709 Record (MARC21 or UNIMARC)
683 if (lc($format) =~ /^marc$/o) {
684 # if we're converting encoding of an ISO2709 file, we need to roundtrip through XML
685 # because MARC::Record doesn't directly provide us with an encoding method
686 # It's definitely less than idea and should be fixed eventually - kados
687 my $marcxml; # temporary storage of MARCXML scalar
688 ($error,$marcxml) = marc2marcxml($record,$to_encoding,$flavour);
690 ($error,$newrecord) = marcxml2marc($marcxml,$to_encoding,$flavour);
694 } elsif (lc($format) =~ /^marcxml$/o) { # MARCXML Record
696 ($error,$marc) = marcxml2marc($record,$to_encoding,$flavour);
698 ($error,$newrecord) = marc2marcxml($record,$to_encoding,$flavour);
701 $error.="Unsupported record format:".$format;
703 return ($error,$newrecord);
706 =head2 marc2bibtex - Convert from MARC21 and UNIMARC to BibTex
708 my ($bibtex) = marc2bibtex($record, $id);
710 Returns a BibTex scalar
712 C<$record> - a MARC::Record object
714 C<$id> - an id for the BibTex record (might be the biblionumber)
720 my ($record, $id) = @_;
722 my $marcflavour = C4::Context->preference("marcflavour");
727 my @authorFields = ('100','110','111','700','710','711');
728 @authorFields = ('700','701','702','710','711','721') if ( $marcflavour eq "UNIMARC" );
730 foreach my $field ( @authorFields ) {
731 # author formatted surname, firstname
733 if ( $marcflavour eq "UNIMARC" ) {
734 $texauthor = join ', ',
735 ( $record->subfield($field,"a"), $record->subfield($field,"b") );
737 $texauthor = $record->subfield($field,"a");
739 push @texauthors, $texauthor if $texauthor;
741 $author = join ' and ', @texauthors;
743 # Defining the conversion array according to the marcflavour
745 if ( $marcflavour eq "UNIMARC" ) {
747 # FIXME, TODO : handle repeatable fields
748 # TODO : handle more types of documents
750 # Unimarc to bibtex array
755 title => $record->subfield("200", "a") || "",
756 editor => $record->subfield("210", "g") || "",
757 publisher => $record->subfield("210", "c") || "",
758 year => $record->subfield("210", "d") || $record->subfield("210", "h") || "",
761 volume => $record->subfield("200", "v") || "",
762 series => $record->subfield("225", "a") || "",
763 address => $record->subfield("210", "a") || "",
764 edition => $record->subfield("205", "a") || "",
765 note => $record->subfield("300", "a") || "",
766 url => $record->subfield("856", "u") || ""
770 # Marc21 to bibtex array
775 title => $record->subfield("245", "a") || "",
776 editor => $record->subfield("260", "f") || "",
777 publisher => $record->subfield("264", "b") || $record->subfield("260", "b") || "",
778 year => $record->subfield("264", "c") || $record->subfield("260", "c") || $record->subfield("260", "g") || "",
781 # unimarc to marc21 specification says not to convert 200$v to marc21
782 series => $record->subfield("490", "a") || "",
783 address => $record->subfield("264", "a") || $record->subfield("260", "a") || "",
784 edition => $record->subfield("250", "a") || "",
785 note => $record->subfield("500", "a") || "",
786 url => $record->subfield("856", "u") || ""
790 my $BibtexExportAdditionalFields = C4::Context->preference('BibtexExportAdditionalFields');
791 my $additional_fields;
792 if ($BibtexExportAdditionalFields) {
793 $BibtexExportAdditionalFields = "$BibtexExportAdditionalFields\n\n";
794 $additional_fields = eval { YAML::XS::Load(Encode::encode_utf8($BibtexExportAdditionalFields)); };
796 warn "Unable to parse BibtexExportAdditionalFields : $@";
797 $additional_fields = undef;
801 if ( $additional_fields && $additional_fields->{'@'} ) {
802 my ( $f, $sf ) = split( /\$/, $additional_fields->{'@'} );
803 my ( $type ) = read_field( { record => $record, field => $f, subfield => $sf, field_numbers => [1] } );
806 $tex .= '@' . $type . '{';
817 for ( my $i = 0 ; $i < scalar( @bh ) ; $i = $i + 2 ) {
818 next unless $bh[$i+1];
819 push @elt, qq|\t$bh[$i] = {$bh[$i+1]}|;
821 $tex .= join(",\n", $id, @elt);
823 if ($additional_fields) {
825 foreach my $bibtex_tag ( keys %$additional_fields ) {
826 next if $bibtex_tag eq '@';
829 ref( $additional_fields->{$bibtex_tag} ) eq 'ARRAY'
830 ? @{ $additional_fields->{$bibtex_tag} }
831 : $additional_fields->{$bibtex_tag};
833 for my $tag (@fields) {
834 my ( $f, $sf ) = split( /\$/, $tag );
835 my @values = read_field( { record => $record, field => $f, subfield => $sf } );
836 foreach my $v (@values) {
837 $tex .= qq(\t$bibtex_tag = {$v}\n);
852 =head1 INTERNAL FUNCTIONS
854 =head2 _entity_encode - Entity-encode an array of strings
856 my ($entity_encoded_string) = _entity_encode($string);
860 my (@entity_encoded_strings) = _entity_encode(@strings);
862 Entity-encode an array of strings
868 my @strings_entity_encoded;
869 foreach my $string (@strings) {
870 my $nfc_string = NFC($string);
871 $nfc_string =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
872 push @strings_entity_encoded, $nfc_string;
874 return @strings_entity_encoded;
877 END { } # module clean-up code here (global destructor)
883 Joshua Ferraro <jmf@liblime.com>