-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
413 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
packages/playground/data-liberation/src/cli/WP_Import_Command.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/WP_Import_Logger.php'; | ||
|
||
/** | ||
* Implements the `wp data-liberation` command. | ||
* | ||
* ## EXAMPLES | ||
* | ||
* # Import a WXR file. | ||
* wp data-liberation import /path/to/file.xml | ||
* | ||
* # Import all files inside a folder. | ||
* wp data-liberation import /path/to/folder | ||
* | ||
* # Import a WXR file from a URL. | ||
* wp data-liberation import http://example.com/file.xml | ||
* | ||
* Success: Imported data. | ||
*/ | ||
class WP_Import_Command { | ||
/** | ||
* @var bool $dry_run Whether to perform a dry run. | ||
*/ | ||
private $dry_run = false; | ||
|
||
/** | ||
* @var WP_Stream_Importer $importer The importer instance. | ||
*/ | ||
private $importer = null; | ||
|
||
private $wxr_path = ''; | ||
|
||
/** | ||
* Import a WXR file. | ||
* | ||
* ## OPTIONS | ||
* | ||
* <path> | ||
* : The path to the WXR file. Either a file, a directory or a URL. | ||
* | ||
* [--dry-run] | ||
* : Perform a dry run if set. | ||
* | ||
* ## EXAMPLES | ||
* | ||
* wp data-liberation import /path/to/file.xml | ||
* | ||
* @param array $args | ||
* @param array $assoc_args | ||
* @return void | ||
*/ | ||
public function import( $args, $assoc_args ) { | ||
$path = $args[0]; | ||
$this->dry_run = WP_CLI\Utils\get_flag_value( $assoc_args, 'dry-run', false ); | ||
$options = array( | ||
'logger' => new WP_Import_logger(), | ||
); | ||
|
||
if ( extension_loaded( 'pcntl' ) ) { | ||
// Set the signal handler. | ||
$this->register_handlers(); | ||
} | ||
|
||
if ( filter_var( $path, FILTER_VALIDATE_URL ) ) { | ||
// Import URL. | ||
$this->import_wxr_url( $path, $options ); | ||
} elseif ( is_dir( $path ) ) { | ||
$count = 0; | ||
// Get all the WXR files in the directory. | ||
foreach ( wp_visit_file_tree( $path ) as $event ) { | ||
foreach ( $event->files as $file ) { | ||
if ( $file->isFile() && 'xml' === pathinfo( $file->getPathname(), PATHINFO_EXTENSION ) ) { | ||
++$count; | ||
|
||
// Import the WXR file. | ||
$this->import_wxr_file( $file->getPathname(), $options ); | ||
} | ||
} | ||
} | ||
|
||
if ( ! $count ) { | ||
WP_CLI::error( WP_CLI::colorize( "No WXR files found in the {$path} directory" ) ); | ||
} | ||
} else { | ||
if ( ! is_file( $path ) ) { | ||
WP_CLI::error( WP_CLI::colorize( "File not found: %R{$path}%n" ) ); | ||
} | ||
|
||
// Import the WXR file. | ||
$this->import_wxr_file( $path, $options ); | ||
} | ||
} | ||
|
||
/** | ||
* Import a WXR file. | ||
* | ||
* @param string $file_path The path to the WXR file. | ||
* @return void | ||
*/ | ||
private function import_wxr_file( $file_path, $options = array() ) { | ||
$this->wxr_path = $file_path; | ||
$this->importer = WP_Stream_Importer::create_for_wxr_file( $file_path, $options ); | ||
|
||
$this->import_wxr(); | ||
} | ||
|
||
/** | ||
* Import a WXR file from a URL. | ||
* | ||
* @param string $url The URL to the WXR file. | ||
* @return void | ||
*/ | ||
private function import_wxr_url( $url, $options = array() ) { | ||
$this->wxr_path = $url; | ||
$this->importer = WP_Stream_Importer::create_for_wxr_url( $url, $options ); | ||
|
||
$this->import_wxr(); | ||
} | ||
|
||
/** | ||
* Import the WXR file. | ||
*/ | ||
private function import_wxr() { | ||
if ( ! $this->importer ) { | ||
WP_CLI::error( 'Could not create importer' ); | ||
} | ||
|
||
WP_CLI::line( "Importing {$this->wxr_path}" ); | ||
|
||
if ( $this->dry_run ) { | ||
WP_CLI::line( 'Dry run enabled.' ); | ||
} else { | ||
while ( $this->importer->next_step() ) { | ||
$current_stage = $this->importer->get_current_stage(); | ||
// WP_CLI::line( "Stage {$current_stage}" ); | ||
} | ||
} | ||
|
||
WP_CLI::success( 'Import finished' ); | ||
} | ||
|
||
/** | ||
* Callback function registered to `pcntl_signal` to handle signals. | ||
* | ||
* @param int $signal The signal number. | ||
* @return void | ||
*/ | ||
protected function signal_handler( $signal ) { | ||
switch ( $signal ) { | ||
case SIGINT: | ||
WP_CLI::line( 'Received SIGINT signal' ); | ||
exit( 0 ); | ||
|
||
case SIGTERM: | ||
WP_CLI::line( 'Received SIGTERM signal' ); | ||
exit( 0 ); | ||
} | ||
} | ||
|
||
/** | ||
* Register signal handlers for the command. | ||
* | ||
* @return void | ||
*/ | ||
private function register_handlers() { | ||
// Handle the Ctrl + C signal to terminate the program. | ||
pcntl_signal( SIGINT, array( $this, 'signal_handler' ) ); | ||
|
||
// Handle the `kill` command to terminate the program. | ||
pcntl_signal( SIGTERM, array( $this, 'signal_handler' ) ); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
packages/playground/data-liberation/src/cli/WP_Import_Logger.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/** | ||
* Entity importer logger class. | ||
*/ | ||
class WP_Import_Logger { | ||
/** | ||
* Log a debug message. | ||
* | ||
* @param string $message Message to log | ||
*/ | ||
public function debug( $message ) { | ||
WP_CLI::debug( $message ); | ||
} | ||
|
||
/** | ||
* Log an info message. | ||
* | ||
* @param string $message Message to log | ||
*/ | ||
public function info( $message ) { | ||
WP_CLI::log( $message ); | ||
} | ||
|
||
/** | ||
* Log a warning message. | ||
* | ||
* @param string $message Message to log | ||
*/ | ||
public function warning( $message ) { | ||
WP_CLI::warning( $message ); | ||
} | ||
|
||
/** | ||
* Log an error message. | ||
* | ||
* @param string $message Message to log | ||
*/ | ||
public function error( $message ) { | ||
WP_CLI::error( $message, false ); | ||
} | ||
|
||
/** | ||
* Log a notice message. | ||
* | ||
* @param string $message Message to log | ||
*/ | ||
public function notice( $message ) { | ||
WP_CLI::log( $message ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/playground/data-liberation/src/import/WP_Logger.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/** | ||
* Entity importer logger class. | ||
*/ | ||
class WP_Logger { | ||
/** | ||
* Log a debug message. | ||
* | ||
* @param string $message Message to log | ||
*/ | ||
public function debug( $message ) { | ||
// echo( '[DEBUG] ' . $message ); | ||
} | ||
|
||
/** | ||
* Log an info message. | ||
* | ||
* @param string $message Message to log | ||
*/ | ||
public function info( $message ) { | ||
// echo( '[INFO] ' . $message ); | ||
} | ||
|
||
/** | ||
* Log a warning message. | ||
* | ||
* @param string $message Message to log | ||
*/ | ||
public function warning( $message ) { | ||
echo( "[WARNING] {$message}\n" ); | ||
} | ||
|
||
/** | ||
* Log an error message. | ||
* | ||
* @param string $message Message to log | ||
*/ | ||
public function error( $message ) { | ||
echo( "[ERROR] $message\n" ); | ||
} | ||
|
||
/** | ||
* Log a notice message. | ||
* | ||
* @param string $message Message to log | ||
*/ | ||
public function notice( $message ) { | ||
// echo( '[NOTICE] ' . $message ); | ||
} | ||
} |
Oops, something went wrong.