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