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