diff --git a/TestWPDBBackup.php b/TestWPDBBackup.php index 1b9e353..1cfca90 100644 --- a/TestWPDBBackup.php +++ b/TestWPDBBackup.php @@ -1,6 +1,9 @@ assertTrue($this->_b->is_wp_secure_enough()); } - /** - * @dataProvider provider__perhaps_compress_file - */ - public function test__perhaps_compress_file($filename, $gzipped_filename, $file_exists, $gzipped_file_exists) - { - if (class_exists('vfsStream') === false) { - $this->markTestSkipped('vfsStream not installed.'); - } - - vfsStreamWrapper::register(); - $root = new vfsStreamDirectory('home'); - - $dir = dirname($filename); - if ( ! empty( $dir ) ) { - $root->addChild(new vfsStreamDirectory($dir)); - $root->getChild($dir)->addChild(vfsStream::newFile(basename($filename))->withContent('Backup SQL text')); - } - vfsStreamWrapper::setRoot($root); - - - $new_filename = $this->_b->perhaps_compress_file(vfsStream::url($filename)); - - if ( function_exists('gzencode') ) { - - $this->assertEquals($gzipped_file_exists, vfsStreamWrapper::getRoot()->hasChild($gzipped_filename)); - $this->assertEquals($file_exists, vfsStreamWrapper::getRoot()->hasChild($filename)); - } else { - $this->assertFalse(vfsStreamWrapper::getRoot()->hasChild($gzipped_filename)); - $this->assertTrue(vfsStreamWrapper::getRoot()->hasChild($filename)); - } - - - } - public function provider__perhaps_compress_file() - { - return array( - array('path/file.sql', 'path/file.sql.gz', false, true), - ); - } - /** * @dataProvider provider__send_mail */ diff --git a/wp-db-backup.php b/wp-db-backup.php index ded4d08..de135d2 100755 --- a/wp-db-backup.php +++ b/wp-db-backup.php @@ -91,7 +91,7 @@ function __construct() $table_prefix = ( isset( $table_prefix ) ) ? $table_prefix : $wpdb->prefix; $datum = gmdate("Ymd_B"); - $this->backup_filename = DB_NAME . "_$table_prefix$datum.sql"; + $this->backup_filename = DB_NAME . "_$table_prefix$datum.sql".($this->has_gz() ? '.gz' : ''); $this->backup_dir = trailingslashit(apply_filters('wp_db_b_backup_dir', WP_BACKUP_DIR)); $this->basename = 'wp-db-backup'; @@ -755,12 +755,24 @@ function backquote($a_name) { function open($filename = '', $mode = 'w') { if ('' == $filename) return false; - $fp = @fopen($filename, $mode); + if ($this->has_gz()) { + $fp = @gzopen($filename, $mode); + } else { + $fp = @fopen($filename, $mode); + } return $fp; } function close($fp) { - fclose($fp); + if ($this->has_gz()) { + gzclose($fp); + } else { + fclose($fp); + } + } + + function has_gz() { + return function_exists('gzencode'); } /** @@ -1099,43 +1111,12 @@ function send_mail($phpmailer = null, $to = '', $subject = '', $message = '', $d } - /** - * Try to gzip the file - * - * @param string $filepath The path to the file which we're trying to compress - * @return string The new filepath. - */ - function perhaps_compress_file( $filename = '' ) - { - if ( ! empty($filename) && function_exists('gzencode') ) { - $gz_filename = "{$filename}.gz"; - if ( function_exists('file_get_contents') ) { - $text = file_get_contents($filename); - } else { - $text = implode("", file($filename)); - } - $gz_text = gzencode($text, 9); - $fp = fopen($gz_filename, "w"); - if ( $fp ) { - fwrite($fp, $gz_text); - if ( fclose($fp) ) { - unlink($filename); - $filename = $gz_filename; - } - } - } - - return $filename; - } - function deliver_backup($filename = '', $delivery = 'http', $recipient = '', $location = 'main') { if ( empty( $filename ) ) { return false; } - - $diskfile = $this->perhaps_compress_file($this->backup_dir . $filename); - $filename = basename($diskfile); + $diskfile = $this->backup_dir . $filename; if ('http' == $delivery) { if (! file_exists($diskfile))