MT2116, Follow-up: CSV Export, fix backporting bugs
[koha.git] / C4 / Record.pm
index 3d5cf7a..bce362b 100644 (file)
@@ -18,34 +18,41 @@ package C4::Record;
 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 # Suite 330, Boston, MA  02111-1307 USA
 #
-# $Id$
 #
-use strict; use warnings; #FIXME: turn off warnings before release
+use strict;# use warnings; #FIXME: turn off warnings before release
 
 # please specify in which methods a given module is used
 use MARC::Record; # marc2marcxml, marcxml2marc, html2marc, changeEncoding
 use MARC::File::XML; # marc2marcxml, marcxml2marc, html2marcxml, changeEncoding
 use MARC::Crosswalk::DublinCore; # marc2dcxml
-#use MODS::Record; # marc2modsxml
+use Biblio::EndnoteStyle;
 use Unicode::Normalize; # _entity_encode
+use XML::LibXSLT;
+use XML::LibXML;
+use C4::Biblio; #marc2bibtex
+use C4::Csv; #marc2csv
+use C4::Koha; #marc2csv
+use YAML; #marcrecords2csv
+use Text::CSV::Encoded; #marc2csv
 
 use vars qw($VERSION @ISA @EXPORT);
 
 # set the version for version checking
-$VERSION = do { my @v = '$Revision$' =~ /\d+/g;
-                shift(@v) . "." . join("_", map {sprintf "%03d", $_ } @v); };
+$VERSION = 3.00;
 
 @ISA = qw(Exporter);
 
 # only export API methods
 
 @EXPORT = qw(
+  &marc2endnote
   &marc2marc
   &marc2marcxml
   &marcxml2marc
   &marc2dcxml
   &marc2modsxml
-
+  &marc2bibtex
+  &marc2csv
   &html2marcxml
   &html2marc
   &changeEncoding
@@ -247,7 +254,19 @@ sub marc2dcxml {
                $crosswalk = MARC::Crosswalk::DublinCore->new( qualified => 1 );
        }
        my $dcxml = $crosswalk->as_dublincore($marc_record_obj);
-       return ($error,$dcxml);
+       my $dcxmlfinal = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
+       $dcxmlfinal .= "<metadata
+  xmlns=\"http://example.org/myapp/\"
+  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
+  xsi:schemaLocation=\"http://example.org/myapp/ http://example.org/myapp/schema.xsd\"
+  xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
+  xmlns:dcterms=\"http://purl.org/dc/terms/\">";
+
+       foreach my $element ( $dcxml->elements() ) {
+                $dcxmlfinal.="<"."dc:".$element->name().">".$element->content()."</"."dc:".$element->name().">\n";
+    }
+       $dcxmlfinal .= "\n</metadata>";
+       return ($error,$dcxmlfinal);
 }
 =head2 marc2modsxml - Convert from ISO-2709 to MODS
 
