All the necessary mySQL tables for those who wish to install before an upgrader...
[koha.git] / misc / export_xml_koharecords.pl
1 #!/usr/bin/perl
2 ## This script allows you to export a rel_2_2 bibliographic db in 
3 #MARC21 format from the command line.
4 #
5 use strict;
6 require Exporter;
7 use C4::Auth;
8 use C4::Biblio;
9 use XML::Simple;
10 use Getopt::Long;
11
12 my ( $out_marc_file, $check) ;
13 GetOptions(
14     'file:s'    => \$out_marc_file,
15     'c:s' => \$check,
16    
17 );
18 ### Usage 
19 ## export_xml_koharecords -file somefilename -c 1
20 ## use the -c flag if you want to check whether you xml is proper or not. Advisable but very slow
21 open(OUT,">" ,$out_marc_file) or die $!;
22
23         my $dbh= C4::Context->dbh;
24
25         my $sth;
26
27                 $sth=$dbh->prepare("select biblionumber,marcxml from biblio  order by biblionumber ");
28 my $sth2=$dbh->prepare("select marcxml from items where biblionumber =?");
29                 $sth->execute();
30         
31 my $header=&collection_header;
32         print OUT '<?xml version="1.0" encoding="UTF-8"?>'."\n";
33         print OUT $header;
34         while (my ($biblionumber,$marcxml) = $sth->fetchrow) {
35 my $hash;
36 if ($check){
37         eval {
38          $hash=XMLin($marcxml);
39         }; ### is it a proper xml? broken xml may crash ZEBRA- slow but safe
40
41
42         if ($@){
43 warn $biblionumber;
44         next;
45         }
46 }
47                 print OUT "<koharecord>\n";
48                 print OUT $marcxml;
49                 print OUT "<holdings>";
50         $sth2->execute($biblionumber);
51                 while (my ($itemxml)=$sth2->fetchrow){
52 if ($check){
53         eval {
54            $hash=XMLin($itemxml);
55         }; ### is it a proper xml? broken xml may crash ZEBRA- slow but safe
56
57         if ($@){
58 warn $biblionumber;
59         next;
60         }
61 }
62
63                 print OUT $itemxml;
64                 }
65         print OUT "</holdings></koharecord>\n";
66         }
67         print OUT "</kohacollection>\n";
68 close(OUT);
69
70 sub collection_header {
71 ####  this one is for koha collection 
72     my $format = shift;
73     my $enc = shift || 'UTF-8';
74     return( <<KOHA_XML_HEADER );
75
76 <kohacollection xmlns:marc="http://loc.gov/MARC21/slim" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://library.neu.edu.tr/kohanamespace/koharecord.xsd">
77 KOHA_XML_HEADER
78 }