Bug 10337: (followup) Upper case MARC flavour
[koha.git] / misc / devel / populate_db.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2016 Koha Development Team
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Getopt::Long;
23 use Pod::Usage;
24
25 use C4::Installer;
26 use C4::Context;
27 use t::lib::Mocks;
28
29 =head1 NAME
30
31 populate_db.pl - Load included sample data into the DB
32
33 =head1 SYNOPSIS
34
35 populate_db.pl [--marcflavour MARCFLAVOUR]
36
37  Options:
38    --help            Brief help message
39    --marcflavour m   Specify the MARC flavour to use (MARC21|UNIMARC). Defaults
40                                 to MARC21.
41    -v                Be verbose.
42
43 =head1 OPTIONS
44
45 =over 8
46
47 =item B<--help>
48
49 Prints a brief help message and exits.
50
51 =item B<--marcflavour>
52
53 Lets you choose the desired MARC flavour for the sample data. Valid options are MARC21 and UNIMARC.
54 It defaults to MARC21.
55
56 =item B<--verbose>
57
58 Make the output more verbose.
59
60 =back
61
62 =cut
63
64 my $help;
65 my $verbose;
66 my $marcflavour = 'MARC21';
67
68 GetOptions(
69     'help|?'        => \$help,
70     'verbose'       => \$verbose,
71     'marcflavour=s' => \$marcflavour
72 ) or pod2usage;
73
74 if ( $help ) {
75     pod2usage;
76 }
77
78 $marcflavour = uc($marcflavour);
79
80 if (     $marcflavour ne 'MARC21'
81      and $marcflavour ne 'UNIMARC' ) {
82     say "Invalid MARC flavour '$marcflavour' passed.";
83     pod2usage;
84 }
85
86 $ENV{KOHA_DB_DO_NOT_RAISE_OR_PRINT_ERROR} = 1;
87 my $dbh = C4::Context->dbh; # At the beginning to die if DB does not exist.
88
89 my ( $prefs_count ) = $dbh->selectrow_array(q|SELECT COUNT(*) FROM systempreferences|);
90 my ( $patrons_count ) = $dbh->selectrow_array(q|SELECT COUNT(*) FROM borrowers|);
91 if ( $prefs_count or $patrons_count ) {
92     die "Database is not empty!";
93 }
94 $dbh->disconnect;
95 $ENV{KOHA_DB_DO_NOT_RAISE_OR_PRINT_ERROR} = 0;
96
97 our $root      = C4::Context->config('intranetdir');
98 our $data_dir  = "$root/installer/data/mysql";
99 our $installer = C4::Installer->new;
100 my $lang                = 'en';
101 my $koha_structure_file = "$data_dir/kohastructure.sql";
102 my @sample_files_mandatory = (
103     glob("$data_dir/mandatory/*.sql"),
104     "$data_dir/audio_alerts.sql",
105     "$data_dir/sysprefs.sql",
106     "$data_dir/userflags.sql",
107     "$data_dir/userpermissions.sql",
108 );
109 my @sample_lang_files_mandatory    = ( glob $root . "/installer/data/mysql/$lang/mandatory/*.sql" );
110 my @sample_lang_files_optional     = ( glob $root . "/installer/data/mysql/$lang/optional/*.sql" );
111 my @marc21_sample_files_mandatory  = ( glob $root . "/installer/data/mysql/$lang/marcflavour/marc21/*/*.sql" );
112 my @unimarc_sample_files_mandatory = ( glob $root . "/installer/data/mysql/$lang/marcflavour/unimarc/*/*.sql" );
113
114 my $version = get_version();
115
116 initialize_data();
117 update_database();
118
119 sub initialize_data {
120     say "Inserting koha db structure..."
121         if $verbose;
122     my $error = $installer->load_db_schema;
123     die $error if $error;
124
125     for my $f (@sample_files_mandatory) {
126         execute_sqlfile($f);
127     }
128
129     for my $f (@sample_lang_files_mandatory) {
130         execute_sqlfile($f);
131     }
132
133     for my $f (@sample_lang_files_optional) {
134         execute_sqlfile($f);
135     }
136
137     if ( $marcflavour eq 'UNIMARC' ) {
138         for my $f (@unimarc_sample_files_mandatory) {
139             execute_sqlfile($f);
140         }
141     } else {
142         for my $f (@marc21_sample_files_mandatory) {
143             execute_sqlfile($f);
144         }
145     }
146
147     # set marcflavour (MARC21)
148     my $dbh = C4::Context->dbh;
149
150     say "Setting the MARC flavour on the sysprefs..."
151         if $verbose;
152     $dbh->do(qq{
153         INSERT INTO `systempreferences` (variable,value,explanation,options,type)
154         VALUES ('marcflavour',?,'Define global MARC flavor (MARC21 or UNIMARC) used for character encoding','MARC21|UNIMARC','Choice')
155     },undef,$marcflavour);
156
157     # set version
158     say "Setting Koha version to $version..."
159         if $verbose;
160     $dbh->do(qq{
161         INSERT INTO systempreferences(variable, value, options, explanation, type)
162         VALUES ('Version', '$version', NULL, 'The Koha database version. WARNING: Do not change this value manually, it is maintained by the webinstaller', NULL)
163     });
164 }
165
166 sub execute_sqlfile {
167     my ($filepath) = @_;
168     say "Inserting $filepath..."
169         if $verbose;
170     my $error = $installer->load_sql($filepath);
171     die $error if $error;
172 }
173
174 sub get_version {
175     do $root . '/kohaversion.pl';
176     my $version = kohaversion();
177     $version =~ s/(\d)\.(\d{2})\.(\d{2})\.(\d{3})/$1.$2$3$4/;
178     return $version;
179 }
180
181 sub update_database {
182     my $update_db_path = $root . '/installer/data/mysql/updatedatabase.pl';
183     say "Updating database..."
184         if $verbose;
185     my $file = `cat $update_db_path`;
186     $file =~ s/exit;//;
187     eval $file;
188     if ($@) {
189         die "updatedatabase.pl process failed: $@";
190     } else {
191         say "updatedatabase.pl process succeeded.";
192     }
193 }