Bug 17702: Add UI to manage account credit types
[koha.git] / misc / migration_tools / buildEDITORS.pl
blob37ece96089b20c69d5b6a8b39d417131ef5b9b9f
1 #!/usr/bin/perl
2 # script that rebuild EDITORS
4 use strict;
5 #use warnings; FIXME - Bug 2505
7 # Koha modules used
8 use MARC::File::USMARC;
9 use MARC::Record;
10 use MARC::Batch;
11 use Koha::Script;
12 use C4::Context;
13 use C4::Biblio;
14 use C4::AuthoritiesMarc;
15 use Time::HiRes qw(gettimeofday);
17 use Getopt::Long;
18 my ( $input_marc_file, $number) = ('',0);
19 my ($version, $verbose, $test_parameter, $confirm,$delete);
20 GetOptions(
21 'h' => \$version,
22 'd' => \$delete,
23 't' => \$test_parameter,
24 'v' => \$verbose,
25 'c' => \$confirm,
28 if ($version or !$confirm) {
29 print <<EOF
30 small script to recreate a authority table into Koha.
31 This will parse all your biblios to recreate isbn / editor / collections for the unimarc_210c and unimarc_225a plugins.
33 Remember those plugins will work only if you have an EDITORS authority type, with
34 \t200a being the first 2 parts of an ISBN
35 \t200b being the editor name
36 \t200c (repeatable) being the series title
38 parameters :
39 \t-c : confirmation flag. the script will run only with this flag. Otherwise, it will just show this help screen.
40 \t-d : delete existing EDITORS before rebuilding them
41 \t-t : test parameters : run the script but don't create really the EDITORS
42 EOF
43 ;#'
45 exit;
48 my $dbh = C4::Context->dbh;
49 if ($delete) {
50 print "deleting EDITORS\n";
51 my $del1 = $dbh->prepare("delete from auth_subfield_table where authid=?");
52 my $del2 = $dbh->prepare("delete from auth_word where authid=?");
53 my $sth = $dbh->prepare("select authid from auth_header where authtypecode='EDITORS'");
54 $sth->execute;
55 while (my ($authid) = $sth->fetchrow) {
56 $del1->execute($authid);
57 $del2->execute($authid);
59 $dbh->do("delete from auth_header where authtypecode='EDITORS'");
62 if ($test_parameter) {
63 print "TESTING MODE ONLY\n DOING NOTHING\n===============\n";
65 $|=1; # flushes output
66 my $starttime = gettimeofday;
67 my $sth = $dbh->prepare("select bibid from marc_biblio");
68 $sth->execute;
69 my $i=1;
70 my %alreadydone;
71 my $counter;
72 my %hash;
73 while (my ($bibid) = $sth->fetchrow) {
74 my $record = GetMarcBiblio({ biblionumber => $bibid });
75 my $isbnField = $record->field('010');
76 next unless $isbnField;
77 my $isbn=$isbnField->subfield('a');
78 my $seg1;
79 if(substr($isbn, 0, 1) <=7) {
80 $seg1 = substr($isbn, 0, 1);
81 } elsif(substr($isbn, 0, 2) <= 94) {
82 $seg1 = substr($isbn, 0, 2);
83 } elsif(substr($isbn, 0, 3) <= 995) {
84 $seg1 = substr($isbn, 0, 3);
85 } elsif(substr($isbn, 0, 4) <= 9989) {
86 $seg1 = substr($isbn, 0, 4);
87 } else {
88 $seg1 = substr($isbn, 0, 5);
90 my $x = substr($isbn, length($seg1));
91 my $seg2;
92 if(substr($x, 0, 2) <= 19) {
93 # if(sTmp2 < 10) sTmp2 = "0" sTmp2;
94 $seg2 = substr($x, 0, 2);
95 } elsif(substr($x, 0, 3) <= 699) {
96 $seg2 = substr($x, 0, 3);
97 } elsif(substr($x, 0, 4) <= 8399) {
98 $seg2 = substr($x, 0, 4);
99 } elsif(substr($x, 0, 5) <= 89999) {
100 $seg2 = substr($x, 0, 5);
101 } elsif(substr($x, 0, 6) <= 9499999) {
102 $seg2 = substr($x, 0, 6);
103 } else {
104 $seg2 = substr($x, 0, 7);
106 $counter++;
107 print ".";
108 my $timeneeded = gettimeofday - $starttime;
109 print "$counter in $timeneeded s\n" unless ($counter % 100);
111 my $field = $record->field('210');
112 my $editor;
113 $editor=$field->subfield('c') if $field;
115 $field = $record->field('225');
116 my $collection;
117 $collection=$field->subfield('a') if $field;
119 # print "WARNING : editor empty for ".$record->as_formatted unless $editor and !$verbose;
121 $hash{$seg1.$seg2}->{editors} = $editor unless ($hash{$seg1.$seg2}->{editors});
122 $hash{$seg1.$seg2}->{collections}->{$collection}++ if $collection;
125 foreach my $isbnstart (sort keys %hash) {
126 print "$isbnstart -- ".$hash{$isbnstart}->{editors} if $verbose;
127 my $collections = $hash{$isbnstart}->{collections};
128 my $seriestitlelist;
129 foreach my $collection (sort keys %$collections) {
130 print " CC $collection : ".$collections->{$collection} if $verbose;
131 $seriestitlelist.=$collection."|";
133 my $authorityRecord = MARC::Record->new();
134 my $newfield = MARC::Field->new(200,'','','a' => "".$isbnstart,
135 'b' => "".$hash{$isbnstart}->{editors},
136 'c' => "".$seriestitlelist);
137 $authorityRecord->insert_fields_ordered($newfield);
138 my $authid=AUTHaddauthority($dbh,$authorityRecord,'','EDITORS');
140 # print $authorityRecord->as_formatted."\n";
141 print "\n" if $verbose;
143 exit;
145 # my $timeneeded = gettimeofday - $starttime;
146 # print "$i in $timeneeded s\n" unless ($i % 50);
147 # foreach my $field ($record->field(995)) {
148 # $record->delete_field($field);
150 # my $totdone=0;
151 # my $authid;
152 # foreach my $fieldnumber (('710','711','712')) {
153 # foreach my $field ($record->field($fieldnumber)) {
154 # # print "=>".$field->as_formatted."\n";
155 # foreach my $authentry ($field->subfield("a")) {
156 # my $hashentry = $authentry;
157 # # la particularit�de ce script l� c'est que l'entr� dans la table d'autorit�est $a -- $b (et pas $x -- $x -- $x -- $a comme pour les autorit� NC)
158 # # si n�essaire, compl�er avec le $c (n'existe pas dans le fichier que j'ai migr�avec cette moulinette
159 # # supprimer les accents, certaines entr�s sont sans, d'autres avec !
160 # # mysql ne diff�encie pas, mais les hash perl oui !
161 # $hashentry =~ s/���e/g;
162 # $hashentry =~ s/��a/g;
163 # $hashentry =~ s/�i/g;
164 # $hashentry =~ s/�o/g;
165 # $hashentry =~ s/|/u/g;
166 # $hashentry = uc($hashentry);
167 # print "==>$hashentry" if $hashentry =~ /.*ETATS.*/;
168 # $totdone++;
169 # if ($alreadydone{$hashentry}) {
170 # $authid = $alreadydone{$hashentry};
171 # print ".";
172 # } else {
173 # print "*";
174 # #create authority.
175 # my $authorityRecord = MARC::Record->new();
176 # my $newfield = MARC::Field->new(210,'','','a' => "".$authentry,
177 # 'b' => "".$field->subfield('b'),
178 # 'c' => "".$field->subfield('c'),
179 # );
180 # $authorityRecord->insert_fields_ordered($newfield);
181 # $authid=AUTHaddauthority($dbh,$authorityRecord,'','CO');
182 # $alreadydone{$hashentry} = $authid;
183 # # OK, on garde la notice d'autorit� on cherche les notices biblio et on les met �jour...
184 # if ($fieldnumber eq '710') {
185 # $sthBIBLIOS710->execute($authentry);
186 # while (my ($bibid,$tag,$tagorder,$subfieldorder) = $sthBIBLIOS710->fetchrow) {
187 # my $inbiblio = GetMarcBiblio({ biblionumber => $bibid });
188 # my $isOK = 0;
189 # foreach my $in7xx ($inbiblio->field($fieldnumber)) {
190 # # !!!!! ici, il faut reconstruire l'entr� de la table de hachage comme ci dessus
191 # # sinon,
192 # my $inEntry = $in7xx->subfield('a');
193 # $inEntry =~ s/���e/g;
194 # $inEntry =~ s/��a/g;
195 # $inEntry =~ s/�i/g;
196 # $inEntry =~ s/�o/g;
197 # $inEntry =~ s/|/u/g;
198 # $inEntry = uc($inEntry);
199 # $isOK=1 if $inEntry eq $hashentry;
201 # C4::Biblio::MARCaddsubfield($dbh,$bibid,$tag,'',$tagorder,9,$subfieldorder,$authid) if $isOK;
204 # if ($fieldnumber eq '711') {
205 # $sthBIBLIOS711->execute($authentry);
206 # while (my ($bibid,$tag,$tagorder,$subfieldorder) = $sthBIBLIOS711->fetchrow) {
207 # my $inbiblio = GetMarcBiblio({ biblionumber => $bibid });
208 # my $isOK = 0;
209 # foreach my $in7xx ($inbiblio->field($fieldnumber)) {
210 # # !!!!! ici, il faut reconstruire l'entr� de la table de hachage comme ci dessus
211 # # sinon,
212 # my $inEntry = $in7xx->subfield('a');
213 # $inEntry =~ s/���e/g;
214 # $inEntry =~ s/��a/g;
215 # $inEntry =~ s/�i/g;
216 # $inEntry =~ s/�o/g;
217 # $inEntry =~ s/|/u/g;
218 # $inEntry = uc($inEntry);
219 # $isOK=1 if $inEntry eq $hashentry;
221 # C4::Biblio::MARCaddsubfield($dbh,$bibid,$tag,'',$tagorder,9,$subfieldorder,$authid) if $isOK;
224 # if ($fieldnumber eq '712') {
225 # $sthBIBLIOS712->execute($authentry);
226 # while (my ($bibid,$tag,$tagorder,$subfieldorder) = $sthBIBLIOS712->fetchrow) {
227 # my $inbiblio = GetMarcBiblio({ biblionumber => $bibid });
228 # my $isOK = 0;
229 # foreach my $in7xx ($inbiblio->field($fieldnumber)) {
230 # # !!!!! ici, il faut reconstruire l'entr� de la table de hachage comme ci dessus
231 # # sinon,
232 # my $inEntry = $in7xx->subfield('a');
233 # $inEntry =~ s/���e/g;
234 # $inEntry =~ s/��a/g;
235 # $inEntry =~ s/�i/g;
236 # $inEntry =~ s/�o/g;
237 # $inEntry =~ s/|/u/g;
238 # $inEntry = uc($inEntry);
239 # $isOK=1 if $inEntry eq $hashentry;
241 # C4::Biblio::MARCaddsubfield($dbh,$bibid,$tag,'',$tagorder,9,$subfieldorder,$authid) if $isOK;
248 # $i++;
250 # my $timeneeded = gettimeofday - $starttime;
251 # print "$i entries done in $timeneeded seconds (".($i/$timeneeded)." per second)\n";