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