Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drzraf no max file size #21

Merged
merged 10 commits into from
Sep 29, 2017
51 changes: 51 additions & 0 deletions features/export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,57 @@ Feature: Export content.
0
"""

Scenario: Export splitting the dump
Given a WP install

When I run `wp export --max_file_size=0.0001`
Then STDOUT should contain:
"""
001.xml
"""
And STDERR should be empty

Scenario: Export without splitting the dump
Given a WP install
# Make export file > 15MB so will split by default. Need to split into 4 * 4MB to stay below 10% of default redo log size of 48MB, otherwise get MySQL error.
And I run `wp db query "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (1, '_dummy', REPEAT( 'A', 4 * 1024 * 1024 ) );"`
And I run `wp db query "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (1, '_dummy', REPEAT( 'A', 4 * 1024 * 1024 ) );"`
And I run `wp db query "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (1, '_dummy', REPEAT( 'A', 4 * 1024 * 1024 ) );"`
And I run `wp db query "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (1, '_dummy', REPEAT( 'A', 4 * 1024 * 1024 ) );"`

When I run `wp export`
Then STDOUT should contain:
"""
000.xml
"""
And STDOUT should contain:
"""
001.xml
"""
And STDERR should be empty

When I run `wp export --max_file_size=0`
Then STDOUT should contain:
"""
000.xml
"""
And STDOUT should contain:
"""
001.xml
"""
And STDERR should be empty

When I run `wp export --max_file_size=-1`
Then STDOUT should contain:
"""
000.xml
"""
And STDOUT should not contain:
"""
001.xml
"""
And STDERR should be empty

Scenario: Export a site to stdout
Given a WP install
And I run `wp comment generate --post_id=1 --count=1`
Expand Down
23 changes: 8 additions & 15 deletions src/Export_Command.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

define( 'WP_CLI_EXPORT_COMMAND_NO_SPLIT', '-1' );

/**
* Export WordPress content to a WXR file.
*
Expand All @@ -22,6 +24,10 @@ class Export_Command extends WP_CLI_Command {
*/
public $export_args = array();

private $stdout;
private $max_file_size;
private $wxr_path;

/**
* Export WordPress content to a WXR file.
*
Expand All @@ -42,7 +48,7 @@ class Export_Command extends WP_CLI_Command {
* : Don't include comments in the WXR export file.
*
* [--max_file_size=<MB>]
* : A single export file should have this many megabytes.
* : A single export file should have this many megabytes. -1 for unlimited.
* ---
* default: 15
* ---
Expand Down Expand Up @@ -160,7 +166,7 @@ public function __invoke( $_, $assoc_args ) {
'filters' => $this->export_args,
'writer' => 'WP_Export_Split_Files_Writer',
'writer_args' => array(
'max_file_size' => $this->max_file_size * MB_IN_BYTES,
'max_file_size' => $this->max_file_size,
'destination_directory' => $this->wxr_path,
'filename_template' => self::get_filename_template( $assoc_args['filename_format'] ),
)
Expand All @@ -184,19 +190,6 @@ private static function get_filename_template( $filename_format ) {
}

private static function load_export_api() {
if ( !defined( 'KB_IN_BYTES' ) ) {
// Constants for expressing human-readable data sizes
// in their respective number of bytes.
define( 'KB_IN_BYTES', 1024 );
define( 'MB_IN_BYTES', 1024 * KB_IN_BYTES );
define( 'GB_IN_BYTES', 1024 * MB_IN_BYTES );
define( 'TB_IN_BYTES', 1024 * GB_IN_BYTES );
define( 'PB_IN_BYTES', 1024 * TB_IN_BYTES );
define( 'EB_IN_BYTES', 1024 * PB_IN_BYTES );
define( 'ZB_IN_BYTES', 1024 * EB_IN_BYTES );
define( 'YB_IN_BYTES', 1024 * ZB_IN_BYTES );
}

require dirname( dirname( __FILE__ ) ) . '/functions.php';
}

Expand Down
22 changes: 20 additions & 2 deletions src/WP_Export_Split_Files_Writer.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
<?php

class WP_Export_Split_Files_Writer extends WP_Export_Base_Writer {
private $max_file_size;
private $destination_directory;
private $filename_template;
private $before_posts_xml;
private $after_posts_xml;

private $result = '';
private $f;
private $next_file_number = 0;
private $current_file_size = 0;

function __construct( $formatter, $writer_args = array() ) {
parent::__construct( $formatter );

if ( ! defined( 'MB_IN_BYTES' ) ) {
define( 'MB_IN_BYTES', 1024 * 1024 );
}

//TODO: check if args are not missing
$this->max_file_size = is_null( $writer_args['max_file_size'] ) ? 15 * MB_IN_BYTES : $writer_args['max_file_size'];
if ( is_null( $writer_args['max_file_size'] ) ) {
$this->max_file_size = 15 * MB_IN_BYTES;
} elseif ( WP_CLI_EXPORT_COMMAND_NO_SPLIT === $writer_args['max_file_size'] ) {
$this->max_file_size = WP_CLI_EXPORT_COMMAND_NO_SPLIT;
} else {
$this->max_file_size = $writer_args['max_file_size'] * MB_IN_BYTES;
}

$this->destination_directory = $writer_args['destination_directory'];
$this->filename_template = $writer_args['filename_template'];
$this->before_posts_xml = $this->formatter->before_posts();
Expand All @@ -19,7 +37,7 @@ function __construct( $formatter, $writer_args = array() ) {
public function export() {
$this->start_new_file();
foreach( $this->formatter->posts() as $post_xml ) {
if ( $this->current_file_size + strlen( $post_xml ) > $this->max_file_size ) {
if ( WP_CLI_EXPORT_COMMAND_NO_SPLIT !== $this->max_file_size && $this->current_file_size + strlen( $post_xml ) > $this->max_file_size ) {
$this->start_new_file();
}
$this->write( $post_xml );
Expand Down