Skip to content

Commit

Permalink
First topological sorter draft
Browse files Browse the repository at this point in the history
  • Loading branch information
zaerl committed Nov 26, 2024
1 parent 4438d72 commit c51d9c4
Show file tree
Hide file tree
Showing 9 changed files with 413 additions and 75 deletions.
2 changes: 2 additions & 0 deletions packages/playground/data-liberation/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
require_once __DIR__ . '/src/import/WP_Attachment_Downloader_Event.php';
require_once __DIR__ . '/src/import/WP_Stream_Importer.php';
require_once __DIR__ . '/src/import/WP_Markdown_Importer.php';
require_once __DIR__ . '/src/import/WP_Logger.php';
require_once __DIR__ . '/src/import/WP_Topological_Sorter.php';

require_once __DIR__ . '/src/utf8_decoder.php';

Expand Down
14 changes: 2 additions & 12 deletions packages/playground/data-liberation/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,10 @@

add_action('init', function() {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
/**
* Import a WXR file.
*
* <file>
* : The WXR file to import.
*/
$command = function ( $args, $assoc_args ) {
$file = $args[0];
data_liberation_import( $file );
};
require_once __DIR__ . '/src/cli/WP_Import_Command.php';

// Register the WP-CLI import command.
// Example usage: wp data-liberation /path/to/file.xml
WP_CLI::add_command( 'data-liberation', $command );
WP_CLI::add_command( 'data-liberation', WP_Import_Command::class );
}
});

Expand Down
173 changes: 173 additions & 0 deletions packages/playground/data-liberation/src/cli/WP_Import_Command.php
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 packages/playground/data-liberation/src/cli/WP_Import_Logger.php
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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function __construct( $options = array() ) {
$this->mapping['term_id'] = array();
$this->requires_remapping = $empty_types;
$this->exists = $empty_types;
$this->logger = new Logger();
$this->logger = isset( $options['logger'] ) ? $options['logger'] : new WP_Logger();

$this->options = wp_parse_args(
$options,
Expand Down Expand Up @@ -1191,57 +1191,3 @@ public static function sort_comments_by_id( $a, $b ) {
return $a['comment_id'] - $b['comment_id'];
}
}

/**
* @TODO how to treat this? Should this class even exist?
* how does WordPress handle different levels? It
* seems useful for usage in wp-cli, Blueprints,
* and other non-web environments.
*/
// phpcs:ignore Generic.Files.OneObjectStructurePerFile.MultipleFound
class 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 );
}

/**
* Log an error message.
*
* @param string $message Message to log
*/
public function error( $message ) {
echo( '[ERROR] ' . $message );
}

/**
* Log a notice message.
*
* @param string $message Message to log
*/
public function notice( $message ) {
// echo( '[NOTICE] ' . $message );
}
}
51 changes: 51 additions & 0 deletions packages/playground/data-liberation/src/import/WP_Logger.php
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 );
}
}
Loading

0 comments on commit c51d9c4

Please sign in to comment.