3 # Copyright 2006 (C) LibLime
4 # Joshua Ferraro <jmf@liblime.com>
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
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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
23 use strict; use warnings; #FIXME: turn off warnings before release
25 # please specify in which methods a given module is used
26 use MARC::Record; # marc2marcxml, marcxml2marc, html2marc, changeEncoding
27 use MARC::File::XML; # marc2marcxml, marcxml2marc, html2marcxml, changeEncoding
28 use MARC::Crosswalk::DublinCore; # marc2dcxml
29 use Unicode::Normalize; # _entity_encode
31 use vars qw($VERSION @ISA @EXPORT);
33 # set the version for version checking
34 $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
35 shift(@v) . "." . join("_", map {sprintf "%03d", $_ } @v); };
39 # only export API methods
55 C4::Record - MARC, MARCXML, DC, MODS, XML, etc. Record Management Functions and API
59 New in Koha 3.x. This module handles all record-related management functions.
61 =head1 API (EXPORTED FUNCTIONS)
63 =head2 marc2marc - Convert from one flavour of ISO-2709 to another
67 my ($error,$newmarc) = marc2marc($marc,$to_flavour,$from_flavour,$encoding);
69 Returns an ISO-2709 scalar
76 my ($marc,$to_flavour,$from_flavour,$encoding) = @_;
77 my $error = "Feature not yet implemented\n";
78 return ($error,$marc);
81 =head2 marc2marcxml - Convert from ISO-2709 to MARCXML
85 my ($error,$marcxml) = marc2marcxml($marc,$encoding,$flavour);
87 Returns a MARCXML scalar
91 C<$marc> - an ISO-2709 scalar or MARC::Record object
93 C<$encoding> - UTF-8 or MARC-8 [UTF-8]
95 C<$flavour> - MARC21 or UNIMARC
97 C<$dont_entity_encode> - a flag that instructs marc2marcxml not to entity encode the xml before returning (optional)
106 my ($marc,$encoding,$flavour,$dont_entity_encode) = @_;
107 my $error; # the error string
108 my $marcxml; # the final MARCXML scalar
110 # test if it's already a MARC::Record object, if not, make it one
112 if ($marc =~ /^MARC::Record/) { # it's already a MARC::Record object
113 $marc_record_obj = $marc;
114 } else { # it's not a MARC::Record object, make it one
115 eval { $marc_record_obj = MARC::Record->new_from_usmarc($marc) }; # handle exceptions
117 # conversion to MARC::Record object failed, populate $error
118 if ($@) { $error .="\nCreation of MARC::Record object failed: ".$MARC::File::ERROR };
120 # only proceed if no errors so far
123 # check the record for warnings
124 my @warnings = $marc_record_obj->warnings();
126 warn "\nWarnings encountered while processing ISO-2709 record with title \"".$marc_record_obj->title()."\":\n";
127 foreach my $warn (@warnings) { warn "\t".$warn };
129 unless($encoding) {$encoding = "UTF-8"}; # set default encoding
130 unless($flavour) {$flavour = C4::Context->preference("marcflavour")}; # set default MARC flavour
132 # attempt to convert the record to MARCXML
133 eval { $marcxml = $marc_record_obj->as_xml_record($flavour) }; #handle exceptions
135 # record creation failed, populate $error
137 $error .= "Creation of MARCXML failed:".$MARC::File::ERROR;
138 $error .= "Additional information:\n";
139 my @warnings = $@->warnings();
140 foreach my $warn (@warnings) { $error.=$warn."\n" };
142 # record creation was successful
145 # check the record for warning flags again (warnings() will be cleared already if there was an error, see above block
146 @warnings = $marc_record_obj->warnings();
148 warn "\nWarnings encountered while processing ISO-2709 record with title \"".$marc_record_obj->title()."\":\n";
149 foreach my $warn (@warnings) { warn "\t".$warn };
153 # only proceed if no errors so far
156 # entity encode the XML unless instructed not to
157 unless ($dont_entity_encode) {
158 my ($marcxml_entity_encoded) = _entity_encode($marcxml);
159 $marcxml = $marcxml_entity_encoded;
163 # return result to calling program
164 return ($error,$marcxml);
167 =head2 marcxml2marc - Convert from MARCXML to ISO-2709
171 my ($error,$marc) = marcxml2marc($marcxml,$encoding,$flavour);
173 Returns an ISO-2709 scalar
177 C<$marcxml> - a MARCXML record
179 C<$encoding> - UTF-8 or MARC-8 [UTF-8]
181 C<$flavour> - MARC21 or UNIMARC
190 my ($marcxml,$encoding,$flavour) = @_;
191 my $error; # the error string
192 my $marc; # the final ISO-2709 scalar
193 unless($encoding) {$encoding = "UTF-8"}; # set the default encoding
194 unless($flavour) {$flavour = C4::Context->preference("marcflavour")}; # set the default MARC flavour
196 # attempt to do the conversion
197 eval { $marc = MARC::Record->new_from_xml($marcxml,$encoding,$flavour) }; # handle exceptions
199 # record creation failed, populate $error
200 if ($@) {$error .="\nCreation of MARCXML Record failed: ".$@;
201 $error.=$MARC::File::ERROR if ($MARC::File::ERROR);
203 # return result to calling program
204 return ($error,$marc);
207 =head2 marc2dcxml - Convert from ISO-2709 to Dublin Core
211 my ($error,$dcxml) = marc2dcxml($marc,$qualified);
213 Returns a DublinCore::Record object, will eventually return a Dublin Core scalar
215 FIXME: should return actual XML, not just an object
219 C<$marc> - an ISO-2709 scalar or MARC::Record object
221 C<$qualified> - specify whether qualified Dublin Core should be used in the input or output [0]
230 my ($marc,$qualified) = @_;
232 # test if it's already a MARC::Record object, if not, make it one
234 if ($marc =~ /^MARC::Record/) { # it's already a MARC::Record object
235 $marc_record_obj = $marc;
236 } else { # it's not a MARC::Record object, make it one
237 eval { $marc_record_obj = MARC::Record->new_from_usmarc($marc) }; # handle exceptions
239 # conversion to MARC::Record object failed, populate $error
241 $error .="\nCreation of MARC::Record object failed: ".$MARC::File::ERROR;
244 my $crosswalk = MARC::Crosswalk::DublinCore->new;
246 $crosswalk = MARC::Crosswalk::DublinCore->new( qualified => 1 );
248 my $dcxml = $crosswalk->as_dublincore($marc_record_obj);
249 return ($error,$dcxml);
251 =head2 marc2modsxml - Convert from ISO-2709 to MODS
255 my ($error,$modsxml) = marc2modsxml($marc);
257 Returns a MODS scalar
265 return ("Feature not yet implemented\n");
271 my ($error,$marcxml) = html2marcxml($tags,$subfields,$values,$indicator,$ind_tag);
273 Returns a MARCXML scalar
275 this is used in addbiblio.pl and additem.pl to build the MARCXML record from
278 FIXME: this could use some better code documentation
285 my ($tags,$subfields,$values,$indicator,$ind_tag) = @_;
287 # add the header info
288 my $marcxml= MARC::File::XML::header(C4::Context->preference('TemplateEncoding'),C4::Context->preference('marcflavour'));
290 # some flags used to figure out where in the record we are
296 # handle characters that would cause the parser to choke FIXME: is there a more elegant solution?
297 for (my $i=0;$i<=@$tags;$i++){
298 @$values[$i] =~ s/&/&/g;
299 @$values[$i] =~ s/</</g;
300 @$values[$i] =~ s/>/>/g;
301 @$values[$i] =~ s/"/"/g;
302 @$values[$i] =~ s/'/'/g;
304 if ((@$tags[$i] ne $prevtag)){
305 $j++ unless (@$tags[$i] eq "");
306 #warn "IND:".substr(@$indicator[$j],0,1).substr(@$indicator[$j],1,1)." ".@$tags[$i];
308 $marcxml.="</datafield>\n";
309 if ((@$tags[$i] > 10) && (@$values[$i] ne "")){
310 my $ind1 = substr(@$indicator[$j],0,1);
311 my $ind2 = substr(@$indicator[$j],1,1);
312 $marcxml.="<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
313 $marcxml.="<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
319 if (@$values[$i] ne "") {
321 if (@$tags[$i] eq "000") {
322 $marcxml.="<leader>@$values[$i]</leader>\n";
324 # rest of the fixed fields
325 } elsif (@$tags[$i] < 010) { #FIXME: <10 was the way it was, there might even be a better way
326 $marcxml.="<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
329 my $ind1 = substr(@$indicator[$j],0,1);
330 my $ind2 = substr(@$indicator[$j],1,1);
331 $marcxml.="<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
332 $marcxml.="<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
337 } else { # @$tags[$i] eq $prevtag
338 if (@$values[$i] eq "") {
341 my $ind1 = substr(@$indicator[$j],0,1);
342 my $ind2 = substr(@$indicator[$j],1,1);
343 $marcxml.="<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
346 $marcxml.="<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
349 $prevtag = @$tags[$i];
351 $marcxml.= MARC::File::XML::footer();
353 return ($error,$marcxml);
360 Probably best to avoid using this ... it has some rather striking problems:
364 * saves blank subfields
366 * subfield order is hardcoded to always start with 'a' for repeatable tags (because it is hardcoded in the addfield routine).
368 * only possible to specify one set of indicators for each set of tags (ie, one for all the 650s). (because they were stored in a hash with the tag as the key).
370 * the underlying routines didn't support subfield reordering or subfield repeatability.
374 I've left it in here because it could be useful if someone took the time to fix it. -- kados
381 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
383 my $record = MARC::Record->new();
384 # my %subfieldlist=();
385 my $prevvalue; # if tag <10
386 my $field; # if tag >=10
387 for (my $i=0; $i< @$rtags; $i++) {
388 # rebuild MARC::Record
389 # warn "0=>".@$rtags[$i].@$rsubfields[$i]." = ".@$rvalues[$i].": ";
390 if (@$rtags[$i] ne $prevtag) {
393 if (($prevtag ne '000') && ($prevvalue ne "")) {
394 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
395 } elsif ($prevvalue ne ""){
396 $record->leader($prevvalue);
400 if (($field) && ($field ne "")) {
401 $record->add_fields($field);
404 $indicators{@$rtags[$i]}.=' ';
405 # skip blank tags, I hope this works
406 if (@$rtags[$i] eq ''){
407 $prevtag = @$rtags[$i];
411 if (@$rtags[$i] <10) {
412 $prevvalue= @$rvalues[$i];
416 if (@$rvalues[$i] eq "") {
419 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
421 # warn "1=>".@$rtags[$i].@$rsubfields[$i]." = ".@$rvalues[$i].": ".$field->as_formatted;
423 $prevtag = @$rtags[$i];
425 if (@$rtags[$i] <10) {
426 $prevvalue=@$rvalues[$i];
428 if (length(@$rvalues[$i])>0) {
429 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
430 # warn "2=>".@$rtags[$i].@$rsubfields[$i]." = ".@$rvalues[$i].": ".$field->as_formatted;
433 $prevtag= @$rtags[$i];
437 # the last has not been included inside the loop... do it now !
439 #warn Dumper($field->{_subfields});
440 $record->add_fields($field) if (($field) && $field ne "");
441 #warn "HTML2MARC=".$record->as_formatted;
445 =head2 changeEncoding - Change the encoding of a record
449 my ($error, $newrecord) = changeEncoding($record,$format,$flavour,$to_encoding,$from_encoding);
451 Changes the encoding of a record
455 C<$record> - the record itself can be in ISO-2709, a MARC::Record object, or MARCXML for now (required)
457 C<$format> - MARC or MARCXML (required)
459 C<$flavour> - MARC21 or UNIMARC, if MARC21, it will change the leader (optional) [defaults to Koha system preference]
461 C<$to_encoding> - the encoding you want the record to end up in (optional) [UTF-8]
463 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)
467 FIXME: the from_encoding doesn't work yet
469 FIXME: better handling for UNIMARC, it should allow management of 100 field
471 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
478 my ($record,$format,$flavour,$to_encoding,$from_encoding) = @_;
481 unless($flavour) {$flavour = C4::Context->preference("marcflavour")};
482 unless($to_encoding) {$to_encoding = "UTF-8"};
484 # ISO-2709 Record (MARC21 or UNIMARC)
485 if (lc($format) =~ /^marc$/o) {
486 # if we're converting encoding of an ISO2709 file, we need to roundtrip through XML
487 # because MARC::Record doesn't directly provide us with an encoding method
488 # It's definitely less than idea and should be fixed eventually - kados
489 my $marcxml; # temporary storage of MARCXML scalar
490 ($error,$marcxml) = marc2marcxml($record,$to_encoding,$flavour);
492 ($error,$newrecord) = marcxml2marc($marcxml,$to_encoding,$flavour);
496 } elsif (lc($format) =~ /^marcxml$/o) { # MARCXML Record
498 ($error,$marc) = marcxml2marc($record,$to_encoding,$flavour);
500 ($error,$newrecord) = marc2marcxml($record,$to_encoding,$flavour);
503 $error.="Unsupported record format:".$format;
505 return ($error,$newrecord);
508 =head1 INTERNAL FUNCTIONS
510 =head2 _entity_encode - Entity-encode an array of strings
514 my ($entity_encoded_string) = _entity_encode($string);
518 my (@entity_encoded_strings) = _entity_encode(@strings);
520 Entity-encode an array of strings
528 my @strings_entity_encoded;
529 foreach my $string (@strings) {
530 my $nfc_string = NFC($string);
531 $nfc_string =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
532 push @strings_entity_encoded, $nfc_string;
534 return @strings_entity_encoded;
537 END { } # module clean-up code here (global destructor)
545 Joshua Ferraro <jmf@liblime.com>