3 # This file is part of Koha.
5 # Copyright 2016 Koha Development Team
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.
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.
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>.
31 populate_db.pl - Load included sample data into the DB
35 populate_db.pl [--marcflavour MARCFLAVOUR]
38 --help Brief help message
39 --marcflavour m Specify the MARC flavour to use (MARC21|UNIMARC). Defaults
49 Prints a brief help message and exits.
51 =item B<--marcflavour>
53 Lets you choose the desired MARC flavour for the sample data. Valid options are MARC21 and UNIMARC.
54 It defaults to MARC21.
58 Make the output more verbose.
66 my $marcflavour = 'MARC21';
70 'verbose' => \
$verbose,
71 'marcflavour=s' => \
$marcflavour
78 if ( $marcflavour ne 'MARC21'
79 and $marcflavour ne 'UNIMARC' ) {
80 say "Invalid MARC flavour '$marcflavour' passed.";
84 $ENV{KOHA_DB_DO_NOT_RAISE_OR_PRINT_ERROR
} = 1;
85 my $dbh = C4
::Context
->dbh; # At the beginning to die if DB does not exist.
87 my ( $prefs_count ) = $dbh->selectrow_array(q
|SELECT COUNT
(*) FROM systempreferences
|);
88 my ( $patrons_count ) = $dbh->selectrow_array(q
|SELECT COUNT
(*) FROM borrowers
|);
89 if ( $prefs_count or $patrons_count ) {
90 die "Database is not empty!";
93 $ENV{KOHA_DB_DO_NOT_RAISE_OR_PRINT_ERROR
} = 0;
95 our $root = C4
::Context
->config('intranetdir');
96 our $data_dir = "$root/installer/data/mysql";
97 our $installer = C4
::Installer
->new;
99 my $koha_structure_file = "$data_dir/kohastructure.sql";
100 my @sample_files_mandatory = (
101 glob("$data_dir/mandatory/*.sql"),
102 "$data_dir/audio_alerts.sql",
103 "$data_dir/sysprefs.sql",
104 "$data_dir/userflags.sql",
105 "$data_dir/userpermissions.sql",
107 my @sample_lang_files_mandatory = ( glob $root . "/installer/data/mysql/$lang/mandatory/*.sql" );
108 my @sample_lang_files_optional = ( glob $root . "/installer/data/mysql/$lang/optional/*.sql" );
109 my @marc21_sample_files_mandatory = ( glob $root . "/installer/data/mysql/$lang/marcflavour/marc21/*/*.sql" );
110 my @unimarc_sample_files_mandatory = ( glob $root . "/installer/data/mysql/$lang/marcflavour/unimarc/*/*.sql" );
112 my $version = get_version
();
117 sub initialize_data
{
118 say "Inserting koha db structure..."
120 my $error = $installer->load_db_schema;
121 die $error if $error;
123 for my $f (@sample_files_mandatory) {
127 for my $f (@sample_lang_files_mandatory) {
131 for my $f (@sample_lang_files_optional) {
135 if ( $marcflavour eq 'UNIMARC' ) {
136 for my $f (@unimarc_sample_files_mandatory) {
140 for my $f (@marc21_sample_files_mandatory) {
145 # set marcflavour (MARC21)
146 my $dbh = C4
::Context
->dbh;
148 say "Setting the MARC flavour on the sysprefs..."
151 INSERT INTO
`systempreferences` (variable
,value
,explanation
,options
,type
)
152 VALUES
('marcflavour',?
,'Define global MARC flavor (MARC21 or UNIMARC) used for character encoding','MARC21|UNIMARC','Choice')
153 },undef,$marcflavour);
156 say "Setting Koha version to $version..."
159 INSERT INTO systempreferences
(variable
, value
, options
, explanation
, type
)
160 VALUES
('Version', '$version', NULL
, 'The Koha database version. WARNING: Do not change this value manually, it is maintained by the webinstaller', NULL
)
164 sub execute_sqlfile
{
166 say "Inserting $filepath..."
168 my $error = $installer->load_sql($filepath);
169 die $error if $error;
173 do $root . '/kohaversion.pl';
174 my $version = kohaversion
();
175 $version =~ s/(\d)\.(\d{2})\.(\d{2})\.(\d{3})/$1.$2$3$4/;
179 sub update_database
{
180 my $update_db_path = $root . '/installer/data/mysql/updatedatabase.pl';
181 say "Updating database..."
183 my $file = `cat $update_db_path`;
187 die "updatedatabase.pl process failed: $@";
189 say "updatedatabase.pl process succeeded.";