Bug 28617: Remove kohalib.pl and rely on PERL5LIB
[koha.git] / misc / migration_tools / 22_to_30 / export_Authorities_xml.pl
1 #!/usr/bin/perl
2 use Modern::Perl;
3 use C4::Context;
4 use MARC::File::XML(BinaryEncoding=>"utf8");
5 use C4::AuthoritiesMarc;
6 use POSIX;
7 MARC::File::XML::default_record_format("UNIMARCAUTH");
8 my $dbh = C4::Context->dbh;
9 my $rq= $dbh->prepare(qq|
10   SELECT authid
11   FROM auth_header
12   |);
13 my $filename= shift @ARGV;
14 $rq->execute;
15 #ATTENTION : Mettre la base en utf8 auparavant.
16 #BEWARE : Set database into utf8 before.
17 while (my ($authid)=$rq->fetchrow){
18 open my $fileoutput, '>:encoding(UTF-8)', "./$filename/$authid.xml" or die "unable to open $filename";
19   my $record=AUTHgetauthority($dbh,$authid);
20   if (! utf8::is_utf8($record)) {
21     utf8::decode($record);
22   }
23                         
24 #  if (C4::Context->preference('marcflavour') eq "UNIMARC"){
25         $record->leader('     nac  22     1u 4500');
26     my @time = localtime(time);
27     my $time = sprintf('%04d%02d%02d', $time[5] + 1900, $time[4] + 1, $time[3]);
28     my $string = ($time=~m/([0-9\-]+)/) ? $1 : undef;
29     $string=~s/\-//g;
30      $string = sprintf("%-*s",26, $string);
31      substr($string,9,6,"frey50");
32      unless ($record->subfield(100,"a")){
33        $record->insert_fields_ordered(MARC::Field->new(100,"","","a"=>$string));
34      }
35      unless ($record->subfield('001')){
36        $record->insert_fields_ordered(MARC::Field->new('001',$authid));
37      }
38      # } else {
39 #    $record->encoding( 'UTF-8' );
40 #  }
41   print {$fileoutput}  $record->as_xml();
42 close $fileoutput;
43
44 }