Bug 32336: (QA follow-up) Use $metadata->schema
[srvgit] / installer / install.pl
index 83bf213..f8de23e 100755 (executable)
 # You should have received a copy of the GNU General Public License
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-use strict;
-use warnings;
+use Modern::Perl;
 use diagnostics;
 
-use C4::InstallAuth;
+use C4::InstallAuth qw( get_template_and_user );
 use CGI qw ( -utf8 );
-use POSIX qw(strftime);
+use POSIX;
 
 use C4::Context;
-use C4::Output;
+use C4::Output qw( output_html_with_http_headers );
 use C4::Templates;
-use C4::Languages qw(getAllLanguages getTranslatedLanguages);
+use C4::Languages qw( getAllLanguages getTranslatedLanguages );
 use C4::Installer;
+use C4::Installer::PerlModules;
 
 use Koha;
 
-my $query = new CGI;
+my $query = CGI->new;
 my $step  = $query->param('step');
 
 my $language = $query->param('language');
@@ -49,14 +49,12 @@ if ( defined($language) ) {
         template_name => "installer/step" . ( $step ? $step : 1 ) . ".tt",
         query         => $query,
         type          => "intranet",
-        authnotrequired => 0,
-        debug           => 1,
     }
 );
 
 my $installer = C4::Installer->new();
 my %info;
