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