@@ -262,32 +281,258 @@ Returns a MODS scalar
 =cut
 
 sub marc2modsxml {
-       use XML::XSLT;
-       #use XML::LibXSLT;
        my ($marc) = @_;
-       my $error;
-       my $marcxml;
+       # grab the XML, run it through our stylesheet, push it out to the browser
+       my $xmlrecord = marc2marcxml($marc);
+       my $xslfile = C4::Context->config('intrahtdocs')."/prog/en/xslt/MARC21slim2MODS3-1.xsl";
+       my $parser = XML::LibXML->new();
+       my $xslt = XML::LibXSLT->new();
+       my $source = $parser->parse_string($xmlrecord);
+       my $style_doc = $parser->parse_file($xslfile);
+       my $stylesheet = $xslt->parse_stylesheet($style_doc);
+       my $results = $stylesheet->transform($source);
+       my $newxmlrecord = $stylesheet->output_string($results);
+       return ($newxmlrecord);
+}
+
+sub marc2endnote {
+    my ($marc) = @_;
+       my $marc_rec_obj =  MARC::Record->new_from_usmarc($marc);
+       my $f260 = $marc_rec_obj->field('260');
+       my $f260a = $f260->subfield('a') if $f260;
+    my $f710 = $marc_rec_obj->field('710');
+    my $f710a = $f710->subfield('a') if $f710;
+       my $f500 = $marc_rec_obj->field('500');
+       my $abstract = $f500->subfield('a') if $f500;
+       my $fields = {
+               DB => C4::Context->preference("LibraryName"),
+               Title => $marc_rec_obj->title(),        
+               Author => $marc_rec_obj->author(),      
+               Publisher => $f710a,
+               City => $f260a,
+               Year => $marc_rec_obj->publication_date,
+               Abstract => $abstract,
+       };
+       my $endnote;
+       my $style = new Biblio::EndnoteStyle();
+       my $template;
+       $template.= "DB - DB\n" if C4::Context->preference("LibraryName");
+       $template.="T1 - Title\n" if $marc_rec_obj->title();
+       $template.="A1 - Author\n" if $marc_rec_obj->author();
+       $template.="PB - Publisher\n" if  $f710a;
+       $template.="CY - City\n" if $f260a;
+       $template.="Y1 - Year\n" if $marc_rec_obj->publication_date;
+       $template.="AB - Abstract\n" if $abstract;
+       my ($text, $errmsg) = $style->format($template, $fields);
+       return ($text);
+       
+}
+
+=head2 marcrecords2csv - Convert several records from UNIMARC to CSV
+Pre and postprocessing can be done through a YAML file
+
+=over 4
+
+my ($csv) = marcrecords2csv($biblios, $csvprofileid);
+
+Returns a CSV scalar
+
+=over 2
+
+C<$biblio> - a list of biblionumbers
+
+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)
+
+=back
+
+=back
+
+=cut
+sub marc2csv {
+    my ($biblios, $id) = @_;
+    my $output;
+    my $csv = Text::CSV::Encoded->new();
+
+    # Getting yaml file
+    my $configfile = "../tools/csv-profiles/$id.yaml";
+    my ($preprocess, $postprocess, $fieldprocessing);
+    if (-e $configfile){
+        ($preprocess,$postprocess, $fieldprocessing) = YAML::LoadFile($configfile);
+    }
+
+    # Preprocessing
+    eval $preprocess if ($preprocess);
+
+    my $firstpass = 1;
+    foreach my $biblio (@$biblios) {
+       $output .= marcrecord2csv($biblio, $id, $firstpass, $csv, $fieldprocessing) ;
+       $firstpass = 0;
+    }
+
+    # Postprocessing
+    eval $postprocess if ($postprocess);
+
+    return $output;
+}
+
+=head2 marc2csv - Convert a single record from UNIMARC to CSV
+
+=over 4
+
+my ($csv) = marc2csv($biblio, $csvprofileid, $header);
+
+Returns a CSV scalar
+
+=over 2
+
+C<$biblio> - a biblionumber
+
+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)
+
+C<$header> - true if the headers are to be printed (typically at first pass)
+
+C<$csv> - an already initialised Text::CSV object
+
+=back
+
+=back
+
+=cut
+
+
+sub marcrecord2csv {
+    my ($biblio, $id, $header, $csv, $fieldprocessing) = @_;
+    my $output;
+
+    # Getting the record
+    my $record = GetMarcBiblio($biblio);
+
+    # Getting the framework
+    my $frameworkcode = GetFrameworkCode($biblio);
+
+    # Getting information about the csv profile
+    my $profile = GetCsvProfile($id);
 
-       # open some files for testing
-       open MARCBIG21MARC21SLIM,"/home/koha/head/koha/C4/MARC21slim2MODS3-1.xsl" or die $!;
-       my $marcbig2marc21_slim; # = scalar (MARC21MARC8);
-       foreach my $line (<MARCBIG21MARC21SLIM>) {
-       $marcbig2marc21_slim .= $line;
+    # Getting output encoding
+    my $encoding          = $profile->{encoding} || 'utf8';
+
+    # Getting separators
+    my $csvseparator      = $profile->{csv_separator}      || ',';
+    my $fieldseparator    = $profile->{field_separator}    || '#';
+    my $subfieldseparator = $profile->{subfield_separator} || '|';
+
+    # TODO: Be more generic (in case we have to handle other protected chars or more separators)
+    if ($csvseparator eq '\t') { $csvseparator = "\t" }
+    if ($fieldseparator eq '\t') { $fieldseparator = "\t" }
+    if ($subfieldseparator eq '\t') { $subfieldseparator = "\t" }
+
+    $csv->encoding_out($encoding) if ($encoding ne 'utf8');
+    $csv->sep_char($csvseparator);
+
+    # Getting the marcfields
+    my $marcfieldslist = $profile->{marcfields};
+
+    # Getting the marcfields as an array
+    my @marcfieldsarray = split('\|', $marcfieldslist);
+
+   # Separating the marcfields from the the user-supplied headers
+    my @marcfields;
+    foreach (@marcfieldsarray) {
+        my @result = split('=', $_);
+        if (scalar(@result) == 2) {
+           push @marcfields, { header => $result[0], field => $result[1] }; 
+        } else {
+           push @marcfields, { field => $result[0] }
+        }
+    }
+
+    # If we have to insert the headers
+    if ($header) {
+       my @marcfieldsheaders;
+       my $dbh   = C4::Context->dbh;
+
+       # For each field or subfield
+       foreach (@marcfields) {
+
+           my $field = $_->{field};
+
+           # If we have a user-supplied header, we use it
+           if (exists $_->{header}) {
+                   push @marcfieldsheaders, $_->{header};
+           } else {
+               # If not, we get the matching tag name from koha
+               if (index($field, '$') > 0) {
+                   my ($fieldtag, $subfieldtag) = split('\$', $field);
+                   my $query = "SELECT liblibrarian FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield=?";
+                   my $sth = $dbh->prepare($query);
+                   $sth->execute($fieldtag, $subfieldtag);
+                   my @results = $sth->fetchrow_array();
+                   push @marcfieldsheaders, $results[0];
+               } else {
+                   my $query = "SELECT liblibrarian FROM marc_tag_structure WHERE tagfield=?";
+                   my $sth = $dbh->prepare($query);
+                   $sth->execute($field);
+                   my @results = $sth->fetchrow_array();
+                   push @marcfieldsheaders, $results[0];
+               }
+           }
        }
+       $csv->combine(@marcfieldsheaders);
+       $output = $csv->string() . "\n";        
+    }
+
+    # For each marcfield to export
+    my @fieldstab;
+    foreach (@marcfields) {
+       my $marcfield = $_->{field};
+       # If it is a subfield
+       if (index($marcfield, '$') > 0) {
+           my ($fieldtag, $subfieldtag) = split('\$', $marcfield);
+           my @fields = $record->field($fieldtag);
+           my @tmpfields;
+
+           # For each field
+           foreach my $field (@fields) {
+
+               # We take every matching subfield
+               my @subfields = $field->subfield($subfieldtag);
+               foreach my $subfield (@subfields) {
+
+                   # Getting authorised value
+                   my $authvalues = GetKohaAuthorisedValuesFromField($fieldtag, $subfieldtag, $frameworkcode, undef);
+                   push @tmpfields, (defined $authvalues->{$subfield}) ? $authvalues->{$subfield} : $subfield;
+               }
+           }
+           push (@fieldstab, join($subfieldseparator, @tmpfields));            
+       # Or a field
+       } else {
+           my @fields = ($record->field($marcfield));
+           my $authvalues = GetKohaAuthorisedValuesFromField($marcfield, undef, $frameworkcode, undef);
+
+           my @valuesarray;
+           foreach (@fields) {
+               my $value;
+
+               # Getting authorised value
+               $value = defined $authvalues->{$_->as_string} ? $authvalues->{$_->as_string} : $_->as_string;
+
+               # Field processing
+               eval $fieldprocessing if ($fieldprocessing);
+
+               push @valuesarray, $value;
+           }
+           push (@fieldstab, join($fieldseparator, @valuesarray)); 
+        }
+    };
+
+    $csv->combine(@fieldstab);
+    $output .= $csv->string() . "\n";
+   
+    return $output;
 
-       # set some defailts
-       my $to_encoding = "UTF-8";
-       my $flavour = "MARC21";
-       
-       # first convert our ISO-2709 to MARCXML
-       ($error,$marcxml) = marc2marcxml($marc,$to_encoding,$flavour);  
-       my $xslt_obj = XML::XSLT->new ($marcbig2marc21_slim, warnings => 1);
-       $xslt_obj->transform ($marcxml);
-       my $xslt_string = $xslt_obj->toString;
-       $xslt_obj->dispose();
-       warn $xslt_string;
-       return ($error,$xslt_string);
 }
+
+
 =head2 html2marcxml
 
 =over 4
@@ -346,7 +591,7 @@ sub html2marcxml {
                                                $marcxml.="<leader>@$values[$i]</leader>\n";
                                                $first=1;
                                        # rest of the fixed fields
-                                       } elsif (@$tags[$i] < 010) { #FIXME: <10 was the way it was, there might even be a better way
+                                       } elsif (@$tags[$i] lt '010') { # don't compare numerically 010 == 8
                                                $marcxml.="<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
                                                $first=1;
                                        } else {
@@ -529,6 +774,94 @@ sub changeEncoding {
        return ($error,$newrecord);
 }
 
+=head2 marc2bibtex - Convert from MARC21 and UNIMARC to BibTex
+
+=over 4
+
+my ($bibtex) = marc2bibtex($record, $id);
+
+Returns a BibTex scalar
+
+=over 2
+
+C<$record> - a MARC::Record object
+
+C<$id> - an id for the BibTex record (might be the biblionumber)
+
+=back
+
+=back
+
+=cut
+
+
+sub marc2bibtex {
+    my ($record, $id) = @_;
+    my $tex;
+
+    # Authors
+    my $marcauthors = GetMarcAuthors($record,C4::Context->preference("marcflavour"));
+    my $author;
+    for my $authors ( map { map { @$_ } values %$_  } @$marcauthors  ) {  
+       $author .= " and " if ($author && $$authors{value});
+       $author .= $$authors{value} if ($$authors{value}); 
+    }
+
+    # Defining the conversion hash according to the marcflavour
+    my %bh;
+    if (C4::Context->preference("marcflavour") eq "UNIMARC") {
+       
+       # FIXME, TODO : handle repeatable fields
+       # TODO : handle more types of documents
+
+       # Unimarc to bibtex hash
+       %bh = (
+
+           # Mandatory
+           author    => $author,
+           title     => $record->subfield("200", "a") || "",
+           editor    => $record->subfield("210", "g") || "",
+           publisher => $record->subfield("210", "c") || "",
+           year      => $record->subfield("210", "d") || $record->subfield("210", "h") || "",
+
+           # Optional
+           volume  =>  $record->subfield("200", "v") || "",
+           series  =>  $record->subfield("225", "a") || "",
+           address =>  $record->subfield("210", "a") || "",
+           edition =>  $record->subfield("205", "a") || "",
+           note    =>  $record->subfield("300", "a") || "",
+           url     =>  $record->subfield("856", "u") || ""
+       );
+    } else {
+
+       # Marc21 to bibtex hash
+       %bh = (
+
+           # Mandatory
+           author    => $author,
+           title     => $record->subfield("245", "a") || "",
+           editor    => $record->subfield("260", "f") || "",
+           publisher => $record->subfield("260", "b") || "",
+           year      => $record->subfield("260", "c") || $record->subfield("260", "g") || "",
+
+           # Optional
+           # unimarc to marc21 specification says not to convert 200$v to marc21
+           series  =>  $record->subfield("490", "a") || "",
+           address =>  $record->subfield("260", "a") || "",
+           edition =>  $record->subfield("250", "a") || "",
+           note    =>  $record->subfield("500", "a") || "",
+           url     =>  $record->subfield("856", "u") || ""
+       );
+    }
+
+    $tex .= "\@book{";
+    $tex .= join(",\n", $id, map { $bh{$_} ? qq(\t$_ = "$bh{$_}") : () } keys %bh);
+    $tex .= "\n}\n";
+
+    return $tex;
+}
+
+
 =head1 INTERNAL FUNCTIONS
 
 =head2 _entity_encode - Entity-encode an array of strings
@@ -562,14 +895,11 @@ END { }       # module clean-up code here (global destructor)
 1;
 __END__
 
-=back
-
 =head1 AUTHOR
 
 Joshua Ferraro <jmf@liblime.com>
 
 =head1 MODIFICATIONS
 
-# $Id$
 
 =cut