-$info{'dbname'} = C4::Context->config("database");
+$info{'dbname'} = C4::Context->config("database_test") || C4::Context->config("database");
 $info{'dbms'}   = (
       C4::Context->config("db_scheme")
     ? C4::Context->config("db_scheme")
@@ -89,36 +87,54 @@ if ( $step && $step == 1 ) {
     #Whenever there is an error, adding a report to the page
     my $op = $query->param('op') || 'noop';
     $template->param( language      => 1 );
+    my $checkmodule = 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.010000 ) {    # Bug 7375
         $template->param( problems => 1, perlversion => 1, checkmodule => 0 );
+        $checkmodule = 0;
     }
 
     my $perl_modules = C4::Installer::PerlModules->new;
     $perl_modules->versions_info;
 
-    my $modules = $perl_modules->get_attr('missing_pm');
-    if ( scalar(@$modules) ) {
-        my @components  = ();
-        my $checkmodule = 1;
-        foreach (@$modules) {
+    my $missing_modules = $perl_modules->get_attr('missing_pm');
+    my $upgrade_modules = $perl_modules->get_attr('upgrade_pm');
+    if ( scalar(@$missing_modules) || scalar(@$upgrade_modules) ) {
+        my @missing  = ();
+        my @upgrade = ();
+        foreach (@$missing_modules) {
             my ( $module, $stats ) = each %$_;
             $checkmodule = 0 if $stats->{'required'};
             push(
-                @components,
+                @missing,
                 {
                     name    => $module,
-                    version => $stats->{'min_ver'},
-                    require => $stats->{'required'},
-                    usage   => $stats->{'usage'},
+                    min_version => $stats->{'min_ver'},
+                    max_version => $stats->{'max_ver'},
+                    require => $stats->{'required'}
                 }
             );
         }
-        @components = sort { $a->{'name'} cmp $b->{'name'} } @components;
+        foreach (@$upgrade_modules) {
+            my ( $module, $stats ) = each %$_;
+            $checkmodule = 0 if $stats->{'required'};
+            push(
+                @upgrade,
+                {
+                    name    => $module,
+                    min_version => $stats->{'min_ver'},
+                    max_version => $stats->{'max_ver'},
+                    require => $stats->{'required'}
+                }
+            );
+        }
+        @missing = sort { $a->{'name'} cmp $b->{'name'} } @missing;
+        @upgrade = sort { $a->{'name'} cmp $b->{'name'} } @upgrade;
         $template->param(
-            missing_modules => \@components,
+            missing_modules => \@missing,
+            upgrade_modules => \@upgrade,
             checkmodule     => $checkmodule,
             op              => $op
         );
@@ -144,10 +160,11 @@ elsif ( $step && $step == 2 ) {
                     $template->param( 'checkdatabasecreated' => 1 );
                 }
 
-                #Check if user have all necessary grants on this database.
-                my $rq =
-                  $dbh->prepare(
-                    "SHOW GRANTS FOR \'$info{user}\'\@'$info{hostname}'");
+                # Check if user have all necessary grants on this database.
+                # CURRENT_USER is ANSI SQL, and doesn't require mysql table
+                # privileges, making the % check pointless, since they
+                # couldn't even check GRANTS if they couldn't connect.
+                my $rq = $dbh->prepare('SHOW GRANTS FOR CURRENT_USER');
                 $rq->execute;
                 my $grantaccess;
                 while ( my ($line) = $rq->fetchrow ) {
@@ -167,27 +184,6 @@ elsif ( $step && $step == 2 ) {
                           );
                     }
                 }
-                unless ($grantaccess) {
-                    $rq =
-                      $dbh->prepare("SHOW GRANTS FOR \'$info{user}\'\@'\%'");
-                    $rq->execute;
-                    while ( my ($line) = $rq->fetchrow ) {
-                        my $dbname = $info{dbname};
-                        if ( $line =~ m/$dbname/ || index( $line, '*.*' ) > 0 )
-                        {
-                            $grantaccess = 1
-                              if (
-                                index( $line, 'ALL PRIVILEGES' ) > 0
-                                || (   ( index( $line, 'SELECT' ) > 0 )
-                                    && ( index( $line, 'INSERT' ) > 0 )
-                                    && ( index( $line, 'UPDATE' ) > 0 )
-                                    && ( index( $line, 'DELETE' ) > 0 )
-                                    && ( index( $line, 'CREATE' ) > 0 )
-                                    && ( index( $line, 'DROP' ) > 0 ) )
-                              );
-                        }
-                    }
-                }
                 $template->param( "checkgrantaccess" => $grantaccess );
             }    # End mysql connect check...
 
@@ -223,15 +219,21 @@ elsif ( $step && $step == 3 ) {
 
     my $op = $query->param('op');
     if ( $op && $op eq 'finished' ) {
-        #
+        # Remove the HandleError set at the beginning of the installer process
+        C4::Context->dbh->disconnect;
+
         # we have finished, just redirect to mainpage.
-        #
         print $query->redirect("/cgi-bin/koha/mainpage.pl");
         exit;
     }
     elsif ( $op && $op eq 'finish' ) {
         $installer->set_version_syspref();
 
+        my $langchoice = $query->param('fwklanguage');
+        $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
+        $langchoice =~ s/[^a-zA-Z_-]*//g;
+        $installer->set_languages_syspref($langchoice);
+
 # Installation is finished.
 # We just deny anybody access to install
 # And we redirect people to mainpage.
@@ -242,10 +244,13 @@ elsif ( $step && $step == 3 ) {
     elsif ( $op && $op eq 'addframeworks' ) {
 
         # 1ST install, 3rd sub-step : insert the SQL files the user has selected
+        my $langchoice = $query->param('fwklanguage');
+        $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
+        $langchoice =~ s/[^a-zA-Z_-]*//g;
 
         my ( $fwk_language, $list ) =
-          $installer->load_sql_in_order( $all_languages,
-            $query->param('framework') );
+          $installer->load_sql_in_order( $langchoice, $all_languages,
+            $query->multi_param('framework') );
         $template->param(
             "fwklanguage" => $fwk_language,
             "list"        => $list
@@ -296,9 +301,6 @@ elsif ( $step && $step == 3 ) {
         $template->param( "levelloop"      => $levellist );
         $template->param( "$op"            => 1 );
 
-        my $setup = $query->param('setup');
-        $template->param( "setup" => $setup );
-
     }
     elsif ( $op && $op eq 'choosemarc' ) {
         #
@@ -314,7 +316,7 @@ elsif ( $step && $step == 3 ) {
 # Where <level> is a category of requirement : required, recommended optional
 # level should contain :
 #   SQL File for import With a readable name.
-#   txt File taht explains what this SQL File is meant for.
+#   txt File that explains what this SQL File is meant for.
 # Could be VERY useful to have A Big file for a kind of library.
 # But could also be useful to have some Authorised values data set prepared here.
 # Marcflavour Selection is achieved through radiobuttons.
@@ -325,7 +327,8 @@ elsif ( $step && $step == 3 ) {
         my $dir =
           C4::Context->config('intranetdir')
           . "/installer/data/$info{dbms}/$langchoice/marcflavour";
-        unless ( opendir( MYDIR, $dir ) ) {
+        my $dir_h;
+        unless ( opendir( $dir_h, $dir ) ) {
             if ( $langchoice eq 'en' ) {
                 warn "cannot open MARC frameworks directory $dir";
             }
@@ -334,25 +337,21 @@ elsif ( $step && $step == 3 ) {
                 # default to English
                 $dir = C4::Context->config('intranetdir')
                   . "/installer/data/$info{dbms}/en/marcflavour";
-                opendir( MYDIR, $dir )
+                opendir( $dir_h, $dir )
                   or warn "cannot open English MARC frameworks directory $dir";
             }
         }
-        my @listdir = grep { !/^\./ && -d "$dir/$_" } readdir(MYDIR);
-        closedir MYDIR;
+        my @listdir = grep { !/^\./ && -d "$dir/$_" } readdir($dir_h);
+        closedir $dir_h;
         my $marcflavour = C4::Context->preference("marcflavour");
         my @flavourlist;
         foreach my $marc (@listdir) {
-            my %cell = (
-                "label"   => ucfirst($marc),
-                "code"    => uc($marc),
-                "checked" => defined($marcflavour)
-                ? uc($marc) eq $marcflavour
-                : 0
-            );
-
+             my %cell=(
+                 "label"=> ucfirst($marc),
+                  "code"=>uc($marc),
+               "checked"=> defined($marcflavour) ? uc($marc) eq $marcflavour : 0);
 #             $cell{"description"}= do { local $/ = undef; open INPUT "<$dir/$marc.txt"||"";<INPUT> };
-            push @flavourlist, \%cell;
+             push @flavourlist, \%cell;
         }
         $template->param( "flavourloop" => \@flavourlist );
         $template->param( "$op"         => 1 );
@@ -398,30 +397,32 @@ elsif ( $step && $step == 3 ) {
         my $cmd = C4::Context->config("intranetdir")
           . "/installer/data/$info{dbms}/updatedatabase.pl >> $logfilepath 2>> $logfilepath_errors";
 
-        system($cmd );
+        system( $cmd );
 
         my $fh;
-        open( $fh, "<", $logfilepath )
+        open( $fh, "<:encoding(utf-8)", $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 ) ) ]
+                  [ map { { line => $_ =~ s/\t/&emsp;&emsp;/gr } } split( /\n/, join( '', @report ) ) ]
             );
             $template->param( has_update_succeeds => 1 );
         }
         else {
             eval { `rm $logfilepath` };
         }
-        open( $fh, "<", $logfilepath_errors )
+        open( $fh, "<:encoding(utf-8)", $logfilepath_errors )
           or die "Cannot open log file $logfilepath_errors: $!";
         @report = <$fh>;
         close $fh;
+        my $update_errors;
         if (@report) {
             $template->param( update_errors =>
                   [ map { { line => $_ } } split( /\n/, join( '', @report ) ) ]
             );
+            $update_errors = 1;
             $template->param( has_update_errors => 1 );
             warn
 "The following errors were returned while attempting to run the updatedatabase.pl script:\n";
@@ -430,6 +431,23 @@ elsif ( $step && $step == 3 ) {
         else {
             eval { `rm $logfilepath_errors` };
         }
+
+        unless ( $update_errors ) {
+            my $db_entries = get_db_entries();
+            my $report = update( $db_entries );
+            my $atomic_update_files = get_atomic_updates;
+            my $atomic_update_report = run_atomic_updates( $atomic_update_files );
+
+            $template->param(
+                success        => $report->{success},
+                error          => $report->{error},
+                atomic_updates => {
+                    success => $atomic_update_report->{success},
+                    error   => $atomic_update_report->{error}
+                }
+            );
+        }
+
         $template->param( $op => 1 );
     }
     else {