Merge remote-tracking branch 'kc/new/bug_5449' into kcmaster
[koha.git] / misc / migration_tools / buildCOUNTRY.pl
1 #!/usr/bin/perl
2 # script that rebuild thesaurus from biblio table.
3
4 # delete  FROM  `marc_subfield_table`  WHERE tag =  "606" AND subfieldcode = 9;
5 use strict;
6 #use warnings; FIXME - Bug 2505
7
8 # Koha modules used
9 use C4::Context;
10 use C4::Biblio;
11 use C4::AuthoritiesMarc;
12 use Time::HiRes qw(gettimeofday);
13
14 use Getopt::Long;
15 my ( $fields, $number,$language) = ('',0);
16 my ($version, $verbose, $test_parameter, $field,$delete,$subfields);
17 GetOptions(
18     'h' => \$version,
19     'd' => \$delete,
20     't' => \$test_parameter,
21     's:s' => \$fields,
22     'v' => \$verbose,
23         'l:s' => \$language,
24 );
25
26 if ($version or !$fields) {
27         print <<EOF
28 Small script to recreate the COUNTRY list in authorised values from existing countries in the catalogue.
29 This script is useful when you migrate your datas with bulkmarcimport.pl as it populates parameters tables that are not modified by bulkmarcimport.
30
31 parameters :
32 \th : this version/help screen
33 \ts : the field or field list where the lang codes are stored.
34 \td : delete every entry of COUNTRY category before doing work.
35 \tl : the language of the language list (fr or en for instance)
36
37 The table is populated with iso codes and meaning (in french).
38 If the complete language name is unknown, the code is used instead and you will be warned by the script
39
40 SAMPLES :
41  ./buildCOUNTRY.pl -d -s "('102a')"
42 EOF
43 ;#
44 exit;
45 }
46
47 my %codesiso;
48
49 %codesiso = (
50         'eng' => 'english',
51         'fre' => 'french'
52         );
53
54 %codesiso = (
55         'an' => 'Antilles Néerlandaises',
56         'at' => 'Autriche',
57         'cr' => 'Costa Rica',
58         'er' => 'Erythrée',
59         'fr' => ' France',
60         'in' => 'Inde',
61         'is' => 'Islande',
62         'lt' => 'Lituanie',
63         'nd' => 'Pays Bas',
64         'nf' => 'Norfolk',
65         'ng' => 'Nigéria',
66         'pa' => 'Manama',
67         'pn' => 'Pitcairn',
68         're' => 'Réunion (ile)',
69         'sp' => 'Espagne',
70         'us' => 'Etats Unis',
71         ) if $language eq 'fr';
72
73 my $dbh = C4::Context->dbh;
74 if ($delete) {
75         print "deleting country list\n";
76         $dbh->do("delete from authorised_values where category='COUNTRY'");
77 }
78
79 if ($test_parameter) {
80         print "TESTING MODE ONLY\n    DOING NOTHING\n===============\n";
81 }
82 my $starttime = gettimeofday;
83
84 my $sth = $dbh->prepare("SELECT count(*) as tot,subfieldvalue FROM marc_subfield_table WHERE tag + subfieldcode IN $fields group by subfieldvalue");
85
86 $sth->execute;
87 my $i=1;
88
89 print "=========================\n";
90 my $sth2 = $dbh->prepare("insert into authorised_values (category, authorised_value, lib) values (?,?,?)");
91 while (my ($tot,$langue) = $sth->fetchrow) {
92         $sth2->execute('COUNTRY',$langue,$langue?$codesiso{$langue}:$langue);
93         print "$langue is unknown is iso list (used $tot times)\n" unless $codesiso{$langue};
94 }
95 print "=========================\n";