App-WordPressTools

 view release on metacpan or  search on metacpan

script/wp-tools  view on Meta::CPAN

$nice_path       =~ s/^\///;
my @parts        = split '/', $nice_path;
my $wpdir        = $parts[-1] || '';
my $parent       = $nice_path;
$parent          =~ s/\Q$wpdir\E\/?//;
my $parentq      = shell_quote($parent);
my $wpdirq       = shell_quote($wpdir);
my $pathq        = shell_quote($nice_path);
my ($wp_cli_path, $version, $canonversion, $plus3713);
my $canon3713    = _canon_ver('3.7.13');
our $maintenance;
our $mode;
our $definition_check_string = '/*WP-CLI_DEFINITION_CHECK*/';
if ($command =~ /^(backup|upgrade)$/) {
    $wp_cli_path  = "$args->{'wp_cli'} --path=$pathq";
    $version      = _run_wpcli($nice_path, 'core version');
    chomp $version;
    $canonversion = _canon_ver($version);
    $plus3713     = !!($canonversion ge $canon3713);
}

if (my $method = __PACKAGE__->can($command)) {
    my $return = eval{$method->()};
    if ($@) {
        print "Unable to $command: $@";
        exit 1;
    }
    elsif ($return && ref $return) {
        print "$return->{'message'}\n" || "Successfully completed $command operation.\n";
        exit($return->{'success'} ? 0 : 1);
    }
    exit 1;
}

sub backup {
    my $time            = time;
    my $hash            = _lock_path($nice_path);

    my $backup_file     = "wp_backup${time}.tar.gz";
    our $db_file        = "wp_backup${hash}.sql";
    our $manifest_file  = "wp_backup${hash}.MANIFEST";
    our $defaults_file  = "wp_backup${hash}.temporary.my.cnf";

    my $skips = {};

    ### check size of WordPress installation
    if ($args->{max_size} && !$args->{force}) {
        # find directories that may be skipped
        my $skippable = {};
        my $wp_content_path = shell_quote("$args->{'path'}/wp-content");
        my $dums = eval{`du -ms $wp_content_path/*`};
        my $wp_content_size;
        for my $entry (split(/[\r\n]+/, $dums)) {
            my ($size, $path) = $entry =~ m/^(\d+).*wp-content\/(.*)$/;
            $wp_content_size->{$path} = $size;
        }
        eval {
            opendir (my $dh, $wp_content_path) || die;
            while (my $path = readdir $dh) {
                next if $path eq '.' || $path eq '..';
                if ($path =~ /(?:backup|upload)/i) {
                    $skippable->{$path} = $wp_content_size->{$path};
                }
                if ($wp_content_size->{$path} && $wp_content_size->{$path} > 200 && $path !~ /^(?:themes|plugins|mu-plugins|translations|languages)$/) {
                    $skippable->{$path} = $wp_content_size->{$path};
                }
            }
        };

        my @paths = map { -e "$nice_path/$_" ? shell_quote("$nice_path/$_") : () } @backup_whitelist;
        my $paths = join(' ', @paths);
        my $size = eval { `du -msc $paths` } || '';
        $size =~ s/.*?(\d+)\s+total.*/$1/s;
        if (!$size || $size !~ /^\d+$/) {
            die 'Cannot determine size of WordPress installation';
        }
        if ($args->{max_size} < $size) {
            # try to reduce content that is included in the backup
            my $backupsize = $size;
            for my $type (sort { $skippable->{$b} <=> $skippable->{$a} } keys %$skippable) {
                my $skip_path   = "$args->{'path'}/wp-content/$type";
                my $skip_pathq  = shell_quote($skip_path);
                next if !-d $skip_path;
                $skips->{$type} = $skippable->{$type} || eval { `du -ms $skip_pathq` } || 0;
                $skips->{$type} =~ s/^(\d+).*/$1/s;
                $backupsize -= $skips->{$type};
                last if $backupsize <= $args->{max_size};
            }
            if ($args->{max_size} < $backupsize) {
                die "Cannot backup this WordPress installation because it is too large ($size/$backupsize)";
            }
        }
    }

    ### backup database
    my $result = '';
    my $dump_command = '';

    # we cannot check the database without wp-config.php (credential storage location)
    if (!-f "$nice_path/wp-config.php") {
        $args->{'__skip_database'} = 1;
    }
    else {
        #acceptable failure states for databaseless accounts
        my $no_db_regex = qr/(?:Access denied for user|Unknown MySQL server host|Unknown database.*when selecting the database|is marked as crashed and last \(automatic\?\) repair failed when using LOCK TABLES|Got error: 130: Incorrect file format|Go...
        if ($plus3713) {
            $dump_command = "db export $db_file --add-drop-table 2>&1";
            $result = _run_wpcli($nice_path, $dump_command);
        }
        if ($result =~ $no_db_regex) {
            $args->{'__skip_database'} = 1;
        }
        elsif ($result !~ /^Success/) {
            # wp-cli backup can fail for wp <3.7.13, so try mysqldump instead
            my $wp_configq = shell_quote("$args->{'path'}/wp-config.php");
            my $username = `grep DB_USER     <$wp_configq | grep -v '$definition_check_string' | cut -d \\' -f 4`;
            my $password = `grep DB_PASSWORD <$wp_configq | grep -v '$definition_check_string' | cut -d \\' -f 4`;
            my $database = `grep DB_NAME     <$wp_configq | grep -v '$definition_check_string' | cut -d \\' -f 4`;
            #disallow starting whitespace
            $username =~ s/^[\r\n]+//;
            $password =~ s/^[\r\n]+//;



( run in 1.716 second using v1.01-cache-2.11-cpan-b16cb0d3907 )