<commit_before># --
# Kernel/Modules/Installer.pm - provides the DB installer
# Copyright (C) 2001-2007 OTRS GmbH, http://otrs.org/
# --
# $Id: Installer.pm,v 1.44 2007-03-09 13:10:47 martin Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see http://www.gnu.org/licenses/gpl.txt.
# --

# Note: this is the first version to support mysql. More databases
# later.

package Kernel::Modules::Installer;

use strict;
use DBI;

use vars qw($VERSION %INC);
$VERSION = '$Revision: 1.44 $';
$VERSION =~ s/^\$.*:\W(.*)\W.+?$/$1/;

sub new {
    my $Type = shift;
    my %Param = @_;

    # allocate new hash for object
    my $Self = {};
    bless ($Self, $Type);

    foreach (keys %Param) {
        $Self->{$_} = $Param{$_};
    }

    # check needed Opjects
    foreach (qw(ParamObject LayoutObject LogObject ConfigObject)) {
        if (!$Self->{$_}) {
            $Self->{LayoutObject}->FatalError(Message => "Got no $_!");
        }
    }

    return $Self;
}

sub Run {
    my $Self = shift;
    my %Param = @_;
    my $Output = '';
    # check env directories
    $Self->{Path} = $Self->{ConfigObject}->Get('Home');
    if (! -d $Self->{Path}) {
        return $Self->{LayoutObject}->ErrorScreen(
            Message => "Directory '$Self->{Path}' doesn't exist!",
            Comment => 'Configure Home in Kernel/Config.pm first!',
        );
    }
    if (! -f "$Self->{Path}/Kernel/Config.pm") {
        return $Self->{LayoutObject}->Error(
            Message => "File '$Self->{Path}/Kernel/Config.pm' not found!",
            Comment => 'Contact your Admin!',
        );
    }
    # check/get sql source files
    my $DirOfSQLFiles = $Self->{Path}.'/scripts/database';
    if (! -d $DirOfSQLFiles) {
        return $Self->{LayoutObject}->ErrorScreen(
            Message => "Directory '$DirOfSQLFiles' not found!",
            Comment => 'Contact your Admin!',
        );
    }
    elsif (! -f "$DirOfSQLFiles/otrs-schema.xml") {
        return $Self->{LayoutObject}->ErrorScreen(
            Message => "File '$DirOfSQLFiles/otrs-schema.xml' not found!",
            Comment => 'Contact your Admin!',
        );
    }
    # check dist
    my %Dist = ();
    $Dist{Vendor} = "Unix/Linux";
    $Dist{Webserver} = "restart your webserver";
    if (-f "/etc/SuSE-release") {
        $Dist{Vendor} = "SuSE";
        if (exists $ENV{MOD_PERL}) {
            eval "require mod_perl";
            if (defined $mod_perl::VERSION) {
                if ($mod_perl::VERSION >= 1.99) {
                    $Dist{Webserver} = "rcapache2 restart";
                }
                else {
                    $Dist{Webserver} = "rcapache restart";
                }
            }
        }
        else {
            $Dist{Webserver} = "";
        }
    }
    elsif (-f "/etc/redhat-release") {
        $Dist{Vendor} = "Redhat";
        $Dist{Webserver} = "service httpd restart";
    }
    else {
        if (exists $ENV{MOD_PERL}) {
            eval "require mod_perl";
            if (defined $mod_perl::VERSION) {
                if ($mod_perl::VERSION >= 1.99) {
                    $Dist{Webserver} = "Apache2 + mod_perl2";
                }
                else {
                    $Dist{Webserver} = "Apache + mod_perl";
                }
            }
        }
    }
    # check if Apache::Reload is loaded
    foreach my $Module (keys %INC) {
        $Module =~ s/\//::/g;
        $Module =~ s/\.pm$//g;
        if ($Module eq 'Apache::Reload' || $Module eq 'Apache2::Reload') {
            $Dist{Vendor} = '';
            $Dist{Webserver} = '';
        }
    }

    # print form
    if (!$Self->{Subaction}) {
        $Output .= $Self->{LayoutObject}->Header(Title => 'License');
        $Self->{LayoutObject}->Block(
            Name => 'License',
            Data => {
                Item => 'License',
                Step => '1/4',
            }
        );
        $Output .= $Self->{LayoutObject}->Output(
            TemplateFile => 'Installer',
            Data => { },
        );
        $Output .= $Self->{LayoutObject}->Footer();
        return $Output;
    }

    # do database settings
    elsif ($Self->{Subaction} eq 'Start') {
        if ($Self->ReConfigure()) {
            $Output .= $Self->{LayoutObject}->Header(Title => 'Error');
            $Output .= $Self->{LayoutObject}->Warning(
                Message => "Kernel/Config.pm isn't writable!",
                Comment => 'If you want to use the installer, set the '.
                    'Kernel/Config.pm writable for the werserver user! '.
                    $Self->ReConfigure(),
            );
            $Output .= $Self->{LayoutObject}->Footer();
            return $Output;
        }
        else {
            $Output .= $Self->{LayoutObject}->Header(Title => 'Create Database');
            $Self->{LayoutObject}->Block(
                Name => 'DatabaseStart',
                Data => {
                    Item => 'Create Database',
                    Step => '2/4',
                }
            );
            $Output .= $Self->{LayoutObject}->Output(
                TemplateFile => 'Installer',
                Data => { },
            );
            $Output .= $Self->{LayoutObject}->Footer();
            return $Output;
        }
    }

    # do database settings
    elsif ($Self->{Subaction} eq 'DB') {
        $Output .= $Self->{LayoutObject}->Header(Title => 'Installer');
        # get params
        my %DB = ();
        $DB{User} = $Self->{ParamObject}->GetParam(Param => 'DBUser') || '';
        $DB{Password} = $Self->{ParamObject}->GetParam(Param => 'DBPassword') || '';
        $DB{DatabaseHost} = $Self->{ParamObject}->GetParam(Param => 'DBHost') || '';
        $DB{Type} = $Self->{ParamObject}->GetParam(Param => 'DBType') || '';
        $DB{Database} = $Self->{ParamObject}->GetParam(Param => 'DBName') || '';
        $DB{DBAction} = $Self->{ParamObject}->GetParam(Param => 'DBAction') || '';
        $DB{DatabaseUser} = $Self->{ParamObject}->GetParam(Param => 'OTRSDBUser') || '';
        $DB{DatabasePw} = $Self->{ParamObject}->GetParam(Param => 'OTRSDBPassword') || '';
        $DB{NewHost} = $Self->{ParamObject}->GetParam(Param => 'OTRSDBConnectHost') || '';
        # check params
        foreach (keys %DB) {
            if (!$DB{$_} && $_ ne 'Password') {
                return $Self->{LayoutObject}->ErrorScreen(
                    Message => "You need '$_'!!",
                    Comment => 'Please go back',
                );
            }
        }
        # connect to database
        my $DBH = DBI->connect(
            "DBI:mysql:database=;host=$DB{DatabaseHost};",
            $DB{User},
            $DB{Password},
        );
        if (!$DBH) {
            return $Self->{LayoutObject}->ErrorScreen(
                Message => "Can't connect to database, read comment!",
                Comment => "$DBI::errstr",
            );
        }

        if ($DB{DBAction} eq 'Create') {
            # FIXME !!! use $DB{Type}!!!
            $Self->{LayoutObject}->Block(
                Name => 'DatabaseResult',
                Data => {
                    Item => 'Create Database',
                    Step => '2/4',
                },
            );

            # create db
            $Self->{LayoutObject}->Block(
                Name => 'DatabaseResultItem',
                Data => {
                    Item => "Creating database '$DB{Database}'",
                },
            );
            if (!$DBH->do("CREATE DATABASE $DB{Database}")) {
                $Self->{LayoutObject}->Block(
                    Name => 'DatabaseResultItemFalse',
                    Data => { },
                );
                $Self->{LayoutObject}->Block(
                    Name => 'DatabaseResultItemMessage',
                    Data => {
                        Message => $DBI::errstr,
                    },
                );
                $Self->{LayoutObject}->Block(
                    Name => 'DatabaseResultBack',
                    Data => { },
                );
                $Output .= $Self->{LayoutObject}->Output(
                    TemplateFile => 'Installer',
                    Data => { },
                );
                $Output .= $Self->{LayoutObject}->Footer();
                return $Output;
            }
            else {
                $Self->{LayoutObject}->Block(
                    Name => 'DatabaseResultItemDone',
                    Data => { },
                );
            }

            # create db tables
            # read otrs-schema.mysql.sql and process stuff
            my @SQL = $Self->ParseSQLFile("$DirOfSQLFiles/otrs-schema.mysql.sql");
            $DBH->do("use $DB{Database}");
            $Self->{LayoutObject}->Block(
                Name => 'DatabaseResultItem',
                Data => {
                    Item => "Creating tables 'otrs-schema.mysql.sql'",
                },
            );
            foreach (@SQL) {
                if (!$DBH->do($_)) {
                    $Self->{LayoutObject}->Block(
                        Name => 'DatabaseResultItemFalse',
                        Data => { },
                    );
                    $Self->{LayoutObject}->Block(
                        Name => 'DatabaseResultItemMessage',
                        Data => {
                            Message => $DBI::errstr,
                        },
                    );
                    $Self->{LayoutObject}->Block(
                        Name => 'DatabaseResultBack',
                        Data => { },
                    );
                    print STDERR "ERR: $DBI::errstr - $_\n";
                    $Output .= $Self->{LayoutObject}->Output(
                        TemplateFile => 'Installer',
                        Data => { },
                    );
                    $Output .= $Self->{LayoutObject}->Footer();
                    return $Output;
                }
            }
            $Self->{LayoutObject}->Block(
                Name => 'DatabaseResultItemDone',
                Data => { },
            );

            # initial insert
            # - read initial_insert.sql and process stuff -
            @SQL = $Self->ParseSQLFile("$DirOfSQLFiles/otrs-initial_insert.mysql.sql");
            $Self->{LayoutObject}->Block(
                Name => 'DatabaseResultItem',
                Data => {
                    Item => "Inserting Inital inserts 'initial_insert.sql'",
                },
            );
            foreach (@SQL) {
                if (!$DBH->do($_)) {
                    $Self->{LayoutObject}->Block(
                        Name => 'DatabaseResultItemFalse',
                        Data => { },
                    );
                    $Self->{LayoutObject}->Block(
                        Name => 'DatabaseResultItemMessage',
                        Data => {
                            Message => $DBI::errstr,
                        },
                    );
                    $Self->{LayoutObject}->Block(
                        Name => 'DatabaseResultBack',<commit_msg>Moved to new inital insert script location also in doc and log messages.<commit_after># --
# Kernel/Modules/Installer.pm - provides the DB installer
# Copyright (C) 2001-2007 OTRS GmbH, http://otrs.org/
# --
# $Id: Installer.pm,v 1.45 2007-03-09 13:15:12 martin Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see http://www.gnu.org/licenses/gpl.txt.
# --

# Note: this is the first version to support mysql. More databases
# later.

package Kernel::Modules::Installer;

use strict;
use DBI;

use vars qw($VERSION %INC);
$VERSION = '$Revision: 1.45 $';
$VERSION =~ s/^\$.*:\W(.*)\W.+?$/$1/;

sub new {
    my $Type = shift;
    my %Param = @_;

    # allocate new hash for object
    my $Self = {};
    bless ($Self, $Type);

    foreach (keys %Param) {
        $Self->{$_} = $Param{$_};
    }

    # check needed Opjects
    foreach (qw(ParamObject LayoutObject LogObject ConfigObject)) {
        if (!$Self->{$_}) {
            $Self->{LayoutObject}->FatalError(Message => "Got no $_!");
        }
    }

    return $Self;
}

sub Run {
    my $Self = shift;
    my %Param = @_;
    my $Output = '';
    # check env directories
    $Self->{Path} = $Self->{ConfigObject}->Get('Home');
    if (! -d $Self->{Path}) {
        return $Self->{LayoutObject}->ErrorScreen(
            Message => "Directory '$Self->{Path}' doesn't exist!",
            Comment => 'Configure Home in Kernel/Config.pm first!',
        );
    }
    if (! -f "$Self->{Path}/Kernel/Config.pm") {
        return $Self->{LayoutObject}->Error(
            Message => "File '$Self->{Path}/Kernel/Config.pm' not found!",
            Comment => 'Contact your Admin!',
        );
    }
    # check/get sql source files
    my $DirOfSQLFiles = $Self->{Path}.'/scripts/database';
    if (! -d $DirOfSQLFiles) {
        return $Self->{LayoutObject}->ErrorScreen(
            Message => "Directory '$DirOfSQLFiles' not found!",
            Comment => 'Contact your Admin!',
        );
    }
    elsif (! -f "$DirOfSQLFiles/otrs-schema.xml") {
        return $Self->{LayoutObject}->ErrorScreen(
            Message => "File '$DirOfSQLFiles/otrs-schema.xml' not found!",
            Comment => 'Contact your Admin!',
        );
    }
    # check dist
    my %Dist = ();
    $Dist{Vendor} = "Unix/Linux";
    $Dist{Webserver} = "restart your webserver";
    if (-f "/etc/SuSE-release") {
        $Dist{Vendor} = "SuSE";
        if (exists $ENV{MOD_PERL}) {
            eval "require mod_perl";
            if (defined $mod_perl::VERSION) {
                if ($mod_perl::VERSION >= 1.99) {
                    $Dist{Webserver} = "rcapache2 restart";
                }
                else {
                    $Dist{Webserver} = "rcapache restart";
                }
            }
        }
        else {
            $Dist{Webserver} = "";
        }
    }
    elsif (-f "/etc/redhat-release") {
        $Dist{Vendor} = "Redhat";
        $Dist{Webserver} = "service httpd restart";
    }
    else {
        if (exists $ENV{MOD_PERL}) {
            eval "require mod_perl";
            if (defined $mod_perl::VERSION) {
                if ($mod_perl::VERSION >= 1.99) {
                    $Dist{Webserver} = "Apache2 + mod_perl2";
                }
                else {
                    $Dist{Webserver} = "Apache + mod_perl";
                }
            }
        }
    }
    # check if Apache::Reload is loaded
    foreach my $Module (keys %INC) {
        $Module =~ s/\//::/g;
        $Module =~ s/\.pm$//g;
        if ($Module eq 'Apache::Reload' || $Module eq 'Apache2::Reload') {
            $Dist{Vendor} = '';
            $Dist{Webserver} = '';
        }
    }

    # print form
    if (!$Self->{Subaction}) {
        $Output .= $Self->{LayoutObject}->Header(Title => 'License');
        $Self->{LayoutObject}->Block(
            Name => 'License',
            Data => {
                Item => 'License',
                Step => '1/4',
            }
        );
        $Output .= $Self->{LayoutObject}->Output(
            TemplateFile => 'Installer',
            Data => { },
        );
        $Output .= $Self->{LayoutObject}->Footer();
        return $Output;
    }

    # do database settings
    elsif ($Self->{Subaction} eq 'Start') {
        if ($Self->ReConfigure()) {
            $Output .= $Self->{LayoutObject}->Header(Title => 'Error');
            $Output .= $Self->{LayoutObject}->Warning(
                Message => "Kernel/Config.pm isn't writable!",
                Comment => 'If you want to use the installer, set the '.
                    'Kernel/Config.pm writable for the werserver user! '.
                    $Self->ReConfigure(),
            );
            $Output .= $Self->{LayoutObject}->Footer();
            return $Output;
        }
        else {
            $Output .= $Self->{LayoutObject}->Header(Title => 'Create Database');
            $Self->{LayoutObject}->Block(
                Name => 'DatabaseStart',
                Data => {
                    Item => 'Create Database',
                    Step => '2/4',
                }
            );
            $Output .= $Self->{LayoutObject}->Output(
                TemplateFile => 'Installer',
                Data => { },
            );
            $Output .= $Self->{LayoutObject}->Footer();
            return $Output;
        }
    }

    # do database settings
    elsif ($Self->{Subaction} eq 'DB') {
        $Output .= $Self->{LayoutObject}->Header(Title => 'Installer');
        # get params
        my %DB = ();
        $DB{User} = $Self->{ParamObject}->GetParam(Param => 'DBUser') || '';
        $DB{Password} = $Self->{ParamObject}->GetParam(Param => 'DBPassword') || '';
        $DB{DatabaseHost} = $Self->{ParamObject}->GetParam(Param => 'DBHost') || '';
        $DB{Type} = $Self->{ParamObject}->GetParam(Param => 'DBType') || '';
        $DB{Database} = $Self->{ParamObject}->GetParam(Param => 'DBName') || '';
        $DB{DBAction} = $Self->{ParamObject}->GetParam(Param => 'DBAction') || '';
        $DB{DatabaseUser} = $Self->{ParamObject}->GetParam(Param => 'OTRSDBUser') || '';
        $DB{DatabasePw} = $Self->{ParamObject}->GetParam(Param => 'OTRSDBPassword') || '';
        $DB{NewHost} = $Self->{ParamObject}->GetParam(Param => 'OTRSDBConnectHost') || '';
        # check params
        foreach (keys %DB) {
            if (!$DB{$_} && $_ ne 'Password') {
                return $Self->{LayoutObject}->ErrorScreen(
                    Message => "You need '$_'!!",
                    Comment => 'Please go back',
                );
            }
        }
        # connect to database
        my $DBH = DBI->connect(
            "DBI:mysql:database=;host=$DB{DatabaseHost};",
            $DB{User},
            $DB{Password},
        );
        if (!$DBH) {
            return $Self->{LayoutObject}->ErrorScreen(
                Message => "Can't connect to database, read comment!",
                Comment => "$DBI::errstr",
            );
        }

        if ($DB{DBAction} eq 'Create') {
            # FIXME !!! use $DB{Type}!!!
            $Self->{LayoutObject}->Block(
                Name => 'DatabaseResult',
                Data => {
                    Item => 'Create Database',
                    Step => '2/4',
                },
            );

            # create db
            $Self->{LayoutObject}->Block(
                Name => 'DatabaseResultItem',
                Data => {
                    Item => "Creating database '$DB{Database}'",
                },
            );
            if (!$DBH->do("CREATE DATABASE $DB{Database}")) {
                $Self->{LayoutObject}->Block(
                    Name => 'DatabaseResultItemFalse',
                    Data => { },
                );
                $Self->{LayoutObject}->Block(
                    Name => 'DatabaseResultItemMessage',
                    Data => {
                        Message => $DBI::errstr,
                    },
                );
                $Self->{LayoutObject}->Block(
                    Name => 'DatabaseResultBack',
                    Data => { },
                );
                $Output .= $Self->{LayoutObject}->Output(
                    TemplateFile => 'Installer',
                    Data => { },
                );
                $Output .= $Self->{LayoutObject}->Footer();
                return $Output;
            }
            else {
                $Self->{LayoutObject}->Block(
                    Name => 'DatabaseResultItemDone',
                    Data => { },
                );
            }

            # create db tables
            # read otrs-schema.mysql.sql and process stuff
            my @SQL = $Self->ParseSQLFile("$DirOfSQLFiles/otrs-schema.mysql.sql");
            $DBH->do("use $DB{Database}");
            $Self->{LayoutObject}->Block(
                Name => 'DatabaseResultItem',
                Data => {
                    Item => "Creating tables 'otrs-schema.mysql.sql'",
                },
            );
            foreach (@SQL) {
                if (!$DBH->do($_)) {
                    $Self->{LayoutObject}->Block(
                        Name => 'DatabaseResultItemFalse',
                        Data => { },
                    );
                    $Self->{LayoutObject}->Block(
                        Name => 'DatabaseResultItemMessage',
                        Data => {
                            Message => $DBI::errstr,
                        },
                    );
                    $Self->{LayoutObject}->Block(
                        Name => 'DatabaseResultBack',
                        Data => { },
                    );
                    print STDERR "ERR: $DBI::errstr - $_\n";
                    $Output .= $Self->{LayoutObject}->Output(
                        TemplateFile => 'Installer',
                        Data => { },
                    );
                    $Output .= $Self->{LayoutObject}->Footer();
                    return $Output;
                }
            }
            $Self->{LayoutObject}->Block(
                Name => 'DatabaseResultItemDone',
                Data => { },
            );

            # initial insert
            # - read otrs-initial_insert.mysql.sql and process stuff -
            @SQL = $Self->ParseSQLFile("$DirOfSQLFiles/otrs-initial_insert.mysql.sql");
            $Self->{LayoutObject}->Block(
                Name => 'DatabaseResultItem',
                Data => {
                    Item => "Inserting Inital inserts 'otrs-initial_insert.mysql.sql'",
                },
            );
            foreach (@SQL) {
                if (!$DBH->do($_)) {
                    $Self->{LayoutObject}->Block(
                        Name => 'DatabaseResultItemFalse',
                        Data => { },
                    );
                    $Self->{LayoutObject}->Block(
                        Name => 'DatabaseResultItemMessage',
                        Data => {
                            Message => $DBI::errstr,
                        },
                    );
                    $Self->{LayoutObject}->Block(
                        Name => 'DatabaseResultBack',
{'fraction_non_alphanumeric': 0.15898853855416314, 'fraction_numerical': 0.004013735896178031, 'mean_word_length': 1.359924226478636, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 401, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '211926', 'n_tokens_mistral': 6378, 'n_tokens_neox': 6061, 'n_words': 1199}