Bug 14544: Get rid of GetSomeShelfNames
[koha.git] / installer / install.pl
index 5fac1f6..cc67557 100755 (executable)
@@ -4,15 +4,18 @@ use strict;
 use warnings;
 use diagnostics;
 
-use InstallAuth;
-use CGI;
-use IPC::Cmd;
+use C4::InstallAuth;
+use CGI qw ( -utf8 );
+use POSIX qw(strftime);
 
 use C4::Context;
 use C4::Output;
+use C4::Templates;
 use C4::Languages qw(getAllLanguages getTranslatedLanguages);
 use C4::Installer;
 
+use Koha;
+
 my $query = new CGI;
 my $step  = $query->param('step');
 
@@ -22,11 +25,11 @@ my ( $template, $loggedinuser, $cookie );
 my $all_languages = getAllLanguages();
 
 if ( defined($language) ) {
-    setlanguagecookie( $query, $language, "install.pl?step=1" );
+    C4::Templates::setlanguagecookie( $query, $language, "install.pl?step=1" );
 }
 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     {
-        template_name => "installer/step" . ( $step ? $step : 1 ) . ".tmpl",
+        template_name => "installer/step" . ( $step ? $step : 1 ) . ".tt",
         query         => $query,
         type          => "intranet",
         authnotrequired => 0,
@@ -52,13 +55,14 @@ my $dbh = DBI->connect(
 );
 
 if ( $step && $step == 1 ) {
-    #First Step
+    #First Step (for both fresh installations and upgrades)
     #Checking ALL perl Modules and services needed are installed.
     #Whenever there is an error, adding a report to the page
+    my $op = $query->param('op') || 'noop';
     $template->param( language => 1 );
     $template->param( 'checkmodule' => 1 ); # we start with the assumption that there are no problems and set this to 0 if there are
 
-    unless ( $] >= 5.008008 ) {    # Bug 4505
+    unless ( $] >= 5.010000 ) {    # Bug 7375
         $template->param( problems => 1, perlversion => 1, checkmodule => 0 );
     }
 
@@ -83,7 +87,7 @@ if ( $step && $step == 1 ) {
             );
         }
         @components = sort {$a->{'name'} cmp $b->{'name'}} @components;
