package Cpanel::Easy::PHP5::Mysqli;

# cpanel - Cpanel/Easy/PHP5/Mysqli.pm             Copyright(c) 2014 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use Cpanel::Version::Compare ();

our $easyconfig = {
    'name'    => 'MySQL "Improved" extension.',
    'note'    => 'MySQLi does not replace the standard MySQL extension. Requires MySQL 4.1 or later. !! mysqli may not work on all systems, contact your support provider for mysqli support !!',
    'url'     => 'http://www.php.net/mysqli',
    'modself' => sub {
        my $easy    = shift;
        my $self_hr = shift;

        my @info = $easy->get_system_mysql_info();

        if (@info) {

            # FB 127149: only support php mysqlnd (native driver) going forward
            my $support = $easy->get_php_mysql_support_type( $info[4], $info[3] );
            $self_hr->{'implies'}{'Cpanel::Easy::PHP5::MysqlOfSystem'} = 0 if $support eq 'mysqlnd';
        }
        else {
            $self_hr->{'hastargz'}                   = 0;
            $self_hr->{'skip'}                       = 1;
            $self_hr->{'treat_as_off_while_skipped'} = 1;
        }
    },
    'dryrun' => {
        '0' => {
            'name'    => q{MySQL and PHP support check},
            'command' => sub {
                my $self        = shift;
                my $php_version = $self->get_php_version();
                my ( $mysql_version, $db_type ) = ( $self->get_system_mysql_info() )[ 3, 4 ];

                if ( Cpanel::Version::Compare::compare( $php_version, '<', '5.3' ) && Cpanel::Version::Compare::compare( $mysql_version, '>=', '5.5' ) ) {
                    $self->print_alert_color( 'red', q{MySQL 5.5+ is not compatible with PHP 5.2.9 using the MySQLi extension; skipping} );
                    $self->{'__'}{'SKIP'} = 1;
                }

                if ( Cpanel::Version::Compare::compare( $mysql_version, '<', '4.1' ) ) {
                    $self->print_alert_color( 'red', q{MySQL < 4.1 is not compatible with the MySQLi extension; skipping} );
                    $self->{'__'}{'SKIP'} = 1;
                }

                return ( 1, 'Ok' );
            },
        },
        '1' => {
            'name'    => 'MySQL Compile Test',
            'command' => sub {
                my ($self) = @_;
                return $self->do_mysql_h_test_compile();
            },
        },
    },
    'step' => {
        '0' => {
            'name'    => 'to config options',
            'command' => sub {
                my $self = shift;

                my $php_version = $self->get_php_version();
                return ( 0, 'Could not determine PHP version' ) unless $php_version;

                if ( $self->get_ns_value_from_profile('Cpanel::Easy::PHP5::MysqlOfSystem') ) {
                    my $config = ( $self->get_system_mysql_info() )[1];
                    $self->add_to_configure( { '--with-mysqli' => $config } );
                }
                else {
                    $self->add_to_configure( { '--with-mysqli' => '' } );
                }

                return ( 1, 'Ok' );
            },
        },
    },
};

1;
