[Koha-cvs] koha/export export.pl [rel_2_2]

Joshua Ferraro jmf at kados.org
Tue May 23 20:22:59 CEST 2006


CVSROOT:	/sources/koha
Module name:	koha
Branch: 	rel_2_2
Changes by:	Joshua Ferraro <kados at savannah.gnu.org>	06/05/23 18:22:59

Modified files:
	export         : export.pl 

Log message:
	Exports bibliographic data from Koha to an ISO2709 or MARCXML file.
	
	OPTIONS:
	-format <format>    MARC, MARCXML [MARC]
	-e <encoding>       MARC-8 or UTF-8 [UTF-8]
	-ignoreerrors       Ignore encoding errors
	-assumeunicode      Assume Unicode when unsure of encoding
	-file <filename>    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

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/koha/koha/export/export.pl.diff?only_with_tag=rel_2_2&tr1=1.1.2.1&tr2=1.1.2.2&r1=text&r2=text

Patches:
Index: koha/export/export.pl
diff -u koha/export/export.pl:1.1.2.1 koha/export/export.pl:1.1.2.2
--- koha/export/export.pl:1.1.2.1	Fri Mar 31 18:07:44 2006
+++ koha/export/export.pl	Tue May 23 18:22:58 2006
@@ -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 <jmf at liblime.com>
 #
-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 <format>    MARC, MARCXML [MARC]
+ -e <encoding>       MARC-8 or UTF-8 [UTF-8]
+ -ignoreerrors       Ignore encoding errors 
+ -assumeunicode      Assume Unicode when unsure of encoding
+ -file <filename>    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);





More information about the Koha-cvs mailing list