Bug - 5511: Added new system preference: SessionRestrictionByIP
[koha.git] / C4 / Record.pm
blob16d3f54410afbb7172f5d30727f198c341f4638f
1 package C4::Record;
3 # Copyright 2006 (C) LibLime
4 # Parts copyright 2010 BibLibre
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 use strict;
23 #use warnings; FIXME - Bug 2505
25 # please specify in which methods a given module is used
26 use MARC::Record; # marc2marcxml, marcxml2marc, changeEncoding
27 use MARC::File::XML; # marc2marcxml, marcxml2marc, changeEncoding
28 use MARC::Crosswalk::DublinCore; # marc2dcxml
29 use Biblio::EndnoteStyle;
30 use Unicode::Normalize; # _entity_encode
31 use C4::Biblio; #marc2bibtex
32 use C4::Csv; #marc2csv
33 use C4::Koha; #marc2csv
34 use C4::XSLT ();
35 use YAML; #marcrecords2csv
36 use Text::CSV::Encoded; #marc2csv
38 use vars qw($VERSION @ISA @EXPORT);
40 # set the version for version checking
41 $VERSION = 3.07.00.049;
43 @ISA = qw(Exporter);
45 # only export API methods
47 @EXPORT = qw(
48 &marc2endnote
49 &marc2marc
50 &marc2marcxml
51 &marcxml2marc
52 &marc2dcxml
53 &marc2modsxml
54 &marc2madsxml
55 &marc2bibtex
56 &marc2csv
57 &changeEncoding
60 =head1 NAME
62 C4::Record - MARC, MARCXML, DC, MODS, XML, etc. Record Management Functions and API
64 =head1 SYNOPSIS
66 New in Koha 3.x. This module handles all record-related management functions.
68 =head1 API (EXPORTED FUNCTIONS)
70 =head2 marc2marc - Convert from one flavour of ISO-2709 to another
72 my ($error,$newmarc) = marc2marc($marc,$to_flavour,$from_flavour,$encoding);
74 Returns an ISO-2709 scalar
76 =cut
78 sub marc2marc {
79 my ($marc,$to_flavour,$from_flavour,$encoding) = @_;
80 my $error;
81 if ($to_flavour =~ m/marcstd/) {
82 my $marc_record_obj;
83 if ($marc =~ /^MARC::Record/) { # it's already a MARC::Record object
84 $marc_record_obj = $marc;
85 } else { # it's not a MARC::Record object, make it one
86 eval { $marc_record_obj = MARC::Record->new_from_usmarc($marc) }; # handle exceptions
88 # conversion to MARC::Record object failed, populate $error
89 if ($@) { $error .="\nCreation of MARC::Record object failed: ".$MARC::File::ERROR };
91 unless ($error) {
92 my @privatefields;
93 foreach my $field ($marc_record_obj->fields()) {
94 if ($field->tag() =~ m/9/ && ($field->tag() != '490' || C4::Context->preference("marcflavour") eq 'UNIMARC')) {
95 push @privatefields, $field;
96 } elsif (! ($field->is_control_field())) {
97 $field->delete_subfield(code => '9') if ($field->subfield('9'));
100 $marc_record_obj->delete_field($_) for @privatefields;
101 $marc = $marc_record_obj->as_usmarc();
103 } else {
104 $error = "Feature not yet implemented\n";
106 return ($error,$marc);
109 =head2 marc2marcxml - Convert from ISO-2709 to MARCXML
111 my ($error,$marcxml) = marc2marcxml($marc,$encoding,$flavour);
113 Returns a MARCXML scalar
115 C<$marc> - an ISO-2709 scalar or MARC::Record object
117 C<$encoding> - UTF-8 or MARC-8 [UTF-8]
119 C<$flavour> - MARC21 or UNIMARC
121 C<$dont_entity_encode> - a flag that instructs marc2marcxml not to entity encode the xml before returning (optional)
123 =cut
125 sub marc2marcxml {
126 my ($marc,$encoding,$flavour,$dont_entity_encode) = @_;
127 my $error; # the error string
128 my $marcxml; # the final MARCXML scalar
130 # test if it's already a MARC::Record object, if not, make it one
131 my $marc_record_obj;
132 if ($marc =~ /^MARC::Record/) { # it's already a MARC::Record object
133 $marc_record_obj = $marc;
134 } else { # it's not a MARC::Record object, make it one
135 eval { $marc_record_obj = MARC::Record->new_from_usmarc($marc) }; # handle exceptions
137 # conversion to MARC::Record object failed, populate $error
138 if ($@) { $error .="\nCreation of MARC::Record object failed: ".$MARC::File::ERROR };
140 # only proceed if no errors so far
141 unless ($error) {
143 # check the record for warnings
144 my @warnings = $marc_record_obj->warnings();
145 if (@warnings) {
146 warn "\nWarnings encountered while processing ISO-2709 record with title \"".$marc_record_obj->title()."\":\n";
147 foreach my $warn (@warnings) { warn "\t".$warn };
149 unless($encoding) {$encoding = "UTF-8"}; # set default encoding
150 unless($flavour) {$flavour = C4::Context->preference("marcflavour")}; # set default MARC flavour
152 # attempt to convert the record to MARCXML
153 eval { $marcxml = $marc_record_obj->as_xml_record($flavour) }; #handle exceptions
155 # record creation failed, populate $error
156 if ($@) {
157 $error .= "Creation of MARCXML failed:".$MARC::File::ERROR;
158 $error .= "Additional information:\n";
159 my @warnings = $@->warnings();
160 foreach my $warn (@warnings) { $error.=$warn."\n" };
162 # record creation was successful
163 } else {
165 # check the record for warning flags again (warnings() will be cleared already if there was an error, see above block
166 @warnings = $marc_record_obj->warnings();
167 if (@warnings) {
168 warn "\nWarnings encountered while processing ISO-2709 record with title \"".$marc_record_obj->title()."\":\n";
169 foreach my $warn (@warnings) { warn "\t".$warn };
173 # only proceed if no errors so far
174 unless ($error) {
176 # entity encode the XML unless instructed not to
177 unless ($dont_entity_encode) {
178 my ($marcxml_entity_encoded) = _entity_encode($marcxml);
179 $marcxml = $marcxml_entity_encoded;
183 # return result to calling program
184 return ($error,$marcxml);
187 =head2 marcxml2marc - Convert from MARCXML to ISO-2709
189 my ($error,$marc) = marcxml2marc($marcxml,$encoding,$flavour);
191 Returns an ISO-2709 scalar
193 C<$marcxml> - a MARCXML record
195 C<$encoding> - UTF-8 or MARC-8 [UTF-8]
197 C<$flavour> - MARC21 or UNIMARC
199 =cut
201 sub marcxml2marc {
202 my ($marcxml,$encoding,$flavour) = @_;
203 my $error; # the error string
204 my $marc; # the final ISO-2709 scalar
205 unless($encoding) {$encoding = "UTF-8"}; # set the default encoding
206 unless($flavour) {$flavour = C4::Context->preference("marcflavour")}; # set the default MARC flavour
208 # attempt to do the conversion
209 eval { $marc = MARC::Record->new_from_xml($marcxml,$encoding,$flavour) }; # handle exceptions
211 # record creation failed, populate $error
212 if ($@) {$error .="\nCreation of MARCXML Record failed: ".$@;
213 $error.=$MARC::File::ERROR if ($MARC::File::ERROR);
215 # return result to calling program
216 return ($error,$marc);
219 =head2 marc2dcxml - Convert from ISO-2709 to Dublin Core
221 my ($error,$dcxml) = marc2dcxml($marc,$qualified);
223 Returns a DublinCore::Record object, will eventually return a Dublin Core scalar
225 FIXME: should return actual XML, not just an object
227 C<$marc> - an ISO-2709 scalar or MARC::Record object
229 C<$qualified> - specify whether qualified Dublin Core should be used in the input or output [0]
231 =cut
233 sub marc2dcxml {
234 my ($marc,$qualified) = @_;
235 my $error;
236 # test if it's already a MARC::Record object, if not, make it one
237 my $marc_record_obj;
238 if ($marc =~ /^MARC::Record/) { # it's already a MARC::Record object
239 $marc_record_obj = $marc;
240 } else { # it's not a MARC::Record object, make it one
241 eval { $marc_record_obj = MARC::Record->new_from_usmarc($marc) }; # handle exceptions
243 # conversion to MARC::Record object failed, populate $error
244 if ($@) {
245 $error .="\nCreation of MARC::Record object failed: ".$MARC::File::ERROR;
248 my $crosswalk = MARC::Crosswalk::DublinCore->new;
249 if ($qualified) {
250 $crosswalk = MARC::Crosswalk::DublinCore->new( qualified => 1 );
252 my $dcxml = $crosswalk->as_dublincore($marc_record_obj);
253 my $dcxmlfinal = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
254 $dcxmlfinal .= "<metadata
255 xmlns=\"http://example.org/myapp/\"
256 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
257 xsi:schemaLocation=\"http://example.org/myapp/ http://example.org/myapp/schema.xsd\"
258 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
259 xmlns:dcterms=\"http://purl.org/dc/terms/\">";
261 foreach my $element ( $dcxml->elements() ) {
262 $dcxmlfinal.="<"."dc:".$element->name().">".$element->content()."</"."dc:".$element->name().">\n";
264 $dcxmlfinal .= "\n</metadata>";
265 return ($error,$dcxmlfinal);
268 =head2 marc2modsxml - Convert from ISO-2709 to MODS
270 my $modsxml = marc2modsxml($marc);
272 Returns a MODS scalar
274 =cut
276 sub marc2modsxml {
277 my ($marc) = @_;
278 return _transformWithStylesheet($marc, "/prog/en/xslt/MARC21slim2MODS3-1.xsl");
281 =head2 marc2madsxml - Convert from ISO-2709 to MADS
283 my $madsxml = marc2madsxml($marc);
285 Returns a MADS scalar
287 =cut
289 sub marc2madsxml {
290 my ($marc) = @_;
291 return _transformWithStylesheet($marc, "/prog/en/xslt/MARC21slim2MADS.xsl");
294 =head2 _transformWithStylesheet - Transform a MARC record with a stylesheet
296 my $xml = _transformWithStylesheet($marc, $stylesheet)
298 Returns the XML scalar result of the transformation. $stylesheet should
299 contain the path to a stylesheet under intrahtdocs.
301 =cut
303 sub _transformWithStylesheet {
304 my ($marc, $stylesheet) = @_;
305 # grab the XML, run it through our stylesheet, push it out to the browser
306 my $xmlrecord = marc2marcxml($marc);
307 my $xslfile = C4::Context->config('intrahtdocs') . $stylesheet;
308 return C4::XSLT::engine->transform($xmlrecord, $xslfile);
311 sub marc2endnote {
312 my ($marc) = @_;
313 my $marc_rec_obj = MARC::Record->new_from_usmarc($marc);
314 my ( $abstract, $f260a, $f710a );
315 my $f260 = $marc_rec_obj->field('260');
316 if ($f260) {
317 $f260a = $f260->subfield('a') if $f260;
319 my $f710 = $marc_rec_obj->field('710');
320 if ($f710) {
321 $f710a = $f710->subfield('a');
323 my $f500 = $marc_rec_obj->field('500');
324 if ($f500) {
325 $abstract = $f500->subfield('a');
327 my $fields = {
328 DB => C4::Context->preference("LibraryName"),
329 Title => $marc_rec_obj->title(),
330 Author => $marc_rec_obj->author(),
331 Publisher => $f710a,
332 City => $f260a,
333 Year => $marc_rec_obj->publication_date,
334 Abstract => $abstract,
336 my $endnote;
337 my $style = new Biblio::EndnoteStyle();
338 my $template;
339 $template.= "DB - DB\n" if C4::Context->preference("LibraryName");
340 $template.="T1 - Title\n" if $marc_rec_obj->title();
341 $template.="A1 - Author\n" if $marc_rec_obj->author();
342 $template.="PB - Publisher\n" if $f710a;
343 $template.="CY - City\n" if $f260a;
344 $template.="Y1 - Year\n" if $marc_rec_obj->publication_date;
345 $template.="AB - Abstract\n" if $abstract;
346 my ($text, $errmsg) = $style->format($template, $fields);
347 return ($text);
351 =head2 marc2csv - Convert several records from UNIMARC to CSV
353 my ($csv) = marc2csv($biblios, $csvprofileid, $itemnumbers);
355 Pre and postprocessing can be done through a YAML file
357 Returns a CSV scalar
359 C<$biblio> - a list of biblionumbers
361 C<$csvprofileid> - the id of the CSV profile to use for the export (see export_format.export_format_id and the GetCsvProfiles function in C4::Csv)
363 C<$itemnumbers> - a list of itemnumbers to export
365 =cut
367 sub marc2csv {
368 my ($biblios, $id, $itemnumbers) = @_;
369 $itemnumbers ||= [];
370 my $output;
371 my $csv = Text::CSV::Encoded->new();
373 # Getting yaml file
374 my $configfile = "../tools/csv-profiles/$id.yaml";
375 my ($preprocess, $postprocess, $fieldprocessing);
376 if (-e $configfile){
377 ($preprocess,$postprocess, $fieldprocessing) = YAML::LoadFile($configfile);
380 # Preprocessing
381 eval $preprocess if ($preprocess);
383 my $firstpass = 1;
384 if ( @$itemnumbers ) {
385 for my $itemnumber ( @$itemnumbers) {
386 my $biblionumber = GetBiblionumberFromItemnumber $itemnumber;
387 $output .= marcrecord2csv( $biblionumber, $id, $firstpass, $csv, $fieldprocessing, [$itemnumber] );
388 $firstpass = 0;
390 } else {
391 foreach my $biblio (@$biblios) {
392 $output .= marcrecord2csv( $biblio, $id, $firstpass, $csv, $fieldprocessing );
393 $firstpass = 0;
397 # Postprocessing
398 eval $postprocess if ($postprocess);
400 return $output;
403 =head2 marcrecord2csv - Convert a single record from UNIMARC to CSV
405 my ($csv) = marcrecord2csv($biblio, $csvprofileid, $header);
407 Returns a CSV scalar
409 C<$biblio> - a biblionumber
411 C<$csvprofileid> - the id of the CSV profile to use for the export (see export_format.export_format_id and the GetCsvProfiles function in C4::Csv)
413 C<$header> - true if the headers are to be printed (typically at first pass)
415 C<$csv> - an already initialised Text::CSV object
417 C<$fieldprocessing>
419 C<$itemnumbers> a list of itemnumbers to export
421 =cut
424 sub marcrecord2csv {
425 my ($biblio, $id, $header, $csv, $fieldprocessing, $itemnumbers) = @_;
426 my $output;
428 # Getting the record
429 my $record = GetMarcBiblio($biblio);
430 next unless $record;
431 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblio, $itemnumbers );
432 # Getting the framework
433 my $frameworkcode = GetFrameworkCode($biblio);
435 # Getting information about the csv profile
436 my $profile = GetCsvProfile($id);
438 # Getting output encoding
439 my $encoding = $profile->{encoding} || 'utf8';
440 # Getting separators
441 my $csvseparator = $profile->{csv_separator} || ',';
442 my $fieldseparator = $profile->{field_separator} || '#';
443 my $subfieldseparator = $profile->{subfield_separator} || '|';
445 # TODO: Be more generic (in case we have to handle other protected chars or more separators)
446 if ($csvseparator eq '\t') { $csvseparator = "\t" }
447 if ($fieldseparator eq '\t') { $fieldseparator = "\t" }
448 if ($subfieldseparator eq '\t') { $subfieldseparator = "\t" }
449 if ($csvseparator eq '\n') { $csvseparator = "\n" }
450 if ($fieldseparator eq '\n') { $fieldseparator = "\n" }
451 if ($subfieldseparator eq '\n') { $subfieldseparator = "\n" }
453 $csv = $csv->encoding_out($encoding) ;
454 $csv->sep_char($csvseparator);
456 # Getting the marcfields
457 my $marcfieldslist = $profile->{content};
459 # Getting the marcfields as an array
460 my @marcfieldsarray = split('\|', $marcfieldslist);
462 # Separating the marcfields from the user-supplied headers
463 my @marcfields;
464 foreach (@marcfieldsarray) {
465 my @result = split('=', $_);
466 if (scalar(@result) == 2) {
467 push @marcfields, { header => $result[0], field => $result[1] };
468 } else {
469 push @marcfields, { field => $result[0] }
473 # If we have to insert the headers
474 if ($header) {
475 my @marcfieldsheaders;
476 my $dbh = C4::Context->dbh;
478 # For each field or subfield
479 foreach (@marcfields) {
481 my $field = $_->{field};
482 # Remove any blank char that might have unintentionally insered into the tag name
483 $field =~ s/\s+//g;
485 # If we have a user-supplied header, we use it
486 if (exists $_->{header}) {
487 push @marcfieldsheaders, $_->{header};
488 } else {
489 # If not, we get the matching tag name from koha
490 if (index($field, '$') > 0) {
491 my ($fieldtag, $subfieldtag) = split('\$', $field);
492 my $query = "SELECT liblibrarian FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield=?";
493 my $sth = $dbh->prepare($query);
494 $sth->execute($fieldtag, $subfieldtag);
495 my @results = $sth->fetchrow_array();
496 push @marcfieldsheaders, $results[0];
497 } else {
498 my $query = "SELECT liblibrarian FROM marc_tag_structure WHERE tagfield=?";
499 my $sth = $dbh->prepare($query);
500 $sth->execute($field);
501 my @results = $sth->fetchrow_array();
502 push @marcfieldsheaders, $results[0];
506 $csv->combine(@marcfieldsheaders);
507 $output = $csv->string() . "\n";
510 # For each marcfield to export
511 my @fieldstab;
512 foreach (@marcfields) {
513 my $marcfield = $_->{field};
514 # If it is a subfield
515 if (index($marcfield, '$') > 0) {
516 my ($fieldtag, $subfieldtag) = split('\$', $marcfield);
517 my @fields = $record->field($fieldtag);
518 my @tmpfields;
520 # For each field
521 foreach my $field (@fields) {
523 # We take every matching subfield
524 my @subfields = $field->subfield($subfieldtag);
525 foreach my $subfield (@subfields) {
527 # Getting authorised value
528 my $authvalues = GetKohaAuthorisedValuesFromField($fieldtag, $subfieldtag, $frameworkcode, undef);
529 push @tmpfields, (defined $authvalues->{$subfield}) ? $authvalues->{$subfield} : $subfield;
532 push (@fieldstab, join($subfieldseparator, @tmpfields));
533 # Or a field
534 } else {
535 my @fields = ($record->field($marcfield));
536 my $authvalues = GetKohaAuthorisedValuesFromField($marcfield, undef, $frameworkcode, undef);
538 my @valuesarray;
539 foreach (@fields) {
540 my $value;
542 # If it is a control field
543 if ($_->is_control_field) {
544 $value = defined $authvalues->{$_->as_string} ? $authvalues->{$_->as_string} : $_->as_string;
545 } else {
546 # If it is a field, we gather all subfields, joined by the subfield separator
547 my @subvaluesarray;
548 my @subfields = $_->subfields;
549 foreach my $subfield (@subfields) {
550 push (@subvaluesarray, defined $authvalues->{$subfield->[1]} ? $authvalues->{$subfield->[1]} : $subfield->[1]);
552 $value = join ($subfieldseparator, @subvaluesarray);
555 # Field processing
556 eval $fieldprocessing if ($fieldprocessing);
558 push @valuesarray, $value;
560 push (@fieldstab, join($fieldseparator, @valuesarray));
564 $csv->combine(@fieldstab);
565 $output .= $csv->string() . "\n";
567 return $output;
572 =head2 changeEncoding - Change the encoding of a record
574 my ($error, $newrecord) = changeEncoding($record,$format,$flavour,$to_encoding,$from_encoding);
576 Changes the encoding of a record
578 C<$record> - the record itself can be in ISO-2709, a MARC::Record object, or MARCXML for now (required)
580 C<$format> - MARC or MARCXML (required)
582 C<$flavour> - MARC21 or UNIMARC, if MARC21, it will change the leader (optional) [defaults to Koha system preference]
584 C<$to_encoding> - the encoding you want the record to end up in (optional) [UTF-8]
586 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)
588 FIXME: the from_encoding doesn't work yet
590 FIXME: better handling for UNIMARC, it should allow management of 100 field
592 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
594 =cut
596 sub changeEncoding {
597 my ($record,$format,$flavour,$to_encoding,$from_encoding) = @_;
598 my $newrecord;
599 my $error;
600 unless($flavour) {$flavour = C4::Context->preference("marcflavour")};
601 unless($to_encoding) {$to_encoding = "UTF-8"};
603 # ISO-2709 Record (MARC21 or UNIMARC)
604 if (lc($format) =~ /^marc$/o) {
605 # if we're converting encoding of an ISO2709 file, we need to roundtrip through XML
606 # because MARC::Record doesn't directly provide us with an encoding method
607 # It's definitely less than idea and should be fixed eventually - kados
608 my $marcxml; # temporary storage of MARCXML scalar
609 ($error,$marcxml) = marc2marcxml($record,$to_encoding,$flavour);
610 unless ($error) {
611 ($error,$newrecord) = marcxml2marc($marcxml,$to_encoding,$flavour);
614 # MARCXML Record
615 } elsif (lc($format) =~ /^marcxml$/o) { # MARCXML Record
616 my $marc;
617 ($error,$marc) = marcxml2marc($record,$to_encoding,$flavour);
618 unless ($error) {
619 ($error,$newrecord) = marc2marcxml($record,$to_encoding,$flavour);
621 } else {
622 $error.="Unsupported record format:".$format;
624 return ($error,$newrecord);
627 =head2 marc2bibtex - Convert from MARC21 and UNIMARC to BibTex
629 my ($bibtex) = marc2bibtex($record, $id);
631 Returns a BibTex scalar
633 C<$record> - a MARC::Record object
635 C<$id> - an id for the BibTex record (might be the biblionumber)
637 =cut
640 sub marc2bibtex {
641 my ($record, $id) = @_;
642 my $tex;
643 my $marcflavour = C4::Context->preference("marcflavour");
645 # Authors
646 my $author;
647 my @texauthors;
648 my @authorFields = ('100','110','111','700','710','711');
649 @authorFields = ('700','701','702','710','711','721') if ( $marcflavour eq "UNIMARC" );
651 foreach my $field ( @authorFields ) {
652 # author formatted surname, firstname
653 my $texauthor = '';
654 if ( $marcflavour eq "UNIMARC" ) {
655 $texauthor = join ', ',
656 ( $record->subfield($field,"a"), $record->subfield($field,"b") );
657 } else {
658 $texauthor = $record->subfield($field,"a");
660 push @texauthors, $texauthor if $texauthor;
662 $author = join ' and ', @texauthors;
664 # Defining the conversion array according to the marcflavour
665 my @bh;
666 if ( $marcflavour eq "UNIMARC" ) {
668 # FIXME, TODO : handle repeatable fields
669 # TODO : handle more types of documents
671 # Unimarc to bibtex array
672 @bh = (
674 # Mandatory
675 author => $author,
676 title => $record->subfield("200", "a") || "",
677 editor => $record->subfield("210", "g") || "",
678 publisher => $record->subfield("210", "c") || "",
679 year => $record->subfield("210", "d") || $record->subfield("210", "h") || "",
681 # Optional
682 volume => $record->subfield("200", "v") || "",
683 series => $record->subfield("225", "a") || "",
684 address => $record->subfield("210", "a") || "",
685 edition => $record->subfield("205", "a") || "",
686 note => $record->subfield("300", "a") || "",
687 url => $record->subfield("856", "u") || ""
689 } else {
691 # Marc21 to bibtex array
692 @bh = (
694 # Mandatory
695 author => $author,
696 title => $record->subfield("245", "a") || "",
697 editor => $record->subfield("260", "f") || "",
698 publisher => $record->subfield("264", "b") || $record->subfield("260", "b") || "",
699 year => $record->subfield("264", "c") || $record->subfield("260", "c") || $record->subfield("260", "g") || "",
701 # Optional
702 # unimarc to marc21 specification says not to convert 200$v to marc21
703 series => $record->subfield("490", "a") || "",
704 address => $record->subfield("264", "a") || $record->subfield("260", "a") || "",
705 edition => $record->subfield("250", "a") || "",
706 note => $record->subfield("500", "a") || "",
707 url => $record->subfield("856", "u") || ""
711 $tex .= "\@book{";
712 my @elt;
713 for ( my $i = 0 ; $i < scalar( @bh ) ; $i = $i + 2 ) {
714 next unless $bh[$i+1];
715 push @elt, qq|\t$bh[$i] = {$bh[$i+1]}|;
717 $tex .= join(",\n", $id, @elt);
718 $tex .= "\n}\n";
720 return $tex;
724 =head1 INTERNAL FUNCTIONS
726 =head2 _entity_encode - Entity-encode an array of strings
728 my ($entity_encoded_string) = _entity_encode($string);
732 my (@entity_encoded_strings) = _entity_encode(@strings);
734 Entity-encode an array of strings
736 =cut
738 sub _entity_encode {
739 my @strings = @_;
740 my @strings_entity_encoded;
741 foreach my $string (@strings) {
742 my $nfc_string = NFC($string);
743 $nfc_string =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
744 push @strings_entity_encoded, $nfc_string;
746 return @strings_entity_encoded;
749 END { } # module clean-up code here (global destructor)
751 __END__
753 =head1 AUTHOR
755 Joshua Ferraro <jmf@liblime.com>
757 =head1 MODIFICATIONS
760 =cut