Bug 19916: Add keyword and standard ID fields to acq external search
[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
28 use Koha::SearchEngine::Elasticsearch;
29
30 =head1 NAME
31
32 populate_db.pl - Load included sample data into the DB
33
34 =head1 SYNOPSIS
35
36 populate_db.pl [--marcflavour MARCFLAVOUR]
37
38  Options:
39    --help            Brief help message
40    --marcflavour m   Specify the MARC flavour to use (MARC21|UNIMARC). Defaults
41                                 to MARC21.
42    -v                Be verbose.
43
44 =head1 OPTIONS
45
46 =over 8
47
48 =item B<--help>
49
50 Prints a brief help message and exits.
51
52 =item B<--marcflavour>
53
54 Lets you choose the desired MARC flavour for the sample data. Valid options are MARC21 and UNIMARC.
55 It defaults to MARC21.
56
57 =item B<--verbose>
58
59 Make the output more verbose.
60
61 =back
62
63 =cut
64
65 my $help;
66 my $verbose;
67 my $marcflavour = 'MARC21';
68
69 GetOptions(
70     'help|?'        => \$help,
71     'verbose'       => \$verbose,
72     'marcflavour=s' => \$marcflavour
73 ) or pod2usage;
74
75 if ( $help ) {
76     pod2usage;
77 }
78
79 $marcflavour = uc($marcflavour);
80
81 if (     $marcflavour ne 'MARC21'
82      and $marcflavour ne 'UNIMARC' ) {
83     say "Invalid MARC flavour '$marcflavour' passed.";
84     pod2usage;
85 }
86
87 $ENV{KOHA_DB_DO_NOT_RAISE_OR_PRINT_ERROR} = 1;
88 my $dbh = C4::Context->dbh; # At the beginning to die if DB does not exist.
89
90 my ( $prefs_count ) = $dbh->selectrow_array(q|SELECT COUNT(*) FROM systempreferences|);
91 my ( $patrons_count ) = $dbh->selectrow_array(q|SELECT COUNT(*) FROM borrowers|);
92 if ( $prefs_count or $patrons_count ) {
93     die "Database is not empty!";
94 }
95 $dbh->disconnect;
96 $ENV{KOHA_DB_DO_NOT_RAISE_OR_PRINT_ERROR} = 0;
97
98 our $root      = C4::Context->config('intranetdir');
99 our $data_dir  = "$root/installer/data/mysql";
100 our $installer = C4::Installer->new;
101 my $lang                = 'en';
102 my $koha_structure_file = "$data_dir/kohastructure.sql";
103 my @sample_files_mandatory = (
104     glob("$data_dir/mandatory/*.sql"),
105     "$data_dir/audio_alerts.sql",
106     "$data_dir/sysprefs.sql",
107     "$data_dir/userflags.sql",
108     "$data_dir/userpermissions.sql",
109     "$data_dir/account_offset_types.sql",
110 );
111 my @sample_lang_files_mandatory    = ( glob $root . "/installer/data/mysql/$lang/mandatory/*.sql" );
112 my @sample_lang_files_optional     = ( glob $root . "/installer/data/mysql/$lang/optional/*.sql" );
113 my @marc21_sample_files_mandatory  = ( glob $root . "/installer/data/mysql/$lang/marcflavour/marc21/*/*.sql" );
114 my @unimarc_sample_files_mandatory = ( glob $root . "/installer/data/mysql/$lang/marcflavour/unimarc/*/*.sql" );
115
116 my $version = get_version();
117
118 initialize_data();
119 update_database();
120
121 sub initialize_data {
122     say "Inserting koha db structure..."
123         if $verbose;
124     my $error = $installer->load_db_schema;
125     die $error if $error;
126
127     for my $f (@sample_files_mandatory) {
128         execute_sqlfile($f);
129     }
130
131     for my $f (@sample_lang_files_mandatory) {
132         execute_sqlfile($f);
133     }
134
135     for my $f (@sample_lang_files_optional) {
136         execute_sqlfile($f);
137     }
138
139     if ( $marcflavour eq 'UNIMARC' ) {
140         for my $f (@unimarc_sample_files_mandatory) {
141             execute_sqlfile($f);
142         }
143     } else {
144         for my $f (@marc21_sample_files_mandatory) {
145             execute_sqlfile($f);
146         }
147     }
148
149     # set marcflavour (MARC21)
150     my $dbh = C4::Context->dbh;
151
152     say "Setting the MARC flavour on the sysprefs..."
153         if $verbose;
154     $dbh->do(qq{
155         INSERT INTO `systempreferences` (variable,value,explanation,options,type)
156         VALUES ('marcflavour',?,'Define global MARC flavor (MARC21 or UNIMARC) used for character encoding','MARC21|UNIMARC','Choice')
157     },undef,$marcflavour);
158
159     # set version
160     say "Setting Koha version to $version..."
161         if $verbose;
162     $dbh->do(qq{
163         INSERT INTO systempreferences(variable, value, options, explanation, type)
164         VALUES ('Version', '$version', NULL, 'The Koha database version. WARNING: Do not change this value manually, it is maintained by the webinstaller', NULL)
165     });
166
167     # Initialize ES mappings
168     Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
169 }
170
171 sub execute_sqlfile {
172     my ($filepath) = @_;
173     say "Inserting $filepath..."
174         if $verbose;
175     my $error = $installer->load_sql($filepath);
176     die $error if $error;
177 }
178
179 sub get_version {
180     do $root . '/kohaversion.pl';
181     my $version = kohaversion();
182     $version =~ s/(\d)\.(\d{2})\.(\d{2})\.(\d{3})/$1.$2$3$4/;
183     return $version;
184 }
185
186 sub update_database {
187     my $update_db_path = $root . '/installer/data/mysql/updatedatabase.pl';
188     say "Updating database..."
189         if $verbose;
190     my $file = `cat $update_db_path`;
191     $file =~ s/exit;//;
192     eval $file;
193     if ($@) {
194         die "updatedatabase.pl process failed: $@";
195     } else {
196         say "updatedatabase.pl process succeeded.";
197     }
198 }