-        $template->param( missing_modules => \@components, checkmodule => $checkmodule );
+        $template->param( missing_modules => \@components, checkmodule => $checkmodule, op => $op );
     }
 }
 elsif ( $step && $step == 2 ) {
@@ -184,7 +188,7 @@ elsif ( $step && $step == 3 ) {
         # we have finished, just redirect to mainpage.
         #
         print $query->redirect("/cgi-bin/koha/mainpage.pl");
-        exit 1;
+        exit;
     }
     elsif ( $op && $op eq 'finish' ) {
         $installer->set_version_syspref();
@@ -195,10 +199,6 @@ elsif ( $step && $step == 3 ) {
         # The installer will have to relogin since we do not pass cookie to redirection.
         $template->param( "$op" => 1 );
     }
-    elsif ( $op && $op eq 'SetIndexingEngine' ) {
-        $installer->set_indexing_engine($query->param('NoZebra'));
-        $template->param( "$op" => 1 );
-    }
     elsif ( $op && $op eq 'addframeworks' ) {
     #
     # 1ST install, 3rd sub-step : insert the SQL files the user has selected
@@ -231,6 +231,7 @@ elsif ( $step && $step == 3 ) {
         # Framework Selection is achieved through checking boxes.
         my $langchoice = $query->param('fwklanguage');
         $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
+       $langchoice =~ s/[^a-zA-Z_-]*//g;
         my $marcflavour = $query->param('marcflavour');
         if ($marcflavour){
             $installer->set_marcflavour_syspref($marcflavour);
@@ -243,7 +244,7 @@ elsif ( $step && $step == 3 ) {
         $template->param( "frameworksloop" => $fwklist );
         $template->param( "marcflavour" => ucfirst($marcflavour));
 
-        my ($sample_defaulted_to_en, $levellist) = $installer->sample_data_sql_list($langchoice, $marcflavour);
+        my ($sample_defaulted_to_en, $levellist) = $installer->sample_data_sql_list($langchoice);
         $template->param( "en_sample_data" => $sample_defaulted_to_en);
         $template->param( "levelloop" => $levellist );
         $template->param( "$op"       => 1 );
@@ -268,6 +269,7 @@ elsif ( $step && $step == 3 ) {
         # Marcflavour Selection is achieved through radiobuttons.
         my $langchoice = $query->param('fwklanguage');
         $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
+       $langchoice =~ s/[^a-zA-Z_-]*//g;
         my $dir =
           C4::Context->config('intranetdir') . "/installer/data/$info{dbms}/$langchoice/marcflavour";
         unless (opendir( MYDIR, $dir )) {
@@ -312,25 +314,54 @@ elsif ( $step && $step == 3 ) {
         # Not 1st install, the only sub-step : update database
         #
         #Do updatedatabase And report
-        my $cmd = C4::Context->config("intranetdir") . "/installer/data/$info{dbms}/updatedatabase.pl";
-        my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) = IPC::Cmd::run(command => $cmd, verbose => 0);
 
-        if (@$stdout_buf) {
-            $template->param(update_report => [ map { { line => $_ } } split(/\n/, join('', @$stdout_buf)) ] );
-            $template->param(has_update_succeeds => 1);
+        if ( ! defined $ENV{PERL5LIB} ) {
+            my $find = "C4/Context.pm";
+            my $path = $INC{$find};
+            $path =~ s/\Q$find\E//;
+            $ENV{PERL5LIB} = "$path:$path/installer";
+            warn "# plack? inserted PERL5LIB $ENV{PERL5LIB}\n";
+        }
+
+        my $now = POSIX::strftime( "%Y-%m-%dT%H:%M:%S", localtime() );
+        my $logdir = C4::Context->config('logdir');
+        my $dbversion = C4::Context->preference('Version');
+        my $kohaversion = Koha::version;
+        $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
+
+        my $filename_suffix = join '_', $now, $dbversion, $kohaversion;
+        my ( $logfilepath, $logfilepath_errors ) = ( chk_log($logdir, "updatedatabase_$filename_suffix"), chk_log($logdir, "updatedatabase-error_$filename_suffix") );
+
+        my $cmd = C4::Context->config("intranetdir") . "/installer/data/$info{dbms}/updatedatabase.pl >> $logfilepath 2>> $logfilepath_errors";
+
+        system($cmd );
+
+        my $fh;
+        open( $fh, "<", $logfilepath ) or die "Cannot open log file $logfilepath: $!";
+        my @report = <$fh>;
+        close $fh;
+        if (@report) {
+            $template->param( update_report => [ map { { line => $_ } } split( /\n/, join( '', @report ) ) ] );
+            $template->param( has_update_succeeds => 1 );
+        } else {
+            eval{ `rm $logfilepath` };
         }
-        if (@$stderr_buf) {
-            $template->param(update_errors => [ map { { line => $_ } } split(/\n/, join('', @$stderr_buf)) ] );
-            $template->param(has_update_errors => 1);
+        open( $fh, "<", $logfilepath_errors ) or die "Cannot open log file $logfilepath_errors: $!";
+        @report = <$fh>;
+        close $fh;
+        if (@report) {
+            $template->param( update_errors => [ map { { line => $_ } } split( /\n/, join( '', @report ) ) ] );
+            $template->param( has_update_errors => 1 );
             warn "The following errors were returned while attempting to run the updatedatabase.pl script:\n";
-            foreach my $line (@$stderr_buf) {warn "$line\n";}
+            foreach my $line (@report) { warn "$line\n"; }
+        } else {
+            eval{ `rm $logfilepath_errors` };
         }
-
         $template->param( $op => 1 );
     }
     else {
         #
-        # check wether it's a 1st install or an update
+        # check whether it's a 1st install or an update
         #
         #Check if there are enough tables.
         # Paul has cleaned up tables so reduced the count
@@ -370,12 +401,10 @@ elsif ( $step && $step == 3 ) {
                 $dbversion = "$1.$2.$3.$4";
                 $template->param("upgrading" => 1,
                                 "dbversion" => $dbversion,
-                                "kohaversion" => C4::Context->KOHAVERSION,
+                                "kohaversion" => Koha::version(),
                                 );
             }
         }
-
-        $dbh->disconnect;
     }
 }
 else {
@@ -391,10 +420,22 @@ else {
         if ( $rq->execute ) {
             my ($version) = $rq->fetchrow;
             if ($version) {
-                $query->redirect("install.pl?step=3");
-                               exit;
+                print $query->redirect("/cgi-bin/koha/installer/install.pl?step=3");
+                exit;
             }
         }
     }
 }
 output_html_with_http_headers $query, $cookie, $template->output;
+
+sub chk_log { #returns a logfile in $dir or - if that failed - in temp dir
+    my ($dir, $name) = @_;
+    my $fn=$dir.'/'.$name.'.log';
+    if( ! open my $fh, '>', $fn ) {
+        $name.= '_XXXX';
+        require File::Temp;
+        ($fh, $fn)= File::Temp::tempfile( $name, TMPDIR => 1, SUFFIX => '.log');
+        #if this should not work, let croak take over
+    }
+    return $fn;
+}