installer: command-line scripts improve finding C4 modules
[koha.git] / misc / migration_tools / 22_to_30 / export_Authorities.pl
1 #!/usr/bin/perl
2 BEGIN {
3     # find Koha's Perl modules
4     # test carefully before changing this
5     use FindBin;
6     eval { require "$FindBin::Bin/../../kohalib.pl" };
7 }
8 use C4::Context;
9 #use MARC::File::XML(BinaryEncoding=>"utf8");
10 #use MARC::File::USMARC;
11 use MARC::Record;
12 use C4::AuthoritiesMarc;
13 use POSIX;
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 $string=$1 if $time=~m/([0-9\-]+)/;
34     $string=~s/\-//g;
35      $string = sprintf("%-*s",26, $string);
36      substr($string,9,6,"frey50");
37      unless ($record->subfield('100',"a")){
38        $record->insert_fields_ordered(MARC::Field->new('100',"","","a"=>$string));
39      }
40      if ($record->field('152')){
41        if ($record->subfield('152','b')){
42         } else {
43              $record->field('152')->add_subfields("b"=>$authtypecode);
44         }
45      } else {
46              $record->insert_fields_ordered(MARC::Field->new('152',"","","b"=>$authtypecode));
47      }
48      unless ($record->field('001')){
49        $record->insert_fields_ordered(MARC::Field->new('001',$authid));
50      }
51      
52      AUTHmodauthority($dbh,$authid,$record,1);
53    } else {
54     $record->encoding( 'UTF-8' );
55   }
56 #  warn $record->as_usmarc;
57      # warn $record->as_formatted;
58      #   warn $record->as_usmarc;
59
60   print $record->as_usmarc();
61
62 }
63 close ;