9 use POSIX
qw(strftime);
14 use C4
::Languages
qw(getAllLanguages getTranslatedLanguages);
20 my $step = $query->param('step');
22 my $language = $query->param('language');
23 my ( $template, $loggedinuser, $cookie );
25 my $all_languages = getAllLanguages
();
27 if ( defined($language) ) {
28 C4
::Templates
::setlanguagecookie
( $query, $language, "install.pl?step=1" );
30 ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
32 template_name
=> "installer/step" . ( $step ?
$step : 1 ) . ".tt",
40 my $installer = C4
::Installer
->new();
42 $info{'dbname'} = C4
::Context
->config("database");
44 ( C4
::Context
->config("db_scheme")
45 ? C4
::Context
->config("db_scheme")
47 $info{'hostname'} = C4
::Context
->config("hostname");
48 $info{'port'} = C4
::Context
->config("port");
49 $info{'user'} = C4
::Context
->config("user");
50 $info{'password'} = C4
::Context
->config("pass");
51 $info{'tls'} = C4
::Context
->config("tls");
52 if ($info{'tls'} && $info{'tls'} eq 'yes'){
53 $info{'ca'} = C4
::Context
->config('ca');
54 $info{'cert'} = C4
::Context
->config('cert');
55 $info{'key'} = C4
::Context
->config('key');
56 $info{'tlsoptions'} = ";mysql_ssl=1;mysql_ssl_client_key=".$info{key
}.";mysql_ssl_client_cert=".$info{cert
}.";mysql_ssl_ca_file=".$info{ca
};
57 $info{'tlscmdline'} = " --ssl-cert ". $info{cert
} . " --ssl-key " . $info{key
} . " --ssl-ca ".$info{ca
}." "
60 my $dbh = DBI
->connect(
61 "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
62 . ( $info{port
} ?
";port=$info{port}" : "" )
63 . ( $info{tlsoptions
} ?
$info{tlsoptions
} : "" ),
64 $info{'user'}, $info{'password'}
67 if ( $step && $step == 1 ) {
68 #First Step (for both fresh installations and upgrades)
69 #Checking ALL perl Modules and services needed are installed.
70 #Whenever there is an error, adding a report to the page
71 my $op = $query->param('op') || 'noop';
72 $template->param( language
=> 1 );
73 $template->param( 'checkmodule' => 1 ); # we start with the assumption that there are no problems and set this to 0 if there are
75 unless ( $] >= 5.010000 ) { # Bug 7375
76 $template->param( problems
=> 1, perlversion
=> 1, checkmodule
=> 0 );
79 my $perl_modules = C4
::Installer
::PerlModules
->new;
80 $perl_modules->versions_info;
82 my $modules = $perl_modules->get_attr('missing_pm');
83 if (scalar(@
$modules)) {
87 my ($module, $stats) = each %$_;
88 $checkmodule = 0 if $stats->{'required'};
93 version
=> $stats->{'min_ver'},
94 require => $stats->{'required'},
95 usage
=> $stats->{'usage'},
99 @components = sort {$a->{'name'} cmp $b->{'name'}} @components;
100 $template->param( missing_modules
=> \
@components, checkmodule
=> $checkmodule, op
=> $op );
103 elsif ( $step && $step == 2 ) {
105 #STEP 2 Check Database connection and access
107 $template->param(%info);
108 my $checkdb = $query->param("checkdb");
109 $template->param( 'dbconnection' => $checkdb );
113 # Can connect to the mysql
114 $template->param( "checkdatabaseaccess" => 1 );
115 if ( $info{dbms
} eq "mysql" ) {
117 #Check if database created
118 my $rv = $dbh->do("SHOW DATABASES LIKE \'$info{dbname}\'");
120 $template->param( 'checkdatabasecreated' => 1 );
123 #Check if user have all necessary grants on this database.
126 "SHOW GRANTS FOR \'$info{user}\'\@'$info{hostname}'");
129 while ( my ($line) = $rq->fetchrow ) {
130 my $dbname = $info{dbname
};
131 if ( $line =~ m/^GRANT (.*?) ON `$dbname`\.\*/ || index( $line, '*.*' ) > 0 ) {
134 index( $line, 'ALL PRIVILEGES' ) > 0
135 || ( ( index( $line, 'SELECT' ) > 0 )
136 && ( index( $line, 'INSERT' ) > 0 )
137 && ( index( $line, 'UPDATE' ) > 0 )
138 && ( index( $line, 'DELETE' ) > 0 )
139 && ( index( $line, 'CREATE' ) > 0 )
140 && ( index( $line, 'DROP' ) > 0 ) )
144 unless ($grantaccess) {
146 $dbh->prepare("SHOW GRANTS FOR \'$info{user}\'\@'\%'");
148 while ( my ($line) = $rq->fetchrow ) {
149 my $dbname = $info{dbname
};
150 if ( $line =~ m/$dbname/ || index( $line, '*.*' ) > 0 )
154 index( $line, 'ALL PRIVILEGES' ) > 0
155 || ( ( index( $line, 'SELECT' ) > 0 )
156 && ( index( $line, 'INSERT' ) > 0 )
157 && ( index( $line, 'UPDATE' ) > 0 )
158 && ( index( $line, 'DELETE' ) > 0 )
159 && ( index( $line, 'CREATE' ) > 0 )
160 && ( index( $line, 'DROP' ) > 0 ) )
165 $template->param( "checkgrantaccess" => $grantaccess );
166 } # End mysql connect check...
168 elsif ( $info{dbms
} eq "Pg" ) {
169 # Check if database has been created...
170 my $rv = $dbh->do( "SELECT * FROM pg_catalog.pg_database WHERE datname = \'$info{dbname}\';" );
172 $template->param( 'checkdatabasecreated' => 1 );
175 # Check if user has all necessary grants on this database...
176 my $rq = $dbh->do( "SELECT u.usesuper
177 FROM pg_catalog.pg_user as u
178 WHERE u.usename = \'$info{user}\';" );
180 $template->param( "checkgrantaccess" => 1 );
182 } # End Pg connect check...
185 $template->param( "error" => DBI
::err
, "message" => DBI
::errstr
);
189 elsif ( $step && $step == 3 ) {
192 # STEP 3 : database setup
195 my $op = $query->param('op');
196 if ( $op && $op eq 'finished' ) {
198 # we have finished, just redirect to mainpage.
200 print $query->redirect("/cgi-bin/koha/mainpage.pl");
203 elsif ( $op && $op eq 'finish' ) {
204 $installer->set_version_syspref();
206 # Installation is finished.
207 # We just deny anybody access to install
208 # And we redirect people to mainpage.
209 # The installer will have to relogin since we do not pass cookie to redirection.
210 $template->param( "$op" => 1 );
212 elsif ( $op && $op eq 'addframeworks' ) {
214 # 1ST install, 3rd sub-step : insert the SQL files the user has selected
217 my ($fwk_language, $list) = $installer->load_sql_in_order($all_languages, $query->param('framework'));
219 "fwklanguage" => $fwk_language,
222 use Koha
::SearchEngine
::Elasticsearch
;
223 Koha
::SearchEngine
::Elasticsearch
->reset_elasticsearch_mappings;
224 $template->param( "$op" => 1 );
226 elsif ( $op && $op eq 'selectframeworks' ) {
229 # 1ST install, 2nd sub-step : show the user the sql datas he can insert in the database.
232 # (note that the term "selectframeworks is not correct. The user can select various files, not only frameworks)
235 #sql data for import are supposed to be located in installer/data/<language>/<level>
236 # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
237 # Where <level> is a category of requirement : required, recommended optional
238 # level should contain :
239 # SQL File for import With a readable name.
240 # txt File that explains what this SQL File is meant for.
241 # Could be VERY useful to have A Big file for a kind of library.
242 # But could also be useful to have some Authorised values data set prepared here.
243 # Framework Selection is achieved through checking boxes.
244 my $langchoice = $query->param('fwklanguage');
245 $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
246 $langchoice =~ s/[^a-zA-Z_-]*//g;
247 my $marcflavour = $query->param('marcflavour');
249 $installer->set_marcflavour_syspref($marcflavour);
251 $marcflavour = C4
::Context
->preference('marcflavour') unless ($marcflavour);
252 #Insert into database the selected marcflavour
254 my ($marc_defaulted_to_en, $fwklist) = $installer->marc_framework_sql_list($langchoice, $marcflavour);
255 $template->param('en_marc_frameworks' => $marc_defaulted_to_en);
256 $template->param( "frameworksloop" => $fwklist );
257 $template->param( "marcflavour" => ucfirst($marcflavour));
259 my ($sample_defaulted_to_en, $levellist) = $installer->sample_data_sql_list($langchoice);
260 $template->param( "en_sample_data" => $sample_defaulted_to_en);
261 $template->param( "levelloop" => $levellist );
262 $template->param( "$op" => 1 );
264 elsif ( $op && $op eq 'choosemarc' ) {
267 # 1ST install, 2nd sub-step : show the user the marcflavour available.
272 #sql data are supposed to be located in installer/data/<dbms>/<language>/marcflavour/marcflavourname
273 # Where <dbms> is database type according to DBD syntax
274 # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
275 # Where <level> is a category of requirement : required, recommended optional
276 # level should contain :
277 # SQL File for import With a readable name.
278 # txt File taht explains what this SQL File is meant for.
279 # Could be VERY useful to have A Big file for a kind of library.
280 # But could also be useful to have some Authorised values data set prepared here.
281 # Marcflavour Selection is achieved through radiobuttons.
282 my $langchoice = $query->param('fwklanguage');
283 $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
284 $langchoice =~ s/[^a-zA-Z_-]*//g;
286 C4
::Context
->config('intranetdir') . "/installer/data/$info{dbms}/$langchoice/marcflavour";
287 unless (opendir( MYDIR
, $dir )) {
288 if ($langchoice eq 'en') {
289 warn "cannot open MARC frameworks directory $dir";
291 # if no translated MARC framework is available,
293 $dir = C4
::Context
->config('intranetdir') . "/installer/data/$info{dbms}/en/marcflavour";
294 opendir(MYDIR
, $dir) or warn "cannot open English MARC frameworks directory $dir";
297 my @listdir = grep { !/^\./ && -d
"$dir/$_" } readdir(MYDIR
);
299 my $marcflavour=C4
::Context
->preference("marcflavour");
301 foreach my $marc (@listdir) {
303 "label"=> ucfirst($marc),
305 "checked"=> defined($marcflavour) ?
uc($marc) eq $marcflavour : 0);
306 # $cell{"description"}= do { local $/ = undef; open INPUT "<$dir/$marc.txt"||"";<INPUT> };
307 push @flavourlist, \
%cell;
309 $template->param( "flavourloop" => \
@flavourlist );
310 $template->param( "$op" => 1 );
312 elsif ( $op && $op eq 'importdatastructure' ) {
315 # 1st install, 1st "sub-step" : import kohastructure
318 my $error = $installer->load_db_schema();
324 elsif ( $op && $op eq 'updatestructure' ) {
326 # Not 1st install, the only sub-step : update database
328 #Do updatedatabase And report
330 if ( ! defined $ENV{PERL5LIB
} ) {
331 my $find = "C4/Context.pm";
332 my $path = $INC{$find};
333 $path =~ s/\Q$find\E//;
334 $ENV{PERL5LIB
} = "$path:$path/installer";
335 warn "# plack? inserted PERL5LIB $ENV{PERL5LIB}\n";
338 my $now = POSIX
::strftime
( "%Y-%m-%dT%H:%M:%S", localtime() );
339 my $logdir = C4
::Context
->config('logdir');
340 my $dbversion = C4
::Context
->preference('Version');
341 my $kohaversion = Koha
::version
;
342 $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
344 my $filename_suffix = join '_', $now, $dbversion, $kohaversion;
345 my ( $logfilepath, $logfilepath_errors ) = ( chk_log
($logdir, "updatedatabase_$filename_suffix"), chk_log
($logdir, "updatedatabase-error_$filename_suffix") );
347 my $cmd = C4
::Context
->config("intranetdir") . "/installer/data/$info{dbms}/updatedatabase.pl >> $logfilepath 2>> $logfilepath_errors";
352 open( $fh, "<", $logfilepath ) or die "Cannot open log file $logfilepath: $!";
356 $template->param( update_report
=> [ map { { line
=> $_ } } split( /\n/, join( '', @report ) ) ] );
357 $template->param( has_update_succeeds
=> 1 );
359 eval{ `rm $logfilepath` };
361 open( $fh, "<", $logfilepath_errors ) or die "Cannot open log file $logfilepath_errors: $!";
365 $template->param( update_errors
=> [ map { { line
=> $_ } } split( /\n/, join( '', @report ) ) ] );
366 $template->param( has_update_errors
=> 1 );
367 warn "The following errors were returned while attempting to run the updatedatabase.pl script:\n";
368 foreach my $line (@report) { warn "$line\n"; }
370 eval{ `rm $logfilepath_errors` };
372 $template->param( $op => 1 );
376 # check whether it's a 1st install or an update
378 #Check if there are enough tables.
379 # Paul has cleaned up tables so reduced the count
380 #I put it there because it implied a data import if condition was not satisfied.
381 my $dbh = DBI
->connect(
382 "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
383 . ( $info{port
} ?
";port=$info{port}" : "" )
384 . ( $info{tlsoptions
} ?
$info{tlsoptions
} : "" ),
385 $info{'user'}, $info{'password'}
388 if ( $info{dbms
} eq 'mysql' ) { $rq = $dbh->prepare( "SHOW TABLES" ); }
389 elsif ( $info{dbms
} eq 'Pg' ) { $rq = $dbh->prepare( "SELECT *
390 FROM information_schema.tables
391 WHERE table_schema='public' and table_type='BASE TABLE';" ); }
393 my $data = $rq->fetchall_arrayref( {} );
394 my $count = scalar(@
$data);
396 # we don't have tables, propose DB import
399 $template->param( "count" => $count, "proposeimport" => 1 );
403 # we have tables, propose to select files to upload or updatedatabase
405 $template->param( "count" => $count, "default" => 1 );
407 # 1st part of step 3 : check if there is a databaseversion systempreference
408 # if there is, then we just need to upgrade
409 # if there is none, then we need to install the database
411 if (C4
::Context
->preference('Version')) {
412 my $dbversion = C4
::Context
->preference('Version');
413 $dbversion =~ /(.*)\.(..)(..)(...)/;
414 $dbversion = "$1.$2.$3.$4";
415 $template->param("upgrading" => 1,
416 "dbversion" => $dbversion,
417 "kohaversion" => Koha
::version
(),
425 # LANGUAGE SELECTION page by default
426 # using opendir + language Hash
427 my $languages_loop = getTranslatedLanguages
('intranet');
428 $template->param( installer_languages_loop
=> $languages_loop );
432 "SELECT * from systempreferences WHERE variable='Version'");
433 if ( $rq->execute ) {
434 my ($version) = $rq->fetchrow;
436 print $query->redirect("/cgi-bin/koha/installer/install.pl?step=3");
442 output_html_with_http_headers
$query, $cookie, $template->output;
444 sub chk_log
{ #returns a logfile in $dir or - if that failed - in temp dir
445 my ($dir, $name) = @_;
446 my $fn=$dir.'/'.$name.'.log';
447 if( ! open my $fh, '>', $fn ) {
450 ($fh, $fn)= File
::Temp
::tempfile
( $name, TMPDIR
=> 1, SUFFIX
=> '.log');
451 #if this should not work, let croak take over