Skip to content

Commit

Permalink
issue #9. Added wp export --stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Droz committed Aug 22, 2017
1 parent 5b72469 commit bbcc22a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
51 changes: 40 additions & 11 deletions src/Export_Command.php
Expand Up @@ -34,6 +34,9 @@ class Export_Command extends WP_CLI_Command {
* [--dir=<dirname>]
* : Full path to directory where WXR export files should be stored. Defaults
* to current working directory.
* [--stdout]
* : Output the whole XML using standard output (incompatible with --dir=)
*
* [--skip_comments]
* : Don't include comments in the WXR export file.
Expand Down Expand Up @@ -104,6 +107,7 @@ class Export_Command extends WP_CLI_Command {
public function __invoke( $_, $assoc_args ) {
$defaults = array(
'dir' => NULL,
'stdout' => FALSE,
'start_date' => NULL,
'end_date' => NULL,
'post_type' => NULL,
Expand All @@ -118,35 +122,54 @@ public function __invoke( $_, $assoc_args ) {
'filename_format' => '{site}.wordpress.{date}.{n}.xml',
);


if (! empty( $assoc_args['stdout'] ) && ( ! empty( $assoc_args['dir'] ) || ! empty( $assoc_args['filename_format'] ) ) ) {
WP_CLI::error( "--stdout and --dir cannot be used together." );
WP_CLI::halt(1);
}

$assoc_args = wp_parse_args( $assoc_args, $defaults );

$this->validate_args( $assoc_args );

if ( !function_exists( 'wp_export' ) ) {
self::load_export_api();
}

WP_CLI::log( 'Starting export process...' );
if ( ! $this->stdout ) {
WP_CLI::log( 'Starting export process...' );
}

add_action( 'wp_export_new_file', function( $file_path ) {
WP_CLI::log( sprintf( "Writing to file %s", $file_path ) );
WP_CLI\Utils\wp_clear_object_cache();
} );

try {
wp_export( array(
'filters' => $this->export_args,
'writer' => 'WP_Export_Split_Files_Writer',
'writer_args' => array(
'max_file_size' => $this->max_file_size * MB_IN_BYTES,
'destination_directory' => $this->wxr_path,
'filename_template' => self::get_filename_template( $assoc_args['filename_format'] ),
)
) );
if ( $this->stdout ) {
wp_export( array(
'filters' => $this->export_args,
'writer' => 'WP_Export_Stdout_Writer',
'writer_args' => array( )
) );
} else {
wp_export( array(
'filters' => $this->export_args,
'writer' => 'WP_Export_Split_Files_Writer',
'writer_args' => array(
'max_file_size' => $this->max_file_size * MB_IN_BYTES,
'destination_directory' => $this->wxr_path,
'filename_template' => self::get_filename_template( $assoc_args['filename_format'] ),
)
) );
}
} catch ( Exception $e ) {
WP_CLI::error( $e->getMessage() );
}

WP_CLI::success( 'All done with export.' );
if ( ! $this->stdout ) {
WP_CLI::success( 'All done with export.' );
}
}

private static function get_filename_template( $filename_format ) {
Expand Down Expand Up @@ -185,6 +208,12 @@ private function validate_args( $args ) {
}
}

// called last to check possible mutual-exclusion with previous arguments
if (isset($args['stdout'])) {
$this->wxr_path = NULL;
$this->stdout = TRUE;
}

if ( $has_errors ) {
WP_CLI::halt(1);
}
Expand Down
22 changes: 22 additions & 0 deletions src/WP_Export_Stdout_Writer.php
@@ -0,0 +1,22 @@
<?php

class WP_Export_Stdout_Writer extends WP_Export_Base_Writer {

function __construct( $formatter, $writer_args = array() ) {
parent::__construct( $formatter );
//TODO: check if args are not missing
$this->before_posts_xml = $this->formatter->before_posts();
$this->after_posts_xml = $this->formatter->after_posts();
}

public function export() {
// WP_CLI\Utils\wp_clear_object_cache(); ?
fwrite( STDOUT, $this->before_posts_xml );
foreach( $this->formatter->posts() as $post_xml ) {
fwrite( STDOUT, $post_xml );
}
fwrite( STDOUT, $this->after_posts_xml );
}

protected function write( $xml ) { }
}

0 comments on commit bbcc22a

Please sign in to comment.