Bug 17600: Standardize our EXPORT_OK
[srvgit] / misc / migration_tools / 22_to_30 / export_Authorities.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 MARC::File::USMARC;
12 use C4::AuthoritiesMarc;
13 use POSIX qw( close localtime sprintf time );
14 #MARC::File::XML::default_record_format("UNIMARCAUTH");
15 my $dbh = C4::Context->dbh;
16 my $rq= $dbh->prepare(qq|
17   SELECT authid,authtypecode
18   FROM auth_header
19   |);
20 my $filename= shift @ARGV;
21 $rq->execute;
22 #ATTENTION : Mettre la base en utf8 auparavant.
23 #BEWARE : Set database into utf8 before.
24 #open FILEOUTPUT,">:utf8", "$filename" or die "unable to open $filename";
25 while (my ($authid,$authtypecode)=$rq->fetchrow){
26   my $record=AUTHgetauthority($dbh,$authid);
27   if (! utf8::is_utf8($record)) {
28           utf8::decode($record);
29   }
30   
31   if (C4::Context->preference('marcflavour') eq "UNIMARC"){
32         $record->leader('     nac  22     1u 4500');
33     my @time = localtime(time);
34     my $time = sprintf('%04d%02d%02d', $time[5] + 1900, $time[4] + 1, $time[3]);
35     my $string= ($time=~m/([0-9\-]+)/) ? $1 : undef;
36     $string=~s/\-//g;
37      $string = sprintf("%-*s",26, $string);
38      substr($string,9,6,"frey50");
39      unless ($record->subfield('100',"a")){
40        $record->insert_fields_ordered(MARC::Field->new('100',"","","a"=>$string));
41      }
42      if ($record->field('152')){
43        if ($record->subfield('152','b')){
44         } else {
45              $record->field('152')->add_subfields("b"=>$authtypecode);
46         }
47      } else {
48              $record->insert_fields_ordered(MARC::Field->new('152',"","","b"=>$authtypecode));
49      }
50      unless ($record->field('001')){
51        $record->insert_fields_ordered(MARC::Field->new('001',$authid));
52      }
53      
54      AUTHmodauthority($dbh,$authid,$record,1);
55    } else {
56     $record->encoding( 'UTF-8' );
57   }
58 #  warn $record->as_usmarc;
59      # warn $record->as_formatted;
60      #   warn $record->as_usmarc;
61
62   print $record->as_usmarc();
63
64 }
65 close ;