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
22 use strict;# use warnings; #FIXME: turn off warnings before release
24 # please specify in which methods a given module is used
25 use MARC::Record; # marc2marcxml, marcxml2marc, html2marc, changeEncoding
26 use MARC::File::XML; # marc2marcxml, marcxml2marc, html2marcxml, changeEncoding
27 use MARC::Crosswalk::DublinCore; # marc2dcxml
28 use Unicode::Normalize; # _entity_encode
32 use vars qw($VERSION @ISA @EXPORT);
34 # set the version for version checking
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 my $dcxmlfinal = "<?xml version=\"1.0\"?>\n";
250 $dcxmlfinal .= "<metadata
251 xmlns=\"http://example.org/myapp/\"
252 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
253 xsi:schemaLocation=\"http://example.org/myapp/ http://example.org/myapp/schema.xsd\"
254 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
255 xmlns:dcterms=\"http://purl.org/dc/terms/\">";
257 foreach my $element ( $dcxml->elements() ) {
258 $dcxmlfinal.="<"."dc:".$element->name().">".$element->content()."</"."dc:".$element->name()."\n";
260 $dcxmlfinal .= "\n</metadata>";
261 return ($error,$dcxmlfinal);
263 =head2 marc2modsxml - Convert from ISO-2709 to MODS
267 my ($error,$modsxml) = marc2modsxml($marc);
269 Returns a MODS scalar
277 # grab the XML, run it through our stylesheet, push it out to the browser
278 my $xmlrecord = marc2marcxml($marc);
279 my $xslfile = C4::Context->config('intranetdir')."/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21slim2MODS3-1.xsl";
280 my $parser = XML::LibXML->new();
281 my $xslt = XML::LibXSLT->new();
282 my $source = $parser->parse_string($xmlrecord);
283 my $style_doc = $parser->parse_file($xslfile);
284 my $stylesheet = $xslt->parse_stylesheet($style_doc);
285 my $results = $stylesheet->transform($source);
286 my $newxmlrecord = $stylesheet->output_string($results);
287 return ($newxmlrecord);
293 my ($error,$marcxml) = html2marcxml($tags,$subfields,$values,$indicator,$ind_tag);
295 Returns a MARCXML scalar
297 this is used in addbiblio.pl and additem.pl to build the MARCXML record from
300 FIXME: this could use some better code documentation
307 my ($tags,$subfields,$values,$indicator,$ind_tag) = @_;
309 # add the header info
310 my $marcxml= MARC::File::XML::header(C4::Context->preference('TemplateEncoding'),C4::Context->preference('marcflavour'));
312 # some flags used to figure out where in the record we are
318 # handle characters that would cause the parser to choke FIXME: is there a more elegant solution?
319 for (my $i=0;$i<=@$tags;$i++){
320 @$values[$i] =~ s/&/&/g;
321 @$values[$i] =~ s/</</g;
322 @$values[$i] =~ s/>/>/g;
323 @$values[$i] =~ s/"/"/g;
324 @$values[$i] =~ s/'/'/g;
326 if ((@$tags[$i] ne $prevtag)){
327 $j++ unless (@$tags[$i] eq "");
328 #warn "IND:".substr(@$indicator[$j],0,1).substr(@$indicator[$j],1,1)." ".@$tags[$i];
330 $marcxml.="</datafield>\n";
331 if ((@$tags[$i] > 10) && (@$values[$i] ne "")){
332 my $ind1 = substr(@$indicator[$j],0,1);
333 my $ind2 = substr(@$indicator[$j],1,1);
334 $marcxml.="<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
335 $marcxml.="<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
341 if (@$values[$i] ne "") {
343 if (@$tags[$i] eq "000") {
344 $marcxml.="<leader>@$values[$i]</leader>\n";
346 # rest of the fixed fields
347 } elsif (@$tags[$i] < 010) { #FIXME: <10 was the way it was, there might even be a better way
348 $marcxml.="<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
351 my $ind1 = substr(@$indicator[$j],0,1);
352 my $ind2 = substr(@$indicator[$j],1,1);
353 $marcxml.="<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
354 $marcxml.="<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
359 } else { # @$tags[$i] eq $prevtag
360 if (@$values[$i] eq "") {
363 my $ind1 = substr(@$indicator[$j],0,1);
364 my $ind2 = substr(@$indicator[$j],1,1);
365 $marcxml.="<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
368 $marcxml.="<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
371 $prevtag = @$tags[$i];
373 $marcxml.= MARC::File::XML::footer();
375 return ($error,$marcxml);
382 Probably best to avoid using this ... it has some rather striking problems:
386 * saves blank subfields
388 * subfield order is hardcoded to always start with 'a' for repeatable tags (because it is hardcoded in the addfield routine).
390 * 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).
392 * the underlying routines didn't support subfield reordering or subfield repeatability.
396 I've left it in here because it could be useful if someone took the time to fix it. -- kados
403 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
405 my $record = MARC::Record->new();
406 # my %subfieldlist=();
407 my $prevvalue; # if tag <10
408 my $field; # if tag >=10
409 for (my $i=0; $i< @$rtags; $i++) {
410 # rebuild MARC::Record
411 # warn "0=>".@$rtags[$i].@$rsubfields[$i]." = ".@$rvalues[$i].": ";
412 if (@$rtags[$i] ne $prevtag) {
415 if (($prevtag ne '000') && ($prevvalue ne "")) {
416 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
417 } elsif ($prevvalue ne ""){
418 $record->leader($prevvalue);
422 if (($field) && ($field ne "")) {
423 $record->add_fields($field);
426 $indicators{@$rtags[$i]}.=' ';
427 # skip blank tags, I hope this works
428 if (@$rtags[$i] eq ''){
429 $prevtag = @$rtags[$i];
433 if (@$rtags[$i] <10) {
434 $prevvalue= @$rvalues[$i];
438 if (@$rvalues[$i] eq "") {
441 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
443 # warn "1=>".@$rtags[$i].@$rsubfields[$i]." = ".@$rvalues[$i].": ".$field->as_formatted;
445 $prevtag = @$rtags[$i];
447 if (@$rtags[$i] <10) {
448 $prevvalue=@$rvalues[$i];
450 if (length(@$rvalues[$i])>0) {
451 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
452 # warn "2=>".@$rtags[$i].@$rsubfields[$i]." = ".@$rvalues[$i].": ".$field->as_formatted;
455 $prevtag= @$rtags[$i];
459 # the last has not been included inside the loop... do it now !
461 #warn Dumper($field->{_subfields});
462 $record->add_fields($field) if (($field) && $field ne "");
463 #warn "HTML2MARC=".$record->as_formatted;
467 =head2 changeEncoding - Change the encoding of a record
471 my ($error, $newrecord) = changeEncoding($record,$format,$flavour,$to_encoding,$from_encoding);
473 Changes the encoding of a record
477 C<$record> - the record itself can be in ISO-2709, a MARC::Record object, or MARCXML for now (required)
479 C<$format> - MARC or MARCXML (required)
481 C<$flavour> - MARC21 or UNIMARC, if MARC21, it will change the leader (optional) [defaults to Koha system preference]
483 C<$to_encoding> - the encoding you want the record to end up in (optional) [UTF-8]
485 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)
489 FIXME: the from_encoding doesn't work yet
491 FIXME: better handling for UNIMARC, it should allow management of 100 field
493 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
500 my ($record,$format,$flavour,$to_encoding,$from_encoding) = @_;
503 unless($flavour) {$flavour = C4::Context->preference("marcflavour")};
504 unless($to_encoding) {$to_encoding = "UTF-8"};
506 # ISO-2709 Record (MARC21 or UNIMARC)
507 if (lc($format) =~ /^marc$/o) {
508 # if we're converting encoding of an ISO2709 file, we need to roundtrip through XML
509 # because MARC::Record doesn't directly provide us with an encoding method
510 # It's definitely less than idea and should be fixed eventually - kados
511 my $marcxml; # temporary storage of MARCXML scalar
512 ($error,$marcxml) = marc2marcxml($record,$to_encoding,$flavour);
514 ($error,$newrecord) = marcxml2marc($marcxml,$to_encoding,$flavour);
518 } elsif (lc($format) =~ /^marcxml$/o) { # MARCXML Record
520 ($error,$marc) = marcxml2marc($record,$to_encoding,$flavour);
522 ($error,$newrecord) = marc2marcxml($record,$to_encoding,$flavour);
525 $error.="Unsupported record format:".$format;
527 return ($error,$newrecord);
530 =head1 INTERNAL FUNCTIONS
532 =head2 _entity_encode - Entity-encode an array of strings
536 my ($entity_encoded_string) = _entity_encode($string);
540 my (@entity_encoded_strings) = _entity_encode(@strings);
542 Entity-encode an array of strings
550 my @strings_entity_encoded;
551 foreach my $string (@strings) {
552 my $nfc_string = NFC($string);
553 $nfc_string =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
554 push @strings_entity_encoded, $nfc_string;
556 return @strings_entity_encoded;
559 END { } # module clean-up code here (global destructor)
565 Joshua Ferraro <jmf@liblime.com>