From dc54b34a91f59358c804632b5b15e759930db952 Mon Sep 17 00:00:00 2001 From: kados Date: Tue, 23 May 2006 18:22:58 +0000 Subject: [PATCH] Exports bibliographic data from Koha to an ISO2709 or MARCXML file. OPTIONS: -format MARC, MARCXML [MARC] -e MARC-8 or UTF-8 [UTF-8] -ignoreerrors Ignore encoding errors -assumeunicode Assume Unicode when unsure of encoding -file Filename to store dump [koha.mrc] -h This file EXAMPLES: \$ ./export.pl --format=MARCXML --encoding=UTF-8 -file=2005-05-23-koha.mrc --ignoreerrors Once I'm able, I'll include unicode support in this script, or if someone else has time, feel free. I'd suggest using the new code in MARC::File::XML for dealing with encoding in UNIMARC --- export/export.pl | 127 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 100 insertions(+), 27 deletions(-) diff --git a/export/export.pl b/export/export.pl index 9ac7c83c5b..dead4fd3cb 100755 --- a/export/export.pl +++ b/export/export.pl @@ -1,34 +1,107 @@ #!/usr/bin/perl -## This script allows you to export a rel_2_2 bibliographic db in -#MARC21 format from the command line. +## Exports bibliographic data from Koha to an ISO2709 (MARC or UNIMARC) +## or MARCXML file +# Copyright 2006 (C) Metavore Inc. +# Joshua Ferraro # -use HTML::Template; -use strict; -require Exporter; -use C4::Database; -use C4::Auth; -use C4::Interface::CGI::Output; -use C4::Output; # contains gettemplate +# use perldoc export.pl for nicely formatted documentation + +=head1 NAME + +export.pl - export bibliographic data from Koha to an ISO2709 or MARCXML file. + +=head1 SYNOPSIS + +./export.pl --format=MARCXML --encoding=UTF-8 --ignoreerrors + +=head1 PREREQUISITES + +Koha - (http://koha.org) :-) + +MARC::File::XML - version 0.83 or greater + +MARC::Record - version 2.0RC1 or greater available from Sourceforge + +=head1 TODO + +handle UNIMARC encodings using new MARC::File::XML settings + +=cut + +use strict; use warnings; use C4::Biblio; -use CGI; use C4::Auth; -my $outfile = $ARGV[0]; -open(OUT,">$outfile") or die $!; -my $query = new CGI; - my $start_bib = $query->param("start_bib"); - my $end_bib = $query->param("end_bib"); - my $dbh=C4::Context->dbh; - my $sth; - if ($start_bib && $end_bib) { - $sth=$dbh->prepare("select bibid from marc_biblio where bibid >=? and bibid <=? order by bibid"); - $sth->execute($start_bib,$end_bib); - } else { - $sth=$dbh->prepare("select bibid from marc_biblio order by bibid"); - $sth->execute(); - } - while (my ($bibid) = $sth->fetchrow) { - my $record = MARCgetbiblio($dbh,$bibid); +use Getopt::Long; + +use MARC::Record; +use MARC::Batch; +use MARC::File::XML; +use MARC::Charset; + +my $USAGE = " +USAGE: export.pl -[options] + +Exports bibliographic data from Koha to an ISO2709 or MARCXML file. + +OPTIONS: + -format MARC, MARCXML [MARC] + -e MARC-8 or UTF-8 [UTF-8] + -ignoreerrors Ignore encoding errors + -assumeunicode Assume Unicode when unsure of encoding + -file Filename to store dump [koha.mrc] + -h This file + +EXAMPLES: +\$ ./export.pl --format=MARCXML --encoding=UTF-8 -file=2005-05-23-koha.mrc --ignoreerrors + +"; + +my $VERSION = '.02'; + +# get the command-line options +my ($format,$encoding,$ignoreerrors,$assumeunicode,$outfile,$help) = ('MARC','UTF-8','','','koha.mrc',''); +GetOptions( + 'format:s' => \$format, + 'encoding:s' => \$encoding, + ignoreerrors => \$ignoreerrors, + assumeunicode => \$assumeunicode, + 'outfile:s' => \$outfile, + h => \$help, +); +if ($help) {die $USAGE}; + +# open our filehandle, if UTF-8, set the utf8 flag for Perl +if ((!$encoding) || (lc($encoding) =~ /^utf-?8$/o)) { + open (OUT,">utf8",$outfile); +} else { + open(OUT,">$outfile") or die $!; +} + +# set the MARC::Charset flags specified by user +if ($ignoreerrors) { + MARC::Charset->ignore_errors(1); +} +if ($assumeunicode) { + MARC::Charset->assume_unicode(1); +} - print OUT $record->as_usmarc(); +# open a coneection to the db +my $dbh=C4::Context->dbh; +my $sth=$dbh->prepare("select bibid from marc_biblio order by bibid"); +$sth->execute(); +while (my ($bibid) = $sth->fetchrow) { + my $record = MARCgetbiblio($dbh,$bibid); + if ((!$format) || (lc($format) =~ /^marc$/o)) { # plain ole binary MARC + if (lc($encoding) =~ /^utf-?8$/o) { + my $xml = $record->as_xml_record(); + my $newrecord = MARC::Record::new_from_xml($xml,$encoding); + print OUT $newrecord->as_usmarc(); + } else { + print OUT $record->as_usmarc(); + } + } elsif (lc($format) =~ /^marc-?xml$/o) { # MARCXML format + my $xml = $record->as_xml_record($encoding); + print OUT $xml; } +} close(OUT); -- 2.39.5